{"thread":{"id":"37955","subject":"[PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.","startedAt":"2014-11-13T19:16:53Z","lastAt":"2014-11-13T21:21:59Z","messageCount":2,"participants":["Paul Smith","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"251840","messageId":"1415906213.32352.43.camel@mad-scientist.net","threadId":"37955","inReplyTo":null,"subject":"[PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.","fromName":"Paul Smith","fromEmail":"paul@mad-scientist.net","sentAt":"2014-11-13T19:16:53Z","receivedAt":"2014-11-13T19:16:53Z","isPatch":true,"sender":{"key":"paul@mad-scientist.net","avatar":"https://avatars.githubusercontent.com/u/109636?v=4"},"body":"From: Paul Smith <paul@mad-scientist.net>\nDate: Thu, 13 Nov 2014 14:01:34 -0500\nSubject: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.\n\nSigned-off-by: Paul Smith <psmith@mad-scientist.net>\n---\n\nI have an environment I want to use new-workdir for, where the directory\nI need to use is pre-created for me and I'm dropped into that directory\nand I have no control over this (it's an automated build system).  The\ndirectory is empty but git-new-workdir still is unhappy about it.  I\nadded a \"-f\" flag to allow the user to force git-new-workdir to continue\neven if the directory exists.  It still bails if there's a .git\ndirectory already, however.\n\n contrib/workdir/git-new-workdir | 16 +++++++++++++---\n 1 file changed, 13 insertions(+), 3 deletions(-)\n\ndiff --git a/contrib/workdir/git-new-workdir b/contrib/workdir/git-new-workdir\nindex 75e8b25..a4079c1 100755\n--- a/contrib/workdir/git-new-workdir\n+++ b/contrib/workdir/git-new-workdir\n@@ -10,11 +10,17 @@ die () {\n \texit 128\n }\n \n-if test $# -lt 2 || test $# -gt 3\n+if test $# -lt 2 || test $# -gt 4\n then\n-\tusage \"$0 <repository> <new_workdir> [<branch>]\"\n+\tusage \"$0 [-f] <repository> <new_workdir> [<branch>]\"\n fi\n \n+force=false\n+if [ x\"$1\" = x-f ]\n+then\n+    force=true\n+    shift\n+fi\n orig_git=$1\n new_workdir=$2\n branch=$3\n@@ -51,7 +57,11 @@ fi\n # don't recreate a workdir over an existing repository\n if test -e \"$new_workdir\"\n then\n-\tdie \"destination directory '$new_workdir' already exists.\"\n+\t$force || die \"destination directory '$new_workdir' already exists.\"\n+\tif test -e \"$new_workdir/.git\"\n+\tthen\n+\t\tdie \"destination directory '$new_workdir/.git' already exists.\"\n+\tfi\n fi\n \n # make sure the links use full paths\n-- \n2.1.3\n"},{"id":"251855","messageId":"xmqqioiizthk.fsf@gitster.dls.corp.google.com","threadId":"37955","inReplyTo":"1415906213.32352.43.camel@mad-scientist.net","subject":"Re: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-11-13T21:21:59Z","receivedAt":"2014-11-13T21:21:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paul Smith <paul@mad-scientist.net> writes:\n\n> From: Paul Smith <paul@mad-scientist.net>\n> Date: Thu, 13 Nov 2014 14:01:34 -0500\n> Subject: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.\n>\n> Signed-off-by: Paul Smith <psmith@mad-scientist.net>\n> ---\n>\n> I have an environment I want to use new-workdir for, where the directory\n> I need to use is pre-created for me and I'm dropped into that directory\n> and I have no control over this (it's an automated build system).  The\n> directory is empty but git-new-workdir still is unhappy about it.  I\n> added a \"-f\" flag to allow the user to force git-new-workdir to continue\n> even if the directory exists.  It still bails if there's a .git\n> directory already, however.\n\nIs there an easy way to check if the existing directory is really\nempty?  For one thing, with your patched version, you may be by\nmistake overwriting things when \"git checkout -f\" happens at the\nend, even if there weren't any existing \".git/\" directory there.\nAnd if you can check that the existing directory is empty, you\nperhaps may not even have to protect this behind a \"-f\" option.\n\n>\n>  contrib/workdir/git-new-workdir | 16 +++++++++++++---\n>  1 file changed, 13 insertions(+), 3 deletions(-)\n>\n> diff --git a/contrib/workdir/git-new-workdir b/contrib/workdir/git-new-workdir\n> index 75e8b25..a4079c1 100755\n> --- a/contrib/workdir/git-new-workdir\n> +++ b/contrib/workdir/git-new-workdir\n> @@ -10,11 +10,17 @@ die () {\n>  \texit 128\n>  }\n>  \n> -if test $# -lt 2 || test $# -gt 3\n> +if test $# -lt 2 || test $# -gt 4\n>  then\n> -\tusage \"$0 <repository> <new_workdir> [<branch>]\"\n> +\tusage \"$0 [-f] <repository> <new_workdir> [<branch>]\"\n>  fi\n>  \n> +force=false\n> +if [ x\"$1\" = x-f ]\n> +then\n> +    force=true\n> +    shift\n> +fi\n>  orig_git=$1\n>  new_workdir=$2\n>  branch=$3\n> @@ -51,7 +57,11 @@ fi\n>  # don't recreate a workdir over an existing repository\n>  if test -e \"$new_workdir\"\n>  then\n> -\tdie \"destination directory '$new_workdir' already exists.\"\n> +\t$force || die \"destination directory '$new_workdir' already exists.\"\n> +\tif test -e \"$new_workdir/.git\"\n> +\tthen\n> +\t\tdie \"destination directory '$new_workdir/.git' already exists.\"\n> +\tfi\n>  fi\n>  \n>  # make sure the links use full paths\n"}]}