git-number 3.2 KB

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