Call mkpathdup() rather than xstrdup(mkpath(...))

In addition to updating the xstrdup(mkpath(...)) call sites with mkpathdup(), we also fix a memory leak (in merge_3way()) caused by neglecting to free the memory allocated to the 'base_name' variable. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramsay Jones committed Sep 4, 2012 at 18:31 UTC 4e2d094dde4f078245d057dd6111ab9d013ae6d0
4 files changed +11 -10
builtin/branch.c
+1 -1
@@ -196,7 +196,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
196
197 free(name);
198
199 - name = xstrdup(mkpath(fmt, bname.buf));
199 + name = mkpathdup(fmt, bname.buf);
200 if (read_ref(name, sha1)) {
201 error(remote_branch
202 ? _("remote branch '%s' not found.")
builtin/clone.c
+2 -2
@@ -236,7 +236,7 @@ static int add_one_reference(struct string_list_item *item, void *cb_data)
236 /* Beware: real_path() and mkpath() return static buffer */
237 ref_git = xstrdup(real_path(item->string));
238 if (is_directory(mkpath("%s/.git/objects", ref_git))) {
239 - char *ref_git_git = xstrdup(mkpath("%s/.git", ref_git));
239 + char *ref_git_git = mkpathdup("%s/.git", ref_git);
240 free(ref_git);
241 ref_git = ref_git_git;
242 } else if (!is_directory(mkpath("%s/objects", ref_git)))
@@ -700,7 +700,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
700 git_dir = xstrdup(dir);
701 else {
702 work_tree = dir;
703 - git_dir = xstrdup(mkpath("%s/.git", dir));
703 + git_dir = mkpathdup("%s/.git", dir);
704 }
705
706 if (!option_bare) {
builtin/prune.c
+1 -1
@@ -168,7 +168,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
168
169 prune_packed_objects(show_only);
170 remove_temporary_files(get_object_directory());
171 - s = xstrdup(mkpath("%s/pack", get_object_directory()));
171 + s = mkpathdup("%s/pack", get_object_directory());
172 remove_temporary_files(s);
173 free(s);
174 return 0;
merge-recursive.c
+7 -6
@@ -862,14 +862,14 @@ static int merge_3way(struct merge_options *o,
862 if (strcmp(a->path, b->path) ||
863 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
864 base_name = o->ancestor == NULL ? NULL :
865 - xstrdup(mkpath("%s:%s", o->ancestor, one->path));
866 - name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
867 - name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
865 + mkpathdup("%s:%s", o->ancestor, one->path);
866 + name1 = mkpathdup("%s:%s", branch1, a->path);
867 + name2 = mkpathdup("%s:%s", branch2, b->path);
868 } else {
869 base_name = o->ancestor == NULL ? NULL :
870 - xstrdup(mkpath("%s", o->ancestor));
871 - name1 = xstrdup(mkpath("%s", branch1));
872 - name2 = xstrdup(mkpath("%s", branch2));
870 + mkpathdup("%s", o->ancestor);
871 + name1 = mkpathdup("%s", branch1);
872 + name2 = mkpathdup("%s", branch2);
873 }
874
875 read_mmblob(&orig, one->sha1);
@@ -879,6 +879,7 @@ static int merge_3way(struct merge_options *o,
879 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
880 &src1, name1, &src2, name2, &ll_opts);
881
882 + free(base_name);
883 free(name1);
884 free(name2);
885 free(orig.ptr);