git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 13, 2014, 21:21 UTC
Message-ID
<xmqqioiizthk.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1415906213.32352.43.camel@mad-scientist.net>
Paul Smith <paul@mad-scientist.net> writes:
Show 14 quoted lines
> From: Paul Smith <paul@mad-scientist.net>
> Date: Thu, 13 Nov 2014 14:01:34 -0500
> Subject: [PATCH] git-new-workdir: Add -f to force new-workdir in existing directory.
>
> Signed-off-by: Paul Smith <psmith@mad-scientist.net>
> ---
>
> 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.

Show 41 quoted lines
>
>  contrib/workdir/git-new-workdir | 16 +++++++++++++---
>  1 file changed, 13 insertions(+), 3 deletions(-)
>
> diff --git a/contrib/workdir/git-new-workdir b/contrib/workdir/git-new-workdir
> index 75e8b25..a4079c1 100755
> --- a/contrib/workdir/git-new-workdir
> +++ b/contrib/workdir/git-new-workdir
> @@ -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
Previous: Paul Smith
Message 2 of 2 in “git-new-workdir: Add -f to force new-workdir in existing directory.”
  1. git-new-workdir: Add -f to force new-workdir in existing directory.Paul Smith, Nov 13, 2014
  2. Junio C HamanoNov 13, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.