forked from TyeMcQueen/whitepages-git-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-corb
executable file
·177 lines (144 loc) · 4.42 KB
/
git-corb
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/perl
=head1 NAME
git-corb - Check Out Remote Branch
=head1 DESCRIPTION
If you want to create a local branch that tracks a remote branch,
while using the same name, you can use this command instead of typing
the name several times in lengthy, error-prone command(s). Lots of
special cases are detected and handled.
=head1 SYNOPSIS
git corb [-f | --fetch] [<remote>] <branch>
-f --force - Fetch from the remote before creating a branch
<remote> - A remote repository, defaults to 'origin'
<branch> - The name of a remote branch
-h - Show the help
=cut
use strict;
use warnings;
use Getopt::Long ();
use Pod::Usage ();
use Carp ();
Getopt::Long::Configure( 'bundling_override' );
Main( @ARGV );
exit;
sub Main {
# Command line options
local @ARGV = @_;
my $do_fetch = 0;
Getopt::Long::GetOptions(
'f|fetch' => \ $do_fetch,
'h|help' => \ &Pod::Usage::pod2usage,
)
or Pod::Usage::pod2usage();
# Command line remote and branch
my ( $remote, $branch );
if ( 1 == @ARGV && $ARGV[0] =~ m{(.+)/(.+)} ) {
# If a remote is actually given as part of the branch, split
# them and use it
$remote = $1;
$branch = $2;
@ARGV = ();
}
elsif ( 1 == @ARGV ) {
# If no remote was specified, assume origin
$remote = 'origin';
$branch = $ARGV[0];
@ARGV = ();
}
elsif ( 2 == @ARGV ) {
$remote = $ARGV[0];
$branch = $ARGV[1];
@ARGV = ();
}
else {
# Command line argument handling is done
Pod::Usage::pod2usage();
}
# Fetch the branch from the remote, if requested
if ( $do_fetch ) {
system 'git', 'fetch', $remote, $branch;
exit $? >> 8 if $?;
}
my $track_remote = config( "branch.$branch.remote" );
my $track_branch = config( "branch.$branch.merge" );
$track_branch =~ s{^.+/}{};
if ( $remote eq $track_remote
&& $branch eq $track_branch
) {
print "$branch already tracks $remote/$branch.\n";
print "] git checkout $branch\n";
exec 'git', 'checkout', $branch;
}
if ( $track_remote ) {
die "$branch tracks $track_remote/$track_branch, not $remote/$branch!\n";
}
if ( ! has_tracking_branch( $remote, $branch ) ) {
die "There is no $remote/$branch (tracking) branch!\n";
}
if ( ! local_branch_exists( $branch )
) {
print "Attempting to checkout and have $branch track from $remote:\n";
print "] git checkout -b $branch $remote/$branch\n";
exec 'git', 'checkout', '-b', $branch, "$remote/$branch";
}
if ( any_commits( "$remote/$branch..$branch" ) ) {
die "Making $branch track $remote/$branch would lose information!\n";
}
print "Replacing $branch to track $remote/$branch:\n";
print "] git checkout $remote/$branch # Make sure we don't have $branch checked out\n";
system 'git', 'checkout', "$remote/$branch";
exit $? >> 8 if $?;
print "] git branch -D $branch # Delete non-tracking local branch\n";
system 'git', 'branch', '-D', $branch;
exit $? >> 8 if $?;
print "] git checkout -b $branch $remote/$branch\n";
exec 'git', 'checkout', '-b', $branch, "$remote/$branch";
}
sub any_commits {
my ( $ref_spec ) = @_;
my $rev_list = Run( "git rev-list $ref_spec" );
chomp $rev_list;
return !! $rev_list;
}
sub has_tracking_branch {
my ( $remote, $branch ) = @_;
my $any_commit = Run( "git for-each-ref refs/remotes/$remote/$branch" );
chomp $any_commit;
return !! $any_commit;
}
sub local_branch_exists {
my ( $branch ) = @_;
my $any_commit = Run( "git for-each-ref refs/heads/$branch" );
chomp $any_commit;
return !! $any_commit;
}
sub config {
my ( $name ) = @_;
my $value = `git config $name`;
my $exit_code = $? >> 8;
if ( 0 == $exit_code ) {
chomp $value;
return $value;
}
elsif ( 1 == $exit_code ) {
# git config exits with 1 when the value doesn't exit
return '';
}
else {
Carp::croak(qq{"git config $name" unexpectedly returned exit value $exit_code});
}
}
sub Run {
my $result = `@_`;
if ( $? ) {
my $exit_code = $? >> 8;
Carp::croak(qq{"@_" unexpectedly returned exit value $exit_code});
}
else {
return $result;
}
}
# Local Variables:
# mode: cperl
# indent-tabs-mode: nil
# End: