user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/2] clone: implement --exit-code
Date: Thu,  5 Jan 2023 11:41:57 +0000	[thread overview]
Message-ID: <20230105114157.2239454-3-e@80x24.org> (raw)
In-Reply-To: <20230105114157.2239454-1-e@80x24.org>

Since public-inbox-clone is now useful for incremental updates
with manifest, --exit-code belongs here, too.
---
 Documentation/public-inbox-clone.pod |  6 ++++++
 lib/PublicInbox/LeiMirror.pm         | 12 +++++++++++-
 script/public-inbox-clone            |  2 +-
 t/clone-coderepo.t                   |  6 +++++-
 4 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/Documentation/public-inbox-clone.pod b/Documentation/public-inbox-clone.pod
index 53acf356..7498aed9 100644
--- a/Documentation/public-inbox-clone.pod
+++ b/Documentation/public-inbox-clone.pod
@@ -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
diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 34a4bd1c..db249ece 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -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 {
diff --git a/script/public-inbox-clone b/script/public-inbox-clone
index 6ed7ab6b..e93ac37b 100755
--- a/script/public-inbox-clone
+++ b/script/public-inbox-clone
@@ -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
diff --git a/t/clone-coderepo.t b/t/clone-coderepo.t
index 66eaa030..2e628963 100644
--- a/t/clone-coderepo.t
+++ b/t/clone-coderepo.t
@@ -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');

      parent reply	other threads:[~2023-01-05 11:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-05 11:41 [PATCH 0/2] public-inbox-clone updates Eric Wong
2023-01-05 11:41 ` [PATCH 1/2] clone: document --project-list and --post-update-hook Eric Wong
2023-01-05 11:41 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230105114157.2239454-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).