-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-refresh
More file actions
executable file
·97 lines (77 loc) · 2.27 KB
/
git-refresh
File metadata and controls
executable file
·97 lines (77 loc) · 2.27 KB
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
#!/bin/bash
# Refresh tracking branch from its remote repo
RED_ON="\033[0;41m"
RED_OFF="\033[0m"
ERROR="${RED_ON}ERROR${RED_OFF}"
while (( "$#" )); do
if [ "$1" == "-q" ]; then
query_only="yes"
elif [ "$1" == "-f" ]; then
force_it="yes"
elif [[ "$1" != -* ]] && [[ $l_branch == "" ]]; then
l_branch="$1"
else
echo "Unexpected argument: $1"
echo
echo "Refresh tracking branch from its repo."
echo "Syntax: git-refresh [-f] [branch]"
echo " git-refresh -q [branch]"
echo
echo "If no branch is given, current branch is used."
echo "-f Force. Otherwise only fast-forward."
echo "-q Query tracked remote branch."
exit 1
fi
shift
done
curr_branch="$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)"
if [[ "$l_branch" = "" ]]; then
l_branch=$curr_branch
fi
if [ "$l_branch" = "HEAD" ]; then
echo -e "$ERROR: Detached head"
exit 1
fi
remote="$(git config branch.$l_branch.remote)"
if [ "$remote" == "" ]; then
echo -e "$ERROR: Branch '$l_branch' is not tracking"
exit 1
fi
full_r_branch="$(git config branch.$l_branch.merge)"
if [[ $full_r_branch =~ refs/heads/(.+) ]]; then
r_branch=${BASH_REMATCH[1]}
echo "'$l_branch' is tracking '$r_branch' at '$remote'"
else
echo -e "$ERROR: Unknown remote branch format: '$full_r_branch'"
exit 1
fi
if [ "$query_only" == "yes" ] ; then exit 1 ; fi
echo "git fetch $remote $r_branch"
if ! git fetch --no-tags $remote +$r_branch:refs/remotes/$remote/$r_branch ; then
echo -e "$ERROR: Failed to fetch '$remote/$r_branch'"
exit 1
fi
if [[ "$l_branch" == "$curr_branch" ]]; then
if [ "$force_it" == "yes" ]; then
git reset --hard refs/remotes/$remote/$r_branch || exit 1
method="reset --hard"
else
git merge --ff-only refs/remotes/$remote/$r_branch || exit 1
method="fast-forward"
fi
else
# Other branch
if ! git-is-merged $l_branch refs/remotes/$remote/$r_branch > /dev/null ; then
if [[ "$force_it" != "yes" ]]; then
echo -e "$ERROR: Cannot fast-forward '$l_branch' to '$remote/$r_branch'"
exit 1
fi
method="branch -f"
else
method="fast-forward"
fi
git branch -f $l_branch refs/remotes/$remote/$r_branch || exit 1
fi
echo
echo "Branch '$l_branch' was successfully refreshed from '$remote/$r_branch' (by $method)"
echo