-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-init
More file actions
executable file
·136 lines (116 loc) · 3.9 KB
/
git-init
File metadata and controls
executable file
·136 lines (116 loc) · 3.9 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
#!/usr/bin/env bash
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
__SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"
__NAME__="${__SOURCE__##*/}"
__AUTHOR__='S0AndS0'
__DESCRIPTION__='Initializes new git repository ready for cloning &/or pushing'
## To-do, get results of 'git rev-parse' without tripping the following trap
## Provides 'failure'
# source "${__DIR__}/shared_functions/modules/trap-failure/failure.sh"
# trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR
## Provides: argument_parser <arg-array-reference> <acceptable-arg-reference>
source "${__DIR__}/shared_functions/modules/argument-parser/argument-parser.sh"
## Provides: __license__ <description> <author>
source "${__DIR__}/shared_functions/license"
usage(){
_message="${1}"
_repo_name="${_repo_name:-repository-name}"
cat <<EOF
## Usage
# ssh ${USER}@host-or-ip ${__NAME__} ${_git_args[@]:-$_repo_name}
#
# ${__DESCRIPTION__}
#
## After initializing
## Try: git clone ${USER}@host-or-ip:git/${_repo_name}
## or: git remote add remote-name ${USER}@host-or-ip:git/${_repo_name}
## git checkout --track remote-name/branch-name
#
# --quiet
# Git initializes quietly
#
# --bare
# Initialize a bare git repository
#
# --shared
# Allow group $(groups | awk '{print $1}') to git push
#
# --template=<path>
# Template git repository that git init should pull from
#
## See: git help init
## for detailed documentation of the above options.
#
# ${_repo_name}
# Name of repository to initialize
#
# -l --license
# Shows script or project license then exits
#
# -h --help help
# Display this message and exit
EOF
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
printf >&2 'Error - %s\n' "${_message}"
}
}
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
_valid_args=('--help|-h|help:bool'
'--quiet:bool'
'--license|-l|license:bool'
'--bare:bool'
'--shared:bool'
'--template:path'
'--repo-name:posix-nil')
argument_parser '_args' '_valid_args'
_exit_status="$?"
_git_args=()
((_bare)) && { _git_args+=('--bare'); }
((_shared)) && { _git_args+=('--shared'); }
((_quiet)) && { _git_args+=('--quiet'); }
(("${#_template}")) && { _git_args+=("--template='${_template}'"); }
(("${#_repo_name}")) && { _git_args+=("${_repo_name}"); }
if ((_help)) || ((_exit_status)); then
usage "${_exit_status}"
exit "${_exit_status}"
elif ((_license)); then
__license__ "${__DESCRIPTION__}" "${__AUTHOR__}"
exit 0
fi
(("${#_repo_name}")) || {
usage 'missing repository name argument!'
exit "1"
}
_git_path="${HOME}/git/${_repo_name:?# No repository name provided}"
_git_parent_dir="${_git_path%/*}"
_old_PWD="${PWD}"
[[ -d "${_git_parent_dir}" ]] || { mkdir -vp "${_git_parent_dir}"; }
## Check if path is already tracked by git
[[ -d "${_git_path}" ]] && {
cd "${_git_path}" || exit 1
_git_preexisting_dir="$(git rev-parse --git-dir 2>/dev/null)"
[[ "$?" == '0' ]] || (("${#_git_preexisting_dir}")) && {
[[ "${_git_preexisting_dir}" == "${_git_path}/.git" ]] || [[ "${_git_preexisting_dir}" == "${_git_path}" ]] && {
printf >&2 '# Already tracked by git: %s\n' "${_git_preexisting_dir}"
exit 1
}
}
}
cd "${_git_parent_dir}" || exit 1
printf '## git init %s\n' "${_git_args[@]}"
git init ${_git_args[@]}
## Allow 'git push' on non-bare repositories
! ((_bare)) && {
cd "${_git_parent_dir}/${_repo_name}" || exit 1
[[ "$(git config receive.denyCurrentBranch)" != 'updateInstead' ]] && {
git config receive.denyCurrentBranch updateInstead
}
}
[[ "${_old_PWD}" == "${_git_path}" ]] || {
cd "${_old_PWD}"
}
printf '# Clone %s via: git clone %s@domain_or_ip:%s\n' "${_repo_name}" "${USER}" "${_git_path//${HOME}\//}"
printf '# %s finished\n' "${__NAME__}"