Git fork

Merge branch 'ps/new-workdir-into-empty-directory'

"git new-workdir" (in contrib/) can be used to populate an empty
and existing directory now.

* ps/new-workdir-into-empty-directory:
git-new-workdir: don't fail if the target directory is empty

+38 -15
+38 -15
contrib/workdir/git-new-workdir
··· 10 exit 128 11 } 12 13 if test $# -lt 2 || test $# -gt 3 14 then 15 usage "$0 <repository> <new_workdir> [<branch>]" ··· 35 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" ··· 48 "a complete repository." 49 fi 50 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 57 - # make sure the links use full paths 58 - git_dir=$(cd "$git_dir"; pwd) 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. explicitly 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. 66 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn 67 do 68 case $x in 69 */*) 70 - mkdir -p "$(dirname "$new_workdir/.git/$x")" 71 ;; 72 esac 73 - ln -s "$git_dir/$x" "$new_workdir/.git/$x" 74 done 75 76 - # now setup the workdir 77 - cd "$new_workdir" 78 # copy the HEAD from the original repository as a default branch 79 - cp "$git_dir/HEAD" .git/HEAD 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
··· 10 exit 128 11 } 12 13 + failed () { 14 + die "unable to create new workdir '$new_workdir'!" 15 + } 16 + 17 if test $# -lt 2 || test $# -gt 3 18 then 19 usage "$0 <repository> <new_workdir> [<branch>]" ··· 39 40 # don't link to a configured bare repository 41 isbare=$(git --git-dir="$git_dir" config --bool --get core.bare) 42 + if test ztrue = "z$isbare" 43 then 44 die "\"$git_dir\" has core.bare set to true," \ 45 " remove from \"$git_dir/config\" to use $0" ··· 52 "a complete repository." 53 fi 54 55 + # make sure the links in the workdir have full paths to the original repo 56 + git_dir=$(cd "$git_dir" && pwd) || exit 1 57 + 58 + # don't recreate a workdir over an existing directory, unless it's empty 59 + if test -d "$new_workdir" 60 then 61 + if test $(ls -a1 "$new_workdir/." | wc -l) -ne 2 62 + then 63 + die "destination directory '$new_workdir' is not empty." 64 + fi 65 + cleandir="$new_workdir/.git" 66 + else 67 + cleandir="$new_workdir" 68 fi 69 70 + mkdir -p "$new_workdir/.git" || failed 71 + cleandir=$(cd "$cleandir" && pwd) || failed 72 73 + cleanup () { 74 + rm -rf "$cleandir" 75 + } 76 + siglist="0 1 2 15" 77 + trap cleanup $siglist 78 79 # create the links to the original repo. explicitly exclude index, HEAD and 80 # logs/HEAD from the list since they are purely related to the current working 81 # directory, and should not be shared. 82 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn 83 do 84 + # create a containing directory if needed 85 case $x in 86 */*) 87 + mkdir -p "$new_workdir/.git/${x%/*}" 88 ;; 89 esac 90 + 91 + ln -s "$git_dir/$x" "$new_workdir/.git/$x" || failed 92 done 93 94 + # commands below this are run in the context of the new workdir 95 + cd "$new_workdir" || failed 96 + 97 # copy the HEAD from the original repository as a default branch 98 + cp "$git_dir/HEAD" .git/HEAD || failed 99 + 100 + # the workdir is set up. if the checkout fails, the user can fix it. 101 + trap - $siglist 102 + 103 + # checkout the branch (either the same as HEAD from the original repository, 104 + # or the one that was asked for) 105 git checkout -f $branch