-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgit-forward
executable file
·84 lines (74 loc) · 1.86 KB
/
git-forward
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
#!/usr/bin/env bash
app_name="$(basename "${BASH_SOURCE[0]}")"
app_dir="$(dirname "${BASH_SOURCE[0]}")"
print_help() {
expand -t 4 << EOF
Usage:
$app_name [REMOTE]
Fetch and fast forward all remote tracking branches.
If a remote is given, tries to update from remote branches with the same name.
Options:
-?, --help
Show this help information and exit.
EOF
}
args=()
for ((i = 1; i <= $#; i++)); do
opt="${!i}"
case "$opt" in
-\?|--help)
print_help
exit 0
;;
--)
args+=("${@:i+1}")
break
;;
-*)
echo "$app_name: [Error] Unknown option: $1" >&2
exit 1
;;
*)
args+=("$opt")
;;
esac
done
if [[ ${#args[@]} -eq 1 ]]; then
remote="${args[0]}"
elif [[ ${#args[@]} -gt 1 ]]; then
echo "$app_name: [Error] Too many arguments." >&2
echo "Try '$app_name -?' for more information." >&2
exit 2
fi
git rev-parse --git-dir > /dev/null || exit $?
interrupt () {
echo "Interrupted."
exit 1
}
trap interrupt INT
git fetch --all &&
current_branch="$(git rev-parse --abbrev-ref HEAD)" &&
for branch in $(git branch --list --format="%(refname:short)"); do
if [[ -v remote ]]; then
upstream="$remote/$(git rev-parse --verify --quiet --abbrev-ref "$branch")"
has_upstream=0
else
upstream="$(git rev-parse --verify --quiet --abbrev-ref "$branch@{upstream}")"
has_upstream="$?"
fi
if [[ has_upstream -eq 0 ]]; then
branch_sha="$(git rev-parse --quiet --verify "$branch")" &&
upstream_sha="$(git rev-parse --quiet --verify "$upstream")" &&
if git merge-base --is-ancestor "$upstream_sha" "$branch_sha"; then
echo "'$branch' is already up to date with '${upstream%/$branch}'."
continue
fi &&
if [[ "$branch" == "$current_branch" ]]; then
git pull --ff-only --quiet
else
git fetch . --quiet "$upstream:$branch"
fi &&
echo "Updated '$branch' -> '$upstream'" ||
echo "Cannot fast forward '$branch' to '$upstream'"
fi
done