unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] lei_mirror: avoid dir/file conflicts in update-ref
Date: Fri, 10 Feb 2023 03:58:52 +0000	[thread overview]
Message-ID: <20230210035852.2918125-1-e@80x24.org> (raw)

Using the files ref backend for git, `delete' and `create'
operations for `update-ref --stdin' need to be processed in
separate transactions to avoid conflicts in cases where a file
becomes a directory (or presumably, vice versa).
---
 lib/PublicInbox/LeiMirror.pm | 47 +++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index afe12bb3..d959b6b6 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -288,6 +288,19 @@ sub upr { # feed `git update-ref --stdin -z' verbosely
 	print $w "$op ", join("\0", @rest, '') or die "print(w): $!";
 }
 
+sub start_update_ref {
+	my ($fgrp) = @_;
+	pipe(my ($r, $w)) or die "pipe: $!";
+	my $cmd = [ 'git', "--git-dir=$fgrp->{cur_dst}",
+		qw(update-ref --stdin -z) ];
+	my $pack = PublicInbox::OnDestroy->new($$, \&satellite_done, $fgrp);
+	start_cmd($fgrp, $cmd, { 0 => $r, 2 => $fgrp->{lei}->{2} }, $pack);
+	close $r or die "close(r): $!";
+	$fgrp->{dry_run} ? undef : $w;
+}
+
+sub upref_warn { warn "E: close(update-ref --stdin): $! (need git 1.8.5+)\n" }
+
 sub fgrp_update {
 	my ($fgrp) = @_;
 	return if !keep_going($fgrp);
@@ -299,14 +312,9 @@ sub fgrp_update {
 	close $srcfh;
 	my %dst = map { chomp; split(/\0/) } (<$dstfh>);
 	close $dstfh;
-	pipe(my ($r, $w)) or die "pipe: $!";
-	my $cmd = [ 'git', "--git-dir=$fgrp->{cur_dst}",
-		qw(update-ref --stdin -z) ];
+	my $w = start_update_ref($fgrp) or return;
 	my $lei = $fgrp->{lei};
-	my $pack = PublicInbox::OnDestroy->new($$, \&satellite_done, $fgrp);
-	start_cmd($fgrp, $cmd, { 0 => $r, 2 => $lei->{2} }, $pack);
-	close $r or die "close(r): $!";
-	return if $fgrp->{dry_run};
+	my $ndel;
 	for my $ref (keys %dst) {
 		my $new = delete $src{$ref};
 		my $old = $dst{$ref};
@@ -315,18 +323,33 @@ sub fgrp_update {
 				upr($lei, $w, 'update', $ref, $new, $old);
 		} else {
 			upr($lei, $w, 'delete', $ref, $old);
+			++$ndel;
 		}
 	}
-	while (my ($ref, $oid) = each %src) {
-		upr($lei, $w, 'create', $ref, $oid);
+	# git's ref files backend doesn't allow directory/file conflicts
+	# between `delete' and `create' ops:
+	if ($ndel && scalar(keys %src)) {
+		$fgrp->{-create_refs} = \%src;
+	} else {
+		while (my ($ref, $oid) = each %src) {
+			upr($lei, $w, 'create', $ref, $oid);
+		}
 	}
-	close($w) or warn "E: close(update-ref --stdin): $! (need git 1.8.5+)\n";
+	close($w) or upref_warn();
 }
 
 sub satellite_done {
 	my ($fgrp) = @_;
-	pack_refs($fgrp, $fgrp->{cur_dst});
-	run_puh($fgrp);
+	if (my $create = delete $fgrp->{-create_refs}) {
+		my $w = start_update_ref($fgrp) or return;
+		while (my ($ref, $oid) = each %$create) {
+			upr($fgrp->{lei}, $w, 'create', $ref, $oid);
+		}
+		close($w) or upref_warn();
+	} else {
+		pack_refs($fgrp, $fgrp->{cur_dst});
+		run_puh($fgrp);
+	}
 }
 
 sub pack_refs {

                 reply	other threads:[~2023-02-10  3:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230210035852.2918125-1-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.
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).