Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Junio C Hamano | e0173ad | 2007-04-28 23:38:52 -0700 | [diff] [blame] | 4 | #ifndef DIFFCORE_H |
| 5 | #define DIFFCORE_H |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 6 | |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 7 | #include "cache.h" |
| 8 | |
| 9 | struct diff_options; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 10 | struct repository; |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 11 | struct strintmap; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 12 | struct strmap; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 13 | struct userdiff_driver; |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 14 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 15 | /* This header file is internal between diff.c and its diff transformers |
| 16 | * (e.g. diffcore-rename, diffcore-pickaxe). Never include this header |
| 17 | * in anything else. |
| 18 | */ |
Junio C Hamano | eeaa460 | 2005-06-03 01:40:28 -0700 | [diff] [blame] | 19 | |
| 20 | /* We internally use unsigned short as the score value, |
| 21 | * and rely on an int capable to hold 32-bits. -B can take |
| 22 | * -Bmerge_score/break_score format and the two scores are |
| 23 | * passed around in one int (high 16-bit for merge and low 16-bit |
| 24 | * for break). |
| 25 | */ |
Junio C Hamano | ee3d299 | 2006-01-15 21:08:42 -0800 | [diff] [blame] | 26 | #define MAX_SCORE 60000.0 |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 27 | #define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */ |
Junio C Hamano | 4d0f39c | 2006-03-04 01:03:53 -0800 | [diff] [blame] | 28 | #define DEFAULT_BREAK_SCORE 30000 /* minimum for break to happen (50%) */ |
Matthieu Moy | cf958af | 2010-08-05 18:14:25 +0200 | [diff] [blame] | 29 | #define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen (60%) */ |
Junio C Hamano | eeaa460 | 2005-06-03 01:40:28 -0700 | [diff] [blame] | 30 | |
| 31 | #define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 32 | |
Heba Waly | 13c4d7e | 2019-11-17 21:04:40 +0000 | [diff] [blame] | 33 | /** |
| 34 | * the internal representation for a single file (blob). It records the blob |
| 35 | * object name (if known -- for a work tree file it typically is a NUL SHA-1), |
| 36 | * filemode and pathname. This is what the `diff_addremove()`, `diff_change()` |
| 37 | * and `diff_unmerge()` synthesize and feed `diff_queue()` function with. |
| 38 | */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 39 | struct diff_filespec { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 40 | struct object_id oid; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 41 | char *path; |
| 42 | void *data; |
Junio C Hamano | c06c796 | 2006-03-12 03:22:10 -0800 | [diff] [blame] | 43 | void *cnt_data; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 44 | unsigned long size; |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 45 | int count; /* Reference count */ |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 46 | int rename_used; /* Count of rename users */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 47 | unsigned short mode; /* file mode */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 48 | unsigned oid_valid : 1; /* if true, use oid and trust mode; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 49 | * if false, use the name and read from |
| 50 | * the filesystem. |
| 51 | */ |
Junio C Hamano | dc7090e | 2005-06-12 17:23:15 -0700 | [diff] [blame] | 52 | #define DIFF_FILE_VALID(spec) (((spec)->mode) != 0) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 53 | unsigned should_free : 1; /* data should be free()'ed */ |
| 54 | unsigned should_munmap : 1; /* data should be munmap()'ed */ |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 55 | unsigned dirty_submodule : 2; /* For submodules: its work tree is dirty */ |
| 56 | #define DIRTY_SUBMODULE_UNTRACKED 1 |
| 57 | #define DIRTY_SUBMODULE_MODIFIED 2 |
Jeff King | b837f5d | 2014-01-16 20:19:46 -0500 | [diff] [blame] | 58 | unsigned is_stdin : 1; |
Junio C Hamano | 25e5e2b | 2011-08-19 23:32:51 -0700 | [diff] [blame] | 59 | unsigned has_more_entries : 1; /* only appear in combined diff */ |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 60 | /* data should be considered "binary"; -1 means "don't know yet" */ |
Richard Lowe | 7d0a9a7 | 2014-02-23 19:54:47 -0500 | [diff] [blame] | 61 | signed int is_binary : 2; |
Jeff King | b38f70a | 2014-01-16 20:22:56 -0500 | [diff] [blame] | 62 | struct userdiff_driver *driver; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 65 | struct diff_filespec *alloc_filespec(const char *); |
| 66 | void free_filespec(struct diff_filespec *); |
| 67 | void fill_filespec(struct diff_filespec *, const struct object_id *, |
| 68 | int, unsigned short); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 69 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Prefetch the entries in diff_queued_diff. The parameter is a pointer to a |
| 72 | * struct repository. |
| 73 | */ |
| 74 | void diff_queued_diff_prefetch(void *repository); |
| 75 | |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 76 | struct diff_populate_filespec_options { |
| 77 | unsigned check_size_only : 1; |
| 78 | unsigned check_binary : 1; |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * If an object is missing, diff_populate_filespec() will invoke this |
| 82 | * callback before attempting to read that object again. |
| 83 | */ |
| 84 | void (*missing_object_cb)(void *); |
| 85 | void *missing_object_data; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 86 | }; |
| 87 | int diff_populate_filespec(struct repository *, struct diff_filespec *, |
| 88 | const struct diff_populate_filespec_options *); |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 89 | void diff_free_filespec_data(struct diff_filespec *); |
| 90 | void diff_free_filespec_blob(struct diff_filespec *); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 91 | int diff_filespec_is_binary(struct repository *, struct diff_filespec *); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 92 | |
Heba Waly | 13c4d7e | 2019-11-17 21:04:40 +0000 | [diff] [blame] | 93 | /** |
| 94 | * This records a pair of `struct diff_filespec`; the filespec for a file in |
| 95 | * the "old" set (i.e. preimage) is called `one`, and the filespec for a file |
| 96 | * in the "new" set (i.e. postimage) is called `two`. A change that represents |
| 97 | * file creation has NULL in `one`, and file deletion has NULL in `two`. |
| 98 | * |
| 99 | * A `filepair` starts pointing at `one` and `two` that are from the same |
| 100 | * filename, but `diffcore_std()` can break pairs and match component filespecs |
| 101 | * with other filespecs from a different filepair to form new filepair. This is |
| 102 | * called 'rename detection'. |
| 103 | */ |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 104 | struct diff_filepair { |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 105 | struct diff_filespec *one; |
| 106 | struct diff_filespec *two; |
Junio C Hamano | 01c4e70 | 2005-05-29 16:56:48 -0700 | [diff] [blame] | 107 | unsigned short int score; |
Yann Dirson | a5a323f | 2008-11-02 14:37:28 +0100 | [diff] [blame] | 108 | char status; /* M C R A D U etc. (see Documentation/diff-format.txt or DIFF_STATUS_* in diff.h) */ |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 109 | unsigned broken_pair : 1; |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 110 | unsigned renamed_pair : 1; |
Junio C Hamano | e9c8409 | 2007-01-05 01:25:18 -0800 | [diff] [blame] | 111 | unsigned is_unmerged : 1; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 112 | unsigned done_skip_stat_unmatch : 1; |
| 113 | unsigned skip_stat_unmatch_result : 1; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 114 | }; |
Heba Waly | 13c4d7e | 2019-11-17 21:04:40 +0000 | [diff] [blame] | 115 | |
Junio C Hamano | e9c8409 | 2007-01-05 01:25:18 -0800 | [diff] [blame] | 116 | #define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 117 | |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 118 | #define DIFF_PAIR_RENAME(p) ((p)->renamed_pair) |
Junio C Hamano | 01c4e70 | 2005-05-29 16:56:48 -0700 | [diff] [blame] | 119 | |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 120 | #define DIFF_PAIR_BROKEN(p) \ |
| 121 | ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \ |
| 122 | ((p)->broken_pair != 0) ) |
| 123 | |
Junio C Hamano | 96716a1 | 2005-05-25 15:07:08 -0700 | [diff] [blame] | 124 | #define DIFF_PAIR_TYPE_CHANGED(p) \ |
| 125 | ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode)) |
| 126 | |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 127 | #define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode) |
| 128 | |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 129 | void diff_free_filepair(struct diff_filepair *); |
Elijah Newren | a8791ef | 2021-07-30 11:47:41 +0000 | [diff] [blame] | 130 | void pool_diff_free_filepair(struct mem_pool *pool, |
| 131 | struct diff_filepair *p); |
Junio C Hamano | 226406f | 2005-05-27 15:50:30 -0700 | [diff] [blame] | 132 | |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 133 | int diff_unmodified_pair(struct diff_filepair *); |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 134 | |
Heba Waly | 13c4d7e | 2019-11-17 21:04:40 +0000 | [diff] [blame] | 135 | /** |
| 136 | * This is a collection of filepairs. Notable members are: |
| 137 | * |
| 138 | * - `queue`: |
| 139 | * An array of pointers to `struct diff_filepair`. This dynamically grows as |
| 140 | * you add filepairs; |
| 141 | * |
| 142 | * - `alloc`: |
| 143 | * The allocated size of the `queue` array; |
| 144 | * |
| 145 | * - `nr`: |
| 146 | * The number of elements in the `queue` array. |
| 147 | */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 148 | struct diff_queue_struct { |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 149 | struct diff_filepair **queue; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 150 | int alloc; |
| 151 | int nr; |
| 152 | }; |
Heba Waly | 13c4d7e | 2019-11-17 21:04:40 +0000 | [diff] [blame] | 153 | |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 154 | #define DIFF_QUEUE_CLEAR(q) \ |
| 155 | do { \ |
| 156 | (q)->queue = NULL; \ |
| 157 | (q)->nr = (q)->alloc = 0; \ |
Jonathan Nieder | 9874606 | 2010-08-12 17:11:15 -0500 | [diff] [blame] | 158 | } while (0) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 159 | |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 160 | extern struct diff_queue_struct diff_queued_diff; |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 161 | struct diff_filepair *diff_queue(struct diff_queue_struct *, |
| 162 | struct diff_filespec *, |
| 163 | struct diff_filespec *); |
| 164 | void diff_q(struct diff_queue_struct *, struct diff_filepair *); |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 165 | |
Elijah Newren | fb52938 | 2021-03-13 22:22:03 +0000 | [diff] [blame] | 166 | /* dir_rename_relevance: the reason we want rename information for a dir */ |
| 167 | enum dir_rename_relevance { |
| 168 | NOT_RELEVANT = 0, |
| 169 | RELEVANT_FOR_ANCESTOR = 1, |
| 170 | RELEVANT_FOR_SELF = 2 |
| 171 | }; |
Elijah Newren | ec59da6 | 2021-03-13 22:22:07 +0000 | [diff] [blame] | 172 | /* file_rename_relevance: the reason(s) we want rename information for a file */ |
| 173 | enum file_rename_relevance { |
| 174 | RELEVANT_NO_MORE = 0, /* i.e. NOT relevant */ |
| 175 | RELEVANT_CONTENT = 1, |
| 176 | RELEVANT_LOCATION = 2 |
| 177 | }; |
Elijah Newren | fb52938 | 2021-03-13 22:22:03 +0000 | [diff] [blame] | 178 | |
Elijah Newren | cd52e00 | 2021-02-27 00:30:43 +0000 | [diff] [blame] | 179 | void partial_clear_dir_rename_count(struct strmap *dir_rename_count); |
| 180 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 181 | void diffcore_break(struct repository *, int); |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 182 | void diffcore_rename(struct diff_options *); |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 183 | void diffcore_rename_extended(struct diff_options *options, |
Elijah Newren | f239fff | 2021-07-30 11:47:42 +0000 | [diff] [blame] | 184 | struct mem_pool *pool, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 185 | struct strintmap *relevant_sources, |
| 186 | struct strintmap *dirs_removed, |
Elijah Newren | 25e65b6 | 2021-05-20 06:09:41 +0000 | [diff] [blame] | 187 | struct strmap *dir_rename_count, |
| 188 | struct strmap *cached_pairs); |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 189 | void diffcore_merge_broken(void); |
| 190 | void diffcore_pickaxe(struct diff_options *); |
| 191 | void diffcore_order(const char *orderfile); |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 192 | void diffcore_rotate(struct diff_options *); |
Junio C Hamano | ce24067 | 2005-06-03 01:36:43 -0700 | [diff] [blame] | 193 | |
Kirill Smelkov | 1df4320 | 2014-01-20 20:20:38 +0400 | [diff] [blame] | 194 | /* low-level interface to diffcore_order */ |
| 195 | struct obj_order { |
| 196 | void *obj; /* setup by caller */ |
| 197 | |
| 198 | /* setup/used by order_objects() */ |
| 199 | int orig_order; |
| 200 | int order; |
| 201 | }; |
| 202 | |
| 203 | typedef const char *(*obj_path_fn_t)(void *obj); |
| 204 | |
| 205 | void order_objects(const char *orderfile, obj_path_fn_t obj_path, |
| 206 | struct obj_order *objs, int nr); |
| 207 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 208 | #define DIFF_DEBUG 0 |
| 209 | #if DIFF_DEBUG |
| 210 | void diff_debug_filespec(struct diff_filespec *, int, const char *); |
| 211 | void diff_debug_filepair(const struct diff_filepair *, int); |
| 212 | void diff_debug_queue(const char *, struct diff_queue_struct *); |
| 213 | #else |
Jonathan Nieder | 9874606 | 2010-08-12 17:11:15 -0500 | [diff] [blame] | 214 | #define diff_debug_filespec(a,b,c) do { /* nothing */ } while (0) |
| 215 | #define diff_debug_filepair(a,b) do { /* nothing */ } while (0) |
| 216 | #define diff_debug_queue(a,b) do { /* nothing */ } while (0) |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 217 | #endif |
| 218 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 219 | int diffcore_count_changes(struct repository *r, |
| 220 | struct diff_filespec *src, |
Nguyễn Thái Ngọc Duy | 78d70d9 | 2018-06-30 11:20:25 +0200 | [diff] [blame] | 221 | struct diff_filespec *dst, |
| 222 | void **src_count_p, |
| 223 | void **dst_count_p, |
| 224 | unsigned long *src_copied, |
| 225 | unsigned long *literal_added); |
Junio C Hamano | 6541675 | 2006-02-28 16:01:36 -0800 | [diff] [blame] | 226 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 227 | /* |
| 228 | * If filespec contains an OID and if that object is missing from the given |
| 229 | * repository, add that OID to to_fetch. |
| 230 | */ |
| 231 | void diff_add_if_missing(struct repository *r, |
| 232 | struct oid_array *to_fetch, |
| 233 | const struct diff_filespec *filespec); |
| 234 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 235 | #endif |