-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_utils.sh
More file actions
executable file
·396 lines (344 loc) · 9.26 KB
/
Copy pathgit_utils.sh
File metadata and controls
executable file
·396 lines (344 loc) · 9.26 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/lib/bashlib.sh"
source "${SCRIPT_DIR}/lib/cli.sh"
# ── git.dir.check option metadata ─────────────────────────────────────────────
_GIT_DIR_CHECK_OPTS_SHORT=(-h -n)
_GIT_DIR_CHECK_OPTS_LONG=(--help --dry-run)
_GIT_DIR_CHECK_OPTS_ARG=("" "")
_GIT_DIR_CHECK_OPTS_DESC=("Display this help message" "Show command without executing")
# ── git.url.to_dir option metadata ────────────────────────────────────────────
_GIT_URL_TO_DIR_OPTS_SHORT=(-h)
_GIT_URL_TO_DIR_OPTS_LONG=(--help)
_GIT_URL_TO_DIR_OPTS_ARG=("")
_GIT_URL_TO_DIR_OPTS_DESC=("Display this help message")
# ── git.clone.to_dir option metadata ──────────────────────────────────────────
_GIT_CLONE_TO_DIR_OPTS_SHORT=(-h -n -d)
_GIT_CLONE_TO_DIR_OPTS_LONG=(--help --dry-run --dir)
_GIT_CLONE_TO_DIR_OPTS_ARG=("" "" "DIR")
_GIT_CLONE_TO_DIR_OPTS_DESC=("Display this help message" "Show commands without executing" "Target directory path")
# ── git.clone.list option metadata ────────────────────────────────────────────
_GIT_CLONE_LIST_OPTS_SHORT=(-h -n -r)
_GIT_CLONE_LIST_OPTS_LONG=(--help --dry-run --root)
_GIT_CLONE_LIST_OPTS_ARG=("" "" "DIR")
_GIT_CLONE_LIST_OPTS_DESC=("Display this help message" "Show commands without executing" "Repository root directory")
# ── git.remote.set_url option metadata ────────────────────────────────────────
_GIT_REMOTE_SET_URL_OPTS_SHORT=(-h -n -r -u -p -s)
_GIT_REMOTE_SET_URL_OPTS_LONG=(--help --dry-run --remote --user --project --host-suffix)
_GIT_REMOTE_SET_URL_OPTS_ARG=("" "" "NAME" "USER" "REPO" "SUF")
_GIT_REMOTE_SET_URL_OPTS_DESC=(
"Display this help message"
"Show command without executing"
"Remote name (default: origin)"
"User/org name (default: parent dir name)"
"Repository name (default: current dir name)"
"SSH host suffix (default: gh)"
)
git.dir.check() {
local repo_path="" show_help=0 dryrun=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-n | --dry-run)
dryrun=1
shift
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*)
repo_path="$1"
shift
;;
esac
done
eval "$(build_usage "GIT_DIR_CHECK" "${FUNCNAME[0]}" "REPO_PATH" "Checks if directory is git repo.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
dep_check git || return
[[ -z "${repo_path}" ]] && {
echo "Repo path required" >&2
return 1
}
eval "$(dry_run_wrapper)"
if ((dryrun)); then
run_cmd git -C "${repo_path}" rev-parse
else
git -C "${repo_path}" rev-parse >/dev/null 2>&1
fi
}
git.url.to_dir() {
local remote_address="" show_help=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*)
remote_address="$1"
shift
;;
esac
done
eval "$(build_usage "GIT_URL_TO_DIR" "${FUNCNAME[0]}" "REMOTE_URL" "Extracts directory name from git remote url.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
dep_check sed || return
[[ -z "${remote_address}" ]] && {
echo "Remote address required" >&2
return 1
}
local author_name
author_name="$(echo "${remote_address}" | sed -E 's/.*[:/]([^/]+)\/.*/\1/')"
local project_name
project_name="$(basename "${remote_address}" .git)"
echo "${author_name}/${project_name}"
}
git.clone.to_dir() {
local remote_address="" target_dir="" show_help=0 dryrun=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-n | --dry-run)
dryrun=1
shift
;;
-d | --dir)
target_dir="$2"
shift 2
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*)
[[ -z "${remote_address}" ]] && {
remote_address="$1"
shift
continue
}
[[ -z "${target_dir}" ]] && {
target_dir="$1"
shift
continue
}
shift
;;
esac
done
eval "$(build_usage "GIT_CLONE_TO_DIR" "${FUNCNAME[0]}" "REMOTE_URL [TARGET_DIR]" "Clones remote repo to local dir.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
dep_check git || return
[[ -z "${remote_address}" ]] && {
echo "Remote address required" >&2
return 1
}
[[ -z "${target_dir}" ]] && target_dir="$(git.url.to_dir "${remote_address}")"
local repo_root
repo_root="$(pwd)"
target_dir="$(realpath -m "${target_dir}")"
eval "$(dry_run_wrapper)"
run_cmd mkdir -p "${target_dir}" && run_cmd git -C "${repo_root}" clone "${remote_address}" "${target_dir}"
}
git.clone.list() {
local list_file="" repo_root="" show_help=0 dryrun=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-n | --dry-run)
dryrun=1
shift
;;
-r | --root)
repo_root="$2"
shift 2
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*)
[[ -z "${list_file}" ]] && {
list_file="$1"
shift
continue
}
[[ -z "${repo_root}" ]] && {
repo_root="$1"
shift
continue
}
shift
;;
esac
done
eval "$(build_usage "GIT_CLONE_LIST" "${FUNCNAME[0]}" "LIST_FILE [REPO_ROOT]" "Clones list of remote repos to local dir.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
dep_check git || return
[[ -z "${list_file}" ]] && {
echo "List file path required" >&2
return 1
}
[[ ! -f "${list_file}" ]] && {
echo "List file doesn't exist" >&2
return 1
}
[[ -z "${repo_root}" ]] && repo_root="$(pwd)"
repo_root="$(realpath -m "${repo_root}")"
local remote_list=()
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -n "${line}" ]] && remote_list+=("$line")
done <"${list_file}"
eval "$(dry_run_wrapper)"
for remote in "${remote_list[@]}"; do
local target_dir="${repo_root}/$(git.url.to_dir "${remote}")"
git.dir.check "${target_dir}" && {
echo "${target_dir} is git repo already"
continue
}
run_cmd mkdir -p "${target_dir}" && run_cmd git -C "${repo_root}" clone "${remote}" "${target_dir}"
done
}
# license.get() {
# local license_code
# local license_tpl_path
# }
# coc.get() {
# local coc_code
# local coc_tpl_path
# }
# readme.get() {
# local readme_tpl_path
# }
# example_struct="
# ./
# lib/
# src/
# .gitignore
# CODE_OF_CONDUCT.md
# LICENSE.md
# README.md
# "
# fs.tree.spawn() {
# local struct
# }
# git.init.proj() {
# fs.tree.spawn struct
# }
# git.remote.repo.init() {
# read -s -p "Enter your API key" -t $TIMEOUT TOKEN
# local REPO_DATA # read from a file - repo.json
# # or assume folder name as project name and local user name as remote user name as defaults
# # and read with a prompt for actual values
# # maybe even handle this in `npm init` manner with generation of an analog of `package.json`
# local API_ENDPOINT
# # GitHub: "${api_domain}/user/repos"
# # GitLab: "${api_domain}/api/v4/projects"
# # Gitea: "${api_domain}/api/v1/user/repos"
# local API_HEADERS
# # --header "Authorization: token ${TOKEN}" \
# # --header "PRIVATE-TOKEN: ${TOKEN}" \
# # --header "Accept: application/vnd.github+json" \
# # --header "Content-Type: application/json" \
# local TIMEOUT=10
# curl \
# --include \
# --verbose \
# --location \
# --request POST \
# "${API_HEADERS}" \
# --data-raw "${REPO_DATA}" \
# --data "${REPO_DATA}" \
# "${API_ENDPOINT}"
# }
# git.localhost.setup() {
# # Setup steps to create a local non-graphical `git` Linux user and allow SSHing to it
# }
# git.dirs.noremote() {
# # prints directories with git repos that don't have a specific remote specified
# local remote="${1?"Remote name is required."}"
# for d in */; do
# if [[ -d "$d/.git/config" && grep -q "url =.*lan" "$d/.git/config" ]]; then
# continue
# fi
# echo "$d"
# done
# }
git.remote.set_url() {
local remote_name="origin" user="" repo="" host_suffix="gh" show_help=0 dryrun=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-n | --dry-run)
dryrun=1
shift
;;
-r | --remote)
remote_name="$2"
shift 2
;;
-u | --user)
user="$2"
shift 2
;;
-p | --project)
repo="$2"
shift 2
;;
-s | --host-suffix)
host_suffix="$2"
shift 2
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*) shift ;;
esac
done
eval "$(build_usage "GIT_REMOTE_SET_URL" "${FUNCNAME[0]}" "" "Sets git remote URL to SSH format derived from cwd.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
dep_check git || return
[[ -z "${user}" ]] && user="$(basename "$(dirname "$(pwd)")")"
[[ -z "${repo}" ]] && repo="$(basename "$(pwd)")"
local url="git@${user}.${host_suffix}:${user}/${repo}.git"
eval "$(dry_run_wrapper)"
run_cmd git remote set-url "${remote_name}" "${url}"
}
register_completion "git.dir.check" "GIT_DIR_CHECK"
register_completion "git.url.to_dir" "GIT_URL_TO_DIR"
register_completion "git.clone.to_dir" "GIT_CLONE_TO_DIR"
register_completion "git.clone.list" "GIT_CLONE_LIST"
register_completion "git.remote.set_url" "GIT_REMOTE_SET_URL"
export -f git.dir.check git.url.to_dir git.clone.to_dir git.clone.list git.remote.set_url