-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ff
More file actions
executable file
·140 lines (119 loc) · 3.9 KB
/
git-ff
File metadata and controls
executable file
·140 lines (119 loc) · 3.9 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
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
#!/bin/bash
# Simple fast-forward without working tree
# Advances the branch ref if it is and ancestror of target.
# No need of a working tree as it does not try to merge but uses git
# merge-base.
#
# use an alias in .gitconfig:
# ff = !git-ff "$@"
#
# author: piotr.lesnicki@ext.bull.net
set -euo pipefail
git_is_ancestor() {
## git merge-base has no `--is-ancestor` option in v1.7
local a="$1"
local b="$2"
if git merge-base --is-ancestor 2>/dev/null ; then
git merge-base --is-ancestor "$a" "$b"
else
local common=$(git merge-base "$a" "$b")
[[ "$common" == $(git rev-parse "$a") ]]
fi
}
git_merge_status() {
## pre: ref and upstream are correct refs
local ref="$1"
local upstream="$2"
if [[ $(git rev-parse "$ref") == $(git rev-parse "$upstream") ]]; then
echo "up-to-date"
elif git_is_ancestor "$ref" "$upstream"; then
n=$(git rev-list "${ref}..${upstream}" | wc -l)
echo "behind $n"
elif git_is_ancestor "$upstream" "$ref"; then
n=$(git rev-list "${upstream}..${ref}" | wc -l)
echo "ahead $n"
else
echo "merge/rebase required"
fi
}
gitff() {
local branch="$1"
if [[ "$branch" == "" ]]; then
branch=$(git rev-parse --abbrev-ref HEAD)
fi
if ! git show-ref --heads -q "$branch"; then
echo "error: branch '$branch' is not a local head reference" >&2
return 1
fi
branch=$(git show-ref --heads "$branch" | cut -d' ' -f2)
if ! branch_sha=$(git rev-parse --verify -q "$branch"); then
echo "error: branch '$branch' invalid" >&2
return 1
fi
if ! upstream=$(git rev-parse --symbolic-full-name \
--verify -q "${branch#refs/heads/}@{upstream}" 2>/dev/null); then
echo "error: branch '$branch' is not tracking anything" >&2
return 1
fi
local status=$(git_merge_status "$branch" "$upstream")
printf "%-20s %-25s : %-s\n" "${branch#refs/}" "[${upstream#refs/}]" "$status"
case "$status" in
behind*)
printf " fast-forwarding %s..%s\n" \
$(git rev-parse --short "$branch") \
$(git rev-parse --short "$upstream")
;;
ahead*)
local remote=$(echo "${upstream#refs/remotes/}" | cut -d'/' -f 1)
local upref=$(echo "${upstream#refs/remotes/}" | cut -d'/' -f 2-)
echo " you can push: git push $remote ${branch#refs/}:${upref}"
return 0
;;
up-to-date) return 0;;
merge*) echo " merge/rebase required"; return 0
;;
*) echo "bad status" >&2; return 1
;;
esac
## here we update the branch
if ! git update-ref "$branch" "$upstream"; then
echo "error: failed to update ref '$branch' to '$upstream'" >&2
return 1
fi
}
gitff_all() {
git for-each-ref --shell --format='ref=%(refname); up=%(upstream);' \
refs/heads/ | while read entry; do
eval "$entry"
if [[ -n "$up" ]]; then
gitff "$ref" || true
fi
done
}
gitff_all_status() {
git for-each-ref --shell --format='ref=%(refname); up=%(upstream);' \
refs/heads/ | while read entry; do
eval "$entry"
if [[ -n "$up" ]]; then
local status=$(git_merge_status "$ref" "$up")
printf "%-20s %-25s : %-s\n" \
"${ref#refs/}" "[${up#refs/}]" "$status"
fi
done
}
if [[ "$BASH_SOURCE" == "$0" ]]; then
arg="${1:-}"
if [[ "$arg" == "-h" ]] || [[ "$arg" == "--help" ]]; then
echo "usage: $0 <branch> || --all || --status" >&2
echo " fast-forward branch <branch>" >&2
exit 1
fi
case "$arg" in
"-s" | "--status") gitff_all_status;;
"-a" | "--all") gitff_all;;
-*)
echo "error: unknown option '$arg'" >&2
exit 1;;
*) gitff "$arg";;
esac
fi