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

[PATCH 09/10] worktree move: accept destination as directory

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 25, 2016, 07:54 UTC
Message-ID
<20160625075433.4608-10-pclouds@gmail.com>
In-Reply-To
<20160625075433.4608-1-pclouds@gmail.com>

Similar to "mv a b/", which is actually "mv a b/a", we extract basename of source worktree and create a directory of the same name at destination if dst path is a directory.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/worktree.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 5d7ca27..443a5ae 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -541,7 +541,13 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 	strbuf_addstr(&dst, prefix_filename(prefix,
 					    strlen(prefix),
 					    av[1]));
-	if (file_exists(dst.buf))
+	if (is_directory(dst.buf))
+		/*
+		 * keep going, dst will be appended after we get the
+		 * source's absolute path
+		 */
+		;
+	else if (file_exists(dst.buf))
 		die(_("target '%s' already exists"), av[1]);
 
 	worktrees = get_worktrees();
@@ -558,6 +564,17 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 	if (validate_worktree(wt, 0))
 		return -1;
 
+	if (is_directory(dst.buf)) {
+		const char *sep = find_last_dir_sep(wt->path);
+
+		if (!sep)
+			die(_("could not figure out destination name from '%s'"),
+			    wt->path);
+		strbuf_addstr(&dst, sep);
+		if (file_exists(dst.buf))
+			die(_("target '%s' already exists"), dst.buf);
+	}
+
 	/*
 	 * First try. Atomically move, and probably cheaper, if both
 	 * source and target are on the same file system.
-- 
2.8.2.526.g02eed6d
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 4 of 16 in “git worktree (re)move”
  1. 00/10 git worktree (re)moveNguyễn Thái Ngọc Duy, Jun 25, 2016
  2. 04/10 copy.c: style fixNguyễn Thái Ngọc Duy, Jun 25, 2016
  3. 02/10 copy.c: delete unused code in copy_file()Nguyễn Thái Ngọc Duy, Jun 25, 2016
  4. 09/10 worktree move: accept destination as directoryNguyễn Thái Ngọc Duy, Jun 25, 2016
  5. 05/10 copy.c: convert copy_file() to copy_dir_recursively()Nguyễn Thái Ngọc Duy, Jun 25, 2016
  6. Eric SunshineAug 1, 2017
  7. 01/10 copy.c: import copy_file() from busyboxNguyễn Thái Ngọc Duy, Jun 25, 2016
  8. 10/10 worktree: add "remove" commandNguyễn Thái Ngọc Duy, Jun 25, 2016
  9. Eric SunshineAug 1, 2017
  10. 06/10 worktree.c: add validate_worktree()Nguyễn Thái Ngọc Duy, Jun 25, 2016
  11. 08/10 worktree: add "move" commmandNguyễn Thái Ngọc Duy, Jun 25, 2016
  12. Eric SunshineAug 1, 2017
  13. 07/10 worktree.c: add update_worktree_location()Nguyễn Thái Ngọc Duy, Jun 25, 2016
  14. Eric SunshineAug 1, 2017
  15. 03/10 copy.c: convert bb_(p)error_msg to error(_errno)Nguyễn Thái Ngọc Duy, Jun 25, 2016
  16. Duy NguyenJul 23, 2016

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.