jacquesg/p5-Git-Raw

rev-parse support is missing

waterkip opened this issue ยท 9 comments

Hello, I'm playing a bit with Git::Raw (I like it), but it seems git rev-parse is missing, is that correct?

Have a look at t/25-revparse.t for usage examples.

I see, I was looking for in particular for these two:

rev-parse --abbrev-ref HEAD
rev-parse --show-toplevel

libgit2 and Git::Raw provide building blocks to implement these yourself.

rev-parse --show-toplevel I think is as simple as calling $repo->workdir.
rev-parse --abbrev-ref HEAD I think could be implemented by calling Git::Raw::Commit->lookup ($repo, $id) where you chop off characters until you get an error? Would expect it to be a Git::Raw::Error->EAMBIGUOUS

The first one doesn't work when you aren't in the top of your working dir:

$ cat t/foo.pl
#!/usr/bin/perl
use warnings;
use strict;
use Git::Raw::Repository;
use Cwd qw(cwd);
my $repo = Git::Raw::Repository->open(cwd());
print $repo->workdir, $/;
14:10 pts/3 0 wesleys@neptune:/home/wesleys/code/git-repo
$ perl t/foo.pl
/home/wesleys/code/git-repo/
14:10 pts/3 0 wesleys@neptune:/home/wesleys/code/git-repo
$ cd t
14:10 pts/3 0 wesleys@neptune:/home/wesleys/code/git-repo/t
$ perl foo.pl
could not find repository from '/home/wesleys/code/git-repo/t' at foo.pl line 814:11 pts/3 2 wesleys@neptune:/home/wesleys/code/git-repo/t
$

Use discover instead of open.

That works! Thanks.

The latter command spits out the branch you are in, and I also want something that returns the tracking branch which I do with git rev-parse --abbrev-ref @{u}

Check out the Git::Raw::Branch documentation. It has lookup, upstream_name and maybe remote_name which I think would be of use here.

Also, $repo->head should give you a Git::Raw::Reference for HEAD, which has name etc.

Will do!