-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-create-bare-repo
More file actions
executable file
·93 lines (72 loc) · 1.65 KB
/
git-create-bare-repo
File metadata and controls
executable file
·93 lines (72 loc) · 1.65 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
#!/usr/bin/env bash
NO_FORMAT="\033[0m"
F_INVERT="\033[7m"
F_BOLD="\033[1m"
F_DIM="\033[2m"
C_GREEN="\033[38;5;2m"
C_GRAY="\033[38;5;8m"
C_RED="\033[38;5;9m"
function log() {
printf "${F_DIM}[%s]${NO_FORMAT} $1${NO_FORMAT}\n" "$(date +'%Y-%m-%d %H:%M:%S.%3N')" ${@:2}
}
function info() {
log "${C_GREEN}INFO${NO_FORMAT} $*"
}
function error() {
log "${C_RED}ERROR${NO_FORMAT} $*"
}
function usage() {
echo -e "$(
cat <<EOF
Create a new bare repository with folders as branch names
Usage:
${C_GREEN}$(basename "$0")${NO_FORMAT} ${F_DIM}${C_GRAY}[flags]${NO_FORMAT} ${F_DIM}${F_BOLD}<repo_name>${NO_FORMAT}
Flags:
--help, -h ${F_DIM}
Show this help text
${NO_FORMAT}
EOF
)"
}
function main() {
repo_folder="$1"
for var in "$@"; do
case "$var" in
--help | -h)
usage
exit 0
;;
esac
done
if [ -z "$repo_folder" ]; then
error "Repo name is missing"
echo
usage
exit 1
fi
if [ -d "$repo_folder" ] || [ -f "$repo_folder" ]; then
error "File or folder \"$repo_folder\" already exists. Cannot create new repo."
exit 1
fi
repo_path="$(realpath "$PWD/$repo_folder")"
info "Creating bare repo at \"$repo_path\""
mkdir "$repo_path"
cd "$repo_path"
out="$(git init --bare '.bare' 2>&1)"
if [ "$?" -ne 0 ]; then
error "Failed to create repo:" "$out"
exit 1
fi
echo 'gitdir: ./.bare' >.git
main_branch_name="$(git config --global init.defaultBranch 2>&1)"
if [ -z "$main_branch_name" ]; then
main_branch_name='main'
fi
out="$(git worktree add --orphan -b "$main_branch_name" "$main_branch_name" 2>&1)"
if [ "$?" -ne 0 ]; then
error "Failed to create repo:" "$out"
exit 1
fi
info "Done!"
}
main "$@"