diff options
author | Yves Orton <demerphq@gemini.(none)> | 2009-07-29 14:19:43 +0200 |
---|---|---|
committer | Yves Orton <demerphq@gemini.(none)> | 2009-07-30 00:58:38 +0200 |
commit | ed34cb0da30723d9663f4b9ef3c1e64b98d47ad5 (patch) | |
tree | bbdbcbe5824eea3aa3b61ea3e692aa75cb0e475f /Porting/GitUtils.pm | |
parent | b20f263759e67c18c512f1ae5e4c7157c7ec76b1 (diff) | |
download | perl-ed34cb0da30723d9663f4b9ef3c1e64b98d47ad5.tar.gz |
convert the contents of make_dot_patch into a function and put it in GitUtils
Diffstat (limited to 'Porting/GitUtils.pm')
-rwxr-xr-x | Porting/GitUtils.pm | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Porting/GitUtils.pm b/Porting/GitUtils.pm new file mode 100755 index 0000000000..7abfe917d3 --- /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; |