]> Sergey Matveev's repositories - public-inbox.git/commitdiff
clone: implement --exit-code
authorEric Wong <e@80x24.org>
Thu, 5 Jan 2023 11:41:57 +0000 (11:41 +0000)
committerEric Wong <e@80x24.org>
Fri, 6 Jan 2023 03:30:04 +0000 (03:30 +0000)
Since public-inbox-clone is now useful for incremental updates
with manifest, --exit-code belongs here, too.

Documentation/public-inbox-clone.pod
lib/PublicInbox/LeiMirror.pm
script/public-inbox-clone
t/clone-coderepo.t

index 53acf35698fd78c97710d461614eed01e9495dcf..7498aed9f46ad93c48279e45c8d4626300579794 100644 (file)
@@ -136,6 +136,12 @@ order specified on the command-line.
 Pass the C<--prune> and C<--prune-tags> flags to L<git-fetch(1)>
 calls on incremental clones.
 
+=item --exit-code
+
+Exit with C<127> if no updates are done when relying on a manifest.
+Updates include fingerprint mismatches in the manifest, new symlinks,
+new repositories, and removed repositories from the L<--project-list>
+
 =item -k
 
 =item --keep-going
index 34a4bd1cb2ea5cc8d68beda701d62c37843b1d75..db249eceff64bc759b619ddb4f9c5e6a9cf1f180 100644 (file)
@@ -516,6 +516,7 @@ sub resume_fetch {
                        fetch_args($self->{lei}, $opt), $rn ];
        push @$cmd, '-P' if $self->{lei}->{prune}; # --prune-tags implied
        my $run_puh = PublicInbox::OnDestroy->new($$, \&run_puh, $self, $fini);
+       ++$self->{chg}->{nr_chg};
        start_cmd($self, $cmd, $opt, $run_puh);
 }
 
@@ -533,6 +534,7 @@ sub fgrp_enqueue {
                $fgrp->{dry_run} ? $fgrp->{lei}->qerr("# @cmd @kv") :
                                run_die([@cmd, @kv], undef, $opt);
        }
+       ++$fgrp->{chg}->{nr_chg};
        push @{$FGRP_TODO->{$fgrp->{-osdir}}}, $fgrp;
 }
 
@@ -565,6 +567,7 @@ sub clone_v1 {
                                                "$self->{dst}$ref";
                        }
                }
+               ++$self->{chg}->{nr_chg};
                start_cmd($self, $cmd, $opt, PublicInbox::OnDestroy->new($$,
                                                \&run_puh, $self, $fini));
        }
@@ -750,6 +753,7 @@ sub update_ent {
                                }
                        }
                        symlink($tgt, $ln) or die "symlink($tgt, $ln): $!";
+                       ++$self->{chg}->{nr_chg};
                }
        }
        if (defined(my $t = $self->{-ent}->{modified})) {
@@ -1051,6 +1055,7 @@ EOM
        warn "\t", $_, "\n" for @local;
 
        my (undef, $dn, $bn) = File::Spec->splitpath($f);
+       $self->{chg}->{nr_chg} += scalar(@remote) + scalar(@local);
        atomic_write($dn, $bn, join("\n", @list, ''));
 }
 
@@ -1082,7 +1087,10 @@ sub try_manifest {
        }
 
        # bail out if curl -z/--timecond hit 304 Not Modified, $ft will be empty
-       return $lei->qerr("# $manifest unchanged") if -f $manifest && !-s $ft;
+       if (-f $manifest && !-s $ft) {
+               $lei->child_error(127 << 8) if $lei->{opt}->{'exit-code'};
+               return $lei->qerr("# $manifest unchanged");
+       }
 
        my $m = eval { decode_manifest($ft, $ft, $uri) };
        if ($@) {
@@ -1170,6 +1178,8 @@ W: The following exist and have not been converted to symlinks
 EOM
        dump_project_list($self, $m);
        ft_rename($ft, $manifest, 0666);
+       !$self->{chg}->{nr_chg} && $lei->{opt}->{'exit-code'} and
+               $lei->child_error(127 << 8);
 }
 
 sub start_clone_url {
index 6ed7ab6b9f25fc48cd9b0062a7a88b26c8af7f1d..e93ac37b2fb9ba2433678d574d6a925b62751823 100755 (executable)
@@ -28,7 +28,7 @@ EOF
 GetOptions($opt, qw(help|h quiet|q verbose|v+ C=s@ c=s@ include|I=s@ exclude=s@
        inbox-config=s inbox-version=i objstore=s manifest=s
        project-list|projectslist=s post-update-hook=s@
-       prune|p keep-going|k
+       prune|p keep-going|k exit-code
        dry-run|n jobs|j=i no-torsocks torsocks=s epoch=s)) or die $help;
 if ($opt->{help}) { print $help; exit };
 require PublicInbox::Admin; # loads Config
index 66eaa03076d996011c8a85260f21bcaf2406b292..2e628963079cd047e185cc7ade6149afb6c18958 100644 (file)
@@ -86,7 +86,7 @@ EOM
 }
 
 my $cmd = [qw(-clone --inbox-config=never --manifest= --project-list=
-       --objstore= -p -q), $url, "$tmpdir/dst"];
+       --objstore= -p -q), $url, "$tmpdir/dst", '--exit-code'];
 ok(run_script($cmd), 'clone');
 is(xqx([qw(git config gitweb.owner)], { GIT_DIR => "$tmpdir/dst/a.git" }),
        "Alice\n", 'a.git gitweb.owner set');
@@ -149,6 +149,10 @@ my $test_puh = sub {
        unlink($log) or xbail "unlink: $!";
        ok(run_script($x, $env), "no-op clone @clone_arg w/ post-update-hook");
        ok(!-e $log, "hooks not run on no-op @clone_arg");
+
+       push @$x, '--exit-code';
+       ok(!run_script($x, $env), 'no-op clone w/ --exit-code fails');
+       is($? >> 8, 127, '--exit-code gave 127');
 };
 $test_puh->();
 ok(!-e "$tmpdir/dst/objstore", 'no objstore, yet');