-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-sm
executable file
·99 lines (85 loc) · 3.29 KB
/
git-sm
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
#!/usr/bin/env bash
#set -e
TIMESTAMP=$(date +%Y-%m-%d.%T)
function banner() {
text="$1"
printf "===============================================[ %20s ]=====\n" "${text}"
}
function detect_root() {
toplevel="$(git rev-parse --git-dir | awk -F'.git' '{print $1}')"
cd "${toplevel:-.}"
pwd
}
function super_update() {
banner "SUPER UPDATE"
git pull --ff-only
}
function store_branchnames() {
banner "STORE BRANCHNAMES"
git submodule foreach 'git config user.default-branch $(git rev-parse --abbrev-ref HEAD)'
}
function dirt_check() {
banner "DIRT CHECK"
git submodule foreach ' \
if [ -n "$(git diff-index HEAD --)" ]; then \
git stash save --quiet "autostash '${TIMESTAMP}'" && \
git config user.autostashed true && \
echo "\tStashed dirt."; \
fi; \
'
}
function submodule_update() {
banner "SUBMODULE UPDATE"
git submodule update --init
}
function checkout() {
banner "CHECKOUT"
default_branch="$(git config user.default-branch)"
git submodule foreach ' \
if [[ -n "${default_branch}" ]]; then \
git checkout ${default_branch}; \
else \
git checkout master || true; \
fi; \
'
}
function update() {
banner "UPDATE"
git submodule foreach ' \
if [[ "HEAD" == "$(git rev-parse --abbrev-ref HEAD)" ]]; then \
echo "\tCannot update detached head."; \
else \
git pull --rebase; \
fi; \
'
}
function poption() {
banner "POP-TION"
git submodule foreach ' \
if [[ -n "$(git config user.autostashed)" ]]; then \
git stash pop --index --quiet && \
git config --unset user.autostashed && \
echo "\tRestored dirt."; \
fi; \
'
}
function save_checkpoint() {
banner "SAVE CHECKPOINT"
git commit --message="checkpoint ${TIMESTAMP}" -- \
.gitmodules $(awk -F\" '/submodule/{printf $2 " "}' .gitmodules)
default_remote=$(git config branch.$(git rev-parse --abbrev-ref HEAD).remote)
echo "git push ${default_remote} HEAD"
}
function run() {
detect_root
super_update
store_branchnames
dirt_check
submodule_update
checkout
update
poption
save_checkpoint
}
run
# vim: set ft=sh ve=all: