threads / discuss / 37041

Show containing branches in log?

Subject: Show containing branches in log?

## tl;dr

10 messages between Jul 2, 2014 and Aug 25, 2014.

replies: 9people: 5as markdown or json

Robert Dailey· Jul 2, 2014, 14:50 UTC · lore

I know that with the `git branch` command I can determine which branches contain a commit. Is there a way to represent this graphically with `git log`? Sometimes I just have a commit, and I need to find out what branch contains that commit. The reason why `git branch --contains` doesn't solve this problem for me is that it names almost all branches because of merge commits. Too much ancestry has been built since this commit, so there is no way to find the "closest" branch that contains that commit.

Is there a way to graphically see what is the "nearest" named ref to the specified commit in the logs?

Jeff King· Jul 2, 2014, 16:34 UTC · re: Robert Dailey · lore

Re: Show containing branches in log?

On Wed, Jul 02, 2014 at 09:50:57AM -0500, Robert Dailey wrote:
Show 11 quoted lines
> I know that with the `git branch` command I can determine which
> branches contain a commit. Is there a way to represent this
> graphically with `git log`? Sometimes I just have a commit, and I need
> to find out what branch contains that commit. The reason why `git
> branch --contains` doesn't solve this problem for me is that it names
> almost all branches because of merge commits. Too much ancestry has
> been built since this commit, so there is no way to find the "closest"
> branch that contains that commit.
> 
> Is there a way to graphically see what is the "nearest" named ref to
> the specified commit in the logs?
Have you tried "git describe --contains --all <commit>"?

To some degree, I fear your question isn't something git can answer. If the branch containing the commit has been merged into other branches, then they all "contain" the commit. There is not really any reason to prefer one over the other ("describe --contains" will try to find the "closest" branch, but that is based on heuristics and is not necessarily well-defined).

-Peff
Robert Dailey· Jul 2, 2014, 16:52 UTC · re: Jeff King · lore

Re: Show containing branches in log?

On Wed, Jul 2, 2014 at 11:34 AM, Jeff King <peff@peff.net> wrote:
Show 8 quoted lines
> Have you tried "git describe --contains --all <commit>"?
>
> To some degree, I fear your question isn't something git can answer. If
> the branch containing the commit has been merged into other branches,
> then they all "contain" the commit. There is not really any reason to
> prefer one over the other ("describe --contains" will try to find the
> "closest" branch, but that is based on heuristics and is not necessarily
> well-defined).

I have not tried that command. Note I mentioned named refs, so nameless branches I'm not worried about. Even if I merge branch A into branch B, branch A is still closest in terms of number of steps to get to the commit, because to get to the commit through B you have to cross over a merge commit. Basically the priority should be directness and distance. The more direct a branch is (i.e. the lesser number of merge commits it goes through to get to the commit) the more relevant it is. As a second condition, distance would be used in cases where the directness of it is the same.

Sorting this in the log graph and seeing it visually (I could even use --simplify-by-decoration) would help me understand the topography of git's history relative to the commit(s) I specify.

Jason Pyeron· Jul 2, 2014, 16:52 UTC · re: Jeff King · lore

RE: Show containing branches in log?

Show 31 quoted lines
> -----Original Message-----
> From: Jeff King
> Sent: Wednesday, July 02, 2014 12:35
> 
> On Wed, Jul 02, 2014 at 09:50:57AM -0500, Robert Dailey wrote:
> 
> > I know that with the `git branch` command I can determine which
> > branches contain a commit. Is there a way to represent this
> > graphically with `git log`? Sometimes I just have a commit, 
> and I need
> > to find out what branch contains that commit. The reason why `git
> > branch --contains` doesn't solve this problem for me is 
> that it names
> > almost all branches because of merge commits. Too much ancestry has
> > been built since this commit, so there is no way to find 
> the "closest"
> > branch that contains that commit.
> > 
> > Is there a way to graphically see what is the "nearest" named ref to
> > the specified commit in the logs?
> 
> Have you tried "git describe --contains --all <commit>"?
> 
> To some degree, I fear your question isn't something git can 
> answer. If
> the branch containing the commit has been merged into other branches,
> then they all "contain" the commit. There is not really any reason to
> prefer one over the other ("describe --contains" will try to find the
> "closest" branch, but that is based on heuristics and is not 
> necessarily
> well-defined).

