trailer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef TRAILER_H
  2. #define TRAILER_H
  3. #include "list.h"
  4. #include "strbuf.h"
  5. enum trailer_where {
  6. WHERE_DEFAULT,
  7. WHERE_END,
  8. WHERE_AFTER,
  9. WHERE_BEFORE,
  10. WHERE_START
  11. };
  12. enum trailer_if_exists {
  13. EXISTS_DEFAULT,
  14. EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
  15. EXISTS_ADD_IF_DIFFERENT,
  16. EXISTS_ADD,
  17. EXISTS_REPLACE,
  18. EXISTS_DO_NOTHING
  19. };
  20. enum trailer_if_missing {
  21. MISSING_DEFAULT,
  22. MISSING_ADD,
  23. MISSING_DO_NOTHING
  24. };
  25. int trailer_set_where(enum trailer_where *item, const char *value);
  26. int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
  27. int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
  28. struct trailer_info {
  29. /*
  30. * True if there is a blank line before the location pointed to by
  31. * trailer_start.
  32. */
  33. int blank_line_before_trailer;
  34. /*
  35. * Pointers to the start and end of the trailer block found. If there
  36. * is no trailer block found, these 2 pointers point to the end of the
  37. * input string.
  38. */
  39. const char *trailer_start, *trailer_end;
  40. /*
  41. * Array of trailers found.
  42. */
  43. char **trailers;
  44. size_t trailer_nr;
  45. };
  46. /*
  47. * A list that represents newly-added trailers, such as those provided
  48. * with the --trailer command line option of git-interpret-trailers.
  49. */
  50. struct new_trailer_item {
  51. struct list_head list;
  52. const char *text;
  53. enum trailer_where where;
  54. enum trailer_if_exists if_exists;
  55. enum trailer_if_missing if_missing;
  56. };
  57. struct process_trailer_options {
  58. int in_place;
  59. int trim_empty;
  60. int only_trailers;
  61. int only_input;
  62. int unfold;
  63. int no_divider;
  64. int value_only;
  65. const struct strbuf *separator;
  66. int (*filter)(const struct strbuf *, void *);
  67. void *filter_data;
  68. };
  69. #define PROCESS_TRAILER_OPTIONS_INIT {0}
  70. void process_trailers(const char *file,
  71. const struct process_trailer_options *opts,
  72. struct list_head *new_trailer_head);
  73. void trailer_info_get(struct trailer_info *info, const char *str,
  74. const struct process_trailer_options *opts);
  75. void trailer_info_release(struct trailer_info *info);
  76. /*
  77. * Format the trailers from the commit msg "msg" into the strbuf "out".
  78. * Note two caveats about "opts":
  79. *
  80. * - this is primarily a helper for pretty.c, and not
  81. * all of the flags are supported.
  82. *
  83. * - this differs from process_trailers slightly in that we always format
  84. * only the trailer block itself, even if the "only_trailers" option is not
  85. * set.
  86. */
  87. void format_trailers_from_commit(struct strbuf *out, const char *msg,
  88. const struct process_trailer_options *opts);
  89. /*
  90. * An interface for iterating over the trailers found in a particular commit
  91. * message. Use like:
  92. *
  93. * struct trailer_iterator iter;
  94. * trailer_iterator_init(&iter, msg);
  95. * while (trailer_iterator_advance(&iter))
  96. * ... do something with iter.key and iter.val ...
  97. * trailer_iterator_release(&iter);
  98. */
  99. struct trailer_iterator {
  100. struct strbuf key;
  101. struct strbuf val;
  102. /* private */
  103. struct trailer_info info;
  104. size_t cur;
  105. };
  106. /*
  107. * Initialize "iter" in preparation for walking over the trailers in the commit
  108. * message "msg". The "msg" pointer must remain valid until the iterator is
  109. * released.
  110. *
  111. * After initializing, note that key/val will not yet point to any trailer.
  112. * Call advance() to parse the first one (if any).
  113. */
  114. void trailer_iterator_init(struct trailer_iterator *iter, const char *msg);
  115. /*
  116. * Advance to the next trailer of the iterator. Returns 0 if there is no such
  117. * trailer, and 1 otherwise. The key and value of the trailer can be
  118. * fetched from the iter->key and iter->value fields (which are valid
  119. * only until the next advance).
  120. */
  121. int trailer_iterator_advance(struct trailer_iterator *iter);
  122. /*
  123. * Release all resources associated with the trailer iteration.
  124. */
  125. void trailer_iterator_release(struct trailer_iterator *iter);
  126. #endif /* TRAILER_H */