Skip to contentSkip to conversation

Introduce Git.pm

RFC PATCH In review Petr Baudis 5 participants 7 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Introduce Git.pm

This patch introduces a very basic and barebone Git.pm module with a sketch of how the generic interface would look like; most functions are missing, but this should give some good base. I will expand it in the next days.

Most desirable now is proper error reporting, generic_in() for feeding input to Git commands and the repository() constructor doing some poking with git-rev-parse to get the git directory and subdirectory prefix. Those three are basically the prerequisities for converting git-mv.

Currently Git.pm just wraps up exec()s of Git commands, but even that is not trivial to get right and various Git perl scripts do it in various inconsistent ways. And things will get much more interesting when we get to XS-izing libgit.

This adds the Git.pm, integrates it to the build system and as an example converts the git-fmt-merge-msg.perl script to it (the result is not very impressive since its advantage is not quite apparent in this one, but I just picked up the simplest Git user around).

This is mostly to show up what I have so far and gather some comments on the general shapes of the interface; I guess in the current shape of the API it's suitable for pu at best, but that's not my call.

My current working state is available all the time at

	http://pasky.or.cz/~xpasky/git-perl/Git.pm

and an irregularily updated API documentation is at

http://pasky.or.cz/~xpasky/git-perl/Git.html

---

 Makefile               |   13 ++
 git-fmt-merge-msg.perl |    9 +-
 perl/Git.pm            |  284 ++++++++++++++++++++++++++++++++++++++++++++++++
 perl/Makefile.PL       |   14 ++
 4 files changed, 314 insertions(+), 6 deletions(-)
Makefile +10 -3
index 0887945..59eca6d 100644
@@ -479,7 +479,8 @@ ### Build rules
 
 all: $(ALL_PROGRAMS) $(BUILT_INS) git$X gitk
 
-all:
+all: perl/Makefile
+	$(MAKE) -C perl
 	$(MAKE) -C templates
 
 strip: $(PROGRAMS) git$X
@@ -511,7 +512,7 @@ common-cmds.h: Documentation/git-*.txt
 
 $(patsubst %.perl,%,$(SCRIPT_PERL)) : % : %.perl
 	rm -f $@ $@+
-	sed -e '1s|#!.*perl|#!$(PERL_PATH_SQ)|' \
+	sed -e '1s|#!.*perl\(.*\)|#!$(PERL_PATH_SQ)\1 -I'"$$(make -s -C perl instlibdir)"'|' \
 	    -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
 	    [email protected] >$@+
 	chmod +x $@+
@@ -599,6 +600,9 @@ XDIFF_OBJS=xdiff/xdiffi.o xdiff/xprepare
 	rm -f $@ && $(AR) rcs $@ $(XDIFF_OBJS)
 
 
+perl/Makefile:	perl/Git.pm perl/Makefile.PL
+	(cd perl && $(PERL_PATH) Makefile.PL PREFIX="$(prefix)")
+
 doc:
 	$(MAKE) -C Documentation all
 
@@ -618,6 +622,7 @@ GIT-CFLAGS: .FORCE-GIT-CFLAGS
 	@FLAGS='$(TRACK_CFLAGS)'; \
 	    if test x"$$FLAGS" != x"`cat GIT-CFLAGS 2>/dev/null`" ; then \
 		echo 1>&2 "    * new build flags or prefix"; \
+		echo 1>&2 "    * $$FLAGS != `cat GIT-CFLAGS 2>/dev/null`"; \
 		echo "$$FLAGS" >GIT-CFLAGS; \
             fi
 
@@ -654,6 +659,7 @@ install: all
 	$(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexecdir_SQ)'
 	$(INSTALL) git$X gitk '$(DESTDIR_SQ)$(bindir_SQ)'
 	$(MAKE) -C templates install
+	$(MAKE) -C perl install
 	$(INSTALL) -d -m755 '$(DESTDIR_SQ)$(GIT_PYTHON_DIR_SQ)'
 	$(INSTALL) $(PYMODULES) '$(DESTDIR_SQ)$(GIT_PYTHON_DIR_SQ)'
 	if test 'z$(bindir_SQ)' != 'z$(gitexecdir_SQ)'; \
@@ -721,7 +727,8 @@ clean:
 	rm -f $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz
 	rm -f $(htmldocs).tar.gz $(manpages).tar.gz
 	$(MAKE) -C Documentation/ clean
