From: Junio C Hamano Date: Thu, 02 Jun 2016 16:49:53 GMT Subject: Re: [PATCH v3 2/6] worktree.c: find_worktree() learns to identify worktrees by basename Message-ID: In-Reply-To: Duy Nguyen writes: > On Thu, Jun 2, 2016 at 1:44 AM, Junio C Hamano wrote: >>> We would >>> need to convert or match both '/' and '\' in "to/foo" case because of >>> Windows, so it's not much easier than basename(). >> >> I never said "easier to implement". But can this codepath get >> backslashed paths in the first place? I somehow thought that >> normalization would happen a lot before the control reaches here. >> >> You'll be calling into fspathcmp() anyway; shouldn't the function >> know that '/' and '\' are equivalent on some platforms, or is it >> legal to only call fspathcmp() on a single path component without >> directory separator? > > We still need to calculate the length to compare, which could be > problematic when utf-8 is involved, or some other encoding. Hmph. I was unaware that fspathcmp() used here does more than byte-for-byte processing, which would cause problems due to encoding issues when you hand code the comparison. +static struct worktree *find_worktree_by_basename(struct worktree **list, + const char *base_name) +{ + struct worktree *found = NULL; + int nr_found = 0; + + for (; *list && nr_found < 2; list++) { + char *path = xstrdup((*list)->path); + if (!fspathcmp(base_name, basename(path))) { + found = *list; + nr_found++; + } + free(path); + } + return nr_found == 1 ? found : NULL; +} > If we always split at '/' boundary though (e.g. "abc/def/ghi", > "def/ghi" or "ghi" but never "ef/ghi") then it should be ok. Does "basename()" used here know '/' and '\' can both be a directory separator, or does worktree->path have a normalized representation of the path, i.e. '/' is the only directory separator?