Re: [PATCH 24/25] worktree move: accept destination as directory
- From
Duy Nguyen <pclouds@gmail.com>
- Date
- May 11, 2016, 13:34 UTC
- Message-ID
- <CACsJy8CCAan9ALxULPFeGSU7wsfwbrywRWFr4Hsjx3=PGwosLA@mail.gmail.com>
- In-Reply-To
- <CAPig+cRtajkynLFj5Fknd72QQ=Eqevh1T9i9MvApwXfNmtMAFQ@mail.gmail.com>
On Wed, May 11, 2016 at 11:43 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 23 quoted lines
> On Wed, Apr 13, 2016 at 9:15 AM, Nguyễn Thái Ngọc Duy <pclouds@gmail.com> wrote:
>> 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>
>> ---
>> diff --git a/builtin/worktree.c b/builtin/worktree.c
>> @@ -538,7 +538,13 @@ static int move_worktree(int ac, const char **av, const char *prefix)
>> - 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]);
>> @@ -558,6 +564,17 @@ static int move_worktree(int ac, const char **av, const char *prefix)
>> + if (is_directory(dst.buf)) {
>> + const char *sep = strrchr(wt->path, '/');
>
> Does this need to take Windows into account?wt->path comes from $GIT_DIR/worktrees/xxx/gitdir, which normally uses forward slashes, so we should be safe. We already rely on forward slashes in get_linked_worktree()
> Perhaps git_find_last_dir_sep()?
But this is probably a good thing to do anyway, to be more robust in future. But it could confuse the reader later on why it's necessary when backward slashes can't exist in wt->path. I don't know. Maybe just have a comment that backward slashes can't never appear here?
There is also a potential problem with find_worktree_by_path(). I was counting on real_path() to normalize paths and could simply do strcmp_icase (or its new name, fspathcmp). But real_path() does not seem to convert unify slashes. I will need to have a closer look at this. Hopefully prefix_filename() already makes sure everything uses forward slashes. Or maybe we could improve fspathcmp to see '/' and '\' the same thing on Windows.
-- Duy