This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocommit.sh
executable file
·88 lines (75 loc) · 3.38 KB
/
autocommit.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
83
84
85
86
87
88
#!/usr/bin/env bash
## This script is designed to be called regularly from cron. It'll autocommit your changes to the dot-files repository in a smarter way.
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source "${HERE}/lib/script-utils.sh"
function runhere()
{
cd "${HERE}" && "$@"
}
function git_is_busy()
{
for i in rebase-merge rebase-apply MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_LOG; do
if test -e "$(runhere git rev-parse --git-path "$i")"; then
return 0
fi
done
return 1
}
reentrance_check
read -r -a LAST_COMMIT_MSG <<< "$(runhere git log -1 --pretty=%B)"
report_good "Last commit: ${LAST_COMMIT_MSG[*]}"
report_cmd runhere git diff --quiet --exit-code && report_cmd runhere git diff --cached --quiet --exit-code
WC_WAS_CLEAN=$?
report_good "Working copy was clean: 0 == ${WC_WAS_CLEAN}"
report_good
report_good "Fetching new code from origin..."
report_cmd runhere git fetch
test -n "$(runhere git log master..origin/master --oneline)"
PULL_HAS_COMMITS=$?
report_good "Pull has commits: 0 == ${PULL_HAS_COMMITS}"
test -n "$(runhere git log origin/master..master --oneline)"
PUSH_HAS_COMMITS=$?
report_good "Push has commits: 0 == ${PUSH_HAS_COMMITS}"
# First, try to commit existing changes. If the last commit is unpushed and was an auto-commit, amend it to include these changes, otherwise create a new auto-commit.
report_good
if [ ${WC_WAS_CLEAN} -ne 0 ]; then
report_good "Working copy was not clean..."
AMEND=
CHANGES="$(runhere git status -s)"
# Autocommit msg format is "Autocommit from <hostname>: X files changed"
if [ ${PUSH_HAS_COMMITS} -eq 0 ] && [ "${LAST_COMMIT_MSG[0]}" == "Autocommit" ] && [ "${LAST_COMMIT_MSG[1]}" == "from" ] && [ "${LAST_COMMIT_MSG[2]}" == "$(hostname):" ]; then
report_good "Last commit is unpushed and was an autocommit from this machine, amending..."
AMEND="--amend"
CHANGES="$(runhere git log -1 --pretty=%B | tail -n+2)
${CHANGES}"
fi
CHANGES="$(echo "${CHANGES}" | sed -re '/^$/d' | LC_ALL=C sort -u)"
THIS_COMMIT_MSG=( "Autocommit from $(hostname): $(echo "${CHANGES}" | wc -l) files changed ($(date '+%Y-%m-%d %H:%M:%S'))" "${CHANGES}" )
report_cmd runhere git commit ${AMEND} -a "${THIS_COMMIT_MSG[@]/#/-m}"
fi
# Second, pull new code.
report_good
report_good "Pulling new code from origin..."
report_cmd runhere git pullme
# Pulling can kick us into a rebase or merge conflict resolution, check for that.
if git_is_busy; then
report_bad "Pull failed due to conflicts, aborting..."
if [ -d "$(runhere git rev-parse --git-path rebase-merge)" ]; then
report_cmd runhere git merge --abort
elif [ -d "$(runhere git rev-parse --git-path rebase-apply)" ]; then
report_cmd runhere git rebase --abort
fi
exit 1
fi
# Third, if there is an unpushed commit, that wasn't just created or amended (reduce churn) push the commits.
if [ ${WC_WAS_CLEAN} -eq 0 ] && [ ${PUSH_HAS_COMMITS} -eq 0 ]; then
report_good
report_good "Detected unpushed code that wasn't just created or amended, pushing..."
report_cmd runhere git push
fi
# Finally, if there have been changes since last time (commits added locally or pulled in) run the tests? and setup-home.sh.
if [ ${WC_WAS_CLEAN} -ne 0 ] || [ ${PULL_HAS_COMMITS} -eq 0 ]; then
report_good
report_good "Detected changes since last run (commits added locally or pulled from origin), running tests and setup-home.sh..."
report_cmd runhere nice -n 10 ./tests/run.sh && report_cmd runhere ./setup-home.sh
fi