blob: 5ffe4ec788f84044df42b13eb847dec1f4e1fd36 [file] [log] [blame]
Junio C Hamano427dcb42005-05-21 02:39:09 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Junio C Hamanoe0173ad2007-04-28 23:38:52 -07004#ifndef DIFFCORE_H
5#define DIFFCORE_H
Junio C Hamano427dcb42005-05-21 02:39:09 -07006
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00007#include "hash-ll.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -07008
9struct diff_options;
Elijah Newren41227cb2023-02-24 00:09:25 +000010struct mem_pool;
11struct oid_array;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020012struct repository;
Elijah Newrena49b55d2021-03-13 22:22:02 +000013struct strintmap;
Elijah Newren0c4fd732021-02-27 00:30:42 +000014struct strmap;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020015struct userdiff_driver;
Elijah Newrenef3ca952018-08-15 10:54:05 -070016
Junio C Hamano427dcb42005-05-21 02:39:09 -070017/* This header file is internal between diff.c and its diff transformers
18 * (e.g. diffcore-rename, diffcore-pickaxe). Never include this header
19 * in anything else.
20 */
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070021
22/* We internally use unsigned short as the score value,
23 * and rely on an int capable to hold 32-bits. -B can take
24 * -Bmerge_score/break_score format and the two scores are
25 * passed around in one int (high 16-bit for merge and low 16-bit
26 * for break).
27 */
Junio C Hamanoee3d2992006-01-15 21:08:42 -080028#define MAX_SCORE 60000.0
Junio C Hamanof345b0a2005-05-30 00:08:37 -070029#define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */
Junio C Hamano4d0f39c2006-03-04 01:03:53 -080030#define DEFAULT_BREAK_SCORE 30000 /* minimum for break to happen (50%) */
Matthieu Moycf958af2010-08-05 18:14:25 +020031#define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen (60%) */
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070032
33#define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
Junio C Hamano427dcb42005-05-21 02:39:09 -070034
Heba Waly13c4d7e2019-11-17 21:04:40 +000035/**
36 * the internal representation for a single file (blob). It records the blob
37 * object name (if known -- for a work tree file it typically is a NUL SHA-1),
38 * filemode and pathname. This is what the `diff_addremove()`, `diff_change()`
39 * and `diff_unmerge()` synthesize and feed `diff_queue()` function with.
40 */
Junio C Hamano427dcb42005-05-21 02:39:09 -070041struct diff_filespec {
brian m. carlsona0d12c42016-06-24 23:09:23 +000042 struct object_id oid;
Junio C Hamano427dcb42005-05-21 02:39:09 -070043 char *path;
44 void *data;
Junio C Hamanoc06c7962006-03-12 03:22:10 -080045 void *cnt_data;
Junio C Hamano427dcb42005-05-21 02:39:09 -070046 unsigned long size;
Linus Torvalds9fb88412007-10-25 11:19:10 -070047 int count; /* Reference count */
Linus Torvalds64479712007-10-25 11:20:56 -070048 int rename_used; /* Count of rename users */
Junio C Hamano427dcb42005-05-21 02:39:09 -070049 unsigned short mode; /* file mode */
brian m. carlson41c95602016-06-24 23:09:24 +000050 unsigned oid_valid : 1; /* if true, use oid and trust mode;
Junio C Hamano427dcb42005-05-21 02:39:09 -070051 * if false, use the name and read from
52 * the filesystem.
53 */
Junio C Hamanodc7090e2005-06-12 17:23:15 -070054#define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -070055 unsigned should_free : 1; /* data should be free()'ed */
56 unsigned should_munmap : 1; /* data should be munmap()'ed */
Jens Lehmannc7e1a732010-03-04 22:20:33 +010057 unsigned dirty_submodule : 2; /* For submodules: its work tree is dirty */
58#define DIRTY_SUBMODULE_UNTRACKED 1
59#define DIRTY_SUBMODULE_MODIFIED 2
Jeff Kingb837f5d2014-01-16 20:19:46 -050060 unsigned is_stdin : 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -070061 unsigned has_more_entries : 1; /* only appear in combined diff */
Jeff King122aa6f2008-10-05 17:43:36 -040062 /* data should be considered "binary"; -1 means "don't know yet" */
Richard Lowe7d0a9a72014-02-23 19:54:47 -050063 signed int is_binary : 2;
Jeff Kingb38f70a2014-01-16 20:22:56 -050064 struct userdiff_driver *driver;
Junio C Hamano427dcb42005-05-21 02:39:09 -070065};
66
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +020067struct diff_filespec *alloc_filespec(const char *);
68void free_filespec(struct diff_filespec *);
69void fill_filespec(struct diff_filespec *, const struct object_id *,
70 int, unsigned short);
Junio C Hamano427dcb42005-05-21 02:39:09 -070071
Jonathan Tan95acf112020-04-07 15:11:43 -070072/*
73 * Prefetch the entries in diff_queued_diff. The parameter is a pointer to a
74 * struct repository.
75 */
76void diff_queued_diff_prefetch(void *repository);
77
Jonathan Tan1c37e862020-04-07 15:11:41 -070078struct diff_populate_filespec_options {
79 unsigned check_size_only : 1;
80 unsigned check_binary : 1;
Jonathan Tan95acf112020-04-07 15:11:43 -070081
82 /*
83 * If an object is missing, diff_populate_filespec() will invoke this
84 * callback before attempting to read that object again.
85 */
86 void (*missing_object_cb)(void *);
87 void *missing_object_data;
Jonathan Tan1c37e862020-04-07 15:11:41 -070088};
89int diff_populate_filespec(struct repository *, struct diff_filespec *,
90 const struct diff_populate_filespec_options *);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +020091void diff_free_filespec_data(struct diff_filespec *);
92void diff_free_filespec_blob(struct diff_filespec *);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020093int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
Junio C Hamano427dcb42005-05-21 02:39:09 -070094
Heba Waly13c4d7e2019-11-17 21:04:40 +000095/**
96 * This records a pair of `struct diff_filespec`; the filespec for a file in
97 * the "old" set (i.e. preimage) is called `one`, and the filespec for a file
98 * in the "new" set (i.e. postimage) is called `two`. A change that represents
99 * file creation has NULL in `one`, and file deletion has NULL in `two`.
100 *
101 * A `filepair` starts pointing at `one` and `two` that are from the same
102 * filename, but `diffcore_std()` can break pairs and match component filespecs
103 * with other filespecs from a different filepair to form new filepair. This is
104 * called 'rename detection'.
105 */
Junio C Hamano52e95782005-05-21 02:40:01 -0700106struct diff_filepair {
Junio C Hamano427dcb42005-05-21 02:39:09 -0700107 struct diff_filespec *one;
108 struct diff_filespec *two;
Junio C Hamano01c4e702005-05-29 16:56:48 -0700109 unsigned short int score;
Yann Dirsona5a323f2008-11-02 14:37:28 +0100110 char status; /* M C R A D U etc. (see Documentation/diff-format.txt or DIFF_STATUS_* in diff.h) */
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700111 unsigned broken_pair : 1;
Junio C Hamanoef677682006-08-03 12:01:01 -0700112 unsigned renamed_pair : 1;
Junio C Hamanoe9c84092007-01-05 01:25:18 -0800113 unsigned is_unmerged : 1;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +0700114 unsigned done_skip_stat_unmatch : 1;
115 unsigned skip_stat_unmatch_result : 1;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700116};
Heba Waly13c4d7e2019-11-17 21:04:40 +0000117
Junio C Hamanoe9c84092007-01-05 01:25:18 -0800118#define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700119
Junio C Hamanoef677682006-08-03 12:01:01 -0700120#define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
Junio C Hamano01c4e702005-05-29 16:56:48 -0700121
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700122#define DIFF_PAIR_BROKEN(p) \
123 ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \
124 ((p)->broken_pair != 0) )
125
Junio C Hamano96716a12005-05-25 15:07:08 -0700126#define DIFF_PAIR_TYPE_CHANGED(p) \
127 ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode))
128
Junio C Hamano4130b992005-05-26 02:24:30 -0700129#define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
130
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200131void diff_free_filepair(struct diff_filepair *);
Elijah Newrena8791ef2021-07-30 11:47:41 +0000132void pool_diff_free_filepair(struct mem_pool *pool,
133 struct diff_filepair *p);
Junio C Hamano226406f2005-05-27 15:50:30 -0700134
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200135int diff_unmodified_pair(struct diff_filepair *);
Junio C Hamanof7c15122005-05-22 21:26:09 -0700136
Heba Waly13c4d7e2019-11-17 21:04:40 +0000137/**
138 * This is a collection of filepairs. Notable members are:
139 *
140 * - `queue`:
141 * An array of pointers to `struct diff_filepair`. This dynamically grows as
142 * you add filepairs;
143 *
144 * - `alloc`:
145 * The allocated size of the `queue` array;
146 *
147 * - `nr`:
148 * The number of elements in the `queue` array.
149 */
Junio C Hamano427dcb42005-05-21 02:39:09 -0700150struct diff_queue_struct {
Junio C Hamano52e95782005-05-21 02:40:01 -0700151 struct diff_filepair **queue;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700152 int alloc;
153 int nr;
154};
Heba Waly13c4d7e2019-11-17 21:04:40 +0000155
Bo Yang9ca5df92010-05-06 21:52:27 -0700156#define DIFF_QUEUE_CLEAR(q) \
157 do { \
158 (q)->queue = NULL; \
159 (q)->nr = (q)->alloc = 0; \
Jonathan Nieder98746062010-08-12 17:11:15 -0500160 } while (0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700161
Junio C Hamano38c6f782005-05-21 19:40:36 -0700162extern struct diff_queue_struct diff_queued_diff;
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200163struct diff_filepair *diff_queue(struct diff_queue_struct *,
164 struct diff_filespec *,
165 struct diff_filespec *);
166void diff_q(struct diff_queue_struct *, struct diff_filepair *);
SZEDER Gábor04ae0002022-11-02 23:01:40 +0100167void diff_free_queue(struct diff_queue_struct *q);
Junio C Hamanof7c15122005-05-22 21:26:09 -0700168
Elijah Newrenfb529382021-03-13 22:22:03 +0000169/* dir_rename_relevance: the reason we want rename information for a dir */
170enum dir_rename_relevance {
171 NOT_RELEVANT = 0,
172 RELEVANT_FOR_ANCESTOR = 1,
173 RELEVANT_FOR_SELF = 2
174};
Elijah Newrenec59da62021-03-13 22:22:07 +0000175/* file_rename_relevance: the reason(s) we want rename information for a file */
176enum file_rename_relevance {
177 RELEVANT_NO_MORE = 0, /* i.e. NOT relevant */
178 RELEVANT_CONTENT = 1,
179 RELEVANT_LOCATION = 2
180};
Elijah Newrenfb529382021-03-13 22:22:03 +0000181
Elijah Newrencd52e002021-02-27 00:30:43 +0000182void partial_clear_dir_rename_count(struct strmap *dir_rename_count);
183
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200184void diffcore_break(struct repository *, int);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200185void diffcore_rename(struct diff_options *);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000186void diffcore_rename_extended(struct diff_options *options,
Elijah Newrenf239fff2021-07-30 11:47:42 +0000187 struct mem_pool *pool,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000188 struct strintmap *relevant_sources,
189 struct strintmap *dirs_removed,
Elijah Newren25e65b62021-05-20 06:09:41 +0000190 struct strmap *dir_rename_count,
191 struct strmap *cached_pairs);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200192void diffcore_merge_broken(void);
193void diffcore_pickaxe(struct diff_options *);
194void diffcore_order(const char *orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -0800195void diffcore_rotate(struct diff_options *);
Junio C Hamanoce240672005-06-03 01:36:43 -0700196
Kirill Smelkov1df43202014-01-20 20:20:38 +0400197/* low-level interface to diffcore_order */
198struct obj_order {
199 void *obj; /* setup by caller */
200
201 /* setup/used by order_objects() */
202 int orig_order;
203 int order;
204};
205
206typedef const char *(*obj_path_fn_t)(void *obj);
207
208void order_objects(const char *orderfile, obj_path_fn_t obj_path,
209 struct obj_order *objs, int nr);
210
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700211#define DIFF_DEBUG 0
212#if DIFF_DEBUG
213void diff_debug_filespec(struct diff_filespec *, int, const char *);
214void diff_debug_filepair(const struct diff_filepair *, int);
215void diff_debug_queue(const char *, struct diff_queue_struct *);
216#else
Jonathan Nieder98746062010-08-12 17:11:15 -0500217#define diff_debug_filespec(a,b,c) do { /* nothing */ } while (0)
218#define diff_debug_filepair(a,b) do { /* nothing */ } while (0)
219#define diff_debug_queue(a,b) do { /* nothing */ } while (0)
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700220#endif
221
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200222int diffcore_count_changes(struct repository *r,
223 struct diff_filespec *src,
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200224 struct diff_filespec *dst,
225 void **src_count_p,
226 void **dst_count_p,
227 unsigned long *src_copied,
228 unsigned long *literal_added);
Junio C Hamano65416752006-02-28 16:01:36 -0800229
Jonathan Tan95acf112020-04-07 15:11:43 -0700230/*
231 * If filespec contains an OID and if that object is missing from the given
232 * repository, add that OID to to_fetch.
233 */
234void diff_add_if_missing(struct repository *r,
235 struct oid_array *to_fetch,
236 const struct diff_filespec *filespec);
237
Junio C Hamano427dcb42005-05-21 02:39:09 -0700238#endif