-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-publish
executable file
·76 lines (56 loc) · 1.83 KB
/
git-publish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!perl
use v5.20.0;
use warnings;
# PODNAME: git-publish
use Git::BranchManager::Util -all;
use Process::Status;
# 0. get config
my $CONFIG = Git::BranchManager::Util->get_config;
my $primary = $CONFIG->{primary}{remote};
my $main = $CONFIG->{primary}{branch};
my $me = $CONFIG->{my}{remote};
my %IS_IMMORTAL = map {; $_ => 1 } (
'HEAD',
$main,
$CONFIG->{branches}{immortal}->@*,
$CONFIG->{branches}{mirrored}->@*,
);
# 1. determine current head
my $line = `git status --branch --porcelain=v2 | grep branch.head`;
my ($branch) = $line =~ /\A# branch\.head (.+)$/m;
die "Can't compute current branch for some reason!\n" unless length $branch;
# 2. abort if it's immortal
die "Refusing to even consider publishing branch $branch\n"
if $IS_IMMORTAL{$branch} || $branch =~ m{/};
# 3. update my remote
`git fetch $me`;
Process::Status->assert_ok("fetching remote $me");
# 4. if remote/branch does not exist, push and exit
`git show-ref $me/$branch`;
if ($?) {
say "Remote branch $me/$branch does not exist, pushing...";
system("git push $me $branch");
exit;
}
# 5. if local/branch is ff of remote/branch, push and exit
my $is_ff = ! system("git merge-base --is-ancestor $branch $me/$branch");
if ($is_ff) {
system("git push $me $branch");
}
# 6. print diff and get confirmation to push --force-with-lease
my $plan = compute_changes(
$branch,
"$me/$branch",
$main,
);
print_change_plan($plan);
my $must_confirm = grep {; $_->[0] eq 'DROP' } @$plan;
if ($must_confirm) {
die "Can't confirm destructive push without terminal\n" unless -t *STDIN;
say ">>>> This plan looks destructive.";
say ">>>> To continue, enter the branch name ($branch).";
my $line = <STDIN>;
chomp $line;
die "You didn't enter the branch name, aborting.\n" unless $line eq $branch;
}
system("git push --force-with-lease $me $branch");