-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-init-empty
More file actions
executable file
·105 lines (94 loc) · 2.61 KB
/
git-init-empty
File metadata and controls
executable file
·105 lines (94 loc) · 2.61 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
#!/usr/bin/env bash
#
# git-init-empty - create a fresh Git repository and start it with a single
# empty "init" commit authored by "nobody <nobody@nowhere>".
#
# Usage:
# git-init-empty [DIRECTORY] [-q|--quiet] [-h|--help]
#
# If DIRECTORY is omitted the current directory is used. When provided the
# directory will be created if it does not yet exist. After initialising the
# repository the script adds an empty commit with the message "init" so that
# a new branch has a first baseline revision straight away.
#
# The commit is made with the author/committer set to:
# name : nobody
# email: nobody@nowhere
#
# Exit codes:
# 0 success
# 1 invalid usage
# 2 directory already contains Git repository with history
# 3 git is not available
#
# Examples:
# git-init-empty # initialise the current directory
# git-init-empty my/project # create directory and initialise it
# git-init-empty -q new/repo # same as above but run quietly
#
# This script is intentionally minimal so it can be copied around quickly
# but it still follows good shell-scripting practices: strict error handling,
# helpful messages, and POSIX-compatible option parsing.
#
set -euo pipefail
print_help() {
grep -E '^#( |$)' "$0" | sed -E 's/^# ?//'
}
quiet=false
target_dir=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_help
exit 0
;;
-q|--quiet)
quiet=true
shift
;;
-*)
echo "Unknown option: $1" >&2
print_help
exit 1
;;
*)
if [[ -z "$target_dir" ]]; then
target_dir="$1"
shift
else
echo "ERROR: Only one positional DIRECTORY argument allowed." >&2
exit 1
fi
;;
esac
done
# default to current directory
if [[ -z "$target_dir" ]]; then
target_dir="."
fi
# ensure directory exists (unless it's ".")
if [[ "$target_dir" != "." ]]; then
mkdir -p -- "$target_dir"
fi
cd -- "$target_dir"
if ! command -v git >/dev/null 2>&1; then
echo "ERROR: git command not found." >&2
exit 3
fi
if [[ -d .git ]]; then
# Already a Git repository – allow proceeding only when it has no commits yet.
if git rev-parse --quiet --verify HEAD >/dev/null; then
echo "ERROR: Directory '$PWD' is already a Git repository with history." >&2
exit 2
else
[[ $quiet == true ]] || echo "Existing empty repository detected."
fi
else
git init ${quiet:+-q}
fi
GIT_AUTHOR_NAME="nobody" \
GIT_AUTHOR_EMAIL="nobody@nowhere" \
GIT_COMMITTER_NAME="nobody" \
GIT_COMMITTER_EMAIL="nobody@nowhere" \
git commit --allow-empty -m "init" ${quiet:+-q}
$quiet || echo "Repository initialised at '$PWD'"