git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Find out on which branch a commit was originally made

From
ASArtur Skawina <art.08.09@gmail.com>
Date
Sep 20, 2010, 15:49 UTC
Message-ID
<4C9782A3.5010005@gmail.com>
In-Reply-To
<4C973E5B.4090201@gmail.com>
On 09/20/10 12:58, Artur Skawina wrote:
Show 37 quoted lines
> On 09/20/10 10:12, Stefan Haller wrote:
>> Seth Robertson <in-gitvger@baka.org> wrote:
> 
>>>          -AA-- subtopic
>>>         /     \
>>>        A---B---C topic
>>>       /         \
>>>  D---E---F---G---H---I---J---K---L---M---N master
>>>                           \         /
>>>                            O---P---Q another-topic
>>>
>>>
>>> In the above example, the subtopic branch merge from AA to C prevents
>>> you from finding out what branch B is on using the original script.
>>
>> When you say "the original script", are you talking about Artur's
>> one-liner or my script?
>>
>> My script gives me exactly the information I want in all cases.  For a
>> given command $1 and a target branch $2, it shows you all merges that
>> were involved in bringing $1 into $2. For example:
>>
>>   Called with "B" "master", it returns H
> 
> No, it will return both C and H, just like my one-liner; this will be
> misleading, the user won't be able to figure out where 'B" came from
> w/o looking at the graph, from output like:
> 
> $ git-show-merges-since B master
> C..... Merge branch 'subtopic' into topic
> H..... Merge branch 'topic'
> 
> The results for 'B' and 'AA' will be exactly the same.
> 
> For 'B', the 'C' merge should be omitted; skipping it because 'B'
> comes in via first parent would probably work, but i can't turn that
> into a one-liner right now...

Yeah, that was it; the script below will correctly handle not only the simple example above, but also some real cases.

Usage: git-find-branch-for <rev> [long-lived-branch]

Returns some merge commits, making it easier to find out from which branch a change originated. Returns nothing if it did not find an interesting merge all the way up to long-lived-branch (which defaults to "master")

Example, using git.git:

$ time ../../../stefans-script bac80370817 4fa088209c81845e73756a4173a8c954e25958d2 Merge branch 'jk/run-command-use-shell' 225f78c817755bebff91629cc525a258cf60eaea Merge branch 'master' of git://repo.or.cz/alt-git into jn/autodep 037c43c68e220739e690540de89a6d5835fefe73 Merge remote branch 'ko/master' into jc/read-tree-cache-tree-fix 1m23.271s user 0m2.173s system 1m26.389s elapsed 98.90% CPU

$ time ../../../git-find-branch-for bac80370817 4fa08820 Merge branch 'jk/run-command-use-shell' * jk/run-command-use 1263743895 225f78c8 Merge branch 'master' of git://repo.or.cz/alt-git into jn/autodep 1264500524 037c43c6 Merge remote branch 'ko/master' into jc/read-tree-cache-tree-fix 1278615350 0m0.270s user 0m0.017s system 0m0.287s elapsed 99.87% CPU $

So it's also a little bit faster. Received very little testing, i only tried it on a few commits, other than the example from this thread, but so far seems to do the right thing for all of them. Unpikifying is left as an exercise for the user. ;)

artur

------------------------------------------------------------------------------ #! /usr/bin/env pike // git-find-branch-for <rev> [long-lived-branch] // Probably needs pike7.8 #define die(a...) exit(1, "Aborting; %s"+a)

string run(string ... cmdline) {
   mapping r;
   mixed e = catch { r = Process.run( ({@cmdline}) ); };
   if (e || r["exitcode"])
      die("", e?e:r["stderr"]);
   return r["stdout"];
}
mapping commits = ([]);
array parsecommits(string from, string to) {
   array res = ({});
   string id;
   string gitout = run("git", "rev-list", "--format=raw",  "--ancestry-path",
                                          "--date-order", from+".."+to);
   array lines = gitout/"\n";
   foreach (lines, string line) {
      array words = line/" ";
      if (words[0]=="commit") {
         id = words[1][0]!='-' ? words[1] : words[1][1..];
	 commits[id] = ([]);
	 res += ({id});
      }
      commits[id][words[0]] += words[1..];
   }
   return res;
}
int ismerge(string id) {
   return commits[id]["parent"] && sizeof(commits[id]["parent"])>1;
}
mapping desc = ([]);
int main(int argc, array argv) {
   argv[1] = (run("git", "rev-parse", argv[1])/"\n")[0];
   if (argc==2)
      argv += ({"master"});
   argv[2] = (run("git", "rev-parse", argv[2])/"\n")[0];
   array merges = parsecommits(argv[1], argv[2]);
   merges = reverse(merges);
   desc[argv[1]] = 1;
   foreach (merges, string id) {
      if (commits[id]["parent"])
         foreach (commits[id]["parent"], string parent)
            if (desc[parent])
        	desc[id] = 1;
      if (ismerge(id))
         if (!desc[commits[id]["parent"][0]]) {
	    int comtime = (int)commits[id]["committer"][-2];
	    comtime += (int)commits[id]["committer"][-1]/100*60*60;
            write("%.8s %.70s %d\n", id, commits[id][""]*" ", comtime );
	 }
   }
}
------------------------------------------------------------------------------
Previous: Artur SkawinaNext: Seth Robertson
Message 20 of 33 in “Find out on which branch a commit was originally made”
  1. Stefan HallerSep 18, 2010
  2. Ævar Arnfjörð BjarmasonSep 18, 2010
  3. Ævar Arnfjörð BjarmasonSep 18, 2010
  4. Tor ArntsenSep 18, 2010
  5. Stefan HallerSep 18, 2010
  6. Artur SkawinaSep 18, 2010
  7. Stefan HallerSep 19, 2010
  8. Clemens BuchacherSep 19, 2010
  9. Artur SkawinaSep 19, 2010
  10. Stefan HallerSep 19, 2010
  11. Artur SkawinaSep 19, 2010
  12. Robin RosenbergSep 19, 2010
  13. Seth RobertsonSep 19, 2010
  14. Artur SkawinaSep 19, 2010
  15. Seth RobertsonSep 19, 2010
  16. Artur SkawinaSep 20, 2010
  17. Seth RobertsonSep 20, 2010
  18. Stefan HallerSep 20, 2010
  19. Artur SkawinaSep 20, 2010
  20. Artur SkawinaSep 20, 2010
  21. Seth RobertsonSep 21, 2010
  22. Artur SkawinaSep 21, 2010
  23. ANNOUNCE git-what-branch (was Re: Find out on which branch a commit was originally made)Seth Robertson, Sep 22, 2010
  24. Artur SkawinaSep 22, 2010
  25. Seth RobertsonSep 22, 2010
  26. Stephen BashSep 23, 2010
  27. Ævar Arnfjörð BjarmasonSep 23, 2010
  28. Artur SkawinaSep 23, 2010
  29. Artur SkawinaSep 24, 2010
  30. Seth RobertsonSep 24, 2010
  31. Seth RobertsonSep 23, 2010
  32. Stefan HallerSep 20, 2010
  33. Bryan DrewerySep 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.