]>
Commit | Line | Data |
---|---|---|
4f01748d JP |
1 | #!/bin/sh |
2 | ||
3 | usage () { | |
4 | echo "usage:" $@ | |
5 | exit 127 | |
6 | } | |
7 | ||
8 | die () { | |
9 | echo $@ | |
10 | exit 128 | |
11 | } | |
12 | ||
13 | if test $# -lt 2 || test $# -gt 3 | |
14 | then | |
15 | usage "$0 <repository> <new_workdir> [<branch>]" | |
16 | fi | |
17 | ||
18 | orig_git=$1 | |
19 | new_workdir=$2 | |
20 | branch=$3 | |
21 | ||
22 | # want to make sure that what is pointed to has a .git directory ... | |
2e4aef58 SP |
23 | git_dir=$(cd "$orig_git" 2>/dev/null && |
24 | git rev-parse --git-dir 2>/dev/null) || | |
f66bc5f9 | 25 | die "Not a git repository: \"$orig_git\"" |
4f01748d | 26 | |
e301bfee SP |
27 | case "$git_dir" in |
28 | .git) | |
09381b45 | 29 | git_dir="$orig_git/.git" |
e301bfee SP |
30 | ;; |
31 | .) | |
32 | git_dir=$orig_git | |
33 | ;; | |
34 | esac | |
09381b45 | 35 | |
8fa0ee3b SP |
36 | # don't link to a configured bare repository |
37 | isbare=$(git --git-dir="$git_dir" config --bool --get core.bare) | |
38 | if test ztrue = z$isbare | |
39 | then | |
40 | die "\"$git_dir\" has core.bare set to true," \ | |
41 | " remove from \"$git_dir/config\" to use $0" | |
42 | fi | |
43 | ||
4f01748d | 44 | # don't link to a workdir |
2e4aef58 | 45 | if test -L "$git_dir/config" |
4f01748d JP |
46 | then |
47 | die "\"$orig_git\" is a working directory only, please specify" \ | |
48 | "a complete repository." | |
49 | fi | |
50 | ||
ea09ea22 SP |
51 | # don't recreate a workdir over an existing repository |
52 | if test -e "$new_workdir" | |
53 | then | |
54 | die "destination directory '$new_workdir' already exists." | |
55 | fi | |
56 | ||
4f01748d | 57 | # make sure the the links use full paths |
2e4aef58 | 58 | git_dir=$(cd "$git_dir"; pwd) |
4f01748d JP |
59 | |
60 | # create the workdir | |
61 | mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!" | |
62 | ||
63 | # create the links to the original repo. explictly exclude index, HEAD and | |
64 | # logs/HEAD from the list since they are purely related to the current working | |
65 | # directory, and should not be shared. | |
ac378633 | 66 | for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn |
4f01748d JP |
67 | do |
68 | case $x in | |
69 | */*) | |
70 | mkdir -p "$(dirname "$new_workdir/.git/$x")" | |
71 | ;; | |
72 | esac | |
2e4aef58 | 73 | ln -s "$git_dir/$x" "$new_workdir/.git/$x" |
4f01748d JP |
74 | done |
75 | ||
76 | # now setup the workdir | |
77 | cd "$new_workdir" | |
78 | # copy the HEAD from the original repository as a default branch | |
2e4aef58 | 79 | cp "$git_dir/HEAD" .git/HEAD |
4f01748d JP |
80 | # checkout the branch (either the same as HEAD from the original repository, or |
81 | # the one that was asked for) | |
82 | git checkout -f $branch |