-	$(MAKE) -C templates clean
+	[ ! -e perl/Makefile ] || $(MAKE) -C perl/ clean
+	$(MAKE) -C templates/ clean
 	$(MAKE) -C t/ clean
 	rm -f GIT-VERSION-FILE GIT-CFLAGS
 
git-fmt-merge-msg.perl +6 -3
index 5986e54..a2da46e 100755
@@ -6,6 +6,9 @@ # Read .git/FETCH_HEAD and make a human 
 # by grouping branches and tags together to form a single line.
 
 use strict;
+use Git;
+
+my $repo = Git->repository();
 
 my @src;
 my %src;
@@ -28,12 +31,12 @@ sub andjoin {
 }
 
 sub repoconfig {
-	my ($val) = qx{git-repo-config --get merge.summary};
+	my ($val) = $repo->generic_oneline('repo-config', '--get', 'merge.summary');
 	return $val;
 }
 
 sub current_branch {
-	my ($bra) = qx{git-symbolic-ref HEAD};
+	my ($bra) = $repo->generic_oneline('symbolic-ref', 'HEAD');
 	chomp($bra);
 	$bra =~ s|^refs/heads/||;
 	if ($bra ne 'master') {
@@ -47,7 +50,7 @@ sub current_branch {
 sub shortlog {
 	my ($tip) = @_;
 	my @result;
-	foreach ( qx{git-log --no-merges --topo-order --pretty=oneline $tip ^HEAD} ) {
+	foreach ($repo->generic('log', '--no-merges', '--topo-order', '--pretty=oneline', $tip, '^HEAD')) {
 		s/^[0-9a-f]{40}\s+//;
 		push @result, $_;
 	}
perl/Git.pm new file +284 -0

284 lines not on this page · raw message

perl/Makefile.PL new file +14 -0
new file mode 100644
index 0000000..703ea98
@@ -0,0 +1,14 @@
+use ExtUtils::MakeMaker;
+
+sub MY::postamble {
+	return <<'MAKE_FRAG';
+instlibdir:
+	@echo $(INSTALLSITELIB)
+
+MAKE_FRAG
+}
+
+WriteMakefile(
+	NAME            => 'Git',
+	VERSION_FROM    => 'Git.pm'
+);
#1 lore raw
Quoted 4 lines · Petr Baudis

Currently Git.pm just wraps up exec()s of Git commands, but even that is not trivial to get right and various Git perl scripts do it in various inconsistent ways. And things will get much more interesting when we get to XS-izing libgit.

git-svn uses fork/pipe/exec very heavily for both reads and writes and also a few bidirectional cases, as well. It would be good exercise to port it to use Git.pm once it's more complete and stable.

XS-ising libgit would be awesome with all the git-hash-object calls in git-svn (and other importers, too, I imagine).

Quoted 3 lines

This is mostly to show up what I have so far and gather some comments on the general shapes of the interface; I guess in the current shape of the API it's suitable for pu at best, but that's not my call.

safe_qx (git-svn) AKA safe_pipe_capture (archimport) would be non-git-specific, but very useful.

perl/Git.pm line 189

Quoted 3 lines
perl/Git.pm new file +3 -0
+	my $pid = open(my $fh, "-|");
+	if ($pid < 0) {
+		die "generic($cmd, @args) open: $!";

In Perl, fork returns undef instead of $pid < 0 on failure. Doing join(' ',@args) will also make the error message more readable, too :)

Signature · 1 line
Eric Wong
#2 lore raw

Dear diary, on Thu, Jun 22, 2006 at 03:03:02AM CEST, I got a letter where Junio C Hamano <[email protected]> said that...

Quoted 7 lines
Quoted 4 lines · Petr Baudis

Currently Git.pm just wraps up exec()s of Git commands, but even that is not trivial to get right and various Git perl scripts do it in various inconsistent ways. And things will get much more interesting when we get to XS-izing libgit.

Quoted 5 lines

We would probably need some moderate to major surgery before doing that depending on which parts. Worrisome are object layer that holds onto parsed objects and keeps flag bits from the previous runs, and lockfile that are cleaned up via atexit() -- there may be others.

Yes, I'm aware of that. Still, some parts should be quite easy...

Dear diary, on Thu, Jun 22, 2006 at 04:31:05AM CEST, I got a letter where Eric Wong <[email protected]> said that...

Quoted 2 lines

XS-ising libgit would be awesome with all the git-hash-object calls in git-svn (and other importers, too, I imagine).

...so I couldn't resist that and did add hash_object() xs-ized function. Looks pretty sweet:

$ time perl -w -I/home/xpasky/lib/perl5/site_perl/5.8.8 -MGit -le 'print Git::generic_oneline("hash-object", "Makefile") for (1..1000)' >/dev/null
real    0m4.800s
user    0m2.072s
sys     0m2.560s
$ time perl -w -I/home/xpasky/lib/perl5/site_perl/5.8.8 -MGit -le 'print Git::hash_object("Makefile") for (1..1000)' >/dev/null
real    0m0.179s
user    0m0.144s
sys     0m0.032s

Dear diary, on Thu, Jun 22, 2006 at 03:03:02AM CEST, I got a letter where Junio C Hamano <[email protected]> said that...

Quoted 4 lines
Quoted 4 lines
Makefile +0 -0
@@ -618,6 +622,7 @@ GIT-CFLAGS: .FORCE-GIT-CFLAGS
 	@FLAGS='$(TRACK_CFLAGS)'; \
 	    if test x"$$FLAGS" != x"`cat GIT-CFLAGS 2>/dev/null`" ; then \
 		echo 1>&2 "    * new build flags or prefix"; \
Quoted 8 lines

Makefile GIT-CFLAGS: .FORCE-GIT-CFLAGS · line 628

Quoted 4 lines
Makefile +1 -0
+		echo 1>&2 "    * $$FLAGS != `cat GIT-CFLAGS 2>/dev/null`"; \
 		echo "$$FLAGS" >GIT-CFLAGS; \
             fi
 

Offtopic; do people mind if we drop $GIT_VERSION from TRACK_CFLAGS? Every time I switch branches or make a new commit it ends up rebuilding everything needlessly.

Please, please, _do_! :-)

I'm sorry about this hunk, I added it to just figure out why the heck it keeps rebuilding everything from scratch and forgot to drop it.

Quoted 8 lines

git-fmt-merge-msg.perl sub andjoin { · line 42

Quoted 6 lines

sub current_branch {

git-fmt-merge-msg.perl +1 -1
-	my ($bra) = qx{git-symbolic-ref HEAD};
+	my ($bra) = $repo->generic_oneline('symbolic-ref', 'HEAD');
 	chomp($bra);
 	$bra =~ s|^refs/heads/||;
 	if ($bra ne 'master') {

Your *_oneline chomps so chomp($bra) is not needed anymore, I think.

Ah, thanks.

Quoted 3 lines
Quoted 1 line

+require Exporter;

Not "use"?

The idiom is to require, I guess; we don't need to import anything. I don't think it matters and I'm not entirely sure about all the implications of the difference between the two, so if you wish, I can change it.

Quoted 4 lines
Quoted 2 lines

+# Methods which can be called as standalone functions as well:
+@EXPORT_OK = qw(generic generic_oneval generic_vocal);

I am not sure generic_xxx is a good name. Perhaps command_xxx?

Technically, other methods can be commands, too. ;-) But yet, it sounds better. I've changed it.

Quoted 4 lines

Also as you say in the comment, "vocal" sounds a bit funny. I wonder if we can use wantarray to detect the case where the caller wants _no_ value, and do the "not piping" optimization in that case.

I think that would be rather confusing since generic_vocal does not make the output go away but makes it go to stdout, while if you just throw away return value of generic(), you expect it to go nowhere.

At least I will change 'vocal' to 'noisy' so that it does not sound so weird.

Dear diary, on Thu, Jun 22, 2006 at 04:31:05AM CEST, I got a letter where Eric Wong <[email protected]> said that...

Quoted 6 lines

perl/Git.pm line 189

Quoted 3 lines · Petr Baudis
perl/Git.pm new file +3 -0
+	my $pid = open(my $fh, "-|");
+	if ($pid < 0) {
+		die "generic($cmd, @args) open: $!";

In Perl, fork returns undef instead of $pid < 0 on failure.

Thanks!

Quoted 1 line

Doing join(' ',@args) will also make the error message more readable, too :)

@args should get automagically interpolated by having the members separated by $" (space by default).

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
#3 lore raw
In reply to Petr Baudis
Quoted 4 lines · Petr Baudis

Currently Git.pm just wraps up exec()s of Git commands, but even that is not trivial to get right and various Git perl scripts do it in various inconsistent ways. And things will get much more interesting when we get to XS-izing libgit.

We would probably need some moderate to major surgery before doing that depending on which parts. Worrisome are object layer that holds onto parsed objects and keeps flag bits from the previous runs, and lockfile that are cleaned up via atexit() -- there may be others.

Makefile GIT-CFLAGS: .FORCE-GIT-CFLAGS · line 628

Quoted 8 lines
Makefile +1 -0
@@ -618,6 +622,7 @@ GIT-CFLAGS: .FORCE-GIT-CFLAGS
 	@FLAGS='$(TRACK_CFLAGS)'; \
 	    if test x"$$FLAGS" != x"`cat GIT-CFLAGS 2>/dev/null`" ; then \
 		echo 1>&2 "    * new build flags or prefix"; \
+		echo 1>&2 "    * $$FLAGS != `cat GIT-CFLAGS 2>/dev/null`"; \
 		echo "$$FLAGS" >GIT-CFLAGS; \
             fi
 

Offtopic; do people mind if we drop $GIT_VERSION from TRACK_CFLAGS? Every time I switch branches or make a new commit it ends up rebuilding everything needlessly.

git-fmt-merge-msg.perl sub andjoin { · line 42

Quoted 6 lines

sub current_branch {

git-fmt-merge-msg.perl +1 -1
-	my ($bra) = qx{git-symbolic-ref HEAD};
+	my ($bra) = $repo->generic_oneline('symbolic-ref', 'HEAD');
 	chomp($bra);
 	$bra =~ s|^refs/heads/||;
 	if ($bra ne 'master') {

Your *_oneline chomps so chomp($bra) is not needed anymore, I think.

In any case, this is an excellent move to clean things up. Various implementations of qx{} will be all in one place so ActiveState folks would have easier time adjusting things to their environment.

Quoted 1 line

+require Exporter;

Not "use"?

Quoted 2 lines

+# Methods which can be called as standalone functions as well:
+@EXPORT_OK = qw(generic generic_oneval generic_vocal);

I am not sure generic_xxx is a good name. Perhaps command_xxx?

Also as you say in the comment, "vocal" sounds a bit funny. I wonder if we can use wantarray to detect the case where the caller wants _no_ value, and do the "not piping" optimization in that case.

#4 lore raw
Quoted 3 lines · Junio C Hamano

Offtopic; do people mind if we drop $GIT_VERSION from TRACK_CFLAGS? Every time I switch branches or make a new commit it ends up rebuilding everything needlessly.

Me too. That's one of the you're too lazy to report, because you
think: "it is not yet annoying enough".

#5 lore raw
In reply to Petr Baudis
Quoted 2 lines · Petr Baudis

This patch introduces a very basic and barebone Git.pm module with a sketch of how the generic interface would look like;

In addition to my previous comments...

perl/Git.pm line 216

Quoted 12 lines
perl/Git.pm new file +12 -0
+  my $version = Git::generic_oneval('version');
+  my $lastrev = $repo->generic_oneval('rev-list', '--all');
+...
+=item generic_oneline ( COMMAND [, ARGUMENTS... ] )
+...
+sub generic_oneline {
+	my $fh = generic(@_);
+	my $line = <$fh>;
+	close $fh;
+	chomp $line;
+	return $line;
+}

Let's have a cleaned up version of Git.pm, Makefile changes and changes to git-fmt-merge-msg.perl that shows how to use the module. Being in-tree would help people to view, use and comment on it.

#6 lore raw
In reply to Petr Baudis
Quoted 4 lines · Petr Baudis

Most desirable now is proper error reporting, generic_in() for feeding input to Git commands and the repository() constructor doing some poking with git-rev-parse to get the git directory and subdirectory prefix. Those three are basically the prerequisities for converting git-mv.

I think that repository() constructor (to be converted in the 'new' constructor I guess) should take 'binbin => "/usr/bin"' argument optionally, to find where git binary is. 'libexecdir' can be also supported, or one can get it from 'git --exec-path' and save.

Quoted 3 lines

Currently Git.pm just wraps up exec()s of Git commands, but even that is not trivial to get right and various Git perl scripts do it in various inconsistent ways.

It would be nice to have also wrapers to reading files from git repository, ate least some generic read_file/readdir.

For example it might be faster to list branches and/or tags by directly reading $GIT_DIR/refs directory than using 'git branch' or 'git tag -l'.

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git