#!/usr/bin/perl
our
$VERSION
=
'0.60'
;
git
confirm current_branch
guarantee_a_clean_working_directory
)
;
my
$stash
= guarantee_a_clean_working_directory();
git
"remote update -p origin"
;
warn
"Updating local branches...\n"
;
my
@needs_rebase
= update_local_branches();
warn
"Packing tags...\n"
;
git
"pack-refs"
;
my
$git_dir
= git
"rev-parse --git-dir"
;
if
( -d
"$git_dir/refs/tags"
) {
system
"find $git_dir/refs/tags -depth -type d -empty -exec rmdir {} \\;"
;
}
if
(
@needs_rebase
> 1 ) {
my
$branches
=
join
' and '
,
@needs_rebase
;
warn
"\n"
.
"$branches have local changes which aren't in the\n"
.
"central repository. To bring them up to date, you need to\n"
.
"rebase each one with something like this:\n"
.
"\n"
.
" git checkout master\n"
.
" git rebase origin/master\n"
.
"\n"
.
"Because you've committed directly to multiple branches, gitc\n"
.
"can't start the rebase for you automatically\n"
;
}
elsif
(
@needs_rebase
) {
my
$branch
=
$needs_rebase
[0];
warn
"\n$branch had local changes and may be outdated\n"
;
if
(
$stash
) {
warn
"Since you stashed changes, you'll have to rebase manually\n"
;
}
elsif
( confirm(
'Shall I start a rebase for you?'
) ) {
git
"checkout $branch"
;
exec
"git rebase origin/$branch"
;
}
}
git
"stash apply $stash"
if
$stash
;
exit
;
sub
branch_exists {
my
(
$branch
) =
@_
;
our
$branches
;
if
( not
$branches
) {
my
%all
=
map
{
substr
(
$_
, 2 ) => 1 } git
"branch"
;
$branches
= \
%all
;
}
return
$branches
->{
$branch
};
}
sub
update_local_branches {
my
$current_branch
= current_branch;
for
my
$branch
(
qw( master test stage prod )
) {
my
$branch_exists
= branch_exists(
$branch
);
my
$has_local_commits
=
$branch_exists
? git
"rev-list -n 1 $branch ^origin/$branch"
:
''
;
if
(
$has_local_commits
) {
warn
"You've committed directly to $branch. I'll leave it alone.\n"
;
push
@needs_rebase
,
$branch
;
}
elsif
(
$branch
eq
$current_branch
) {
git
"reset --hard origin/$branch"
;
}
elsif
(
$branch_exists
) {
git
"update-ref -m 'gitc sync' refs/heads/$branch origin/$branch"
;
}
else
{
git
"branch $branch origin/$branch"
;
}
}
return
@needs_rebase
;
}