-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-commit-modules
executable file
·158 lines (130 loc) · 3.32 KB
/
git-commit-modules
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
#! /usr/bin/env bash
# Author: Thomas DEBESSE <[email protected]>
# License: ISC
set -u -e -o pipefail
printError () {
local format="${1}"; shift
printf "ERROR: ${format}\n" "${@}" >&2
false
}
printStatus () {
local format="${1}"; shift
printf "STATUS: ${format}\n" "${@}" >&2
}
askForConfirmation () {
if "${auto}"
then
printStatus 'Continuing without asking for confirmation.'
else
read -p 'ok? [yes]|shell|no ' answer
case "${answer}" in
[yY]|'yes'|'')
;;
[sS]|'shell')
printStatus 'Opening a shell.'
/usr/bin/env bash
printStatus 'Returning to operation.'
;;
*)
printError 'Aborting.'
false
;;
esac
fi
}
getOptionLength () {
local option_name="${1}"
# HACK: '\n' has same length than '='
printf -- "${1}" | cut -f1 -d'=' | wc -c
}
auto='false'
commit_branch=''
merge_branch=''
repository_list=()
for option_name in "${@}"
do
case "${option_name}" in
'--yes')
auto='true'
;;
'--branch='*)
option_length="$(getOptionLength "${option_name}")"
commit_branch="${option_name:${option_length}}"
;;
'--merge='*)
option_length="$(getOptionLength "${option_name}")"
merge_branch="${option_name:${option_length}}"
;;
'--'*)
printError 'Unknown option: %s' "${option_name}"
;;
*)
if [ ! -d "${option_name}" ]
then
printError 'Not a directory: %s' "${option_name}"
fi
repository_list+=("$(realpath "${option_name}")")
;;
esac
shift
done
if [ -z "${commit_branch}" ]
then
printError 'Missing branch name.'
fi
if [ "${#repository_list[@]}" = 0 ]
then
printError 'Missing repository.'
fi
export TZ='GMT'
export GIT_AUTHOR_DATE="${GIT_AUTHOR_DATE:-$(date --utc --rfc-3339='s')}"
export GIT_COMMITTER_DATE="${GIT_AUTHOR_DATE}"
echo "${GIT_AUTHOR_DATE}"
askForConfirmation
fixRepositoryUrls () {
local config_path
for config_path in '.git/config' '.git/modules'
do
if [ -e "${config_path}" ]
then
find -type f -name 'config' \
-exec sed -e 's|https://github.com/|[email protected]:|' -i {} \;
fi
done
}
for repository in "${repository_list[@]}"
do
cd "${repository}"
# Prune all deleted branches, this is needed if 'something' is deleted but 'something/more' has to be pulled.
# Clone submodules of every repositories if needed.
# Fetch available remote branches references of every repositories.
git-checkout-modules --run-after='git remote prune origin'
git-checkout-modules --update
git-checkout-modules --fetch
# Clone submodules of every repositories if needed.
# Checkout primary branch of every repositories (there may be new ones).
# Pull updates for primary branch of every repositories.
# FIXME: Parent branch should never be merge branch if child branch is commit branch.
git-checkout-modules "${commit_branch}"
git-checkout-modules --pull
git-checkout-modules --print
askForConfirmation
case "${merge_branch}" in
'')
# Commit submodules references of every repositories.
git-checkout-modules --commit
;;
*)
# Merge secondary branch of every repositories.
git-checkout-modules --merge="${merge_branch}"
;;
esac
git-checkout-modules --print
askForConfirmation
# Fix urls of every repositories to make push possible.
# Push commits to every repositories.
fixRepositoryUrls
git-checkout-modules --push
# Checkout primary branch of every repositories.
git-checkout-modules "${commit_branch}"
done