summaryrefslogtreecommitdiffstats
path: root/src/subgit-clone
blob: 205b5186b19240191d7e5a574e50c5d84881366c (plain) (blame)
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
#!/bin/bash

# Clones a repository for use in .subgit for tracking worktree changes.

usage() {
	die $(cat <<-EOF
	Usage: subgit clone [-b BRANCH] [CLONE-ARGS..] REMOTE PATH

	  Clones a bare repository for use in .subgit directory. The bare
	  repository is configured to track changes from a remote while
	  allowing the working tree to be symlinked separately.
	EOF
	)
}

while [ "$1" ]; do
	case $1 in
	-b) [ $# -lt 2 ] && die "Missing branch" || branch="$2"; shift; shift;;
	-h) usage;;
	--) shift; break;;
	-*) die "Invalid option $1";;
	*) break;;
	esac
done

[ $# -lt 2 ] && usage

path="${@: -1}"
[ -d "$path" ] && die "Path already exists"

args=()
if [ ! -z "$branch" ]; then
	args+=(--single-branch -b "$branch")
fi
git clone --bare "${args[@]}" "$@"
git -C "$path" config --local --bool core.bare false
git -C "$path" config --local remote.origin.fetch \
	"+refs/heads/*:refs/remotes/origin/*"