contrib / workdir / git-new-workdiron commit Merge branch 'maint' (bd724be)
   1#!/bin/sh
   2
   3usage () {
   4        echo "usage:" $@
   5        exit 127
   6}
   7
   8die () {
   9        echo $@
  10        exit 128
  11}
  12
  13if test $# -lt 2 || test $# -gt 3
  14then
  15        usage "$0 <repository> <new_workdir> [<branch>]"
  16fi
  17
  18orig_git=$1
  19new_workdir=$2
  20branch=$3
  21
  22# want to make sure that what is pointed to has a .git directory ...
  23git_dir=$(cd "$orig_git" 2>/dev/null &&
  24  git rev-parse --git-dir 2>/dev/null) ||
  25  die "\"$orig_git\" is not a git repository!"
  26
  27# don't link to a workdir
  28if test -L "$git_dir/config"
  29then
  30        die "\"$orig_git\" is a working directory only, please specify" \
  31                "a complete repository."
  32fi
  33
  34# make sure the the links use full paths
  35git_dir=$(cd "$git_dir"; pwd)
  36
  37# create the workdir
  38mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
  39
  40# create the links to the original repo.  explictly exclude index, HEAD and
  41# logs/HEAD from the list since they are purely related to the current working
  42# directory, and should not be shared.
  43for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache
  44do
  45        case $x in
  46        */*)
  47                mkdir -p "$(dirname "$new_workdir/.git/$x")"
  48                ;;
  49        esac
  50        ln -s "$git_dir/$x" "$new_workdir/.git/$x"
  51done
  52
  53# now setup the workdir
  54cd "$new_workdir"
  55# copy the HEAD from the original repository as a default branch
  56cp "$git_dir/HEAD" .git/HEAD
  57# checkout the branch (either the same as HEAD from the original repository, or
  58# the one that was asked for)
  59git checkout -f $branch