Skip to content

Commit a990c3c

Browse files
authored
gh-15 : change shabang for compatibility and test for bash 4.3+ (#16)
1 parent c0158ce commit a990c3c

File tree

2 files changed

+73
-26
lines changed

2 files changed

+73
-26
lines changed

_tests.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
clear
44

@@ -9,8 +9,8 @@ set -euo pipefail
99
}
1010
PATH=$(pwd):$PATH
1111

12-
which git
13-
git --version
12+
source "$(dirname "$0")/git-artifact"
13+
check_environment
1414

1515
cd .test
1616
root_folder=$(pwd)
@@ -22,7 +22,6 @@ remote_tester_repo=.remote
2222
clone_tester_repo=.clone
2323
global_exit_code=0
2424

25-
2625
function testcase_header() {
2726
[[ ${verbose:-} == true ]] || return 0
2827
echo

git-artifact

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,70 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
#
33
# git-artifact: use a git repository as artifact management storage
44
#
55

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)"
958
}
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
2559

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
2666
#inspiration: https://github.com/git/git/blob/master/contrib/subtree/git-subtree.sh
27-
OPTS_SPEC="\
67+
OPTS_SPEC="\
2868
git artifact commmand <options>
2969
3070
commands:
@@ -61,6 +101,7 @@ r,regex= the reg-ex pattern to latest of
61101
options for 'fetch-tags'
62102
s,sha1= The sha1 of which to get tags from from
63103
"
104+
}
64105

65106
debug () {
66107
if test -n "$arg_debug"
@@ -395,6 +436,7 @@ main () {
395436
done
396437
arg_command=$1
397438
shift
439+
398440
which git-sh-setup
399441
case "$arg_command" in
400442
init) if test -z "${arg_remoteurl:-}" ; then
@@ -485,5 +527,11 @@ main () {
485527
"cmd_$arg_command" "$@"
486528
}
487529

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

Comments
 (0)