merge-recursive: convert malloc / strcpy to strbuf

This would be a fairly routine use of xstrfmt, except that
we need to remember the length of the result to pass to
cache_name_pos. So just use a strbuf, which makes this
simple.

As a bonus, this gets rid of confusing references to
"pathlen+1". The "1" is for the trailing slash we added, but
that is automatically accounted for in the strbuf's len
parameter.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2015-09-24 17:07:43 -04:00 коммит произвёл Junio C Hamano
Родитель bd22d4ffbc
Коммит b4600fbe07
1 изменённых файлов: 8 добавлений и 9 удалений

Просмотреть файл

@ -630,25 +630,24 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
static int dir_in_way(const char *path, int check_working_copy) static int dir_in_way(const char *path, int check_working_copy)
{ {
int pos, pathlen = strlen(path); int pos;
char *dirpath = xmalloc(pathlen + 2); struct strbuf dirpath = STRBUF_INIT;
struct stat st; struct stat st;
strcpy(dirpath, path); strbuf_addstr(&dirpath, path);
dirpath[pathlen] = '/'; strbuf_addch(&dirpath, '/');
dirpath[pathlen+1] = '\0';
pos = cache_name_pos(dirpath, pathlen+1); pos = cache_name_pos(dirpath.buf, dirpath.len);
if (pos < 0) if (pos < 0)
pos = -1 - pos; pos = -1 - pos;
if (pos < active_nr && if (pos < active_nr &&
!strncmp(dirpath, active_cache[pos]->name, pathlen+1)) { !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
free(dirpath); strbuf_release(&dirpath);
return 1; return 1;
} }
free(dirpath); strbuf_release(&dirpath);
return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode); return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
} }