diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2019-09-23 07:19:18 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2019-09-23 07:19:18 -0700 |
commit | a904e3f4868bff70cd57bc3df501fdde3553a4cb (patch) | |
tree | a08426bbe2d19b73fb30635150fb642bd92a4368 /bin/git-branchmove | |
parent | a48ec487cdfddf35fcf61a936bec3c63d704934b (diff) | |
download | dotfiles-a904e3f4868bff70cd57bc3df501fdde3553a4cb.tar.gz |
attempt git-branchmove code cleanup
Diffstat (limited to 'bin/git-branchmove')
-rwxr-xr-x | bin/git-branchmove | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/bin/git-branchmove b/bin/git-branchmove index 5c9f29bb..c4b2e657 100755 --- a/bin/git-branchmove +++ b/bin/git-branchmove @@ -78,8 +78,7 @@ try { }; # process arguments -die "git-branchmove: not enough arguments\n" - if (scalar @ARGV < 3); +die "git-branchmove: not enough arguments\n" if @ARGV < 3; my $attempt_detach = 0; if ($ARGV[0] eq '-d' or $ARGV[0] eq '--detach') { $attempt_detach = 1; @@ -87,7 +86,7 @@ if ($ARGV[0] eq '-d' or $ARGV[0] eq '--detach') { } my ($op, $remote, @patterns) = @ARGV; die "git-branchmove: unknown operation\n" - unless ($op eq 'get' or $op eq 'put'); + unless $op eq 'get' or $op eq 'put'; # is this a named remote or a git URL? my $named_remote = $remote =~ m|:| or $remote =~ m|^[/.]|; @@ -95,7 +94,7 @@ my $named_remote = $remote =~ m|:| or $remote =~ m|^[/.]|; # Attempt to determine how we might be able to run commands in the # remote repo. This will only be used if we need to try to detach the # remote HEAD. These regexps are lifted from Ian's script -my $rurl, my $rrune, my $rdir; +my ($rurl, $rrune, $rdir); if ($named_remote) { # this will expand insteadOf and pushInsteadOf $rurl = `git remote get-url --push $remote`; @@ -105,18 +104,15 @@ if ($named_remote) { $rurl = `git ls-remote --get-url $remote`; } if ($rurl =~ m#^ssh://([^:/]+)(?:\:(\w+))?#) { - $rdir = "$'\''"; + $rdir = "$'\''"; $rrune = "ssh "; - if ($2) { - $rrune .= " -p $3"; - } + $rrune .= " -p $3" if $2; $rrune .= "$1"; } elsif ($rurl =~ m#^([-+_.0-9a-zA-Z\@]+):(?!//|:)#) { - $rdir = "$'\''"; + $rdir = "$'\''"; $rrune = "ssh $1"; } elsif ($rurl =~ m#^[/.]#) { $rdir = $rurl; - $rrune = undef; } # If we don't prefix the patterns, we might match branches the user @@ -124,43 +120,40 @@ if ($rurl =~ m#^ssh://([^:/]+)(?:\:(\w+))?#) { my @branch_pats = map { s|^|[r]efs/heads/|r } @patterns; # get lists of branches, prefixed with 'refs/heads/' in each case -my @source_branches, my @dest_branches; +my (@source_branches, @dest_branches); my @local_branches = map { - my ($hash, undef, $ref) = split(/\s/, $_); + my ($hash, undef, $ref) = split ' '; { hash => $hash, ref => $ref } } $git->for_each_ref(@branch_pats); my @remote_branches = map { - my ($hash, $ref) = split(/\s/, $_); + my ($hash, $ref) = split ' '; { hash => $hash, ref => $ref } } $git->ls_remote($remote, @branch_pats); if ($op eq 'put') { @source_branches = @local_branches; - @dest_branches = @remote_branches; + @dest_branches = @remote_branches; } elsif ($op eq 'get') { @source_branches = @remote_branches; - @dest_branches = @local_branches; + @dest_branches = @local_branches; } # do we have anything to move? -die "git-branchmove: nothing to do" unless (@source_branches); +die "git-branchmove: nothing to do" unless @source_branches; # check for deleting the current branch on the source -my $source_head = undef; +my $source_head; if ($op eq "put") { my @lines = try { $git->symbolic_ref('-q', 'HEAD') }; - if (@lines) { - # the HEAD is not detached - $source_head = $lines[0]; - } + $source_head = $lines[0] if @lines; # the HEAD is not detached } elsif ($op eq "get") { my @lines = try { $git->ls_remote('--symref', $remote, 'HEAD') }; if (@lines and $lines[0] =~ m|^ref: refs/heads/|) { # the HEAD is not detached - $source_head = (split /\s/, $lines[0])[1]; + (undef, $source_head) = split ' ', $lines[0]; } } -if (defined $source_head and - grep /^$source_head$/, map {$_->{ref}} @source_branches) { +if (defined $source_head and grep /^\Q$source_head\E$/, + map { $_->{ref} } @source_branches) { if ($attempt_detach) { if ($op eq 'put') { $git->checkout('--detach'); @@ -184,14 +177,14 @@ if (defined $source_head and foreach my $source_branch (@source_branches) { foreach my $dest_branch (@dest_branches) { die "git-branchmove: would overwrite $source_branch->{ref}" - if ($source_branch->{ref} eq $dest_branch->{ref} - and $source_branch->{hash} ne $dest_branch->{hash}) + if ( $source_branch->{ref} eq $dest_branch->{ref} + and $source_branch->{hash} ne $dest_branch->{hash}); } } # time to actually move the branches -my @refspecs = map { my $ref = $_->{ref}; "$ref:$ref" } @source_branches; -my @nuke_refspecs = map { my $ref = $_->{ref}; ":$ref" } @source_branches; +my @refspecs = map { "$_->{ref}:$_->{ref}" } @source_branches; +my @nuke_refspecs = map { ":$_->{ref}" } @source_branches; if ($op eq 'put') { $git->push('--no-follow-tags', $remote, @refspecs); foreach my $source_branch (@source_branches) { @@ -200,14 +193,15 @@ if ($op eq 'put') { } } elsif ($op eq 'get') { $git->fetch('--no-tags', $remote, @refspecs); - $git->push('--no-follow-tags', $remote, @nuke_refspecs) + $git->push('--no-follow-tags', $remote, @nuke_refspecs); } # if the remote is a named remote, rather than just a URI, update # remote-tracking branches +# TODO if this really should be 'unless' not 'if', document it unless ($named_remote) { foreach my $source_branch (@source_branches) { - my $branch = $source_branch->{ref} =~ s|^refs/heads/||r; + my $branch = $source_branch->{ref} =~ s|^refs/heads/||r; my $tracking_ref = "refs/remotes/$remote/$branch"; if ($op eq 'put') { $git->update_ref($tracking_ref, $source_branch->{hash}); |