Another way I answer this question is git log --oneline --graph --all and then search for the commit and follow the lines.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-                                                               -
- Jason Pyeron                      PD Inc. http://www.pdinc.us -
- Principal Consultant              10 West 24th Street #100    -
- +1 (443) 269-1555 x333            Baltimore, Maryland 21218   -
-                                                               -
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
This message is copyright PD Inc, subject to license 20080407P00.
 
Robert Dailey· Jul 2, 2014, 16:59 UTC · re: Jason Pyeron · lore

Re: Show containing branches in log?

On Wed, Jul 2, 2014 at 11:52 AM, Jason Pyeron <jpyeron@pdinc.us> wrote:
Show 34 quoted lines
>> -----Original Message-----
>> From: Jeff King
>> Sent: Wednesday, July 02, 2014 12:35
>>
>> On Wed, Jul 02, 2014 at 09:50:57AM -0500, Robert Dailey wrote:
>>
>> > I know that with the `git branch` command I can determine which
>> > branches contain a commit. Is there a way to represent this
>> > graphically with `git log`? Sometimes I just have a commit,
>> and I need
>> > to find out what branch contains that commit. The reason why `git
>> > branch --contains` doesn't solve this problem for me is
>> that it names
>> > almost all branches because of merge commits. Too much ancestry has
>> > been built since this commit, so there is no way to find
>> the "closest"
>> > branch that contains that commit.
>> >
>> > Is there a way to graphically see what is the "nearest" named ref to
>> > the specified commit in the logs?
>>
>> Have you tried "git describe --contains --all <commit>"?
>>
>> To some degree, I fear your question isn't something git can
>> answer. If
>> the branch containing the commit has been merged into other branches,
>> then they all "contain" the commit. There is not really any reason to
>> prefer one over the other ("describe --contains" will try to find the
>> "closest" branch, but that is based on heuristics and is not
>> necessarily
>> well-defined).
>
> Another way I answer this question is git log --oneline --graph --all and then
> search for the commit and follow the lines.

If that were a practical solution I wouldn't be here asking this question. Unfortunately, in a repository with multiple parallel release branches, it is impossible to just "eye-ball" the graph and find what you're looking for. Especially when the last 4 weeks worth of commits consumes over 10 pages of log graph.

Peter Krefting· Jul 3, 2014, 14:18 UTC · re: Robert Dailey · lore

Re: Show containing branches in log?

Robert Dailey:
> Is there a way to graphically see what is the "nearest" named ref to 
> the specified commit in the logs?
git log --graph --decorate commit..

will display all the commits that happened after the commit "commit", with the branch names indicated, with lines indicating the ancestry. That's the closest I can come to think of.

-- 
\\// Peter - http://www.softwolves.pp.se/
Jeff King· Jul 3, 2014, 17:25 UTC · re: Peter Krefting · lore

Re: Show containing branches in log?

On Thu, Jul 03, 2014 at 03:18:42PM +0100, Peter Krefting wrote:
Show 10 quoted lines
> Robert Dailey:
> 
> >Is there a way to graphically see what is the "nearest" named ref to the
> >specified commit in the logs?
> 
> git log --graph --decorate commit..
> 
> will display all the commits that happened after the commit "commit", with
> the branch names indicated, with lines indicating the ancestry. That's the
> closest I can come to think of.

The trouble with "--decorate" is that you have to manually find the decorated commit that is closest. That can be hard if you have a lot of commits.

There is also "--source", which will transmit the "source name" down from parent to child. So if you do:

  git log --all --source

each tip will be painted with its ref name, and will pass that to its ancestors as we walk the graph. Of course ancestors that have multiple children (e.g., something that got merged) will only show one source, but that painting will never cross an actual branch boundary (so if branch "A" merged branch "B", the commits that are in "B" will still get painted as "B").

