-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgit-force-pull
executable file
·78 lines (69 loc) · 1.86 KB
/
git-force-pull
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
#!/usr/bin/env bash
app_name="$(basename "${BASH_SOURCE[0]}")"
print_help() {
expand -t 4 << EOF
Usage:
$app_name [--all]
Fetch and force pull remote tracking branch(es) by doing a hard reset.
Options:
--all
Force pull all branches instead of just the currently checked out one.
-?, --help
Show this help information and exit.
EOF
}
all=0
while [[ $# -gt 0 ]]; do
case "$1" in
--all)
all=1
shift
;;
-\?|--help)
print_help
exit 0
;;
-*)
echo "$app_name: [Error] Unknown option: $1" >&2
exit 1
;;
*)
echo "$app_name: [Error] Unexpected argument: $1" >&2
exit 1
;;
esac
done
force_pull() {
local branch_full_name="$1"
local branch="${branch_full_name#refs/heads/}"
local upstream="$(git rev-parse --quiet --verify --abbrev-ref "$branch@{upstream}")"
local has_upstream=$?
if [[ has_upstream -eq 0 ]]; then
if git merge-base --is-ancestor "$upstream" "$branch"; then
local abbrev="${upstream%/$branch}"
echo "${branch@Q} is already up to date with ${abbrev@Q}."
else
branch_sha_short="$(git rev-parse --quiet --short --verify "$branch")" &&
upstream_sha_short="$(git rev-parse --quiet --short --verify "$upstream")" &&
if [[ "$branch" == "$current_branch" ]]; then
git reset --quiet --hard "$upstream"
else
git update-ref "$branch_full_name" "$upstream"
fi &&
echo "Update ${branch@Q} ("$branch_sha_short") to ${upstream@Q} ("$upstream_sha_short"): $(git show --no-patch --format="%s" "$upstream")"
fi
fi
}
current_branch="$(git rev-parse --abbrev-ref HEAD)" &&
git fetch --all &&
if [[ all -eq 1 ]]; then
for branch_full_name in $(git for-each-ref --format='%(refname)' refs/heads); do
force_pull "$branch_full_name"
done
else
current_branch_full_name="$(git symbolic-ref --quiet HEAD)"
is_head_attached=$?
if [[ is_head_attached -eq 0 ]]; then
force_pull "$current_branch_full_name"
fi
fi