git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* using rev-list to tell if a branch is behind or ahead
@ 2008-05-20 20:14 Tim Harper
  2008-05-20 20:24 ` Shawn O. Pearce
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Harper @ 2008-05-20 20:14 UTC (permalink / raw)
  To: git

I'm implementing a ruby interface to git and am wanting to be able to  
ask if a branch is ahead or behind.

I looked in the builtin-checkout.c file and see this code:

	/* Run "rev-list --left-right ours...theirs" internally... */
	rev_argc = 0;
	rev_argv[rev_argc++] = NULL;
	rev_argv[rev_argc++] = "--left-right";
	rev_argv[rev_argc++] = symmetric;
	rev_argv[rev_argc++] = "--";
	rev_argv[rev_argc] = NULL;

	strcpy(symmetric, sha1_to_hex(ours->object.sha1));
	strcpy(symmetric + 40, "...");
	strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));

	init_revisions(&revs, NULL);
	setup_revisions(rev_argc, rev_argv, &revs, NULL);
	prepare_revision_walk(&revs);

	/* ... and count the commits on each side. */
	num_ours = 0;
	num_theirs = 0;
	while (1) {
		struct commit *c = get_revision(&revs);
		if (!c)
			break;
		if (c->object.flags & SYMMETRIC_LEFT)
			num_ours++;
		else
			num_theirs++;
	}


It looks like it's calling rev-parse.  But, when I call it with the  
same arguments (using branches or commit sha1's), it only will list  
commits that are in right and not in left.  I need it to show both  
ways: commits that are in the right and not in left, and commits that  
are in the left but not in right.

Do I need to call rev-parse twice to achieve this?

Here's a sample of what I'm trying currently:
~ $ mkdir test
~ $ cd test/
~/test $ git init
Initialized empty Git repository in .git/
~/test $ git
~/test $ echo content > file.txt
~/test $ git add file.txt && git commit -m "Initial commit"
Created initial commit f5e4160: Initial commit
  1 files changed, 1 insertions(+), 0 deletions(-)
  create mode 100644 file.txt
~/test master$ git co -b task
Switched to a new branch "task"
~/test task$ echo changes >> file.txt
~/test task$ git add file.txt && git commit -m "Some changes"
Created commit 96492ee: Some changes
  1 files changed, 1 insertions(+), 0 deletions(-)
~/test task$ git rev-list --left-right task..master --
~/test task$ git rev-list --left-right master..task --
 >96492ee80143f43417b00699ff29330d0027df7f


Thanks,

Tim

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: using rev-list to tell if a branch is behind or ahead
  2008-05-20 20:14 using rev-list to tell if a branch is behind or ahead Tim Harper
@ 2008-05-20 20:24 ` Shawn O. Pearce
  0 siblings, 0 replies; 3+ messages in thread
From: Shawn O. Pearce @ 2008-05-20 20:24 UTC (permalink / raw)
  To: Tim Harper; +Cc: git

Tim Harper <timcharper@gmail.com> wrote:
> I'm implementing a ruby interface to git and am wanting to be able to  
> ask if a branch is ahead or behind.
> 
> I looked in the builtin-checkout.c file and see this code:
> 
> 	/* Run "rev-list --left-right ours...theirs" internally... */
...
> It looks like it's calling rev-parse.  But, when I call it with the  
> same arguments (using branches or commit sha1's), it only will list  
> commits that are in right and not in left.  I need it to show both  
> ways: commits that are in the right and not in left, and commits that  
> are in the left but not in right.
> 
> Do I need to call rev-parse twice to achieve this?

No.  You need to use the triple dot operator ("...") not the
double dot operator ("..").

> Here's a sample of what I'm trying currently:
> ~ $ mkdir test
> ~ $ cd test/
> ~/test $ git init
> Initialized empty Git repository in .git/
> ~/test $ git
> ~/test $ echo content > file.txt
> ~/test $ git add file.txt && git commit -m "Initial commit"
> Created initial commit f5e4160: Initial commit
>  1 files changed, 1 insertions(+), 0 deletions(-)
>  create mode 100644 file.txt
> ~/test master$ git co -b task
> Switched to a new branch "task"
> ~/test task$ echo changes >> file.txt
> ~/test task$ git add file.txt && git commit -m "Some changes"
> Created commit 96492ee: Some changes
>  1 files changed, 1 insertions(+), 0 deletions(-)
> ~/test task$ git rev-list --left-right task..master --

You need an extra "." between task and master, this should be:

  git rev-list --left-right task...master --

> ~/test task$ git rev-list --left-right master..task --
> >96492ee80143f43417b00699ff29330d0027df7f

-- 
Shawn.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: using rev-list to tell if a branch is behind or ahead
       [not found] <F3CD27F7-D509-41A3-B3C8-0B9124537DDA@gmail.com>
@ 2008-05-20 20:33 ` Tim Harper
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Harper @ 2008-05-20 20:33 UTC (permalink / raw)
  To: git

On May 20, 2008, at 2:24 PM, Shawn O. Pearce wrote:
> Tim Harper <timcharper@gmail.com> wrote:
>> I'm implementing a ruby interface to git and am wanting to be able to
>> ask if a branch is ahead or behind.
>>
>> I looked in the builtin-checkout.c file and see this code:
>>
>> /* Run "rev-list --left-right ours...theirs" internally... */
> ...
>> It looks like it's calling rev-parse.  But, when I call it with the
>> same arguments (using branches or commit sha1's), it only will list
>> commits that are in right and not in left.  I need it to show both
>> ways: commits that are in the right and not in left, and commits that
>> are in the left but not in right.
>>
>> Do I need to call rev-parse twice to achieve this?
>
> No.  You need to use the triple dot operator ("...") not the
> double dot operator ("..").
>

Ugh... figured it was something simple.  Funny thing is, when I was  
looking at the c code:
strcpy(symmetric, sha1_to_hex(ours->object.sha1));
strcpy(symmetric + 40, "...");
strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));

I was like "I wonder why the 3rd line is +43, and not +42".



>> Here's a sample of what I'm trying currently:
>> ~ $ mkdir test
>> ~ $ cd test/
>> ~/test $ git init
>> Initialized empty Git repository in .git/
>> ~/test $ git
>> ~/test $ echo content > file.txt
>> ~/test $ git add file.txt && git commit -m "Initial commit"
>> Created initial commit f5e4160: Initial commit
>> 1 files changed, 1 insertions(+), 0 deletions(-)
>> create mode 100644 file.txt
>> ~/test master$ git co -b task
>> Switched to a new branch "task"
>> ~/test task$ echo changes >> file.txt
>> ~/test task$ git add file.txt && git commit -m "Some changes"
>> Created commit 96492ee: Some changes
>> 1 files changed, 1 insertions(+), 0 deletions(-)
>> ~/test task$ git rev-list --left-right task..master --
>
> You need an extra "." between task and master, this should be:
>
> git rev-list --left-right task...master --
>
>> ~/test task$ git rev-list --left-right master..task --
>>> 96492ee80143f43417b00699ff29330d0027df7f
>
> -- 
> Shawn.

That did it, thanks

Tim

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-05-20 20:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-20 20:14 using rev-list to tell if a branch is behind or ahead Tim Harper
2008-05-20 20:24 ` Shawn O. Pearce
     [not found] <F3CD27F7-D509-41A3-B3C8-0B9124537DDA@gmail.com>
2008-05-20 20:33 ` Tim Harper

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).