Skip to content

Commit c64a8d2

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9f792bb commit c64a8d2

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

builtin/worktree.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,17 @@ static int move_worktree(int ac, const char **av, const char *prefix)
631631
die(_("'%s' is not a working tree"), av[0]);
632632
if (is_main_worktree(wt))
633633
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+
}
634643
if (file_exists(dst.buf))
635-
die(_("target '%s' already exists"), av[1]);
644+
die(_("target '%s' already exists"), dst.buf);
636645

637646
reason = is_worktree_locked(wt);
638647
if (reason) {

strbuf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,21 @@ void strbuf_trim(struct strbuf *sb)
9595
strbuf_rtrim(sb);
9696
strbuf_ltrim(sb);
9797
}
98+
9899
void strbuf_rtrim(struct strbuf *sb)
99100
{
100101
while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
101102
sb->len--;
102103
sb->buf[sb->len] = '\0';
103104
}
104105

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+
105113
void strbuf_ltrim(struct strbuf *sb)
106114
{
107115
char *b = sb->buf;

strbuf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ extern void strbuf_trim(struct strbuf *);
179179
extern void strbuf_rtrim(struct strbuf *);
180180
extern void strbuf_ltrim(struct strbuf *);
181181

182+
/* Strip trailing directory separators */
183+
extern void strbuf_trim_trailing_dir_sep(struct strbuf *);
184+
182185
/**
183186
* Replace the contents of the strbuf with a reencoded form. Returns -1
184187
* on error, 0 on success.

t/t2028-worktree-move.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,15 @@ test_expect_success 'move main worktree' '
8585
test_must_fail git worktree move . def
8686
'
8787

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+
8899
test_done

0 commit comments

Comments
 (0)