Repository navigation
Expand file tree
/
Copy pathgit-worktree-init
More file actions
executable file
·71 lines (56 loc) · 1.62 KB
/
Copy pathgit-worktree-init
File metadata and controls
executable file
·71 lines (56 loc) · 1.62 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
#!/usr/bin/env bash
function main {
local initial_dir="$PWD"
local REPO_URL="${1:-}"
local REPO_NAME_OVERRIDE="${2:-}"
local REPO_NAME
REPO_NAME="$(basename "$REPO_URL" '.git')"
if [ -z "$REPO_NAME" ]; then
print-usage
exit 1
fi
if [ "$REPO_NAME" = "-h" ] || [ "$REPO_NAME" = "--help" ]; then
print-usage
exit 0
fi
REPO_NAME="$REPO_NAME-overview"
if [ -n "$REPO_NAME_OVERRIDE" ]; then
REPO_NAME="$REPO_NAME_OVERRIDE"
fi
echo "|> New directory name will be \`$REPO_NAME\`"
local MAIN_BRANCH
echo "|> Fetching default branch name..."
MAIN_BRANCH="$(
git ls-remote --symref "$REPO_URL" HEAD 2>/dev/null |
awk '$1 == "ref:" { sub("^refs/heads/", "", $2); print $2 }'
)"
if [ -z "$MAIN_BRANCH" ]; then
fail "Could not fetch remote's main branch"
fi
echo "|> Default branch is \`$MAIN_BRANCH\`"
local REPO_PATH="./$REPO_NAME/$MAIN_BRANCH"
echo "|> Creating a new directory \`$REPO_PATH\`..."
mkdir -p "$REPO_PATH"
REPO_PATH="$(realpath "$REPO_PATH")"
echo "|> Cloning repo into directory..."
git clone "$REPO_URL" "$REPO_PATH" || fail 'Failed to clone repository'
echo "All done!"
echo "To switch to this workdir, run:"
echo " cd $(realpath --relative-to="$initial_dir" "$REPO_PATH")"
}
function fail {
echo "Error: $*" >&2
exit 1
}
function print-usage {
local script_name
script_name="$(basename "$0")"
printf "%s - Initialize a git workdir the \"standard\" way.\n" "$script_name"
echo "Usage:"
printf " %s <git-repo-url> (<repository-name-override>)\n" "$script_name"
}
set -uo pipefail
if [ -n "${DEBUG:-}" ]; then
set -x
fi
main "$@"