ref-filter.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef REF_FILTER_H
  2. #define REF_FILTER_H
  3. #include "oid-array.h"
  4. #include "refs.h"
  5. #include "commit.h"
  6. #include "parse-options.h"
  7. /* Quoting styles */
  8. #define QUOTE_NONE 0
  9. #define QUOTE_SHELL 1
  10. #define QUOTE_PERL 2
  11. #define QUOTE_PYTHON 4
  12. #define QUOTE_TCL 8
  13. #define FILTER_REFS_INCLUDE_BROKEN 0x0001
  14. #define FILTER_REFS_TAGS 0x0002
  15. #define FILTER_REFS_BRANCHES 0x0004
  16. #define FILTER_REFS_REMOTES 0x0008
  17. #define FILTER_REFS_OTHERS 0x0010
  18. #define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
  19. FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
  20. #define FILTER_REFS_DETACHED_HEAD 0x0020
  21. #define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
  22. struct atom_value;
  23. struct ref_sorting {
  24. struct ref_sorting *next;
  25. int atom; /* index into used_atom array (internal) */
  26. unsigned reverse : 1,
  27. ignore_case : 1,
  28. version : 1;
  29. };
  30. struct ref_array_item {
  31. struct object_id objectname;
  32. int flag;
  33. unsigned int kind;
  34. const char *symref;
  35. struct commit *commit;
  36. struct atom_value *value;
  37. char refname[FLEX_ARRAY];
  38. };
  39. struct ref_array {
  40. int nr, alloc;
  41. struct ref_array_item **items;
  42. struct rev_info *revs;
  43. };
  44. struct ref_filter {
  45. const char **name_patterns;
  46. struct oid_array points_at;
  47. struct commit_list *with_commit;
  48. struct commit_list *no_commit;
  49. struct commit_list *reachable_from;
  50. struct commit_list *unreachable_from;
  51. unsigned int with_commit_tag_algo : 1,
  52. match_as_path : 1,
  53. ignore_case : 1,
  54. detached : 1;
  55. unsigned int kind,
  56. lines;
  57. int abbrev,
  58. verbose;
  59. };
  60. struct ref_format {
  61. /*
  62. * Set these to define the format; make sure you call
  63. * verify_ref_format() afterwards to finalize.
  64. */
  65. const char *format;
  66. int quote_style;
  67. int use_color;
  68. /* Internal state to ref-filter */
  69. int need_color_reset_at_eol;
  70. };
  71. #define REF_FORMAT_INIT { NULL, 0, -1 }
  72. /* Macros for checking --merged and --no-merged options */
  73. #define _OPT_MERGED_NO_MERGED(option, filter, h) \
  74. { OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
  75. PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
  76. parse_opt_merge_filter, (intptr_t) "HEAD" \
  77. }
  78. #define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
  79. #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
  80. #define OPT_REF_SORT(var) \
  81. OPT_CALLBACK_F(0, "sort", (var), \
  82. N_("key"), N_("field name to sort on"), \
  83. PARSE_OPT_NONEG, parse_opt_ref_sorting)
  84. /*
  85. * API for filtering a set of refs. Based on the type of refs the user
  86. * has requested, we iterate through those refs and apply filters
  87. * as per the given ref_filter structure and finally store the
  88. * filtered refs in the ref_array structure.
  89. */
  90. int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
  91. /* Clear all memory allocated to ref_array */
  92. void ref_array_clear(struct ref_array *array);
  93. /* Used to verify if the given format is correct and to parse out the used atoms */
  94. int verify_ref_format(struct ref_format *format);
  95. /* Sort the given ref_array as per the ref_sorting provided */
  96. void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
  97. /* Set the ignore_case flag for all elements of a sorting list */
  98. void ref_sorting_icase_all(struct ref_sorting *sorting, int flag);
  99. /* Based on the given format and quote_style, fill the strbuf */
  100. int format_ref_array_item(struct ref_array_item *info,
  101. const struct ref_format *format,
  102. struct strbuf *final_buf,
  103. struct strbuf *error_buf);
  104. /* Print the ref using the given format and quote_style */
  105. void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
  106. /* Parse a single sort specifier and add it to the list */
  107. void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
  108. /* Callback function for parsing the sort option */
  109. int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
  110. /* Default sort option based on refname */
  111. struct ref_sorting *ref_default_sorting(void);
  112. /* Function to parse --merged and --no-merged options */
  113. int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
  114. /* Get the current HEAD's description */
  115. char *get_head_description(void);
  116. /* Set up translated strings in the output. */
  117. void setup_ref_filter_porcelain_msg(void);
  118. /*
  119. * Print a single ref, outside of any ref-filter. Note that the
  120. * name must be a fully qualified refname.
  121. */
  122. void pretty_print_ref(const char *name, const struct object_id *oid,
  123. const struct ref_format *format);
  124. /*
  125. * Push a single ref onto the array; this can be used to construct your own
  126. * ref_array without using filter_refs().
  127. */
  128. struct ref_array_item *ref_array_push(struct ref_array *array,
  129. const char *refname,
  130. const struct object_id *oid);
  131. #endif /* REF_FILTER_H */