diffcore.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2005 Junio C Hamano
  3. */
  4. #ifndef DIFFCORE_H
  5. #define DIFFCORE_H
  6. #include "cache.h"
  7. struct diff_options;
  8. struct repository;
  9. struct userdiff_driver;
  10. /* This header file is internal between diff.c and its diff transformers
  11. * (e.g. diffcore-rename, diffcore-pickaxe). Never include this header
  12. * in anything else.
  13. */
  14. /* We internally use unsigned short as the score value,
  15. * and rely on an int capable to hold 32-bits. -B can take
  16. * -Bmerge_score/break_score format and the two scores are
  17. * passed around in one int (high 16-bit for merge and low 16-bit
  18. * for break).
  19. */
  20. #define MAX_SCORE 60000.0
  21. #define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */
  22. #define DEFAULT_BREAK_SCORE 30000 /* minimum for break to happen (50%) */
  23. #define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen (60%) */
  24. #define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
  25. /**
  26. * the internal representation for a single file (blob). It records the blob
  27. * object name (if known -- for a work tree file it typically is a NUL SHA-1),
  28. * filemode and pathname. This is what the `diff_addremove()`, `diff_change()`
  29. * and `diff_unmerge()` synthesize and feed `diff_queue()` function with.
  30. */
  31. struct diff_filespec {
  32. struct object_id oid;
  33. char *path;
  34. void *data;
  35. void *cnt_data;
  36. unsigned long size;
  37. int count; /* Reference count */
  38. int rename_used; /* Count of rename users */
  39. unsigned short mode; /* file mode */
  40. unsigned oid_valid : 1; /* if true, use oid and trust mode;
  41. * if false, use the name and read from
  42. * the filesystem.
  43. */
  44. #define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
  45. unsigned should_free : 1; /* data should be free()'ed */
  46. unsigned should_munmap : 1; /* data should be munmap()'ed */
  47. unsigned dirty_submodule : 2; /* For submodules: its work tree is dirty */
  48. #define DIRTY_SUBMODULE_UNTRACKED 1
  49. #define DIRTY_SUBMODULE_MODIFIED 2
  50. unsigned is_stdin : 1;
  51. unsigned has_more_entries : 1; /* only appear in combined diff */
  52. /* data should be considered "binary"; -1 means "don't know yet" */
  53. signed int is_binary : 2;
  54. struct userdiff_driver *driver;
  55. };
  56. struct diff_filespec *alloc_filespec(const char *);
  57. void free_filespec(struct diff_filespec *);
  58. void fill_filespec(struct diff_filespec *, const struct object_id *,
  59. int, unsigned short);
  60. /*
  61. * Prefetch the entries in diff_queued_diff. The parameter is a pointer to a
  62. * struct repository.
  63. */
  64. void diff_queued_diff_prefetch(void *repository);
  65. struct diff_populate_filespec_options {
  66. unsigned check_size_only : 1;
  67. unsigned check_binary : 1;
  68. /*
  69. * If an object is missing, diff_populate_filespec() will invoke this
  70. * callback before attempting to read that object again.
  71. */
  72. void (*missing_object_cb)(void *);
  73. void *missing_object_data;
  74. };
  75. int diff_populate_filespec(struct repository *, struct diff_filespec *,
  76. const struct diff_populate_filespec_options *);
  77. void diff_free_filespec_data(struct diff_filespec *);
  78. void diff_free_filespec_blob(struct diff_filespec *);
  79. int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
  80. /**
  81. * This records a pair of `struct diff_filespec`; the filespec for a file in
  82. * the "old" set (i.e. preimage) is called `one`, and the filespec for a file
  83. * in the "new" set (i.e. postimage) is called `two`. A change that represents
  84. * file creation has NULL in `one`, and file deletion has NULL in `two`.
  85. *
  86. * A `filepair` starts pointing at `one` and `two` that are from the same
  87. * filename, but `diffcore_std()` can break pairs and match component filespecs
  88. * with other filespecs from a different filepair to form new filepair. This is
  89. * called 'rename detection'.
  90. */
  91. struct diff_filepair {
  92. struct diff_filespec *one;
  93. struct diff_filespec *two;
  94. unsigned short int score;
  95. char status; /* M C R A D U etc. (see Documentation/diff-format.txt or DIFF_STATUS_* in diff.h) */
  96. unsigned broken_pair : 1;
  97. unsigned renamed_pair : 1;
  98. unsigned is_unmerged : 1;
  99. unsigned done_skip_stat_unmatch : 1;
  100. unsigned skip_stat_unmatch_result : 1;
  101. };
  102. #define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
  103. #define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
  104. #define DIFF_PAIR_BROKEN(p) \
  105. ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \
  106. ((p)->broken_pair != 0) )
  107. #define DIFF_PAIR_TYPE_CHANGED(p) \
  108. ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode))
  109. #define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
  110. void diff_free_filepair(struct diff_filepair *);
  111. int diff_unmodified_pair(struct diff_filepair *);
  112. /**
  113. * This is a collection of filepairs. Notable members are:
  114. *
  115. * - `queue`:
  116. * An array of pointers to `struct diff_filepair`. This dynamically grows as
  117. * you add filepairs;
  118. *
  119. * - `alloc`:
  120. * The allocated size of the `queue` array;
  121. *
  122. * - `nr`:
  123. * The number of elements in the `queue` array.
  124. */
  125. struct diff_queue_struct {
  126. struct diff_filepair **queue;
  127. int alloc;
  128. int nr;
  129. };
  130. #define DIFF_QUEUE_CLEAR(q) \
  131. do { \
  132. (q)->queue = NULL; \
  133. (q)->nr = (q)->alloc = 0; \
  134. } while (0)
  135. extern struct diff_queue_struct diff_queued_diff;
  136. struct diff_filepair *diff_queue(struct diff_queue_struct *,
  137. struct diff_filespec *,
  138. struct diff_filespec *);
  139. void diff_q(struct diff_queue_struct *, struct diff_filepair *);
  140. void diffcore_break(struct repository *, int);
  141. void diffcore_rename(struct diff_options *);
  142. void diffcore_merge_broken(void);
  143. void diffcore_pickaxe(struct diff_options *);
  144. void diffcore_order(const char *orderfile);
  145. /* low-level interface to diffcore_order */
  146. struct obj_order {
  147. void *obj; /* setup by caller */
  148. /* setup/used by order_objects() */
  149. int orig_order;
  150. int order;
  151. };
  152. typedef const char *(*obj_path_fn_t)(void *obj);
  153. void order_objects(const char *orderfile, obj_path_fn_t obj_path,
  154. struct obj_order *objs, int nr);
  155. #define DIFF_DEBUG 0
  156. #if DIFF_DEBUG
  157. void diff_debug_filespec(struct diff_filespec *, int, const char *);
  158. void diff_debug_filepair(const struct diff_filepair *, int);
  159. void diff_debug_queue(const char *, struct diff_queue_struct *);
  160. #else
  161. #define diff_debug_filespec(a,b,c) do { /* nothing */ } while (0)
  162. #define diff_debug_filepair(a,b) do { /* nothing */ } while (0)
  163. #define diff_debug_queue(a,b) do { /* nothing */ } while (0)
  164. #endif
  165. int diffcore_count_changes(struct repository *r,
  166. struct diff_filespec *src,
  167. struct diff_filespec *dst,
  168. void **src_count_p,
  169. void **dst_count_p,
  170. unsigned long *src_copied,
  171. unsigned long *literal_added);
  172. /*
  173. * If filespec contains an OID and if that object is missing from the given
  174. * repository, add that OID to to_fetch.
  175. */
  176. void diff_add_if_missing(struct repository *r,
  177. struct oid_array *to_fetch,
  178. const struct diff_filespec *filespec);
  179. #endif