1#include"cache.h" 2#include"dir.h" 3#include"pathspec.h" 4 5/* 6 * Finds which of the given pathspecs match items in the index. 7 * 8 * For each pathspec, sets the corresponding entry in the seen[] array 9 * (which should be specs items long, i.e. the same size as pathspec) 10 * to the nature of the "closest" (i.e. most specific) match found for 11 * that pathspec in the index, if it was a closer type of match than 12 * the existing entry. As an optimization, matching is skipped 13 * altogether if seen[] already only contains non-zero entries. 14 * 15 * If seen[] has not already been written to, it may make sense 16 * to use find_pathspecs_matching_against_index() instead. 17 */ 18voidadd_pathspec_matches_against_index(const char**pathspec, 19char*seen,int specs) 20{ 21int num_unmatched =0, i; 22 23/* 24 * Since we are walking the index as if we were walking the directory, 25 * we have to mark the matched pathspec as seen; otherwise we will 26 * mistakenly think that the user gave a pathspec that did not match 27 * anything. 28 */ 29for(i =0; i < specs; i++) 30if(!seen[i]) 31 num_unmatched++; 32if(!num_unmatched) 33return; 34for(i =0; i < active_nr; i++) { 35struct cache_entry *ce = active_cache[i]; 36match_pathspec(pathspec, ce->name,ce_namelen(ce),0, seen); 37} 38} 39 40/* 41 * Finds which of the given pathspecs match items in the index. 42 * 43 * This is a one-shot wrapper around add_pathspec_matches_against_index() 44 * which allocates, populates, and returns a seen[] array indicating the 45 * nature of the "closest" (i.e. most specific) matches which each of the 46 * given pathspecs achieves against all items in the index. 47 */ 48char*find_pathspecs_matching_against_index(const char**pathspec) 49{ 50char*seen; 51int i; 52 53for(i =0; pathspec[i]; i++) 54;/* just counting */ 55 seen =xcalloc(i,1); 56add_pathspec_matches_against_index(pathspec, seen, i); 57return seen; 58} 59 60/* 61 * Check the index to see whether path refers to a submodule, or 62 * something inside a submodule. If the former, returns the path with 63 * any trailing slash stripped. If the latter, dies with an error 64 * message. 65 */ 66const char*check_path_for_gitlink(const char*path) 67{ 68int i, path_len =strlen(path); 69for(i =0; i < active_nr; i++) { 70struct cache_entry *ce = active_cache[i]; 71if(S_ISGITLINK(ce->ce_mode)) { 72int ce_len =ce_namelen(ce); 73if(path_len <= ce_len || path[ce_len] !='/'|| 74memcmp(ce->name, path, ce_len)) 75/* path does not refer to this 76 * submodule or anything inside it */ 77continue; 78if(path_len == ce_len +1) { 79/* path refers to submodule; 80 * strip trailing slash */ 81returnxstrndup(ce->name, ce_len); 82}else{ 83die(_("Path '%s' is in submodule '%.*s'"), 84 path, ce_len, ce->name); 85} 86} 87} 88return path; 89} 90 91/* 92 * Dies if the given path refers to a file inside a symlinked 93 * directory in the index. 94 */ 95voiddie_if_path_beyond_symlink(const char*path,const char*prefix) 96{ 97if(has_symlink_leading_path(path,strlen(path))) { 98int len = prefix ?strlen(prefix) :0; 99die(_("'%s' is beyond a symbolic link"), path + len); 100} 101} 102 103/* 104 * Magic pathspec 105 * 106 * NEEDSWORK: These need to be moved to dir.h or even to a new 107 * pathspec.h when we restructure get_pathspec() users to use the 108 * "struct pathspec" interface. 109 * 110 * Possible future magic semantics include stuff like: 111 * 112 * { PATHSPEC_NOGLOB, '!', "noglob" }, 113 * { PATHSPEC_ICASE, '\0', "icase" }, 114 * { PATHSPEC_RECURSIVE, '*', "recursive" }, 115 * { PATHSPEC_REGEXP, '\0', "regexp" }, 116 * 117 */ 118#define PATHSPEC_FROMTOP (1<<0) 119 120static struct pathspec_magic { 121unsigned bit; 122char mnemonic;/* this cannot be ':'! */ 123const char*name; 124} pathspec_magic[] = { 125{ PATHSPEC_FROMTOP,'/',"top"}, 126}; 127 128/* 129 * Take an element of a pathspec and check for magic signatures. 130 * Append the result to the prefix. 131 * 132 * For now, we only parse the syntax and throw out anything other than 133 * "top" magic. 134 * 135 * NEEDSWORK: This needs to be rewritten when we start migrating 136 * get_pathspec() users to use the "struct pathspec" interface. For 137 * example, a pathspec element may be marked as case-insensitive, but 138 * the prefix part must always match literally, and a single stupid 139 * string cannot express such a case. 140 */ 141static const char*prefix_pathspec(const char*prefix,int prefixlen,const char*elt) 142{ 143unsigned magic =0; 144const char*copyfrom = elt; 145int i; 146 147if(elt[0] !=':') { 148;/* nothing to do */ 149}else if(elt[1] =='(') { 150/* longhand */ 151const char*nextat; 152for(copyfrom = elt +2; 153*copyfrom && *copyfrom !=')'; 154 copyfrom = nextat) { 155size_t len =strcspn(copyfrom,",)"); 156if(copyfrom[len] ==',') 157 nextat = copyfrom + len +1; 158else 159/* handle ')' and '\0' */ 160 nextat = copyfrom + len; 161if(!len) 162continue; 163for(i =0; i <ARRAY_SIZE(pathspec_magic); i++) 164if(strlen(pathspec_magic[i].name) == len && 165!strncmp(pathspec_magic[i].name, copyfrom, len)) { 166 magic |= pathspec_magic[i].bit; 167break; 168} 169if(ARRAY_SIZE(pathspec_magic) <= i) 170die("Invalid pathspec magic '%.*s' in '%s'", 171(int) len, copyfrom, elt); 172} 173if(*copyfrom !=')') 174die("Missing ')' at the end of pathspec magic in '%s'", elt); 175 copyfrom++; 176}else{ 177/* shorthand */ 178for(copyfrom = elt +1; 179*copyfrom && *copyfrom !=':'; 180 copyfrom++) { 181char ch = *copyfrom; 182 183if(!is_pathspec_magic(ch)) 184break; 185for(i =0; i <ARRAY_SIZE(pathspec_magic); i++) 186if(pathspec_magic[i].mnemonic == ch) { 187 magic |= pathspec_magic[i].bit; 188break; 189} 190if(ARRAY_SIZE(pathspec_magic) <= i) 191die("Unimplemented pathspec magic '%c' in '%s'", 192 ch, elt); 193} 194if(*copyfrom ==':') 195 copyfrom++; 196} 197 198if(magic & PATHSPEC_FROMTOP) 199returnxstrdup(copyfrom); 200else 201returnprefix_path(prefix, prefixlen, copyfrom); 202} 203 204/* 205 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec" 206 * based interface - see pathspec_magic above. 207 * 208 * Arguments: 209 * - prefix - a path relative to the root of the working tree 210 * - pathspec - a list of paths underneath the prefix path 211 * 212 * Iterates over pathspec, prepending each path with prefix, 213 * and return the resulting list. 214 * 215 * If pathspec is empty, return a singleton list containing prefix. 216 * 217 * If pathspec and prefix are both empty, return an empty list. 218 * 219 * This is typically used by built-in commands such as add.c, in order 220 * to normalize argv arguments provided to the built-in into a list of 221 * paths to process, all relative to the root of the working tree. 222 */ 223const char**get_pathspec(const char*prefix,const char**pathspec) 224{ 225const char*entry = *pathspec; 226const char**src, **dst; 227int prefixlen; 228 229if(!prefix && !entry) 230return NULL; 231 232if(!entry) { 233static const char*spec[2]; 234 spec[0] = prefix; 235 spec[1] = NULL; 236return spec; 237} 238 239/* Otherwise we have to re-write the entries.. */ 240 src = pathspec; 241 dst = pathspec; 242 prefixlen = prefix ?strlen(prefix) :0; 243while(*src) { 244*(dst++) =prefix_pathspec(prefix, prefixlen, *src); 245 src++; 246} 247*dst = NULL; 248if(!*pathspec) 249return NULL; 250return pathspec; 251}