tree.c: update read_tree_recursive callback to pass strbuf as base

This allows the callback to use 'base' as a temporary buffer to
quickly assemble full path "without" extra allocation. The callback
has to restore it afterwards of course.

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy
2014-11-30 16:05:00 +07:00
committed by Junio C Hamano
parent b260d265e1
commit 6a0b0b6de9
7 changed files with 50 additions and 37 deletions

View File

@ -275,23 +275,20 @@ struct tree *write_tree_from_memory(struct merge_options *o)
}
static int save_files_dirs(const unsigned char *sha1,
const char *base, int baselen, const char *path,
struct strbuf *base, const char *path,
unsigned int mode, int stage, void *context)
{
int len = strlen(path);
char *newpath = xmalloc(baselen + len + 1);
int baselen = base->len;
struct merge_options *o = context;
memcpy(newpath, base, baselen);
memcpy(newpath + baselen, path, len);
newpath[baselen + len] = '\0';
strbuf_addstr(base, path);
if (S_ISDIR(mode))
string_list_insert(&o->current_directory_set, newpath);
string_list_insert(&o->current_directory_set, base->buf);
else
string_list_insert(&o->current_file_set, newpath);
free(newpath);
string_list_insert(&o->current_file_set, base->buf);
strbuf_setlen(base, baselen);
return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
}