-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-ext-init
202 lines (171 loc) · 4.67 KB
/
git-ext-init
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
#|
#| Initializes a Git repository according to a template.
#|
#| * Makes an empty root commit, which can be useful for merging or splitting
#| whole repositories or separate trees.
#| * Applies the given template, which may define custom steps and/or commonly
#| present files (e.g., .gitignore, licenses or README).
#| * Adds .gitignore, .gitattributes and README.md if provided.
#| * Prints the hints how to proceed then.
#|
#| Usage:
#|
#| git-ext-init [-h] | [-e] [-t TEMPLATE] [[-r REMOTE] -u URL] [DIR]
#|
#| Options:
#| -e, --edit edit the configuration files
#| -h, --help show this help and exit
#| -r, --remote NAME the NAME for the remote (default: origin)
#| -t, --template TEMPLATE the TEMPLATE to apply
#| -u, --url URL add URL for the given remote
#|
#| Arguments:
#| DIR the directory of the repository (default: .)
#|
#| If DIR does not exist, the path is created (including necessary parent
#| directories).
#|
#| TEMPLATE refers to the name of the directory containing the template to apply.
#| The template has following structure:
#|
#| {template}/
#| resources/
#| prepare
#| finish
#|
#| The template is applied by following steps:
#| 1. Execute 'prepare'.
#| 2. Copy the content of 'resources/' to the root of the repository.
#| 3. Execute 'finish'.
#|
#| If any item is missing, the step is skipped. The commands are executed in the
#| the repository directory and they are supposed to perform adjustments and any
#| other suitable actions for the initialization.
#|
#| The TEMPLATE is searched in '~/.config/git/ext/init/templates/' and if not
#| found there, then in 'templates/' subdirectory of this command's directory.
FORCE_EDIT=0
REMOTE_NAME=origin
REMOTE_URL=
TEMPLATE=
SCRIPT_FILE="${BASH_SOURCE[0]}"
while [[ -n "$1" ]]; do
case "$1" in
-h|--help)
sed '/#|.*/ s/^#|[[:space:]]\?//p;d' < "$SCRIPT_FILE"
exit 1
;;
-e|--edit)
FORCE_EDIT=1
shift
;;
-r|--remote)
REMOTE_NAME="$2"
shift 2
;;
-t|--template)
TEMPLATE="$2"
shift 2
;;
-u|--url)
REMOTE_URL="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "Unknown option encountered: '$1'" >&2
exit 1
;;
*)
break
;;
esac
done
# Preliminary sanity checks
DIR="${1:-.}"
if [[ -d "$DIR/.git" ]]; then
echo "The repository already exists. Aborting." >&2
exit 2
fi
if [[ -n "$TEMPLATE" ]]; then
TEMPLATE_DIR="~/.config/git/ext/init/templates/$TEMPLATE"
if ! [[ -d "$TEMPLATE_DIR" ]]; then
TEMPLATE_DIR=`dirname "$SCRIPT_FILE"`"/templates/$TEMPLATE"
if ! [[ -d "$TEMPLATE_DIR" ]]; then
echo "Could not locate given template '$TEMPLATE'." >&2
exit 2
fi
fi
fi
# Finish the script preparation steps
if [[ -z "$EDITOR" ]]; then
EDITOR=vim
fi
# Start the actual process
if [[ "$DIR" != '.' ]]; then
echo ":: Switching to $DIR"
mkdir -p "$DIR"
cd "$DIR"
fi
echo ":: Setting up repository"
git init
if [[ -n "$REMOTE_URL" ]]; then
echo "Setting remote"
git remote add "$REMOTE_NAME" "$REMOTE_URL"
fi
echo ":: Review the configuration"
git config --list
if [[ $FORCE_EDIT -ne 0 ]]; then
git config --edit
fi
echo ":: Setting up the root commit"
git commit --allow-empty -m "Make the root commit"
git tag ROOT
if [[ -n "$TEMPLATE" ]]; then
echo ":: Applying template '$TEMPLATE'"
TEMPLATE_RESOURCES="$TEMPLATE_DIR/resources"
test -d "$TEMPLATE_RESOURCES" && cp -R "$TEMPLATE_RESOURCES/." .
TEMPLATE_COMMAND="$TEMPLATE_DIR/prepare"
test -f "$TEMPLATE_COMMAND" && "$TEMPLATE_COMMAND"
fi
echo ":: Adding .gitignore"
if [[ -z "$TEMPLATE" || $FORCE_EDIT -ne 0 ]]; then
"$EDITOR" .gitignore
fi
if [[ -f .gitignore ]]; then
git add .gitignore
git commit -m "Add .gitignore"
fi
echo ":: Adding .gitattributes"
if [[ -z "$TEMPLATE" || $FORCE_EDIT -ne 0 ]]; then
"$EDITOR" .gitattributes
fi
if [[ -f .gitattributes ]]; then
git add .gitattributes
git commit -m "Add .gitattributes"
fi
if [[ -f README.md ]]; then
echo ":: Preparing README.md"
"$EDITOR" README.md
git add README.md
git commit -m "Add README.md"
fi
if [[ -n "$TEMPLATE" ]]; then
TEMPLATE_COMMAND="$TEMPLATE_DIR/finish"
test -f "$TEMPLATE_COMMAND" && "$TEMPLATE_COMMAND"
fi
echo ":: Finishing"
echo
echo "Keep good work:"
echo
echo "* Add a skeleton of the project that passes the build"
echo "* Update documentation accordingly"
echo "* Commit the skeleton"
echo "* Supply the rest :-)"
echo
echo "Happy coding!"
echo