]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: simplify command generation for git commands
authorEric Wong <e@80x24.org>
Thu, 26 Jan 2017 04:27:02 +0000 (04:27 +0000)
committerEric Wong <e@80x24.org>
Thu, 26 Jan 2017 07:55:23 +0000 (07:55 +0000)
This shortens the code quite a bit at a negligible performance cost,
and the diffstat agrees.

lib/PublicInbox/Git.pm
lib/PublicInbox/RepobrowseGitAtom.pm
lib/PublicInbox/RepobrowseGitCommit.pm
lib/PublicInbox/RepobrowseGitDiff.pm
lib/PublicInbox/RepobrowseGitLog.pm
lib/PublicInbox/RepobrowseGitPatch.pm
lib/PublicInbox/RepobrowseGitPlain.pm
lib/PublicInbox/RepobrowseGitSnapshot.pm
lib/PublicInbox/RepobrowseGitSummary.pm
lib/PublicInbox/RepobrowseGitTree.pm

index 648aaaf0e77dd3de2b148c6305178557fe55b7d9..caca3b093148d43c3cf9d5acf34774bf69cc3bd0 100644 (file)
@@ -50,6 +50,11 @@ sub err ($) {
        $buf;
 }
 
+sub cmd {
+       my $self = shift;
+       [ 'git', "--git-dir=$self->{git_dir}", @_ ];
+}
+
 sub _bidi_pipe {
        my ($self, $batch, $in, $out, $pid) = @_;
        return if $self->{$pid};
@@ -58,9 +63,8 @@ sub _bidi_pipe {
        pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
        pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
 
-       my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
        my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
-       my $p = spawn(\@cmd, undef, $redir);
+       my $p = spawn(cmd($self, qw(cat-file), $batch), undef, $redir);
        defined $p or fail($self, "spawn failed: $!");
        $self->{$pid} = $p;
        $out_w->autoflush(1);
@@ -167,7 +171,7 @@ sub fail {
 
 sub popen {
        my ($self, @cmd) = @_;
-       my $cmd = [ 'git', "--git-dir=$self->{git_dir}" ];
+       my $cmd = cmd($self);
        my ($env, $opt);
        if (ref $cmd[0]) {
                push @$cmd, @{$cmd[0]};
index 87fc60a7c174c860aedd0929676214beef642009..c610d44df7154ad271810e35699b72f99dd0afb3 100644 (file)
@@ -145,11 +145,10 @@ sub call_git_atom {
        my $env = $req->{env};
        my $q =$req->{'q'} = PublicInbox::RepobrowseGitQuery->new($env);
        my $h = $q->{h};
-       my $git_dir = "--git-dir=$git->{git_dir}";
        my $read_log = sub {
-               my $cmd = ['git', $git_dir,
-                               qw(log --no-notes --no-color --abbrev-commit),
-                               $git->abbrev, $ATOM_FMT, "-$max", $h, '--' ];
+               my $cmd = $git->cmd(qw(log --no-notes --no-color
+                                       --abbrev-commit), $git->abbrev,
+                                       $ATOM_FMT, "-$max", $h, '--');
                my $expath = $req->{expath};
                push @$cmd, $expath if $expath ne '';
                my $rdr = { 2 => $git->err_begin };
@@ -161,7 +160,7 @@ sub call_git_atom {
                $env->{'qspawn.response'} = $_[0];
                return $read_log->() if $h ne '';
 
-               my $cmd = [ 'git', $git_dir, qw(symbolic-ref --short HEAD) ];
+               my $cmd = $git->cmd(qw(symbolic-ref --short HEAD));
                my $rdr = { 2 => $git->err_begin };
                my $qsp = PublicInbox::Qspawn->new($cmd, undef, undef, $rdr);
                $qsp->psgi_qx($env, undef, sub {
index 328d2d4044fc62b9063ab16b8b5e5301f37b2826..6eadd2e54d95d47e3028d0f530807f2e21b872bd 100644 (file)
@@ -131,10 +131,9 @@ sub call_git_commit { # RepobrowseBase calls this
        }
 
        my $git = $req->{repo_info}->{git};
-       my $cmd = [ 'git', "--git-dir=$git->{git_dir}", qw(show
-                       -z --numstat -p --encoding=UTF-8
+       my $cmd = $git->cmd(qw(show -z --numstat -p --encoding=UTF-8
                        --no-notes --no-color -c),
-                       $git->abbrev, GIT_FMT, $id, '--' ];
+                       $git->abbrev, GIT_FMT, $id, '--');
        my $rdr = { 2 => $git->err_begin };
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
        $req->{'q'} = $q;
index eb64d1fda0d140864c3bbfb3af510c0116df22e5..e2b7179fb71c11fa697190582c83711db4cdca92 100644 (file)
@@ -40,10 +40,8 @@ sub call_git_diff {
        my $id2 = $q->{id2};
 
        my $git = $req->{repo_info}->{git};
-       my $cmd = [ 'git', "--git-dir=$git->{git_dir}", qw(diff-tree
-                       -z --numstat -p --encoding=UTF-8
-                       --no-color -M -B -D -r),
-                       $id2, $id, '--' ];
+       my $cmd = $git->cmd(qw(diff-tree -z --numstat -p --encoding=UTF-8
+                               --no-color -M -B -D -r), $id2, $id, '--');
        my $expath = $req->{expath};
        push @$cmd, $expath if $expath ne '';
        my $o = { nofollow => 1, noindex => 1 };
index 21c23fd3263aec57ee282a68804a361f39c301cf..85593cb8503695d3242d53aad37fa1dd28b97b37 100644 (file)
@@ -130,15 +130,8 @@ sub call_git_log {
        my $h = $q->{h};
        $h eq '' and $h = 'HEAD';
        my $git = $repo_info->{git};
-       my $git_dir = $git->{git_dir};
-
-       # n.b. no need to escape $h, this -debug line will never
-       # be seen if $h is invalid
-       # XXX but we should probably validate refnames before execve...
-       $req->{-debug} = "git log --git-dir=$git_dir $h --";
-       my $cmd = [ 'git', "--git-dir=$git_dir",
-                       qw(log --no-notes --no-color --abbrev-commit),
-                       $git->abbrev, $LOG_FMT, "-$max", $h, '--' ];
+       my $cmd = $git->cmd(qw(log --no-notes --no-color --abbrev-commit),
+                               $git->abbrev, $LOG_FMT, "-$max", $h, '--');
        my $rdr = { 2 => $git->err_begin };
        my $title = "log: $repo_info->{repo} (" . utf8_html($h). ')';
        $req->{lhtml} = $self->html_start($req, $title) . "\n\n";
index dc6cd7930eac2959d3c706cb4dfe45389217abdc..5ae768e8faf26de00c1d79b1d3f70c44b33d358c 100644 (file)
@@ -24,12 +24,11 @@ sub call_git_patch {
        # limit scope, don't take extra args to avoid wasting server
        # resources buffering:
        my $range = "$id~1..$id^0";
-       my @cmd = ('git', "--git-dir=$git->{git_dir}", @CMD,
-                       $sig." $range", $range, '--');
+       my $cmd = $git->cmd(@CMD, $sig." $range", $range, '--');
        my $expath = $req->{expath};
-       push @cmd, $expath if $expath ne '';
+       push @$cmd, $expath if $expath ne '';
 
-       my $qsp = PublicInbox::Qspawn->new(\@cmd);
+       my $qsp = PublicInbox::Qspawn->new($cmd);
        $qsp->psgi_return($env, undef, sub {
                my ($r) = @_;
                my $h = ['Content-Type', 'text/plain; charset=UTF-8'];
index 8032449859f2c8fd5cef5bd25a3ef23987df66d1..24cc70b042d56de659ccdbca20afdef99aada57e 100644 (file)
@@ -82,8 +82,7 @@ sub git_tree_plain {
 
        $req->{tpfx} = $pfx;
        $req->{tstart} = "<html><head><title>$title</title></head><body>".$t;
-       my $cmd = [ 'git', "--git-dir=$git->{git_dir}",
-               qw(ls-tree --name-only -z), $git->abbrev, $hex ];
+       my $cmd = $git->cmd(qw(ls-tree --name-only -z), $git->abbrev, $hex);
        my $rdr = { 2 => $git->err_begin };
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
        my $env = $req->{env};
index a9751b97e0131f44d58f281fa0c782420d47a527..450fdad6e05bb51ea5f296ce6b9c7119a0a1282a 100644 (file)
@@ -65,8 +65,8 @@ sub call_git_snapshot ($$) { # invoked by PublicInbox::RepobrowseBase::call
                delete $env->{'repobrowse.tree_cb'};
                delete $env->{'qspawn.quiet'};
                my $pfx = "$repo_info->{snapshot_pfx}-$ref/";
-               my $cmd = [ 'git', "--git-dir=$git->{git_dir}", 'archive',
-                               "--prefix=$pfx", "--format=$fmt", $tree ];
+               my $cmd = $git->cmd('archive',
+                               "--prefix=$pfx", "--format=$fmt", $tree);
                my $rdr = { 2 => $git->err_begin };
                my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
                $qsp->psgi_return($env, undef, sub {
@@ -80,8 +80,7 @@ sub call_git_snapshot ($$) { # invoked by PublicInbox::RepobrowseBase::call
                });
        };
 
-       my @cmd = ('git', "--git-dir=$git->{git_dir}",
-                       qw(rev-parse --verify --revs-only));
+       my $cmd = $git->cmd(qw(rev-parse --verify --revs-only));
        # try prefixing "v" or "V" for tag names to get the tree
        my @refs = ("V$ref", "v$ref", $ref);
        $env->{'qspawn.quiet'} = 1;
@@ -98,7 +97,7 @@ sub call_git_snapshot ($$) { # invoked by PublicInbox::RepobrowseBase::call
                }
                my $rdr = { 2 => $git->err_begin };
                my $r = pop @refs;
-               my $qsp = PublicInbox::Qspawn->new([@cmd, $r], undef, $rdr);
+               my $qsp = PublicInbox::Qspawn->new([@$cmd, $r], undef, $rdr);
                $qsp->psgi_qx($env, undef, $env->{'repobrowse.tree_cb'});
        };
        sub {
index f571a4f515e2a3902a941f2267ec72d3ae26dc9b..b6b960289644dd59375b7e6e9ff7a6d70d89f9bd 100644 (file)
@@ -16,7 +16,7 @@ sub call_git_summary {
 
        # n.b. we would use %(HEAD) in for-each-ref --format if we could
        # rely on git 1.9.0+, but it's too soon for that in early 2017...
-       my $cmd = [ 'git', "--git-dir=$git->{git_dir}", qw(symbolic-ref HEAD) ];
+       my $cmd = $git->cmd(qw(symbolic-ref HEAD));
        my $rdr = { 2 => $git->err_begin };
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
        sub {
index a2e38017a81acfdaa2584455594624c7ec1728af..c242fd1a8e0cd98d60e7bceef50a01364ec0dfc0 100644 (file)
@@ -189,8 +189,7 @@ sub git_tree_sed ($) {
 sub git_tree_show {
        my ($req, $hex, $q) = @_;
        my $git = $req->{repo_info}->{git};
-       my $cmd = [ 'git', "--git-dir=$git->{git_dir}", qw(ls-tree -l -z),
-               $git->abbrev, $hex ];
+       my $cmd = $git->cmd(qw(ls-tree -l -z), $git->abbrev, $hex);
        my $rdr = { 2 => $git->err_begin };
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
        my $t = cur_path($req, $q);