From: Yves Orton Date: Wed, 29 Jul 2009 12:19:43 +0000 (+0200) Subject: convert the contents of make_dot_patch into a function and put it in GitUtils X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ed34cb0da30723d9663f4b9ef3c1e64b98d47ad5;p=p5sagit%2Fp5-mst-13.2.git convert the contents of make_dot_patch into a function and put it in GitUtils --- diff --git a/Porting/GitUtils.pm b/Porting/GitUtils.pm new file mode 100755 index 0000000..7abfe91 --- /dev/null +++ b/Porting/GitUtils.pm @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use strict; +use warnings; +use POSIX qw(strftime); + +use base qw/Exporter/; +our @EXPORT_OK=qw(iso_time_with_dot gen_dot_patch); + +sub iso_time_with_dot { + strftime "%Y-%m-%d.%H:%M:%S",gmtime(shift||time) +} + +# generate the contents of a .patch file for an arbitrary commitish, or for HEAD if none is supplied +# assumes the CWD is inside of a perl git repository. If the repository is bare then refs/heads/* +# is used to determine the branch. If the repository is not bare then refs/remotes/origin/* is used +# to determine the branch. (The assumption being that if its bare then this is running inside of +# the master git repo - if its not bare then it is a checkout which may not have all the branches) +sub gen_dot_patch { + my $target= shift || 'HEAD'; + chomp(my ($git_dir, $is_bare, $sha1)=`git rev-parse --git-dir --is-bare-repository $target`); + die "Not in a git repository!" if !$git_dir; + $is_bare= "" if $is_bare and $is_bare eq 'false'; + + # which branches to scan - the order here is important, the first hit we find we use + # so if two branches can both reach a ref we want the right one first. + my @branches=( + 'blead', + 'maint-5.10', + 'maint-5.8', + 'maint-5.8-dor', + 'maint-5.6', + 'maint-5.005', + 'maint-5.004', + # and more generalized searches... + 'refs/heads/*', + 'refs/remotes/*', + 'refs/*', + ); + my $reftype= $is_bare ? "heads" : "remotes/origin"; + my $branch; + foreach my $name (@branches) { + my $refs= $name=~m!^refs/! ? $name : "refs/$reftype/$name"; + my $cmd= "git name-rev --name-only --refs=$refs $sha1"; + chomp($branch= `$cmd`); + last if $branch ne 'undefined'; + } + for ($branch) { + $_ ||= "error"; # hmm, we didnt get /anything/ from name-rev? + s!^\Q$reftype\E/!! || # strip off the reftype + s!^refs/heads/!! || # possible other places it was found + s!^refs/remotes/!! || # ... + s!^refs/!!; # might even be a tag or something weirdo... + s![~^].*\z!!; # strip off how far we are from the item + } + my $tstamp= iso_time_with_dot(`git log -1 --pretty="format:%ct" $sha1`); + chomp(my $describe= `git describe`); + join(" ", $branch, $tstamp, $sha1, $describe); +} + +1; diff --git a/Porting/make_dot_patch.pl b/Porting/make_dot_patch.pl index 4cc7f84..b50fd85 100755 --- a/Porting/make_dot_patch.pl +++ b/Porting/make_dot_patch.pl @@ -20,33 +20,8 @@ use warnings; # # Yves -use POSIX qw(strftime); -sub isotime { strftime "%Y-%m-%d.%H:%M:%S",gmtime(shift||time) } +use lib "Porting"; +use GitUtils qw(gen_dot_patch); +print gen_dot_patch(@ARGV), -t STDOUT ? "\n" : ""; -my $target= shift || 'HEAD'; -chomp(my ($git_dir, $is_bare, $sha1)=`git rev-parse --git-dir --is-bare-repository $target`); -die "Not in a git repository!" if !$git_dir; -$is_bare= "" if $is_bare and $is_bare eq 'false'; -my @branches=( - 'blead', - 'maint-5.10', - 'maint-5.8', - 'maint-5.8-dor', - 'maint-5.6', - 'maint-5.005', - 'maint-5.004', -); -my $reftype= $is_bare ? "heads" : "remotes/origin"; -my $branch; -foreach my $name (@branches) { - my $cmd= "git name-rev --name-only --refs=refs/$reftype/$name $sha1"; - chomp($branch= `$cmd`); - last if $branch ne 'undefined'; -} -$branch ||= "error"; -$branch =~ s!^\Q$reftype\E/!!; -$branch =~ s![~^].*\z!!; -my $tstamp= isotime(`git log -1 --pretty="format:%ct" $sha1`); -chomp(my $describe= `git describe`); -print join(" ", $branch, $tstamp, $sha1, $describe) . "\n";