Skip to contentSkip to conversation

git-new-workdir: Add -f to force new-workdir in existing directory.

PATCH In review Paul Smith 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore

Paul Smith

author
#0 lore raw

PATCH 1/1 git-new-workdir: Add -f to force new-workdir in existing directory.

From: Paul Smith <[email protected]>
Date: Thu, 13 Nov 2014 14:01:34 -0500
Subject: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.

---

I have an environment I want to use new-workdir for, where the directory I need to use is pre-created for me and I'm dropped into that directory and I have no control over this (it's an automated build system). The directory is empty but git-new-workdir still is unhappy about it. I added a "-f" flag to allow the user to force git-new-workdir to continue even if the directory exists. It still bails if there's a .git directory already, however.

 contrib/workdir/git-new-workdir | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
contrib/workdir/git-new-workdir +13 -3
index 75e8b25..a4079c1 100755
@@ -10,11 +10,17 @@ die () {
 	exit 128
 }
 
-if test $# -lt 2 || test $# -gt 3
+if test $# -lt 2 || test $# -gt 4
 then
-	usage "$0 <repository> <new_workdir> [<branch>]"
+	usage "$0 [-f] <repository> <new_workdir> [<branch>]"
 fi
 
+force=false
+if [ x"$1" = x-f ]
+then
+    force=true
+    shift
+fi
 orig_git=$1
 new_workdir=$2
 branch=$3
@@ -51,7 +57,11 @@ fi
 # don't recreate a workdir over an existing repository
 if test -e "$new_workdir"
 then
-	die "destination directory '$new_workdir' already exists."
+	$force || die "destination directory '$new_workdir' already exists."
+	if test -e "$new_workdir/.git"
+	then
+		die "destination directory '$new_workdir/.git' already exists."
+	fi
 fi
 
 # make sure the links use full paths
Signature · 1 line
2.1.3
#1 lore raw
Quoted 7 lines · Paul Smith

From: Paul Smith <[email protected]>
Date: Thu, 13 Nov 2014 14:01:34 -0500
Subject: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.

---

Quoted 7 lines

I have an environment I want to use new-workdir for, where the directory I need to use is pre-created for me and I'm dropped into that directory and I have no control over this (it's an automated build system). The directory is empty but git-new-workdir still is unhappy about it. I added a "-f" flag to allow the user to force git-new-workdir to continue even if the directory exists. It still bails if there's a .git directory already, however.

Is there an easy way to check if the existing directory is really empty? For one thing, with your patched version, you may be by mistake overwriting things when "git checkout -f" happens at the end, even if there weren't any existing ".git/" directory there. And if you can check that the existing directory is empty, you perhaps may not even have to protect this behind a "-f" option.

Quoted 41 lines
 contrib/workdir/git-new-workdir | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
contrib/workdir/git-new-workdir +13 -3
index 75e8b25..a4079c1 100755
@@ -10,11 +10,17 @@ die () {
 	exit 128
 }
 
-if test $# -lt 2 || test $# -gt 3
+if test $# -lt 2 || test $# -gt 4
 then
-	usage "$0 <repository> <new_workdir> [<branch>]"
+	usage "$0 [-f] <repository> <new_workdir> [<branch>]"
 fi
 
+force=false
+if [ x"$1" = x-f ]
+then
+    force=true
+    shift
+fi
 orig_git=$1
 new_workdir=$2
 branch=$3
@@ -51,7 +57,11 @@ fi
 # don't recreate a workdir over an existing repository
 if test -e "$new_workdir"
 then
-	die "destination directory '$new_workdir' already exists."
+	$force || die "destination directory '$new_workdir' already exists."
+	if test -e "$new_workdir/.git"
+	then
+		die "destination directory '$new_workdir/.git' already exists."
+	fi
 fi
 
 # make sure the links use full paths