This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
git-gitlab-sync
executable file
·135 lines (113 loc) · 3.81 KB
/
git-gitlab-sync
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
#!/usr/bin/env bash
#=============================================================================
# Copyright 2010-2015 Kitware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
USAGE='[<remote>] [<options>...] [--]
OPTIONS
--dry-run
Show what would be changed without actually updating
--autostash
automatically stash/stash pop before and after
'
OPTIONS_SPEC=
SUBDIRECTORY_OK=Yes
. "$(git --exec-path)/git-sh-setup"
egrep_q() {
egrep "$@" >/dev/null 2>/dev/null
}
# Load the project configuration.
require_work_tree_exists
state_dir="$GIT_DIR"/gitlab-sync
#-----------------------------------------------------------------------------
remote=''
autostash="$(git config --bool gitlab.sync.autostash || echo false)"
dry_run=false
# Parse the command line options.
while test $# != 0; do
case "$1" in
--autostash) autostash=true ;;
--no-autostash) autostash=false ;;
--dry-run) dry_run=true ;;
--) shift; break ;;
-*) usage ;;
*) test -z "$remote" || usage ; remote="$1" ;;
esac
shift
done
test $# = 0 || usage
# Default remote.
test -n "$remote" || remote="gitlab"
# Identify and validate the topic branch name.
head="$(git symbolic-ref HEAD)" && topic="${head#refs/heads/}" || topic=''
if test -z "$topic" -o "$topic" = "master"; then
die 'You cant sync the master branch, please checkout the correct a branch with:
git checkout <branch>'
fi
#-----------------------------------------------------------------------------
apply_autostash () {
if test -f "$state_dir/autostash"
then
stash_sha1=$(cat "$state_dir/autostash")
if git stash apply $stash_sha1 2>&1 >/dev/null
then
gettext 'Applied autostash.'
else
git stash store -m "autostash" -q $stash_sha1 ||
die "$(eval_gettext "Cannot store \$stash_sha1")"
gettext 'Applying autostash resulted in conflicts.
Your changes are safe in the stash.
You can run "git stash pop" or "git stash drop" at any time.
'
fi
fi
}
finish_sync () {
apply_autostash &&
{ git gc --auto || true; } &&
rm -rf "$state_dir"
}
#-----------------------------------------------------------------------------
if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
then
gettext 'trying to stash local changes' &&
stash_sha1=$(git stash create "autostash") ||
die "$(gettext 'Cannot autostash')"
mkdir -p "$state_dir" &&
echo $stash_sha1 >"$state_dir/autostash" &&
stash_abbrev=$(git rev-parse --short $stash_sha1) &&
echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
git reset --hard
fi
require_clean_work_tree "sync" "$(gettext "Error syncing \
We are trying to overwrite all local changes on this branch with the version on \
gitlab. Before you do this make sure to stash your changes or commit these \
changes to a different branch.")"
#-----------------------------------------------------------------------------
fetch_stdout=$(git fetch "$remote" $topic); fetch_exit=$?
gettext "$fetch_stdout"
if [ $fetch_exit -eq 0 ]
then
if test "$dry_run" = true
then
reset_stdout=$(git diff --color HEAD..FETCH_HEAD); fetch_exit=$?
gettext "$reset_stdout"
else
reset_stdout=$(git reset --hard FETCH_HEAD); fetch_exit=$?
gettext "$reset_stdout"
fi
fi
finish_sync
# Reproduce the push exit code.
exit $fetch_exit