-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_patch.sh
executable file
·82 lines (70 loc) · 2.29 KB
/
git_patch.sh
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
source "$(dirname "${BASH_SOURCE}")/helper.sh"
cd "${WORKDIR}"
QUIET="${QUIET:-y}"
STARTING_BRANCH="$(git symbolic-ref --short HEAD)"
REPO_ROOT="$(git rev-parse --show-toplevel)"
REBASE_APPLY="${REPO_ROOT}/.git/rebase-apply"
if [[ "$#" -lt 1 ]]; then
echo "${0} patchfile...: patch one or more patch onto branch"
exit 2
fi
if git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; then
echo "!!! Dirty tree. Clean up and try again."
exit 1
fi
if [[ -e "${REBASE_APPLY}" ]]; then
echo "!!! 'git rebase' or 'git am' in progress. Clean up and try again."
exit 1
fi
cd "${REPO_ROOT}"
GIT_AM_CLEANUP=false
function cleanup() {
if [[ "${GIT_AM_CLEANUP}" == "true" ]]; then
echo
echo "+++ Aborting in-progress git am."
git am --abort >/dev/null 2>&1 || true
fi
echo
echo "+++ Returning you to the ${STARTING_BRANCH} branch and cleaning up."
git checkout -f "${STARTING_BRANCH}" >/dev/null 2>&1 || true
}
trap cleanup EXIT
GIT_AM_CLEANUP=true
PATCHES=("$@")
for PATCH in "${PATCHES[@]}"; do
echo
echo "+++ About to attempt patch. To reattempt:"
echo " $ git am -3 ${PATCH}"
echo
git am -3 "${PATCH}" || {
conflicts=false
while unmerged=$(git status --porcelain | grep ^U) && [[ -n ${unmerged} ]] || [[ -e "${REBASE_APPLY}" ]]; do
conflicts=true
echo
echo "+++ Conflicts detected:"
echo
(git status --porcelain | grep ^U) || echo "!!! None. Did you git am --continue?"
if [[ "${QUIET}" =~ ^[yY]$ ]]; then
echo "Aborting." >&2
exit 1
fi
echo
echo "+++ Please resolve the conflicts in another window (and remember to 'git add / git am --continue')"
read -p "+++ Proceed (anything but 'y' aborts the patch)? [y/n] " -r
echo
if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
echo "Aborting." >&2
exit 1
fi
done
if [[ "${conflicts}" != "true" ]]; then
echo "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'"
exit 1
fi
}
done
GIT_AM_CLEANUP=false