Skip to content

Commit 4d42b9d

Browse files
authored
Update git-slice
1 parent 099fa97 commit 4d42b9d

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

git-slice

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,32 @@
3535

3636
set -euo pipefail
3737

38+
unset _will_exit
3839
for _i in bash git ; do
3940
if ! command -v "${_i}" &> /dev/null ; then
40-
>&2 echo "ERROR: ${_i} is not installed."
41+
>&2 echo "ERROR: No Such Command: ${_i}"
4142
_will_exit=1
4243
fi
4344
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
4855
fi
4956
for _i in gh curl awk ; do
5057
if ! command -v "${_i}" &> /dev/null ; then
51-
>&2 echo "ERROR: ${_i} is not installed."
58+
>&2 echo "ERROR: No Such Command: ${_i}"
5259
_will_exit=1
5360
fi
5461
done
5562
fi
56-
[ -n "${_will_exit:+x}" ] && exit 1
63+
[ -n "${_will_exit:-}" ] && exit 1
5764

5865
_git_subtree_branch_prefix="___"
5966

@@ -67,17 +74,20 @@ for _i in "${@}" ; do
6774
_dir="${_i}"
6875
_basename="$(basename "${_i}")"
6976

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
7181
if ! git subtree split -P "${_dir}" -b "${_git_subtree_branch_prefix}${_basename}" ; then
7282
>&2 echo "ERROR: 'git subtree split' exit with error ${?}"
73-
exit 1
83+
exit 2
7484
fi
7585
fi
7686

7787
done
7888

7989

80-
if [[ -n "${ENABLE_SUBTREE_PUSH:+x}" ]] ; then
90+
if [[ -n "${ENABLE_SUBTREE_PUSH:-}" ]] ; then
8191

8292
readarray -t _git_branches < <(git branch --list "${_git_subtree_branch_prefix}*")
8393

@@ -86,24 +96,25 @@ if [[ -n "${ENABLE_SUBTREE_PUSH:+x}" ]] ; then
8696
_git_subtree_branch="${_i}"
8797
_git_remote_repo_name="${_git_subtree_branch#${_git_subtree_branch_prefix}}"
8898
_git_branch="$(git rev-parse --abbrev-ref HEAD)"
99+
GITHUB_REPOSITORY="${GITHUB_REPOSITORY:-}"
89100
_github_owner="${GITHUB_REPOSITORY%%/*}"
90101
_git_subtree_remote="subtree_${_github_owner}_${_git_remote_repo_name}"
91102
_git_new_repo_url="https://github.com/${_github_owner}/${_git_remote_repo_name}.git"
92103
_git_new_repo_id="${_github_owner}/${_git_remote_repo_name}"
93104

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
96107

97-
>&2 echo "Remote subtree repo does not exist yet."
108+
>&2 echo "INFO: Remote subtree repo does not exist yet."
98109

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:-}"
101112
gh repo create "${_git_new_repo_id}" --public --confirm --template="" --remote
102113

103114
fi
104115

105116
# 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
107118
git remote add "${_git_subtree_remote}" "${_git_new_repo_url}"
108119
fi
109120

0 commit comments

Comments
 (0)