From 5ab19a876587b2f5676c00cc00b230e693d81592 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 6 Mar 2018 13:17:18 -0800 Subject: [PATCH] projtool.pl: add [set]hooks command Add a command to conveniently change the current setting of the core.hooksPath value to a local, global or custom setting. Depending on the actual value of $Girocco::Config::reporoot and potential symbolic links in the path, manually setting the correct "canonical" value for a local or global hookspath can sometimes be awkward to get right. Using the new sethooks command greatly simplifies this. Signed-off-by: Kyle J. McKay --- toolbox/projtool.pl | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/toolbox/projtool.pl b/toolbox/projtool.pl index b1b31e3..ff5b510 100755 --- a/toolbox/projtool.pl +++ b/toolbox/projtool.pl @@ -146,6 +146,10 @@ Usage: %s [--quiet] is cleanmirror|reverseorder|summaryonly|statusupdtaes without "set" and only 2 args, just show current flag value + [set]hooks [--force] local | global | + set project hookspath to local, global or + without "set" and only 1 arg, just show current hookspath + [set]autogchack | unset set project autogchack to or "unset" it without "set" just show current autogchack setting if enabled @@ -170,7 +174,7 @@ Usage: %s [--quiet] get show project field - is owner|desc|readme|head|users + is owner|desc|readme|head|hooks|users or |autogchack|| set [--force] @@ -1455,6 +1459,81 @@ sub cmd_sethead { return 0; } +sub cmd_sethooks { + my $force = 0; + shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force'; + @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage; + my $project = get_project($ARGV[0]); + my $projconfig = read_config_file_hash($project->{path}."/config"); + my $ghp = $Girocco::Config::reporoot."/_global/hooks"; + my $rghp = realpath($ghp); + my $lhp = $project->{path}."/hooks"; + my $rlhp = realpath($lhp); + my $ahp = ""; + my $rahp = undef; + if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) { + $ahp = $projconfig->{"core.hookspath"}; + $rahp = realpath($ahp); + } + if (@ARGV == 1) { + if (defined($rahp) && $rahp ne "") { + if ($rahp eq $rghp) { + my $nc = ($ahp eq $ghp ? "" : " non-canonical"); + printf "%s \t(global%s)\n", $ahp, $nc; + } elsif ($rahp eq $rlhp) { + my $nc = ($ahp eq $lhp ? "" : " non-canonical"); + printf "%s \t(local%s)\n", $ahp, $nc; + } elsif ($rahp ne $ahp) { + print "$ahp \t($rahp)\n"; + } else { + print "$ahp\n"; + } + } elsif ($ahp ne "") { + print "$ahp \t(non-existent)\n"; + } + return 0; + } + my $shp = $ARGV[1]; + if (lc($shp) eq "global") { + $shp = $ghp; + } elsif (lc($shp) eq "local") { + $shp = $lhp; + } elsif (substr($shp, 0, 2) eq "~/") { + $shp = $ENV{"HOME"}.substr($shp,1); + } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) { + my $sfx = $2; + my $hd = (getpwnam($1))[7]; + $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd; + } + $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n"; + my $rshp = realpath($shp); + defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n"; + $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n"; + die "refusing to switch from current non-global hookspath without --force\n" + if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp; + if (!$force && defined($rahp) && $rahp ne "") { + if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) { + warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet; + return 0; + } + } + $rshp = $ghp if $rshp eq $rghp; + $rshp = $lhp if $rshp eq $rlhp; + if ($rshp eq $ahp) { + warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet; + return 0; + } + die "refusing to set neither local nor global hookspath without --force\n" + if !$force && $rshp ne $ghp && $rshp ne $lhp; + system($Girocco::Config::git_bin, '--git-dir='.$project->{path}, + 'config', "core.hookspath", $rshp); + my $newval = '"'.$rshp.'"'; + $newval = "global" if $rshp eq $ghp; + $newval = "local" if $rshp eq $lhp; + warn $project->{name}, ": hookspath set to $newval\n" unless $quiet; + return 0; +} + our %boolfields; BEGIN { %boolfields = ( @@ -1673,6 +1752,8 @@ BEGIN { readme => [\&cmd_setreadme, 0], head => [\&cmd_sethead, 0], HEAD => [\&cmd_sethead, 0], + hooks => [\&cmd_sethooks, 0], + hookspath => [\&cmd_sethooks, 0], cleanmirror => [\&cmd_setbool, 1], reverseorder => [\&cmd_setbool, 1], summaryonly => [\&cmd_setbool, 1], @@ -1736,9 +1817,12 @@ BEGIN { setdescription => \&cmd_setdesc, setreadme => \&cmd_setreadme, sethead => \&cmd_sethead, + sethooks => \&cmd_sethooks, + sethookspath => \&cmd_sethooks, setbool => \&cmd_setbool, - setautogchack => \&cmd_setautogchack, + setboolean => \&cmd_setbool, setflag => \&cmd_setbool, + setautogchack => \&cmd_setautogchack, seturl => \&cmd_seturl, setmsgs => \&cmd_setmsgs, setusers => \&cmd_setusers, -- 2.11.4.GIT