Skip to content

Commit fe9ec8b

Browse files
committed
Merge branch 'bw/pathspec-cleanup'
Code clean-up in the pathspec API. * bw/pathspec-cleanup: pathspec: rename prefix_pathspec to init_pathspec_item pathspec: small readability changes pathspec: create strip submodule slash helpers pathspec: create parse_element_magic helper pathspec: create parse_long_magic function pathspec: create parse_short_magic function pathspec: factor global magic into its own function pathspec: simpler logic to prefix original pathspec elements pathspec: always show mnemonic and name in unsupported_magic pathspec: remove unused variable from unsupported_magic pathspec: copy and free owned memory pathspec: remove the deprecated get_pathspec function ls-tree: convert show_recursive to use the pathspec struct interface dir: convert fill_directory to use the pathspec struct interface dir: remove struct path_simplify mv: remove use of deprecated 'get_pathspec()'
2 parents e51058f + 27ec428 commit fe9ec8b

File tree

7 files changed

+388
-357
lines changed

7 files changed

+388
-357
lines changed

Documentation/technical/api-setup.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ parse_pathspec(). This function takes several arguments:
2727

2828
- prefix and args come from cmd_* functions
2929

30-
get_pathspec() is obsolete and should never be used in new code.
31-
3230
parse_pathspec() helps catch unsupported features and reject them
3331
politely. At a lower level, different pathspec-related functions may
3432
not support the same set of features. Such pathspec-sensitive

builtin/ls-tree.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {
3131

3232
static int show_recursive(const char *base, int baselen, const char *pathname)
3333
{
34-
const char **s;
34+
int i;
3535

3636
if (ls_options & LS_RECURSIVE)
3737
return 1;
3838

39-
s = pathspec._raw;
40-
if (!s)
39+
if (!pathspec.nr)
4140
return 0;
4241

43-
for (;;) {
44-
const char *spec = *s++;
42+
for (i = 0; i < pathspec.nr; i++) {
43+
const char *spec = pathspec.items[i].match;
4544
int len, speclen;
4645

47-
if (!spec)
48-
return 0;
4946
if (strncmp(base, spec, baselen))
5047
continue;
5148
len = strlen(pathname);
@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
5956
continue;
6057
return 1;
6158
}
59+
return 0;
6260
}
6361

6462
static int show_tree(const unsigned char *sha1, struct strbuf *base,
@@ -175,8 +173,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
175173
* cannot be lifted until it is converted to use
176174
* match_pathspec() or tree_entry_interesting()
177175
*/
178-
parse_pathspec(&pathspec, PATHSPEC_GLOB | PATHSPEC_ICASE |
179-
PATHSPEC_EXCLUDE,
176+
parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC &
177+
~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
180178
PATHSPEC_PREFER_CWD,
181179
prefix, argv + 1);
182180
for (i = 0; i < pathspec.nr; i++)

builtin/mv.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright (C) 2006 Johannes Schindelin
55
*/
66
#include "builtin.h"
7+
#include "pathspec.h"
78
#include "lockfile.h"
89
#include "dir.h"
910
#include "cache-tree.h"
@@ -19,31 +20,42 @@ static const char * const builtin_mv_usage[] = {
1920
#define DUP_BASENAME 1
2021
#define KEEP_TRAILING_SLASH 2
2122

22-
static const char **internal_copy_pathspec(const char *prefix,
23-
const char **pathspec,
24-
int count, unsigned flags)
23+
static const char **internal_prefix_pathspec(const char *prefix,
24+
const char **pathspec,
25+
int count, unsigned flags)
2526
{
2627
int i;
2728
const char **result;
29+
int prefixlen = prefix ? strlen(prefix) : 0;
2830
ALLOC_ARRAY(result, count + 1);
29-
COPY_ARRAY(result, pathspec, count);
30-
result[count] = NULL;
31+
32+
/* Create an intermediate copy of the pathspec based on the flags */
3133
for (i = 0; i < count; i++) {
32-
int length = strlen(result[i]);
34+
int length = strlen(pathspec[i]);
3335
int to_copy = length;
36+
char *it;
3437
while (!(flags & KEEP_TRAILING_SLASH) &&
35-
to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
38+
to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
3639
to_copy--;
37-
if (to_copy != length || flags & DUP_BASENAME) {
38-
char *it = xmemdupz(result[i], to_copy);
39-
if (flags & DUP_BASENAME) {
40-
result[i] = xstrdup(basename(it));
41-
free(it);
42-
} else
43-
result[i] = it;
40+
41+
it = xmemdupz(pathspec[i], to_copy);
42+
if (flags & DUP_BASENAME) {
43+
result[i] = xstrdup(basename(it));
44+
free(it);
45+
} else {
46+
result[i] = it;
4447
}
4548
}
46-
return get_pathspec(prefix, result);
49+
result[count] = NULL;
50+
51+
/* Prefix the pathspec and free the old intermediate strings */
52+
for (i = 0; i < count; i++) {
53+
const char *match = prefix_path(prefix, prefixlen, result[i]);
54+
free((char *) result[i]);
55+
result[i] = match;
56+
}
57+
58+
return result;
4759
}
4860

4961
static const char *add_slash(const char *path)
@@ -130,7 +142,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
130142
if (read_cache() < 0)
131143
die(_("index file corrupt"));
132144

133-
source = internal_copy_pathspec(prefix, argv, argc, 0);
145+
source = internal_prefix_pathspec(prefix, argv, argc, 0);
134146
modes = xcalloc(argc, sizeof(enum update_mode));
135147
/*
136148
* Keep trailing slash, needed to let
@@ -140,16 +152,16 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
140152
flags = KEEP_TRAILING_SLASH;
141153
if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
142154
flags = 0;
143-
dest_path = internal_copy_pathspec(prefix, argv + argc, 1, flags);
155+
dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
144156
submodule_gitfile = xcalloc(argc, sizeof(char *));
145157

146158
if (dest_path[0][0] == '\0')
147159
/* special case: "." was normalized to "" */
148-
destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
160+
destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
149161
else if (!lstat(dest_path[0], &st) &&
150162
S_ISDIR(st.st_mode)) {
151163
dest_path[0] = add_slash(dest_path[0]);
152-
destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
164+
destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
153165
} else {
154166
if (argc != 1)
155167
die(_("destination '%s' is not a directory"), dest_path[0]);

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,6 @@ extern void set_git_work_tree(const char *tree);
514514

515515
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
516516

517-
extern const char **get_pathspec(const char *prefix, const char **pathspec);
518517
extern void setup_work_tree(void);
519518
extern const char *setup_git_directory_gently(int *);
520519
extern const char *setup_git_directory(void);

0 commit comments

Comments
 (0)