nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

git-number (3254B)


      1 #!/usr/bin/perl
      2 use strict;
      3 use warnings;
      4 use File::Basename;
      5 
      6 # git-number:
      7 #
      8 
      9 =pod
     10 
     11 =head1 NAME
     12 
     13 git-number
     14 
     15 =head1 SYNOPSIS
     16 
     17     git-number [-h|--color=<when>|-s] [<git-cmd|-c <cmd>> [git-or-cmd-options] [files or numbers]] [-- ...]
     18 
     19 =head1 DESCRIPTION
     20 
     21 When run without arguments, runs git-status and assign numeric ids to filenames shown by git-status.
     22 
     23 When run with arguments, runs <git-cmd> or <cmd>, and replaces any number in
     24 the arguments with the corresponding filename from the previous run of
     25 git-number.
     26 
     27 Any args given after `--' are passed to the underlying command verbatim.
     28 
     29 =head1 OPTIONS
     30 
     31 =over 4
     32 
     33 =item -c <cmd>
     34 
     35 Runs I<E<lt>cmdE<gt>> instead of git on the given arguments.
     36 All arguments that follows <cmd> will be passed on to <cmd>.
     37 
     38 =item -v
     39 
     40 Show version information
     41 
     42 =item -h
     43 
     44 Show this help message
     45 
     46 =item -s
     47 
     48 =item --column
     49 
     50 =item -u(no|normal|all)
     51 
     52 =item --color=(always|auto|never)
     53 
     54 These options are similar to git-status'.
     55 
     56 =back
     57 
     58 =head1 SEE ALSO
     59 
     60     git-id(1), git-list(1)
     61 
     62 =head1 VERSION
     63 
     64 1.0.1
     65 =cut
     66 my $VERSION = '1.0.1'; # Don't forget to update the one in pod!
     67 
     68 my $me = basename(__FILE__);
     69 sub show_usage_and_exit {
     70     my ($exit_value) = @_;
     71     $exit_value = 0 if ! defined $exit_value;
     72     system(qq[perldoc "$0"]);
     73     exit $exit_value;
     74 }
     75 
     76 sub run_cmd {
     77     my ($cmd) = @_;
     78     system($cmd);
     79     if ($? == -1) {
     80         print STDERR "$me: $!\n";
     81         return 1;
     82     }
     83     return $? >> 8;
     84 }
     85 
     86 my $run = 'git';
     87 my $color = 'always';
     88 my $status_opt = '';
     89 my $passthru_args = '';
     90 if (grep(/^--$/, @ARGV) == 0 && $ARGV[-1] && $ARGV[-1] eq '.') {
     91     $ARGV[-1] = '--';
     92     push @ARGV, '.';
     93 }
     94 while (scalar @ARGV && $ARGV[0] =~ /^-/) {
     95     my $option = shift @ARGV;
     96     if ($option eq '--') {
     97         $passthru_args = join(' ', @ARGV);
     98         @ARGV = ();
     99         last;
    100     }
    101     if ($option eq '-h') {
    102         show_usage_and_exit();
    103     } elsif ($option eq '-v') {
    104         print "$VERSION\n";
    105         exit 0;
    106     } elsif ($option eq '-c') {
    107         $run = shift @ARGV;
    108         last;
    109     } elsif ($option =~ m{--color=(always|auto|never)}) {
    110         $color = $1;
    111     } elsif ($option eq '-s') {
    112         $status_opt .= " --short";
    113     } elsif ($option =~ m{^-u(no|normal|all)$}) {
    114         $status_opt .= " $option";
    115     } elsif ($option =~ m{^--column}) {
    116         $status_opt .= " $option";
    117     } else {
    118         print "Unknown option: $option\n";
    119         exit 1;
    120     }
    121 }
    122 
    123 if ($run eq 'git' && scalar @ARGV == 0) {
    124     my $cmd = join(' ',
    125             "git-id",
    126             "--color=$color",
    127             "$status_opt",
    128             "-- $passthru_args",
    129         );
    130     exit run_cmd($cmd);
    131 }
    132 
    133 my @args;
    134 
    135 my $converted=0;
    136 while (scalar @ARGV) {
    137     my $arg = shift @ARGV;
    138     if ($arg eq '--') {
    139         push(@args, @ARGV);
    140         last;
    141     }
    142 
    143     if ( $arg =~ m/^[0-9][0-9]*$/ ) {
    144         push @args, split("\n", `git-list $arg`);
    145         $converted=1;
    146     } elsif ( $arg =~ m/^[0-9][0-9]*-[0-9][0-9]*$/ ) {
    147         push @args, split("\n", `git-list $arg`);
    148         $converted=1;
    149     } else {
    150         if (index($arg, ' ') != -1) {
    151             $arg = "\"$arg\"";
    152         }
    153         push @args, $arg;
    154     }
    155 }
    156 
    157 my $cmd = "$run " . join(' ', @args);
    158 if (-t STDOUT && $converted) {
    159     print $cmd . "\n";
    160 }
    161 exit run_cmd($cmd);