The big downside is that you are now traversing _all_ refs, instead of just the ones you wanted. We could fix that by letting you specify a set of refs to paint. For example, with the patch below, you can do:

  git log --source=refs/heads

and see only commits in HEAD, but painted by their source in refs/heads/*. I'm not planning to work on it further anytime soon, but if somebody wants to pick it up and run with it, feel free. I think before inclusion one would want to consider:

  1. Calling it "--contains" instead of "--source", since it is not
     really about the source anymore.
  2. Moving the storage out of commit->util into a commit-slab. This
     would prevent it from conflicting with "--source" (for that matter,
     it would be nice if "--source" used a slab, too).
  3. Rather than showing one arbitrary way of getting to the commit,
     keep track of _all_ of them. Unfortunately this is not quite a true
     list of which refs contain the commit, as we might show the commit
     before its parent paints down to it (this can happen if the commit
     timestamps are out of order, I think). We could get around that by
     using --topo-order.
  4. Make the output less ugly (probably more like --decorate, with
     colors and parentheses).
---
diff --git a/builtin/log.c b/builtin/log.c
index a7ba211..8ad7b46 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -25,6 +25,7 @@
 #include "version.h"
 #include "mailmap.h"
 #include "gpg-interface.h"
+#include "refs.h"
 
 /* Set a default date-time format for git log ("log.date" config variable) */
 static const char *default_date_mode = NULL;
@@ -116,16 +117,61 @@ static void cmd_log_init_defaults(struct rev_info *rev)
 	rev->diffopt.touched_flags = 0;
 }
 
+struct source_opt {
+	struct string_list refs;
+	int enabled;
+};
+
+static int parse_opt_source(const struct option *opt, const char *arg, int unset)
+{
+	struct source_opt *source = opt->value;
+
+	if (unset) {
+		source->enabled = 0;
+		string_list_clear(&source->refs, 0);
+	} else {
+		source->enabled = 1;
+		if (arg)
+			string_list_append(&source->refs, arg);
+	}
+
+	return 0;
+}
+
+static int paint_source_ref(const char *refname, const unsigned char *sha1,
+			    int flags, void *data)
+{
+	const char *prefix = data;
+	struct commit *c = lookup_commit_reference_gently(sha1, 1);
+
+	if (c && !c->util) {
+		struct strbuf buf = STRBUF_INIT;
+		strbuf_addstr(&buf, prefix);
+		strbuf_addstr(&buf, refname);
+		c->util = strbuf_detach(&buf, NULL);
+	}
+	return 0;
+}
+
+static int paint_source_refs_in(struct string_list_item *it, void *data)
+{
+	for_each_ref_in(it->string, paint_source_ref, it->string);
+	return 0;
+}
+
+
 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
 			 struct rev_info *rev, struct setup_revision_opt *opt)
 {
 	struct userformat_want w;
-	int quiet = 0, source = 0, mailmap = 0;
+	struct source_opt source = { STRING_LIST_INIT_DUP, 0 };
+	int quiet = 0, mailmap = 0;
 	static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
 
 	const struct option builtin_log_options[] = {
 		OPT__QUIET(&quiet, N_("suppress diff output")),
-		OPT_BOOL(0, "source", &source, N_("show source")),
+		{ OPTION_CALLBACK, 0, "source", &source, N_("refs"), N_("show source"),
+		  PARSE_OPT_OPTARG, parse_opt_source },
 		OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")),
 		{ OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
 		  PARSE_OPT_OPTARG, decorate_callback},
@@ -164,8 +210,10 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
 	    DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES))
 		rev->always_show_header = 0;
 
