worktree move: accept destination as directory
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Feb 12, 2018 at 16:49 UTC
c64a8d200f4109df86c6d4716ea4da58df450e34
4 files changed
+32
-1
builtin/worktree.c
+10
-1
@@ -631,8 +631,17 @@ static int move_worktree(int ac, const char **av, const char *prefix)
631
die(_("'%s' is not a working tree"), av[0]);
632
if (is_main_worktree(wt))
633
die(_("'%s' is a main working tree"), av[0]);
634
+ if (is_directory(dst.buf)) {
635
+ const char *sep = find_last_dir_sep(wt->path);
636
+
637
+ if (!sep)
638
+ die(_("could not figure out destination name from '%s'"),
639
+ wt->path);
640
+ strbuf_trim_trailing_dir_sep(&dst);
641
+ strbuf_addstr(&dst, sep);
642
+ }
643
if (file_exists(dst.buf))
635
- die(_("target '%s' already exists"), av[1]);
644
+ die(_("target '%s' already exists"), dst.buf);
645
646
reason = is_worktree_locked(wt);
647
if (reason) {
strbuf.c
+8
@@ -95,6 +95,7 @@ void strbuf_trim(struct strbuf *sb)
95
strbuf_rtrim(sb);
96
strbuf_ltrim(sb);
97
}
98
+
99
void strbuf_rtrim(struct strbuf *sb)
100
{
101
while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
@@ -102,6 +103,13 @@ void strbuf_rtrim(struct strbuf *sb)
103
sb->buf[sb->len] = '\0';
104
}
105
106
+void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
107
+{
108
+ while (sb->len > 0 && is_dir_sep((unsigned char)sb->buf[sb->len - 1]))
109
+ sb->len--;
110
+ sb->buf[sb->len] = '\0';
111
+}
112
+
113
void strbuf_ltrim(struct strbuf *sb)
114
{
115
char *b = sb->buf;
strbuf.h
+3
@@ -179,6 +179,9 @@ extern void strbuf_trim(struct strbuf *);
179
extern void strbuf_rtrim(struct strbuf *);
180
extern void strbuf_ltrim(struct strbuf *);
181
182
+/* Strip trailing directory separators */
183
+extern void strbuf_trim_trailing_dir_sep(struct strbuf *);
184
+
185
/**
186
* Replace the contents of the strbuf with a reencoded form. Returns -1
187
* on error, 0 on success.
t/t2028-worktree-move.sh
+11
@@ -85,4 +85,15 @@ test_expect_success 'move main worktree' '
85
test_must_fail git worktree move . def
86
'
87
88
+test_expect_success 'move worktree to another dir' '
89
+ toplevel="$(pwd)" &&
90
+ mkdir some-dir &&
91
+ git worktree move destination some-dir &&
92
+ test_path_is_missing source &&
93
+ git worktree list --porcelain | grep "^worktree.*/some-dir/destination" &&
94
+ git -C some-dir/destination log --format=%s >actual2 &&
95
+ echo init >expected2 &&
96
+ test_cmp expected2 actual2
97
+'
98
+
99
test_done