pathspec: add copy_pathspec
Because free_pathspec wants to free "items" pointer in the pathspec structure, a simple structure assignment is not enough if you want to copy an existing pathspec into another. Freeing the original will damage the copy unless a deep copy is made. Note that the strings in pathspec->items->match and the array pathspec->raw[] are still shared between the original and the copy. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jul 14, 2013 at 15:35 UTC
e4d92cdcd9450af7ae4c40de2b7c5c27a1faa138
3 files changed
+16
-6
builtin/mv.c
+7
-6
@@ -15,8 +15,9 @@ static const char * const builtin_mv_usage[] = {
15
NULL
16
};
17
18
-static const char **copy_pathspec(const char *prefix, const char **pathspec,
19
- int count, int base_name)
18
+static const char **internal_copy_pathspec(const char *prefix,
19
+ const char **pathspec,
20
+ int count, int base_name)
21
{
22
int i;
23
const char **result = xmalloc((count + 1) * sizeof(const char *));
@@ -81,17 +82,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
82
if (read_cache() < 0)
83
die(_("index file corrupt"));
84
84
- source = copy_pathspec(prefix, argv, argc, 0);
85
+ source = internal_copy_pathspec(prefix, argv, argc, 0);
86
modes = xcalloc(argc, sizeof(enum update_mode));
86
- dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
87
+ dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
88
89
if (dest_path[0][0] == '\0')
90
/* special case: "." was normalized to "" */
90
- destination = copy_pathspec(dest_path[0], argv, argc, 1);
91
+ destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
92
else if (!lstat(dest_path[0], &st) &&
93
S_ISDIR(st.st_mode)) {
94
dest_path[0] = add_slash(dest_path[0]);
94
- destination = copy_pathspec(dest_path[0], argv, argc, 1);
95
+ destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
96
} else {
97
if (argc != 1)
98
die("destination '%s' is not a directory", dest_path[0]);
pathspec.c
+8
@@ -249,3 +249,11 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
249
return NULL;
250
return pathspec;
251
}
252
+
253
+void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
254
+{
255
+ *dst = *src;
256
+ dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
257
+ memcpy(dst->items, src->items,
258
+ sizeof(struct pathspec_item) * dst->nr);
259
+}
pathspec.h
+1
@@ -18,6 +18,7 @@ struct pathspec {
18
};
19
20
extern int init_pathspec(struct pathspec *, const char **);
21
+extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
22
extern void free_pathspec(struct pathspec *);
23
24
extern int limit_pathspec_to_literal(void);