-	if (source)
+	if (source.enabled) {
 		rev->show_source = 1;
+		for_each_string_list(&source.refs, paint_source_refs_in, NULL);
+	}
 
 	if (mailmap) {
 		rev->mailmap = xcalloc(1, sizeof(struct string_list));
Øyvind A. Holm· Jul 3, 2014, 19:41 UTC · re: Robert Dailey · lore

Re: Show containing branches in log?

On 2 July 2014 16:50, Robert Dailey <rcdailey.lists@gmail.com> wrote:
Show 11 quoted lines
> I know that with the `git branch` command I can determine which
> branches contain a commit. Is there a way to represent this
> graphically with `git log`? Sometimes I just have a commit, and I need
> to find out what branch contains that commit. The reason why `git
> branch --contains` doesn't solve this problem for me is that it names
> almost all branches because of merge commits. Too much ancestry has
> been built since this commit, so there is no way to find the "closest"
> branch that contains that commit.
>
> Is there a way to graphically see what is the "nearest" named ref to
> the specified commit in the logs?

I have created a script for just this functionality which I use very often, and have created a gist with the files at <https://gist.github.com/sunny256/2eb583f21e0ffcfe994f>, I think it should solve your problem. It contains these files:

  git-wn

"wn" means "What's New" and will create a visual graph of all commits which has a specified ref as ancestor. It also needs the following script, just put it into your $PATH somewhere:

  git-lc

"lc" means "List branches Containing this commit" and generates a list of all branches containing a specified ref.

The files originates from <https://github.com/sunny256/utils>, but I've modified them in the gist to make your life easier. :)

Hope that helps, Øyvind

Robert Dailey· Aug 1, 2014, 18:01 UTC · re: Øyvind A. Holm · lore

Re: Show containing branches in log?

On Thu, Jul 3, 2014 at 2:41 PM, Øyvind A. Holm <sunny@sunbase.org> wrote:
Show 18 quoted lines
> I have created a script for just this functionality which I use very
> often, and have created a gist with the files at
> <https://gist.github.com/sunny256/2eb583f21e0ffcfe994f>, I think it
> should solve your problem. It contains these files:
>
>   git-wn
>
> "wn" means "What's New" and will create a visual graph of all commits
> which has a specified ref as ancestor. It also needs the following
> script, just put it into your $PATH somewhere:
>
>   git-lc
>
> "lc" means "List branches Containing this commit" and generates a list
> of all branches containing a specified ref.
>
> The files originates from <https://github.com/sunny256/utils>, but
> I've modified them in the gist to make your life easier. :)

Sorry for the late response. I forgot to thank you for this! This is very detailed and helpful... I will try this soon and see how it works.

Great job.
Robert Dailey· Aug 25, 2014, 23:35 UTC · re: Øyvind A. Holm · lore

Re: Show containing branches in log?

On Thu, Jul 3, 2014 at 2:41 PM, Øyvind A. Holm <sunny@sunbase.org> wrote:
Show 34 quoted lines
> On 2 July 2014 16:50, Robert Dailey <rcdailey.lists@gmail.com> wrote:
>> I know that with the `git branch` command I can determine which
>> branches contain a commit. Is there a way to represent this
>> graphically with `git log`? Sometimes I just have a commit, and I need
>> to find out what branch contains that commit. The reason why `git
>> branch --contains` doesn't solve this problem for me is that it names
>> almost all branches because of merge commits. Too much ancestry has
>> been built since this commit, so there is no way to find the "closest"
>> branch that contains that commit.
>>
>> Is there a way to graphically see what is the "nearest" named ref to
>> the specified commit in the logs?
>
> I have created a script for just this functionality which I use very
> often, and have created a gist with the files at
> <https://gist.github.com/sunny256/2eb583f21e0ffcfe994f>, I think it
> should solve your problem. It contains these files:
>
>   git-wn
>
> "wn" means "What's New" and will create a visual graph of all commits
> which has a specified ref as ancestor. It also needs the following
> script, just put it into your $PATH somewhere:
>
>   git-lc
>
> "lc" means "List branches Containing this commit" and generates a list
> of all branches containing a specified ref.
>
> The files originates from <https://github.com/sunny256/utils>, but
> I've modified them in the gist to make your life easier. :)
>
> Hope that helps,
> Øyvind

I'm finally getting around to trying this out but it isn't working on Windows because there is no fmt command in msysgit. Do you have a workaround I can use? Thanks.

← back to recent threads