-
Notifications
You must be signed in to change notification settings - Fork 44
/
git-publish.sh
executable file
·78 lines (58 loc) · 1.89 KB
/
git-publish.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
#!/bin/bash
DIR="${1:-$PWD}"
function maybe_rebase {
if [[ $1 == 'pull' ]]; then
git pull --rebase || fail "Please resolve conflicts before continuing."
fi
if [[ $1 == 'rebase' ]]; then
# if rebasing we assume that jungle already fetched, so we try to directly rebase
git rebase || fail "Please resolve conflicts before continuing."
fi
}
function commit {
if [[ $1 == 'pr' ]]; then
echo "Here are the changes you made:"
git diff HEAD
branch_and_commit
else
git commit --verbose --all
fi
}
function branch_and_commit {
read -p "Enter a description of these changes for the commit and related PR (leave empty to skip these changes for now) and press enter:" comment
if [[ -n "$comment" ]]; then
name=${comment// /_}
sanitized_name=${name//[^a-zA-Z0-9\-_]/}
(git checkout -b "PR-${sanitized_name}" || branch_and_commit) && git commit --all -m "$comment" && gh pr create --fill
else
fail "No comment entered, skipping these changes..."
fi
}
function post_commit {
# merge/rebase local changes
maybe_rebase $1
if [[ $2 != 'only' ]]; then
git push && echo "Published changes!"
fi
}
function fail {
printf '%s\n' "$1" >&2 ## Send message to stderr.
exit "${2-1}" ## Return a code specified by $2, or 1 by default.
}
echo "Checking ($2) for changes in $DIR"
cd $DIR
git config core.fileMode false
set +e # Grep succeeds with nonzero exit codes to show results.
if [ -z "$(git status --porcelain)" ]; then
# there are no changes
set -e
echo "No local changes to push"
maybe_rebase $2
else
# there are changes
set -e
# add all changes (including untracked files)
git add --all .
# if there are changes, commit them (needed before being able to rebase)
(commit $3 && post_commit $2 $3) || echo "Skipped..."
fi