Upgrade youtube-dl to version 2020.03.08
[sunny256-utils.git] / git-scanrefs
blob384602ded069ea8fb19d8e8db45fa8ed71b834d2
1 #!/usr/bin/env perl
3 #=======================================================================
4 # git-scanrefs
5 # File ID: 43e51bbe-2825-11e5-9340-000df06acc56
7 # [Description]
9 # Character set: UTF-8
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'help' => 0,
24 'quiet' => 0,
25 'verbose' => 0,
26 'version' => 0,
30 our $progname = $0;
31 $progname =~ s/^.*\/(.*?)$/$1/;
32 our $VERSION = '0.1.0';
34 Getopt::Long::Configure('bundling');
35 GetOptions(
37 'help|h' => \$Opt{'help'},
38 'quiet|q+' => \$Opt{'quiet'},
39 'verbose|v+' => \$Opt{'verbose'},
40 'version' => \$Opt{'version'},
42 ) || die("$progname: Option error. Use -h for help.\n");
44 $Opt{'verbose'} -= $Opt{'quiet'};
45 $Opt{'help'} && usage(0);
46 if ($Opt{'version'}) {
47 print_version();
48 exit(0);
51 my %checked = (); # Contains all potential shas checked
53 exit(main());
55 sub main {
56 # {{{
57 my $Retval = 0;
58 my %printed = ();
60 while (my $line = <>) {
61 chomp($line);
62 my @result = potential_refs($line);
63 for my $curr (@result) {
64 my $act = actual_ref($curr);
65 if (length($act) && !$printed{"$act"}) {
66 print("$act\n");
67 $printed{"$act"} = 1;
72 return $Retval;
73 # }}}
74 } # main()
76 sub potential_refs {
77 my $orig = shift;
78 my $str = $orig;
79 my @retval = ();
80 msg(4, "Entering potential_refs('$orig')");
81 $str =~ s{
82 ([0-9a-f]{7,})
84 my $pot = $1;
85 msg(4, "\$pot = '$pot'");
86 if (!$checked{"$pot"}) {
87 msg(4, "'$pot' is not checked before");
88 push(@retval, $pot);
89 $checked{"$pot"} = 1;
91 '';
92 }xegs;
93 msg(4, "\$str afterwards: '$str'");
94 msg(4, "scalar(retval) = '" . scalar(@retval) . "'");
95 msg(3, "potential_refs('$orig') returns ['" . join("', '", @retval) . "']");
96 return(@retval);
97 } # potential_refs()
99 sub actual_ref {
100 my $ref = shift;
101 chomp(my $result = `git rev-parse --quiet --verify $ref`);
102 msg(4, "is_actual_ref(): \$result = '$result'");
103 my $retval = ($result =~ /^[0-9a-f]{40}/) ? $result : '';
104 msg(4, "is_actual_ref('$ref') returns '$retval'");
105 return($retval);
106 } # actual_ref()
108 sub print_version {
109 # Print program version {{{
110 print("$progname $VERSION\n");
111 return;
112 # }}}
113 } # print_version()
115 sub usage {
116 # Send the help message to stdout {{{
117 my $Retval = shift;
119 if ($Opt{'verbose'}) {
120 print("\n");
121 print_version();
123 print(<<"END");
125 Usage: $progname [options] [file [files [...]]]
127 Options:
129 -h, --help
130 Show this help.
131 -q, --quiet
132 Be more quiet. Can be repeated to increase silence.
133 -v, --verbose
134 Increase level of verbosity. Can be repeated.
135 --version
136 Print version information.
139 exit($Retval);
140 # }}}
141 } # usage()
143 sub msg {
144 # Print a status message to stderr based on verbosity level {{{
145 my ($verbose_level, $Txt) = @_;
147 if ($Opt{'verbose'} >= $verbose_level) {
148 print(STDERR "$progname: $Txt\n");
150 return;
151 # }}}
152 } # msg()
154 __END__
156 # This program is free software; you can redistribute it and/or modify
157 # it under the terms of the GNU General Public License as published by
158 # the Free Software Foundation; either version 2 of the License, or (at
159 # your option) any later version.
161 # This program is distributed in the hope that it will be useful, but
162 # WITHOUT ANY WARRANTY; without even the implied warranty of
163 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
164 # See the GNU General Public License for more details.
166 # You should have received a copy of the GNU General Public License
167 # along with this program.
168 # If not, see L<http://www.gnu.org/licenses/>.
170 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :