3 #=======================================================================
5 # File ID: 5915171c-8768-11e0-9a68-00023faf1383
6 # Add lpar info of current repo to lpar.git/$1.lpar
9 # ©opyleft 2011– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
17 use Cwd
qw{ abs_path getcwd
};
18 use File
::Path
qw{ make_path
};
36 $progname =~ s/^.*\/(.*?)$/$1/;
37 our $VERSION = '0.00';
38 my $MAX_NAME_LENGTH = 245;
39 my $allowed_str = "Allowed characters: A-Z, a-z, 0-9, '-', '.', '/' and '_'.\n" .
40 "Maximum length: $MAX_NAME_LENGTH chars.\n";
42 Getopt
::Long
::Configure
('bundling');
45 'all|a' => \
$Opt{'all'},
46 'debug' => \
$Opt{'debug'},
47 'help|h' => \
$Opt{'help'},
48 'set|s=s' => \
$Opt{'set'},
49 'verbose|v+' => \
$Opt{'verbose'},
50 'version' => \
$Opt{'version'},
52 ) || die("$progname: Option error. Use -h for help.\n");
54 $Opt{'debug'} && ($Debug = 1);
55 $Opt{'help'} && usage
(0);
56 if ($Opt{'version'}) {
61 if (get_config_value
('lpar.skip') eq 'true') {
62 msg
(0, "lpar.skip is 'true', skip lpar in " . getcwd
());
66 if (defined($Opt{'set'}) && $Opt{'set'} eq '?') {
67 printf("%s\n", get_config_value
('lpar.name'));
71 $Opt{'all'} || system("lpar -a");
73 if (defined($Opt{'set'})) {
74 my $name = $Opt{'set'};
75 my $prev = get_config_value
('lpar.name');
77 if (valid_repo_name
($name)) {
79 msg
(0, "Value of lpar.name is already '$name', doing nothing");
82 msg
(0, "Changing value of lpar.name from '$prev' to '$name'");
83 exec("git", "config", "lpar.name", $name);
86 die("$progname: Invalid value in -s/--set argument\n$allowed_str");
89 msg
(0, "Deleting lpar.name config variable, previous value was '$prev'");
90 exec("git", "config", "--unset", "lpar.name");
94 my $repo = $Opt{'all'} ?
'all' : get_config_value
("lpar.name");
95 length($repo) || die("$progname: lpar.name not defined\n");
96 if (!valid_repo_name
($repo)) {
97 die("$progname: lpar.name contains illegal value\n$allowed_str");
100 my $lpar_dir = $Opt{'all'} ?
"$ENV{'HOME'}/src/git/all-lpar" : "$ENV{'HOME'}/src/git/lpar";
101 my $lpar_file = "$lpar_dir/$repo.lpar.new";
102 -d
$lpar_dir || make_path
($lpar_dir);
104 my @shas = scalar(@ARGV) ?
@ARGV : ();
105 lock_file
($lpar_file);
107 for my $curr (@shas) {
108 chomp(my $sha1 = `git log -1 --format=%H "$curr" 2>/dev/null`);
109 length($sha1) || die("$progname: $curr: Invalid ref, must point to a commit\n");
110 system("git lpar `git branch -a --contains=\"$sha1\" | cut -c3- | grep -v -e '->' -e '(no branch)'` >>$lpar_file");
113 system("git lpar --all >>$lpar_file");
115 system("git for-each-ref >>$lpar_file");
116 unlock_file
($lpar_file);
121 my $lockdir = "$file.lock";
123 until (mkdir($lockdir)) {
124 print(STDERR
"$progname: $file: File is locked, waiting for access...\n");
128 $did_lock && print(STDERR
"$progname: $file: Obtained lock\n");
136 my $lockdir = "$file.lock";
137 rmdir($lockdir) || warn("$progname: $lockdir: Lockdir unexpectedly disappeared");
142 sub valid_repo_name
{
145 my $retval = ($repo =~ /[^\-\.\/0-9A
-Z_a
-z
]/) ?
0 : 1;
146 (!length($repo) || length($repo) > $MAX_NAME_LENGTH) && ($retval = 0);
149 } # valid_repo_name()
151 sub get_config_value
{
155 chomp($retval = `git config --get "$name"`);
158 } # get_config_value()
161 # Print program version {{{
162 print("$progname v$VERSION\n");
168 # Send the help message to stdout {{{
171 if ($Opt{'verbose'}) {
177 Usage: $progname [options] [included_SHA [...]]
182 Dump everything into a single file, ~/src/git/all-lpar/all.lpar .
186 Set the lpar.name variable to X in git config.
187 To delete, specify "". A value of "?" will print the current value.
188 To avoid shell expansion, prefix the question mark with a backslash
189 ("\\") or specify it as "-s?" or "--set=?".
191 Increase level of verbosity. Can be repeated.
193 Print version information.
195 Print debugging messages.
197 If the Git configuration variable lpar.skip is set to 'true' in a
198 specific repository, the command is ignored. This is useful when dealing
199 with shallow clones or grafts where parents or root commits are
200 different from the original repository.
208 # Print a status message to stderr based on verbosity level {{{
209 my ($verbose_level, $Txt) = @_;
211 if ($Opt{'verbose'} >= $verbose_level) {
212 print(STDERR
"$progname: $Txt\n");
219 # Print a debugging message {{{
221 my @call_info = caller;
222 chomp(my $Txt = shift);
223 my $File = $call_info[1];
225 $File =~ s
#^.*/(.*?)$#$1#;
226 print(STDERR
"$File:$call_info[2] $$ $Txt\n");
233 # Plain Old Documentation (POD) {{{
243 [options] [file [files [...]]]
253 =item B<-h>, B<--help>
255 Print a brief help summary.
257 =item B<-v>, B<--verbose>
259 Increase level of verbosity. Can be repeated.
263 Print version information.
267 Print debugging messages.
277 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
281 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
282 This is free software; see the file F<COPYING> for legalese stuff.
286 This program is free software: you can redistribute it and/or modify it
287 under the terms of the GNU General Public License as published by the
288 Free Software Foundation, either version 2 of the License, or (at your
289 option) any later version.
291 This program is distributed in the hope that it will be useful, but
292 WITHOUT ANY WARRANTY; without even the implied warranty of
293 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
294 See the GNU General Public License for more details.
296 You should have received a copy of the GNU General Public License along
298 If not, see L<http://www.gnu.org/licenses/>.
306 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :