-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-atomic-update
91 lines (79 loc) · 2.74 KB
/
git-atomic-update
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
#!/usr/bin/env bash
function _display_help {
echo ""
echo "==============================================================================="
echo "Atomic branch update tool:"
echo -e "\tThis command assists in maintaining a series of branches that are used to"
echo -e "\tenforce an atomic commit behavior at the PR level. It assumes the each"
echo -e "\tbranch properly declares a dependency on another branch until it reaches"
echo -e "\tthe branch origin."
echo ""
echo -e "Example update:"
echo -e "\tcurrent branch is: jdoe0001/issue_401_fix_layer"
echo -e "\t$ git atomic-update"
echo -e "\tThis will walk down the parent branches until it reaches master at which point"
echo -e "\tit begins to update the branches to include all changes back to the parent."
echo ""
echo -e "\tEach branch contains a single commit work item, and may contain CR corrections."
echo -e "\tLive Simply."
echo ""
echo -e "Optional flags:"
echo -e "\t--push\tPublish local commits found on each branch visited"
echo -e "\t-h\tDisplay this menu"
echo "==============================================================================="
echo ""
}
REPO_REMOTE="$(git remote)"
DEFAULT_BRANCH=$(git remote show "${REPO_REMOTE}" | awk '/HEAD branch/ {print $NF}')
start_branch=
branch_prefix=
function _do_update {
local branch_name="${1}"
local stop_recursion="false"
local parent_branch=
parent_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u})
#
# Recursively call down to build a stack up until we reach the
# origin of everything
#
if [ -n "${branch_name}" ]; then
if [[ "${parent_branch}" = "${REPO_REMOTE}/"* ]]; then
stop_recursion="true"
fi
if [ "${branch_name}" = "${DEFAULT_BRANCH}" ]; then
stop_recursion="true"
fi
if [ "${stop_recursion}" != "true" ]; then
git checkout "${parent_branch}"
_do_update "${parent_branch}"
git checkout "${branch_name}"
fi
fi
git pull --prune --rebase || exit #?
if [ -n "${branch_prefix}" ] && [ "$(git rev-list --count ${parent_branch}..HEAD)" -ne 0 ]; then
git push "${REPO_REMOTE}" "${branch_name}:${branch_prefix}${branch_name}" --force-with-lease || exit $?
fi
}
if [ -n "${1}" ]; then
case "${1}" in
--push)
if ! branch_prefix=$(git config --get user.email)
then
echo "Global git user.email is not set. Local commits will not be published"
branch_prefix=
break
fi
branch_prefix="$(echo ${branch_prefix} | awk -F"@" '{print $1}')/"
;;
*)
_display_help
exit
;;
esac
fi
if ! start_branch=$(git symbolic-ref --short -q HEAD)
then
echo "Currently not on a branch, this script can only work on an active branch"
exit 1
fi
_do_update "${start_branch}"