Introduct pathspec struct
PATCH v2 1/6 Add struct pathspec
In reply to Nguyễn Thái Ngọc DuyThis struct for now is just a wrapper for the current pathspec form:
const char **. It is intended to be extended with more useful pathspec-related information over time.
The data structure for passing pathspec around remains const char **, struct pathspec will be initialized locally to be used and destroyed. Hopefully all pathspec related code will be gradually migrated to pass this struct instead.
--- cache.h | 7 +++++++ dir.c | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 0 deletions(-)
cache.h +7 -0
@@ -492,6 +492,13 @@ extern int index_name_is_other(const struct index_state *, const char *, int);extern int ie_match_stat (const struct index_state *,struct cache_entry *,struct stat *,unsigned int );extern int ie_modified (const struct index_state *,struct cache_entry *,struct stat *,unsigned int ); +struct pathspec { +const char **raw;/* get_pathspec() result, not freed by free_pathspec() */ +int nr; +}; + +extern int init_pathspec (struct pathspec *,const char **); +extern void free_pathspec (struct pathspec *);extern int ce_path_match (const struct cache_entry *ce,const char **pathspec);extern int index_fd (unsigned char *sha1,int fd,struct stat *st,int write_object,enum object_type type,const char *path);extern int index_path (unsigned char *sha1,const char *path,struct stat *st,int write_object);
dir.c +18 -0
@@ -1071,3 +1071,21 @@ int remove_path(const char *name)return 0 ; } +int init_pathspec (struct pathspec *pathspec,const char **paths) +{ +const char **p = paths; + +memset (pathspec,0 ,sizeof (*pathspec)); +if (!p) +return 0 ; +while (*p) + p++; + pathspec->raw = paths; + pathspec->nr = p - paths; +return 0 ; +} + +void free_pathspec (struct pathspec *pathspec) +{ + ;/* do nothing */ +}
Signature · 1 line
1.7.1.rc1.70.g788ca
v2 cover Introduct pathspec struct
Just minor changes from Junio's comments. A comment managed to sneak in cache.h, to briefly explain "raw" field.
Nguyễn Thái Ngọc Duy (6):
Add struct pathspec
diff-no-index: use diff_tree_setup_paths()
pathspec: cache string length when initializing pathspec Convert struct diff_options to use struct pathspec tree_entry_interesting(): remove dependency on struct diff_options Move tree_entry_interesting() to tree-walk.c and export it
builtin/diff-files.c | 2 +- builtin/diff.c | 4 +- builtin/log.c | 2 +- cache.h | 10 +++ diff-lib.c | 2 +- diff-no-index.c | 13 ++-- diff.h | 4 +- dir.c | 29 +++++++++ revision.c | 6 +-- tree-diff.c | 157 +++----------------------------------------------- tree-walk.c | 111 +++++++++++++++++++++++++++++++++++ tree-walk.h | 2 + 12 files changed, 175 insertions(+), 167 deletions(-)
PATCH v2 2/6 diff-no-index: use diff_tree_setup_paths()
diff_options.{paths,nr_paths} will be removed later. Do not modify them directly.
--- diff-no-index.c | 9 +++++---- 1 files changed, 5 insertions(+), 4 deletions(-)
diff-no-index.c +5 -4
@@ -231,8 +231,9 @@ void diff_no_index(struct rev_info *revs,if (prefix) {int len =strlen (prefix); +const char *paths[3 ]; +memset (paths,0 ,sizeof (paths)); - revs->diffopt.paths =xcalloc (2 ,sizeof (char *));for (i =0 ; i <2 ; i++) {const char *p = argv[argc -2 + i];/*
@@ -242,12 +243,12 @@ void diff_no_index(struct rev_info *revs, p = (strcmp (p,"-" ) ?xstrdup (prefix_filename (prefix, len, p)) : p); - revs->diffopt.paths[i] = p; + paths[i] = p; } +diff_tree_setup_paths (paths, &revs->diffopt); }else - revs->diffopt.paths = argv + argc -2 ; - revs->diffopt.nr_paths =2 ; +diff_tree_setup_paths (argv + argc -2 , &revs->diffopt); revs->diffopt.skip_stat_unmatch =1 ;if (!revs->diffopt.output_format) revs->diffopt.output_format =DIFF_FORMAT_PATCH ;
Signature · 1 line
1.7.1.rc1.70.g788ca
PATCH v2 3/6 pathspec: cache string length when initializing pathspec
In reply to Nguyễn Thái Ngọc DuyThis field will be used when tree_entry_interesting() is converted to use struct pathspec. Currently it uses pathlens[] in struct diff_options to avoid calculating string over and over again.
--- cache.h | 3 +++ dir.c | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletions(-)
cache.h +3 -0
@@ -495,6 +495,9 @@ extern int ie_modified(const struct index_state *, struct cache_entry *, structstruct pathspec {const char **raw;/* get_pathspec() result, not freed by free_pathspec() */ int nr; +struct pathspec_item { +int len; + } *items; };extern int init_pathspec (struct pathspec *,const char **);
dir.c +12 -1
@@ -1074,6 +1074,7 @@ int remove_path(const char *name)int init_pathspec (struct pathspec *pathspec,const char **paths) {const char **p = paths; +int i;memset (pathspec,0 ,sizeof (*pathspec));if (!p)
@@ -1082,10 +1083,20 @@ int init_pathspec(struct pathspec *pathspec, const char **paths) p++; pathspec->raw = paths; pathspec->nr = p - paths; +if (!pathspec->nr) +return 0 ; + + pathspec->items =xmalloc (sizeof (struct pathspec_item )*pathspec->nr); +for (i =0 ; i < pathspec->nr; i++) { +struct pathspec_item *item = pathspec->items+i; + + item->len =strlen (paths[i]); + }return 0 ; }void free_pathspec (struct pathspec *pathspec) { - ;/* do nothing */ +free (pathspec->items); + pathspec->items =NULL ; }
Signature · 1 line
1.7.1.rc1.70.g788ca
PATCH v2 4/6 Convert struct diff_options to use struct pathspec
In reply to Nguyễn Thái Ngọc Duy--- builtin/diff-files.c | 2 +- builtin/diff.c | 4 ++-- builtin/log.c | 2 +- diff-lib.c | 2 +- diff-no-index.c | 4 ++-- diff.h | 4 +--- revision.c | 6 +----- tree-diff.c | 46 +++++++++++----------------------------------- 8 files changed, 20 insertions(+), 50 deletions(-)
builtin/diff-files.c +1 -1
@@ -61,7 +61,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix) (rev.diffopt.output_format & DIFF_FORMAT_PATCH)) rev.combine_merges = rev.dense_combined_merges = 1; -if (read_cache_preload (rev.diffopt.paths) <0 ) { +if (read_cache_preload (rev.diffopt.pathspec.raw) <0 ) {perror ("read_cache_preload" );return -1 ; }
builtin/diff.c +2 -2
@@ -135,7 +135,7 @@ static int builtin_diff_index(struct rev_info *revs, revs->max_count != -1 || revs->min_age != -1 || revs->max_age != -1)usage (builtin_diff_usage); -if (read_cache_preload (revs->diffopt.paths) <0 ) { +if (read_cache_preload (revs->diffopt.pathspec.raw) <0 ) {perror ("read_cache_preload" );return -1 ; }
@@ -237,7 +237,7 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv revs->combine_merges = revs->dense_combined_merges =1 ;setup_work_tree (); -if (read_cache_preload (revs->diffopt.paths) <0 ) { +if (read_cache_preload (revs->diffopt.pathspec.raw) <0 ) {perror ("read_cache_preload" );return -1 ; }
builtin/log.c +1 -1
@@ -89,7 +89,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix, rev->always_show_header =0 ;if (DIFF_OPT_TST (&rev->diffopt,FOLLOW_RENAMES )) { rev->always_show_header =0 ; -if (rev->diffopt.nr_paths !=1 ) +if (rev->diffopt.pathspec.nr !=1 )usage ("git logs can only follow renames on one pathname at a time" ); }for (i =1 ; i < argc; i++) {
diff-lib.c +1 -1
@@ -501,7 +501,7 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt) active_nr = dst - active_cache;init_revisions (&revs,NULL ); - revs.prune_data = opt->paths; + revs.prune_data = opt->pathspec.raw; tree =parse_tree_indirect (tree_sha1);if (!tree)die ("bad tree object %s" ,sha1_to_hex (tree_sha1));
diff-no-index.c +2 -2
@@ -260,8 +260,8 @@ void diff_no_index(struct rev_info *revs,if (diff_setup_done (&revs->diffopt) <0 )die ("diff_setup_done failed" ); -if (queue_diff (&revs->diffopt, revs->diffopt.paths[0 ], - revs->diffopt.paths[1 ])) +if (queue_diff (&revs->diffopt, revs->diffopt.pathspec.raw[0 ], + revs->diffopt.pathspec.raw[1 ]))exit (1 );diff_set_mnemonic_prefix (&revs->diffopt,"1/" ,"2/" );diffcore_std (&revs->diffopt);
diff.h +1 -3
@@ -133,9 +133,7 @@ struct diff_options {FILE *file;int close_file; -int nr_paths; -const char **paths; -int *pathlens; +struct pathspec pathspec;change_fn_t change;add_remove_fn_t add_remove;diff_format_fn_t format_callback;
revision.c +1 -5
@@ -553,11 +553,7 @@ static void cherry_pick_list(struct commit_list *list, struct rev_info *revs) left_first = left_count < right_count;init_patch_ids (&ids); -if (revs->diffopt.nr_paths) { - ids.diffopts.nr_paths = revs->diffopt.nr_paths; - ids.diffopts.paths = revs->diffopt.paths; - ids.diffopts.pathlens = revs->diffopt.pathlens; - } + ids.diffopts.pathspec = revs->diffopt.pathspec;/* Compute patch-ids for one side */ for (p = list; p; p = p->next) {
tree-diff.c +11 -35
92 lines not on this page · raw message
Signature · 1 line
1.7.1.rc1.70.g788ca
PATCH v2 5/6 tree_entry_interesting(): remove dependency on struct diff_options
In reply to Nguyễn Thái Ngọc DuyThis function can be potentially used in more places than just tree-diff.c. "struct diff_options" does not make much sense outside diff_tree_sha1().
While removing the use of diff_options, it also removes tree_entry_extract() call. This means S_ISDIR() uses the entry->mode directly, without being filtered by canon_mode() (called internally inside tree_entry_extract)
--- tree-diff.c | 28 +++++++++++----------------- 1 files changed, 11 insertions(+), 17 deletions(-)
tree-diff.c +11 -17
@@ -91,25 +91,20 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const* - zero for no * - negative for "no, and no subsequent entries will be either" */ -static int tree_entry_interesting (struct tree_desc *desc,const char *base,int baselen,struct diff_options *opt) +static int tree_entry_interesting (const struct name_entry *entry,const char *base,int baselen,const struct pathspec *ps) { -const char *path; -const unsigned char *sha1; -unsigned mode;int i;int pathlen;int never_interesting =-1 ; -if (!opt->pathspec.nr) +if (!ps || !ps->nr)return 1 ; - sha1 =tree_entry_extract (desc, &path, &mode); - - pathlen =tree_entry_len (path, sha1); + pathlen =tree_entry_len (entry->path, entry->sha1); -for (i =0 ; i < opt->pathspec.nr; i++) { -const char *match = opt->pathspec.raw[i]; -int matchlen = opt->pathspec.items[i].len; +for (i =0 ; i < ps->nr; i++) { +const char *match = ps->raw[i]; +int matchlen = ps->items[i].len;int m =-1 ;/* signals that we haven't called strncmp() */ if (baselen >= matchlen) {
@@ -147,7 +142,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int* Does match sort strictly earlier than path * with their common parts? */ - m =strncmp (match, path, + m =strncmp (match, entry->path, (matchlen < pathlen) ? matchlen : pathlen);if (m <0 )continue ;
@@ -174,7 +169,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, intif (matchlen > pathlen) {if (match[pathlen] !='/' )continue ; -if (!S_ISDIR (mode)) +if (!S_ISDIR (entry->mode))continue ; }
@@ -183,7 +178,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int* we cheated and did not do strncmp(), so we do * that here. */ - m =strncmp (match, path, pathlen); + m =strncmp (match, entry->path, pathlen);/* * If common part matched earlier then it is a hit,
@@ -206,8 +201,7 @@ static void show_tree(struct diff_options *opt, const char *prefix, struct tree_if (all_interesting) show =1 ;else { - show =tree_entry_interesting (desc, base, baselen, - opt); + show =tree_entry_interesting (&desc->entry, base, baselen, &opt->pathspec);if (show ==2 ) all_interesting =1 ; }
@@ -266,7 +260,7 @@ static void skip_uninteresting(struct tree_desc *t, const char *base, int baseleif (all_interesting) show =1 ;else { - show =tree_entry_interesting (t, base, baselen, opt); + show =tree_entry_interesting (&t->entry, base, baselen, &opt->pathspec);if (show ==2 ) all_interesting =1 ; }
Signature · 1 line
1.7.1.rc1.70.g788ca
PATCH v2 6/6 Move tree_entry_interesting() to tree-walk.c and export it
In reply to Nguyễn Thái Ngọc Duy--- tree-diff.c | 109 --------------------------------------------------------- tree-walk.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tree-walk.h | 2 + 3 files changed, 113 insertions(+), 109 deletions(-)
tree-diff.c +0 -109
@@ -82,115 +82,6 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, constreturn 0 ; } -/* -* Is a tree entry interesting given the pathspec we have? -* -* Return: -* - 2 for "yes, and all subsequent entries will be" -* - 1 for yes -* - zero for no -* - negative for "no, and no subsequent entries will be either" -*/ -static int tree_entry_interesting (const struct name_entry *entry,const char *base,int baselen,const struct pathspec *ps) -{ -int i; -int pathlen; -int never_interesting =-1 ; - -if (!ps || !ps->nr) -return 1 ; - - pathlen =tree_entry_len (entry->path, entry->sha1); - -for (i =0 ; i < ps->nr; i++) { -const char *match = ps->raw[i]; -int matchlen = ps->items[i].len; -int m =-1 ;/* signals that we haven't called strncmp() */ - -if (baselen >= matchlen) { -/* If it doesn't match, move along... */ -if (strncmp (base, match, matchlen)) -continue ; - -/* -* If the base is a subdirectory of a path which -* was specified, all of them are interesting. -*/ -if (!matchlen || - base[matchlen] =='/' || - match[matchlen -1 ] =='/' ) -return 2 ; - -/* Just a random prefix match */ -continue ; - } - -/* Does the base match? */ -if (strncmp (base, match, baselen)) -continue ; - - match += baselen; - matchlen -= baselen; - -if (never_interesting) { -/* -* We have not seen any match that sorts later -* than the current path. -*/ - -/* -* Does match sort strictly earlier than path -* with their common parts? -*/ - m =strncmp (match, entry->path, - (matchlen < pathlen) ? matchlen : pathlen); -if (m <0 ) -continue ; - -/* -* If we come here even once, that means there is at -* least one pathspec that would sort equal to or -* later than the path we are currently looking at. -* In other words, if we have never reached this point -* after iterating all pathspecs, it means all -* pathspecs are either outside of base, or inside the -* base but sorts strictly earlier than the current -* one. In either case, they will never match the -* subsequent entries. In such a case, we initialized -* the variable to -1 and that is what will be -* returned, allowing the caller to terminate early. -*/ - never_interesting =0 ; - } - -if (pathlen > matchlen) -continue ; - -if (matchlen > pathlen) { -if (match[pathlen] !='/' ) -continue ; -if (!S_ISDIR (entry->mode)) -continue ; - } - -if (m ==-1 ) -/* -* we cheated and did not do strncmp(), so we do -* that here. -*/ - m =strncmp (match, entry->path, pathlen); - -/* -* If common part matched earlier then it is a hit, -* because we rejected the case where path is not a -* leading directory and is shorter than match. -*/ -if (!m) -return 1 ; - } -return never_interesting;/* No matches */ -} -/* A whole sub-tree went away or appeared */ static void show_tree (struct diff_options *opt,const char *prefix,struct tree_desc *desc,const char *base,int baselen) {
tree-walk.c +111 -0
114 lines not on this page · raw message
tree-walk.h +2 -0
@@ -57,4 +57,6 @@ static inline int traverse_path_len(const struct traverse_info *info, const stru return info->pathlen + tree_entry_len(n->path, n->sha1); } +extern int tree_entry_interesting (const struct name_entry *,const char *,int ,const struct pathspec *ps); +#endif
Signature · 1 line
1.7.1.rc1.70.g788ca