1
- #! /bin/bash
1
+ #! /usr/ bin/env bash
2
2
#
3
3
# git-artifact: use a git repository as artifact management storage
4
4
#
5
5
6
- if test -z " $GIT_EXEC_PATH " || ! test -f " $GIT_EXEC_PATH /git-sh-setup" || {
7
- test " ${PATH# " ${GIT_EXEC_PATH} :" } " = " $PATH " &&
8
- test ! " $GIT_EXEC_PATH " -ef " ${PATH%%:* } " 2> /dev/null
6
+ function check_environment() {
7
+ if [[ " ${BASH_VERSINFO:- } " == " " ]] ; then
8
+ echo " ERROR: git-artifact only runs in bash - please check that the variable BASH_VERSINFO is set"
9
+ echo " BASH_VERSINFO: ${BASH_VERSINFO:- unknown} "
10
+ echo " BASH_VERSION: ${BASH_VERSION:- unknown} "
11
+ echo " SHELL: ${SHELL:- unknown} "
12
+ exit 1
13
+ fi
14
+
15
+ if [[ " ${BASH_VERSINFO[0]} " -lt 4 ]]; then
16
+ echo " ERROR: git-artifact requires Bash version 4.3 or higher."
17
+ echo " BASH_VERSINFO: ${BASH_VERSINFO:- unknown} "
18
+ echo " BASH_VERSION: ${BASH_VERSION:- unknown} "
19
+ exit 1
20
+ elif [[ " ${BASH_VERSINFO[0]} " -eq 4 && " ${BASH_VERSINFO[1]} " -lt 3 ]]; then
21
+ echo " ERROR: git-artifact requires Bash version 4.3 or higher."
22
+ echo " BASH_VERSINFO: ${BASH_VERSINFO:- unknown} "
23
+ echo " BASH_VERSION: ${BASH_VERSION:- unknown} "
24
+ exit 1
25
+ fi
26
+
27
+ }
28
+
29
+ function check_git_environment() {
30
+ if test -z " $GIT_EXEC_PATH " || ! test -f " $GIT_EXEC_PATH /git-sh-setup" || {
31
+ test " ${PATH# " ${GIT_EXEC_PATH} :" } " = " $PATH " &&
32
+ test ! " $GIT_EXEC_PATH " -ef " ${PATH%%:* } " 2> /dev/null
33
+ }
34
+ then
35
+ basename=${0##* [/\\]}
36
+ echo >&2 ' It looks like your git installation broken'
37
+ echo >&2
38
+ echo >&2 " Tips:"
39
+ echo >&2 " - If \` git --exec-path\` does not print the correct path to"
40
+ echo >&2 " your git install directory, then set the GIT_EXEC_PATH"
41
+ echo >&2 " environment variable to the correct directory."
42
+ echo >&2 " - Make sure that your \` $basename \` file is either in your"
43
+ echo >&2 " PATH or in your git exec path (\` $( git --exec-path) \` )."
44
+ echo >&2 " - You should run git-subtree as \` git ${basename# git-} \` ,"
45
+ echo >&2 " not as \` $basename \` ." >&2
46
+ exit 126
47
+ fi
48
+
49
+ }
50
+
51
+ function show_info() {
52
+ echo " git-artifact: use a git repository as artifact management storage"
53
+ echo " Version: $( git --version | cut -d ' ' -f 3) "
54
+ echo " Bash version: ${BASH_VERSION:- unknown} "
55
+ echo " Bash version info: ${BASH_VERSINFO[*]:- unknown} "
56
+ echo " Git exec path: ${GIT_EXEC_PATH:- unknown} "
57
+ echo " Git version: $( git --version) "
9
58
}
10
- then
11
- basename=${0##* [/\\]}
12
- echo >&2 ' It looks like either your git installation or your'
13
- echo >&2 ' git-subtree installation is broken.'
14
- echo >&2
15
- echo >&2 " Tips:"
16
- echo >&2 " - If \` git --exec-path\` does not print the correct path to"
17
- echo >&2 " your git install directory, then set the GIT_EXEC_PATH"
18
- echo >&2 " environment variable to the correct directory."
19
- echo >&2 " - Make sure that your \` $basename \` file is either in your"
20
- echo >&2 " PATH or in your git exec path (\` $( git --exec-path) \` )."
21
- echo >&2 " - You should run git-subtree as \` git ${basename# git-} \` ,"
22
- echo >&2 " not as \` $basename \` ." >&2
23
- exit 126
24
- fi
25
59
60
+ function set_opts_spec() {
61
+ # Set the options specification for git rev-parse --parseopt
62
+ # This is used to parse command line arguments
63
+ # It is used in the main function to set the arguments
64
+ # It is also used in the cmd_* functions to parse the arguments
65
+ # It is also used in the git artifact help command to show the options
26
66
# inspiration: https://github.com/git/git/blob/master/contrib/subtree/git-subtree.sh
27
- OPTS_SPEC=" \
67
+ OPTS_SPEC=" \
28
68
git artifact commmand <options>
29
69
30
70
commands:
@@ -61,6 +101,7 @@ r,regex= the reg-ex pattern to latest of
61
101
options for 'fetch-tags'
62
102
s,sha1= The sha1 of which to get tags from from
63
103
"
104
+ }
64
105
65
106
debug () {
66
107
if test -n " $arg_debug "
@@ -395,6 +436,7 @@ main () {
395
436
done
396
437
arg_command=$1
397
438
shift
439
+
398
440
which git-sh-setup
399
441
case " $arg_command " in
400
442
init) if test -z " ${arg_remoteurl:- } " ; then
@@ -485,5 +527,11 @@ main () {
485
527
" cmd_$arg_command " " $@ "
486
528
}
487
529
488
-
489
- main " $@ "
530
+ # Only run main if not being sourced
531
+ if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
532
+ check_environment
533
+ check_git_environment
534
+ [[ ${arg_debug:- } == true ]] && show_info
535
+ set_opts_spec
536
+ main " $@ "
537
+ fi
0 commit comments