pathspec.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef PATHSPEC_H
  2. #define PATHSPEC_H
  3. struct index_state;
  4. /* Pathspec magic */
  5. #define PATHSPEC_FROMTOP (1<<0)
  6. #define PATHSPEC_MAXDEPTH (1<<1)
  7. #define PATHSPEC_LITERAL (1<<2)
  8. #define PATHSPEC_GLOB (1<<3)
  9. #define PATHSPEC_ICASE (1<<4)
  10. #define PATHSPEC_EXCLUDE (1<<5)
  11. #define PATHSPEC_ATTR (1<<6)
  12. #define PATHSPEC_ALL_MAGIC \
  13. (PATHSPEC_FROMTOP | \
  14. PATHSPEC_MAXDEPTH | \
  15. PATHSPEC_LITERAL | \
  16. PATHSPEC_GLOB | \
  17. PATHSPEC_ICASE | \
  18. PATHSPEC_EXCLUDE | \
  19. PATHSPEC_ATTR)
  20. #define PATHSPEC_ONESTAR 1 /* the pathspec pattern satisfies GFNM_ONESTAR */
  21. /**
  22. * See glossary-context.txt for the syntax of pathspec.
  23. * In memory, a pathspec set is represented by "struct pathspec" and is
  24. * prepared by parse_pathspec().
  25. */
  26. struct pathspec {
  27. int nr;
  28. unsigned int has_wildcard:1;
  29. unsigned int recursive:1;
  30. unsigned int recurse_submodules:1;
  31. unsigned magic;
  32. int max_depth;
  33. struct pathspec_item {
  34. char *match;
  35. char *original;
  36. unsigned magic;
  37. int len, prefix;
  38. int nowildcard_len;
  39. int flags;
  40. int attr_match_nr;
  41. struct attr_match {
  42. char *value;
  43. enum attr_match_mode {
  44. MATCH_SET,
  45. MATCH_UNSET,
  46. MATCH_VALUE,
  47. MATCH_UNSPECIFIED
  48. } match_mode;
  49. } *attr_match;
  50. struct attr_check *attr_check;
  51. } *items;
  52. };
  53. #define GUARD_PATHSPEC(ps, mask) \
  54. do { \
  55. if ((ps)->magic & ~(mask)) \
  56. die("BUG:%s:%d: unsupported magic %x", \
  57. __FILE__, __LINE__, (ps)->magic & ~(mask)); \
  58. } while (0)
  59. /* parse_pathspec flags */
  60. #define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
  61. #define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
  62. #define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */
  63. /* die if a symlink is part of the given path's directory */
  64. #define PATHSPEC_SYMLINK_LEADING_PATH (1<<3)
  65. #define PATHSPEC_PREFIX_ORIGIN (1<<4)
  66. #define PATHSPEC_KEEP_ORDER (1<<5)
  67. /*
  68. * For the callers that just need pure paths from somewhere else, not
  69. * from command line. Global --*-pathspecs options are ignored. No
  70. * magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
  71. * allowed, then it will automatically set for every pathspec.
  72. */
  73. #define PATHSPEC_LITERAL_PATH (1<<6)
  74. /**
  75. * Given command line arguments and a prefix, convert the input to
  76. * pathspec. die() if any magic in magic_mask is used.
  77. *
  78. * Any arguments used are copied. It is safe for the caller to modify
  79. * or free 'prefix' and 'args' after calling this function.
  80. *
  81. * - magic_mask specifies what features that are NOT supported by the following
  82. * code. If a user attempts to use such a feature, parse_pathspec() can reject
  83. * it early.
  84. *
  85. * - flags specifies other things that the caller wants parse_pathspec to
  86. * perform.
  87. *
  88. * - prefix and args come from cmd_* functions
  89. *
  90. * parse_pathspec() helps catch unsupported features and reject them politely.
  91. * At a lower level, different pathspec-related functions may not support the
  92. * same set of features. Such pathspec-sensitive functions are guarded with
  93. * GUARD_PATHSPEC(), which will die in an unfriendly way when an unsupported
  94. * feature is requested.
  95. *
  96. * The command designers are supposed to make sure that GUARD_PATHSPEC() never
  97. * dies. They have to make sure all unsupported features are caught by
  98. * parse_pathspec(), not by GUARD_PATHSPEC. grepping GUARD_PATHSPEC() should
  99. * give the designers all pathspec-sensitive codepaths and what features they
  100. * support.
  101. *
  102. * A similar process is applied when a new pathspec magic is added. The designer
  103. * lifts the GUARD_PATHSPEC restriction in the functions that support the new
  104. * magic. At the same time (s)he has to make sure this new feature will be
  105. * caught at parse_pathspec() in commands that cannot handle the new magic in
  106. * some cases. grepping parse_pathspec() should help.
  107. */
  108. void parse_pathspec(struct pathspec *pathspec,
  109. unsigned magic_mask,
  110. unsigned flags,
  111. const char *prefix,
  112. const char **args);
  113. /*
  114. * Same as parse_pathspec() but uses file as input.
  115. * When 'file' is exactly "-" it uses 'stdin' instead.
  116. */
  117. void parse_pathspec_file(struct pathspec *pathspec,
  118. unsigned magic_mask,
  119. unsigned flags,
  120. const char *prefix,
  121. const char *file,
  122. int nul_term_line);
  123. void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
  124. void clear_pathspec(struct pathspec *);
  125. static inline int ps_strncmp(const struct pathspec_item *item,
  126. const char *s1, const char *s2, size_t n)
  127. {
  128. if (item->magic & PATHSPEC_ICASE)
  129. return strncasecmp(s1, s2, n);
  130. else
  131. return strncmp(s1, s2, n);
  132. }
  133. static inline int ps_strcmp(const struct pathspec_item *item,
  134. const char *s1, const char *s2)
  135. {
  136. if (item->magic & PATHSPEC_ICASE)
  137. return strcasecmp(s1, s2);
  138. else
  139. return strcmp(s1, s2);
  140. }
  141. void add_pathspec_matches_against_index(const struct pathspec *pathspec,
  142. const struct index_state *istate,
  143. char *seen);
  144. char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
  145. const struct index_state *istate);
  146. int match_pathspec_attrs(const struct index_state *istate,
  147. const char *name, int namelen,
  148. const struct pathspec_item *item);
  149. #endif /* PATHSPEC_H */