From f5dce90558d4591cafe126744f56cb1e35c682ed Mon Sep 17 00:00:00 2001
From: Ryan Stone <rstone@FreeBSD.org>
Date: Fri, 11 Nov 2016 23:07:31 +0000
Subject: [PATCH] Fix git tools when run against a worktree

In a git worktree, the gitdir is in an entirely different location.
In arcgit, use git rev-parse --git-dir to get the correct path to it
always.

When running git from outside of the work tree, as in importgit,
the path provided by git rev-parse --git-dir can be either a
relative or absolute path depending on the work tree.  Rather
than trying to deal with that, just use git -C.

Differential Revision:	https://reviews.freebsd.org/D8501
Reviewed by: markj
---
 tools/tools/git/arcgit    |  2 +-
 tools/tools/git/importgit | 32 ++++++++++++++++----------------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/tools/tools/git/arcgit b/tools/tools/git/arcgit
index 4b98042edd42..60d9abe58595 100755
--- a/tools/tools/git/arcgit
+++ b/tools/tools/git/arcgit
@@ -69,7 +69,7 @@ create_review()
 
 	git checkout $commit > /dev/null || error "Could not checkout $commit"
 
-	arc_dir="$(git rev-parse --show-toplevel)/.git/arc"
+	arc_dir="$(git rev-parse --git-dir)/arc"
 	arc_msg="$arc_dir/create-message"
 	mkdir -p $arc_dir
 	git show -s --format='%B' HEAD > $arc_msg
diff --git a/tools/tools/git/importgit b/tools/tools/git/importgit
index 786930bc5248..5149f2d9e157 100755
--- a/tools/tools/git/importgit
+++ b/tools/tools/git/importgit
@@ -50,7 +50,7 @@ error()
 	exit 1
 }
 
-unset git range commit dry_run
+unset git_repo range commit dry_run
 
 while getopts ":c:g:nr:" o
 do
@@ -95,18 +95,18 @@ then
 	error -u "-g <repo> argument is mandatory"
 fi
 
-git="$git_repo/.git"
-
-if [ ! -d "$git" ]
-then
-	error "$git_repo does not seem to be a git repo"
-fi
-
 if ! type git > /dev/null 2> /dev/null
 then
 	error "Install devel/git first"
 fi
 
+GIT="git -C $git_repo"
+
+if ! $GIT rev-parse --git-dir 2> /dev/null > /dev/null
+then
+	error "$git_repo does not seem to be a git repo"
+fi
+
 if ! type svn > /dev/null 2> /dev/null
 then
 	error "Install devel/subversion first"
@@ -122,21 +122,21 @@ then
 	error "Could not communicate with svn server.  Is your ssh key loaded?"
 fi
 
-git --git-dir=$git log --format=%H $range | tail -r | while read -r commit
+$GIT log --format=%H $range | tail -r | while read -r commit
 do
-	echo "Applying `git --git-dir=$git show -s --oneline $commit`"
+	echo "Applying `$GIT show -s --oneline $commit`"
 
-	if [ -n "$(git --git-dir=$git show --diff-filter=CDRTUXB $commit)" ]
+	if [ -n "$($GIT show --diff-filter=CDRTUXB $commit)" ]
 	then
 		error "Commit performed unsupported change (e.g. delete/rename)"
 	fi
 
-	if [ "$(git --git-dir=$git show -s --format=%P $commit | wc -w)" -ne 1 ]
+	if [ "$($GIT show -s --format=%P $commit | wc -w)" -ne 1 ]
 	then
 		error "Cannot import merge commits"
 	fi
 
-	git --git-dir=$git diff --diff-filter=A --name-only \
+	$GIT diff --diff-filter=A --name-only \
 	    ${commit}~..$commit | while read -r newfile
 	do
 		if [ -f "$newfile" ]
@@ -158,10 +158,10 @@ do
 		continue
 	fi
 
-	git --git-dir=$git show $commit | patch -p 1 -s || \
+	$GIT show $commit | patch -p 1 -s || \
 	    error "Failed to apply patch"
 
-	git --git-dir=$git diff --diff-filter=A --name-only \
+	$GIT diff --diff-filter=A --name-only \
 	    ${commit}~..$commit | while read -r newfile
 	do
 		svn add --parents --depth=infinity $newfile || \
@@ -176,7 +176,7 @@ do
 		exit $ret
 	fi
 
-	git --git-dir=$git show -s --format='%B' $commit | svn commit -F - || \
+	$GIT show -s --format='%B' $commit | svn commit -F - || \
 	    error "Failed to commit"
 done