+ bless {
+ %$self, -osdir => $dir, -remote => $rn, -uri => $uri
+ }, __PACKAGE__;
+}
+
+sub fp_done {
+ my ($self, $go_fetch) = @_;
+ my $fh = delete $self->{-show_ref} // die 'BUG: no show-ref output';
+ seek($fh, SEEK_SET, 0) or die "seek(show_ref): $!";
+ $self->{-ent} // die 'BUG: no -ent';
+ my $A = $self->{-ent}->{fingerprint} // die 'BUG: no fingerprint';
+ my $B = sha1_hex(do { local $/; <$fh> } // die("read(show_ref): $!"));
+ return if $A ne $B; # $go_fetch->DESTROY fires
+ $go_fetch->cancel;
+ $self->{lei}->qerr("# $self->{-key} up-to-date");
+}
+
+sub cmp_fp_fetch {
+ my ($self, $go_fetch) = @_;
+ # $go_fetch is either resume_fetch or fgrp_enqueue
+ my $new = $self->{-ent}->{fingerprint} // die 'BUG: no fingerprint';
+ my $key = $self->{-key} // die 'BUG: no -key';
+ if (my $cur_ent = $self->{-local_manifest}->{$key}) {
+ # runs go_fetch->DESTROY run if eq
+ return $go_fetch->cancel if $cur_ent->{fingerprint} eq $new;
+ }
+ my $dst = $self->{cur_dst} // $self->{dst};
+ my $cmd = ['git', "--git-dir=$dst", 'show-ref'];
+ my $opt = { 2 => $self->{lei}->{2} };
+ open($opt->{1}, '+>', undef) or die "open(tmp): $!";
+ $self->{-show_ref} = $opt->{1};
+ my $done = PublicInbox::OnDestroy->new($$, \&fp_done, $self, $go_fetch);
+ start_cmd($self, $cmd, $opt, $done);
+}
+
+sub resume_fetch_maybe {
+ my ($self, $uri, $fini) = @_;
+ my $go_fetch = PublicInbox::OnDestroy->new($$, \&resume_fetch, @_);
+ cmp_fp_fetch($self, $go_fetch) if $self->{-ent} &&
+ defined($self->{-ent}->{fingerprint});
+}
+
+sub resume_fetch {
+ my ($self, $uri, $fini) = @_;
+ my $dst = $self->{cur_dst} // $self->{dst};
+ my @git = ('git', "--git-dir=$dst");
+ my $opt = +{ map { $_ => $self->{lei}->{$_} } (0..2) };
+ my $rn = 'origin'; # configurable?
+ for ("url=$uri", "fetch=+refs/*:refs/*", 'mirror=true') {
+ my @kv = split(/=/, $_, 2);
+ $kv[0] = "remote.$rn.$kv[0]";
+ next if $self->{dry_run};
+ run_die([@git, 'config', @kv], undef, $opt);
+ }
+ my $cmd = [ @{$self->{-torsocks}}, @git,
+ fetch_args($self->{lei}, $opt), $rn ];
+ start_cmd($self, $cmd, $opt, $fini);
+}
+
+sub fgrp_enqueue_maybe {
+ my ($self, $fgrp) = @_;
+ my $enq = PublicInbox::OnDestroy->new($$, \&fgrp_enqueue, $self, $fgrp);
+ cmp_fp_fetch($self, $enq) if $self->{-ent} &&
+ defined($self->{-ent}->{fingerprint});
+ # $enq->DESTROY calls fgrp_enqueue otherwise
+}
+
+sub fgrp_enqueue {
+ my ($self, $fgrp) = @_;
+ my $opt = { 2 => $self->{lei}->{2} };
+ # --no-tags is required to avoid conflicts
+ my $u = $fgrp->{-uri} // die 'BUG: no {-uri}';
+ my $rn = $fgrp->{-remote} // die 'BUG: no {-remote}';
+ my @cmd = ('git', "--git-dir=$fgrp->{-osdir}", 'config');
+ for ("url=$u", "fetch=+refs/*:refs/remotes/$rn/*", 'tagopt=--no-tags') {
+ my @kv = split(/=/, $_, 2);
+ $kv[0] = "remote.$rn.$kv[0]";
+ $self->{dry_run} ? $self->{lei}->qerr("# @cmd @kv") :
+ run_die([@cmd, @kv], undef, $opt);
+ }
+ push @{$self->{fgrp_todo}->{$fgrp->{-osdir}}}, $fgrp;