-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-publish-generated-branch
executable file
·176 lines (139 loc) · 3.63 KB
/
git-publish-generated-branch
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
set -e
VERSION="0"
USAGE="\
usage:
git-publish-generated-branch <BUILD_DIR> --branch-prefix <PREFIX="x-build/">
git-publish-generated-branch <BUILD_DIR> --branch <FULL_NAME_OF_NEW_BRANCH="{prefix}{source-branch}">
required:
[--dir] BUILD_DIR
optional:
-p, --branch-prefix = "x-build/"
-b, --branch = "{prefix}{source-branch}"
--extra-commit-body = ""
--remote = origin
-V, --version
-h, --help
git-publish-generated-branch v${VERSION}
"
while (( $# > 0 )); do
case $1 in
-d|--dir)
shift
BUILD_DIR="$1"
shift
;;
-p|--branch-prefix)
shift
BUILD_BRANCH_PREFIX="$1"
shift
;;
-b|--branch)
shift
BUILD_BRANCH="$1"
shift
;;
--extra-commit-body)
shift
EXTRA_COMMIT_BODY="$1"
shift
;;
# -v|--verbose)
# shift
# VERBOSE_FLAG="-v"
# ;;
-V|--version)
shift
printf "git-publish-generated-branch v${VERSION}"
exit 0
;;
-h|--help)
shift
printf "$USAGE"
exit 0
;;
*)
if [ -z "$BUILD_DIR" ]; then
BUILD_DIR="$1"
shift
else
printf "\nunknown option "$1".\n\n"
exit 1
fi
;;
esac
done
[ -z "$BUILD_DIR" ] && {
printf "$USAGE"
exit 1
}
SOURCE_BRANCH="$(git branch --show-current)"
SOURCE_BRANCH_COMMIT="$(git rev-parse "$SOURCE_BRANCH")"
# both set
[ -n "$BUILD_BRANCH" ] && [ -n "$BUILD_BRANCH_PREFIX" ] && {
>&2 printf "\nWARNING: both --branch-prefix and --branch were specified; ignoring --branch-prefix.\n\n"
unset BUILD_BRANCH_PREFIX
}
# only prefix set
[ -z "$BUILD_BRANCH" ] && [ -n "$BUILD_BRANCH_PREFIX" ] && {
BUILD_BRANCH="${BUILD_BRANCH_PREFIX}${SOURCE_BRANCH}"
unset BUILD_BRANCH_PREFIX
}
# neither set
[ -z "$BUILD_BRANCH" ] && [ -z "$BUILD_BRANCH_PREFIX" ] && {
BUILD_BRANCH_PREFIX="x-build/"
BUILD_BRANCH="${BUILD_BRANCH_PREFIX}${SOURCE_BRANCH}"
unset BUILD_BRANCH_PREFIX
printf "\nneither --branch-prefix nor --branch specified. defaulting to ${BUILD_BRANCH}.\n\n"
}
[ -z "$EXTRA_COMMIT_BODY" ] && EXTRA_COMMIT_BODY=""
TMP_BUILD_DIR="$(mktemp -d)"
mv -v "$BUILD_DIR" "$TMP_BUILD_DIR"
# ~~TODO~~ handle case where user tracks their BUILD_DIR in git (in the non-derived branch).
# edit: wait, in that case they don't need this tool lol
repo_has_uncommitted_changes() {
local HAS=0
local DOES_NOT_HAVE=1
[ -n "$(git status -s)" ] && return $HAS || return $DOES_NOT_HAVE
}
FOUND_UNTRACKED_CHANGES_OUTSIDE_BUILD=0
repo_has_uncommitted_changes && {
FOUND_UNTRACKED_CHANGES_OUTSIDE_BUILD=1
git status
printf "\nWARNING: untracked changes found. resetting them.\n\n"
git checkout -- .
git add .
git reset --hard HEAD
}
# https://stackoverflow.com/a/31914717/9285308
if git rev-parse --quiet --verify "origin/$BUILD_BRANCH"; then
echo "remote branch __does__ exist"
git checkout "$BUILD_BRANCH"
git pull --rebase
else
echo "remote branch __does NOT__ exist"
git checkout --orphan "$BUILD_BRANCH"
fi
git clean -xdf
git rm -rf .
mv -v -f "$TMP_BUILD_DIR/$BUILD_DIR"/* .
git add .
NO_CHANGES_IN_BUILD=0
ALLOW_EMPTY_FLAG=()
COMMIT_TITLE_PREFIX=""
repo_has_uncommitted_changes || {
printf "\nWARNING: 0 changes detected. will create an empty commit.\n\n"
NO_CHANGES_IN_BUILD=1
ALLOW_EMPTY_FLAG=("--allow-empty")
COMMIT_TITLE_PREFIX="(NO CHANGES) "
}
cat <<EOF | git commit ${ALLOW_EMPTY_FLAG[@]} -F-
${COMMIT_TITLE_PREFIX}build $SOURCE_BRANCH_COMMIT from $SOURCE_BRANCH
---
NETWORK_OVERRIDES_BOT_VERSION: 0
FOUND_UNTRACKED_CHANGES_OUTSIDE_BUILD: $FOUND_UNTRACKED_CHANGES_OUTSIDE_BUILD
NO_CHANGES_IN_BUILD: $NO_CHANGES_IN_BUILD
---
$EXTRA_COMMIT_BODY
EOF
git -c push.autoSetupRemote=true push --force