From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 9DCCA1F5A0 for ; Fri, 10 Feb 2023 03:58:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1676001532; bh=P/W+pv0z8IeGW6YLTdLW/USNe/DaghfYtDrE/9WCvmk=; h=From:To:Subject:Date:From; b=CCLgTJLwldYJobt4uij/iDmGTOWYjjtHOhfan3xbJQoA209qDTunQsxAj51htBEN7 T9fqBJmmjwyANYxL6lPt2lfFB4uefF11F0PFzu/kuGt+N8ca7rIpMuumkoiT5iCr4S 7flvrJtnvfSXaAYz9WJu5BhP/SmFmPfq9BUzQztw= From: Eric Wong 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 Message-Id: <20230210035852.2918125-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 {