35
35
36
36
set -euo pipefail
37
37
38
+ unset _will_exit
38
39
for _i in bash git ; do
39
40
if ! command -v " ${_i} " & > /dev/null ; then
40
- >&2 echo " ERROR: ${_i} is not installed. "
41
+ >&2 echo " ERROR: No Such Command: ${_i} "
41
42
_will_exit=1
42
43
fi
43
44
done
44
- if [[ -n " ${ENABLE_SUBTREE_PUSH: +x} " ]] ; then
45
- if [[ -z " ${ACCESS_TOKEN} " ]] ; then
46
- >&2 echo " ERROR: \$ {ACCESS_TOKEN} environment not provided."
47
- exit 1
45
+ if [[ -n " ${ENABLE_SUBTREE_PUSH:- } " ]] ; then
46
+ if [[ -z " ${ACCESS_TOKEN:- } " ]] ; then
47
+ >&2 echo " ERROR: \$ {ENABLE_SUBTREE_PUSH} require \$ {ACCESS_TOKEN} environment variable."
48
+ >&2 echo " ERROR: No Such environment variable: \$ {ACCESS_TOKEN}"
49
+ _will_exit=1
50
+ fi
51
+ if [[ -z " ${GITHUB_REPOSITORY:- } " ]] ; then
52
+ >&2 echo " ERROR: \$ {ENABLE_SUBTREE_PUSH} require \$ {GITHUB_REPOSITORY} environment variable."
53
+ >&2 echo " ERROR: No Such environment variable: \$ {GITHUB_REPOSITORY}"
54
+ _will_exit=1
48
55
fi
49
56
for _i in gh curl awk ; do
50
57
if ! command -v " ${_i} " & > /dev/null ; then
51
- >&2 echo " ERROR: ${_i} is not installed. "
58
+ >&2 echo " ERROR: No Such Command: ${_i} "
52
59
_will_exit=1
53
60
fi
54
61
done
55
62
fi
56
- [ -n " ${_will_exit: +x } " ] && exit 1
63
+ [ -n " ${_will_exit:- } " ] && exit 1
57
64
58
65
_git_subtree_branch_prefix=" ___"
59
66
@@ -67,17 +74,20 @@ for _i in "${@}" ; do
67
74
_dir=" ${_i} "
68
75
_basename=" $( basename " ${_i} " ) "
69
76
70
- if ! git show-ref --quiet --verify " refs/heads/${_git_subtree_branch_prefix}${_basename} " ; then
77
+ if git show-ref --quiet --verify " refs/heads/${_git_subtree_branch_prefix}${_basename} " ; then
78
+ >&2 echo " WARNING: git branch '${_git_subtree_branch_prefix}${_basename} ' exist, skipping '${_i} '."
79
+ continue
80
+ else
71
81
if ! git subtree split -P " ${_dir} " -b " ${_git_subtree_branch_prefix}${_basename} " ; then
72
82
>&2 echo " ERROR: 'git subtree split' exit with error ${?} "
73
- exit 1
83
+ exit 2
74
84
fi
75
85
fi
76
86
77
87
done
78
88
79
89
80
- if [[ -n " ${ENABLE_SUBTREE_PUSH: +x } " ]] ; then
90
+ if [[ -n " ${ENABLE_SUBTREE_PUSH:- } " ]] ; then
81
91
82
92
readarray -t _git_branches < <( git branch --list " ${_git_subtree_branch_prefix} *" )
83
93
@@ -86,24 +96,25 @@ if [[ -n "${ENABLE_SUBTREE_PUSH:+x}" ]] ; then
86
96
_git_subtree_branch=" ${_i} "
87
97
_git_remote_repo_name=" ${_git_subtree_branch# ${_git_subtree_branch_prefix} } "
88
98
_git_branch=" $( git rev-parse --abbrev-ref HEAD) "
99
+ GITHUB_REPOSITORY=" ${GITHUB_REPOSITORY:- } "
89
100
_github_owner=" ${GITHUB_REPOSITORY%%/* } "
90
101
_git_subtree_remote=" subtree_${_github_owner} _${_git_remote_repo_name} "
91
102
_git_new_repo_url=" https://github.com/${_github_owner} /${_git_remote_repo_name} .git"
92
103
_git_new_repo_id=" ${_github_owner} /${_git_remote_repo_name} "
93
104
94
- # Check if the remote repo exists
95
- if curl -fsL " ${_git_new_repo_url} " ; then
105
+ # Check if the remote repo does not exists
106
+ if ! curl -fsSL " ${_git_new_repo_url} " & > /dev/null ; then
96
107
97
- >&2 echo " Remote subtree repo does not exist yet."
108
+ >&2 echo " INFO: Remote subtree repo does not exist yet."
98
109
99
- # If it doesn't exist, create it
100
- gh auth login --with-token <<< " ${ACCESS_TOKEN}"
110
+ # If it does not exist, create it
111
+ gh auth login --with-token <<< " ${ACCESS_TOKEN:- }"
101
112
gh repo create " ${_git_new_repo_id} " --public --confirm --template=" " --remote
102
113
103
114
fi
104
115
105
116
# Add the remote if it doesn't exist
106
- if ! git ls-remote " ${_git_subtree_remote} " & > /dev/null; then
117
+ if ! git ls-remote " ${_git_subtree_remote} " & > /dev/null ; then
107
118
git remote add " ${_git_subtree_remote} " " ${_git_new_repo_url} "
108
119
fi
109
120
0 commit comments