#!/usr/bin/perl use strict; use warnings; use File::Basename; # git-number: # =pod =head1 NAME git-number =head1 SYNOPSIS git-number [-h|--color=|-s] [> [git-or-cmd-options] [files or numbers]] [-- ...] =head1 DESCRIPTION When run without arguments, runs git-status and assign numeric ids to filenames shown by git-status. When run with arguments, runs or , and replaces any number in the arguments with the corresponding filename from the previous run of git-number. Any args given after `--' are passed to the underlying command verbatim. =head1 OPTIONS =over 4 =item -c Runs IcmdE> instead of git on the given arguments. All arguments that follows will be passed on to . =item -v Show version information =item -h Show this help message =item -s =item --column =item -u(no|normal|all) =item --color=(always|auto|never) These options are similar to git-status'. =back =head1 SEE ALSO git-id(1), git-list(1) =head1 VERSION 1.0.1 =cut my $VERSION = '1.0.1'; # Don't forget to update the one in pod! my $me = basename(__FILE__); sub show_usage_and_exit { my ($exit_value) = @_; $exit_value = 0 if ! defined $exit_value; system(qq[perldoc "$0"]); exit $exit_value; } sub run_cmd { my ($cmd) = @_; system($cmd); if ($? == -1) { print STDERR "$me: $!\n"; return 1; } return $? >> 8; } my $run = 'git'; my $color = 'always'; my $status_opt = ''; my $passthru_args = ''; if (grep(/^--$/, @ARGV) == 0 && $ARGV[-1] && $ARGV[-1] eq '.') { $ARGV[-1] = '--'; push @ARGV, '.'; } while (scalar @ARGV && $ARGV[0] =~ /^-/) { my $option = shift @ARGV; if ($option eq '--') { $passthru_args = join(' ', @ARGV); @ARGV = (); last; } if ($option eq '-h') { show_usage_and_exit(); } elsif ($option eq '-v') { print "$VERSION\n"; exit 0; } elsif ($option eq '-c') { $run = shift @ARGV; last; } elsif ($option =~ m{--color=(always|auto|never)}) { $color = $1; } elsif ($option eq '-s') { $status_opt .= " --short"; } elsif ($option =~ m{^-u(no|normal|all)$}) { $status_opt .= " $option"; } elsif ($option =~ m{^--column}) { $status_opt .= " $option"; } else { print "Unknown option: $option\n"; exit 1; } } if ($run eq 'git' && scalar @ARGV == 0) { my $cmd = join(' ', "git-id", "--color=$color", "$status_opt", "-- $passthru_args", ); exit run_cmd($cmd); } my @args; my $converted=0; while (scalar @ARGV) { my $arg = shift @ARGV; if ($arg eq '--') { push(@args, @ARGV); last; } if ( $arg =~ m/^[0-9][0-9]*$/ ) { push @args, split("\n", `git-list $arg`); $converted=1; } elsif ( $arg =~ m/^[0-9][0-9]*-[0-9][0-9]*$/ ) { push @args, split("\n", `git-list $arg`); $converted=1; } else { if (index($arg, ' ') != -1) { $arg = "\"$arg\""; } push @args, $arg; } } my $cmd = "$run " . join(' ', @args); if (-t STDOUT && $converted) { print $cmd . "\n"; } exit run_cmd($cmd);