-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathwtclone.sh
More file actions
executable file
·83 lines (68 loc) · 1.65 KB
/
wtclone.sh
File metadata and controls
executable file
·83 lines (68 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
#!/usr/bin/env bash
# Based on: https://morgan.cugerone.com/blog/workarounds-to-git-worktree-using-bare-repository-and-cannot-fetch-remote-branches/
set -e
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
CLEAR="\033[0m"
VERBOSE=
function usage {
cat <<EOF
Usage: wtclone [-vh] REPO_URL [DIR_NAME]
Clone a repository into a bare worktree layout.
This will:
- create a directory named DIR_NAME (defaults to the repo name)
- clone the repo as a bare repo into .bare
- fetch all branches
- add a worktree for the default branch
FLAGS:
-h, --help Print this help
-v, --verbose Verbose mode
EOF
kill -INT $$
}
# Parse flags
while true; do
case $1 in
help | -h | --help)
usage
;;
-v | --verbose)
VERBOSE=true
shift
;;
*)
break
;;
esac
done
if [ -z "$1" ]; then
usage
fi
url=$1
basename=${url##*/}
name=${2:-${basename%.*}}
if [ -n "$VERBOSE" ]; then
set -x
fi
mkdir $name
cd "$name"
# Moves all the administrative git files (a.k.a $GIT_DIR) under .bare directory.
#
# Plan is to create worktrees as siblings of this directory.
# Example targeted structure:
# .bare
# main
# new-awesome-feature
# hotfix-bug-12
# ...
git clone --bare "$url" .bare
echo "gitdir: ./.bare" > .git
# Explicitly sets the remote origin fetch so we can fetch remote branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# Gets all branches from origin
git fetch origin
# Add worktree for main branch
main_branch=$(git branch --show-current)
git worktree add "$main_branch"
printf "%bCloned repo to %s%b\n" "$GREEN" "$name" "$CLEAR"