{"thread":{"id":"13590","subject":"using rev-list to tell if a branch is behind or ahead","startedAt":"2008-05-20T20:14:46Z","lastAt":"2008-05-20T20:24:38Z","messageCount":2,"participants":["Tim Harper","Shawn O. Pearce"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"77338","messageId":"3703BF54-3619-432F-8B65-09AF17299CAD@gmail.com","threadId":"13590","inReplyTo":null,"subject":"using rev-list to tell if a branch is behind or ahead","fromName":"Tim Harper","fromEmail":"timcharper@gmail.com","sentAt":"2008-05-20T20:14:46Z","receivedAt":"2008-05-20T20:14:46Z","isPatch":false,"sender":{"key":"timcharper@gmail.com","avatar":"https://gravatar.com/avatar/1a2e0c06c7862ff065ee6b1d53195333a5a0577c040ecb2856a150d8e0b00ecd?d=mp&s=160"},"body":"I'm implementing a ruby interface to git and am wanting to be able to  \nask if a branch is ahead or behind.\n\nI looked in the builtin-checkout.c file and see this code:\n\n\t/* Run \"rev-list --left-right ours...theirs\" internally... */\n\trev_argc = 0;\n\trev_argv[rev_argc++] = NULL;\n\trev_argv[rev_argc++] = \"--left-right\";\n\trev_argv[rev_argc++] = symmetric;\n\trev_argv[rev_argc++] = \"--\";\n\trev_argv[rev_argc] = NULL;\n\n\tstrcpy(symmetric, sha1_to_hex(ours->object.sha1));\n\tstrcpy(symmetric + 40, \"...\");\n\tstrcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));\n\n\tinit_revisions(&revs, NULL);\n\tsetup_revisions(rev_argc, rev_argv, &revs, NULL);\n\tprepare_revision_walk(&revs);\n\n\t/* ... and count the commits on each side. */\n\tnum_ours = 0;\n\tnum_theirs = 0;\n\twhile (1) {\n\t\tstruct commit *c = get_revision(&revs);\n\t\tif (!c)\n\t\t\tbreak;\n\t\tif (c->object.flags & SYMMETRIC_LEFT)\n\t\t\tnum_ours++;\n\t\telse\n\t\t\tnum_theirs++;\n\t}\n\n\nIt looks like it's calling rev-parse.  But, when I call it with the  \nsame arguments (using branches or commit sha1's), it only will list  \ncommits that are in right and not in left.  I need it to show both  \nways: commits that are in the right and not in left, and commits that  \nare in the left but not in right.\n\nDo I need to call rev-parse twice to achieve this?\n\nHere's a sample of what I'm trying currently:\n~ $ mkdir test\n~ $ cd test/\n~/test $ git init\nInitialized empty Git repository in .git/\n~/test $ git\n~/test $ echo content > file.txt\n~/test $ git add file.txt && git commit -m \"Initial commit\"\nCreated initial commit f5e4160: Initial commit\n  1 files changed, 1 insertions(+), 0 deletions(-)\n  create mode 100644 file.txt\n~/test master$ git co -b task\nSwitched to a new branch \"task\"\n~/test task$ echo changes >> file.txt\n~/test task$ git add file.txt && git commit -m \"Some changes\"\nCreated commit 96492ee: Some changes\n  1 files changed, 1 insertions(+), 0 deletions(-)\n~/test task$ git rev-list --left-right task..master --\n~/test task$ git rev-list --left-right master..task --\n >96492ee80143f43417b00699ff29330d0027df7f\n\n\nThanks,\n\nTim\n"},{"id":"77341","messageId":"20080520202438.GG29038@spearce.org","threadId":"13590","inReplyTo":"3703BF54-3619-432F-8B65-09AF17299CAD@gmail.com","subject":"Re: using rev-list to tell if a branch is behind or ahead","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-05-20T20:24:38Z","receivedAt":"2008-05-20T20:24:38Z","isPatch":false,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Tim Harper <timcharper@gmail.com> wrote:\n> I'm implementing a ruby interface to git and am wanting to be able to  \n> ask if a branch is ahead or behind.\n> \n> I looked in the builtin-checkout.c file and see this code:\n> \n> \t/* Run \"rev-list --left-right ours...theirs\" internally... */\n...\n> It looks like it's calling rev-parse.  But, when I call it with the  \n> same arguments (using branches or commit sha1's), it only will list  \n> commits that are in right and not in left.  I need it to show both  \n> ways: commits that are in the right and not in left, and commits that  \n> are in the left but not in right.\n> \n> Do I need to call rev-parse twice to achieve this?\n\nNo.  You need to use the triple dot operator (\"...\") not the\ndouble dot operator (\"..\").\n\n> Here's a sample of what I'm trying currently:\n> ~ $ mkdir test\n> ~ $ cd test/\n> ~/test $ git init\n> Initialized empty Git repository in .git/\n> ~/test $ git\n> ~/test $ echo content > file.txt\n> ~/test $ git add file.txt && git commit -m \"Initial commit\"\n> Created initial commit f5e4160: Initial commit\n>  1 files changed, 1 insertions(+), 0 deletions(-)\n>  create mode 100644 file.txt\n> ~/test master$ git co -b task\n> Switched to a new branch \"task\"\n> ~/test task$ echo changes >> file.txt\n> ~/test task$ git add file.txt && git commit -m \"Some changes\"\n> Created commit 96492ee: Some changes\n>  1 files changed, 1 insertions(+), 0 deletions(-)\n> ~/test task$ git rev-list --left-right task..master --\n\nYou need an extra \".\" between task and master, this should be:\n\n  git rev-list --left-right task...master --\n\n> ~/test task$ git rev-list --left-right master..task --\n> >96492ee80143f43417b00699ff29330d0027df7f\n\n-- \nShawn.\n"}]}