blob: badc2261c201831a620fcc7c29edcc1fd3bdbf1e [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 Newrenef3ca952018-08-15 10:54:05 -07007#include "cache.h"
8
9struct diff_options;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020010struct repository;
Elijah Newrena49b55d2021-03-13 22:22:02 +000011struct strintmap;
Elijah Newren0c4fd732021-02-27 00:30:42 +000012struct strmap;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020013struct userdiff_driver;
Elijah Newrenef3ca952018-08-15 10:54:05 -070014
Junio C Hamano427dcb42005-05-21 02:39:09 -070015/* 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 Hamanoeeaa4602005-06-03 01:40:28 -070019
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 Hamanoee3d2992006-01-15 21:08:42 -080026#define MAX_SCORE 60000.0
Junio C Hamanof345b0a2005-05-30 00:08:37 -070027#define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */
Junio C Hamano4d0f39c2006-03-04 01:03:53 -080028#define DEFAULT_BREAK_SCORE 30000 /* minimum for break to happen (50%) */
Matthieu Moycf958af2010-08-05 18:14:25 +020029#define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen (60%) */
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070030
31#define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
Junio C Hamano427dcb42005-05-21 02:39:09 -070032
Heba Waly13c4d7e2019-11-17 21:04:40 +000033/**
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 Hamano427dcb42005-05-21 02:39:09 -070039struct diff_filespec {
brian m. carlsona0d12c42016-06-24 23:09:23 +000040 struct object_id oid;
Junio C Hamano427dcb42005-05-21 02:39:09 -070041 char *path;
42 void *data;
Junio C Hamanoc06c7962006-03-12 03:22:10 -080043 void *cnt_data;
Junio C Hamano427dcb42005-05-21 02:39:09 -070044 unsigned long size;
Linus Torvalds9fb88412007-10-25 11:19:10 -070045 int count; /* Reference count */
Linus Torvalds64479712007-10-25 11:20:56 -070046 int rename_used; /* Count of rename users */
Junio C Hamano427dcb42005-05-21 02:39:09 -070047 unsigned short mode; /* file mode */
brian m. carlson41c95602016-06-24 23:09:24 +000048 unsigned oid_valid : 1; /* if true, use oid and trust mode;
Junio C Hamano427dcb42005-05-21 02:39:09 -070049 * if false, use the name and read from
50 * the filesystem.
51 */
Junio C Hamanodc7090e2005-06-12 17:23:15 -070052#define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -070053 unsigned should_free : 1; /* data should be free()'ed */
54 unsigned should_munmap : 1; /* data should be munmap()'ed */
Jens Lehmannc7e1a732010-03-04 22:20:33 +010055 unsigned dirty_submodule : 2; /* For submodules: its work tree is dirty */
56#define DIRTY_SUBMODULE_UNTRACKED 1
57#define DIRTY_SUBMODULE_MODIFIED 2
Jeff Kingb837f5d2014-01-16 20:19:46 -050058 unsigned is_stdin : 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -070059 unsigned has_more_entries : 1; /* only appear in combined diff */
Jeff King122aa6f2008-10-05 17:43:36 -040060 /* data should be considered "binary"; -1 means "don't know yet" */
Richard Lowe7d0a9a72014-02-23 19:54:47 -050061 signed int is_binary : 2;
Jeff Kingb38f70a2014-01-16 20:22:56 -050062 struct userdiff_driver *driver;
Junio C Hamano427dcb42005-05-21 02:39:09 -070063};
64
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +020065struct diff_filespec *alloc_filespec(const char *);
66void free_filespec(struct diff_filespec *);
67void fill_filespec(struct diff_filespec *, const struct object_id *,
68 int, unsigned short);
Junio C Hamano427dcb42005-05-21 02:39:09 -070069
Jonathan Tan95acf112020-04-07 15:11:43 -070070/*
71 * Prefetch the entries in diff_queued_diff. The parameter is a pointer to a
72 * struct repository.
73 */
74void diff_queued_diff_prefetch(void *repository);
75
Jonathan Tan1c37e862020-04-07 15:11:41 -070076struct diff_populate_filespec_options {
77 unsigned check_size_only : 1;
78 unsigned check_binary : 1;
Jonathan Tan95acf112020-04-07 15:11:43 -070079
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 Tan1c37e862020-04-07 15:11:41 -070086};
87int diff_populate_filespec(struct repository *, struct diff_filespec *,
88 const struct diff_populate_filespec_options *);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +020089void diff_free_filespec_data(struct diff_filespec *);
90void diff_free_filespec_blob(struct diff_filespec *);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020091int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
Junio C Hamano427dcb42005-05-21 02:39:09 -070092
Heba Waly13c4d7e2019-11-17 21:04:40 +000093/**
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 Hamano52e95782005-05-21 02:40:01 -0700104struct diff_filepair {
Junio C Hamano427dcb42005-05-21 02:39:09 -0700105 struct diff_filespec *one;
106 struct diff_filespec *two;
Junio C Hamano01c4e702005-05-29 16:56:48 -0700107 unsigned short int score;
Yann Dirsona5a323f2008-11-02 14:37:28 +0100108 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 -0700109 unsigned broken_pair : 1;
Junio C Hamanoef677682006-08-03 12:01:01 -0700110 unsigned renamed_pair : 1;
Junio C Hamanoe9c84092007-01-05 01:25:18 -0800111 unsigned is_unmerged : 1;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +0700112 unsigned done_skip_stat_unmatch : 1;
113 unsigned skip_stat_unmatch_result : 1;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700114};
Heba Waly13c4d7e2019-11-17 21:04:40 +0000115
Junio C Hamanoe9c84092007-01-05 01:25:18 -0800116#define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700117
Junio C Hamanoef677682006-08-03 12:01:01 -0700118#define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
Junio C Hamano01c4e702005-05-29 16:56:48 -0700119
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700120#define DIFF_PAIR_BROKEN(p) \
121 ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \
122 ((p)->broken_pair != 0) )
123
Junio C Hamano96716a12005-05-25 15:07:08 -0700124#define DIFF_PAIR_TYPE_CHANGED(p) \
125 ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode))
126
Junio C Hamano4130b992005-05-26 02:24:30 -0700127#define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
128
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200129void diff_free_filepair(struct diff_filepair *);
Elijah Newrena8791ef2021-07-30 11:47:41 +0000130void pool_diff_free_filepair(struct mem_pool *pool,
131 struct diff_filepair *p);
Junio C Hamano226406f2005-05-27 15:50:30 -0700132
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200133int diff_unmodified_pair(struct diff_filepair *);
Junio C Hamanof7c15122005-05-22 21:26:09 -0700134
Heba Waly13c4d7e2019-11-17 21:04:40 +0000135/**
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 Hamano427dcb42005-05-21 02:39:09 -0700148struct diff_queue_struct {
Junio C Hamano52e95782005-05-21 02:40:01 -0700149 struct diff_filepair **queue;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700150 int alloc;
151 int nr;
152};
Heba Waly13c4d7e2019-11-17 21:04:40 +0000153
Bo Yang9ca5df92010-05-06 21:52:27 -0700154#define DIFF_QUEUE_CLEAR(q) \
155 do { \
156 (q)->queue = NULL; \
157 (q)->nr = (q)->alloc = 0; \
Jonathan Nieder98746062010-08-12 17:11:15 -0500158 } while (0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700159
Junio C Hamano38c6f782005-05-21 19:40:36 -0700160extern struct diff_queue_struct diff_queued_diff;
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200161struct diff_filepair *diff_queue(struct diff_queue_struct *,
162 struct diff_filespec *,
163 struct diff_filespec *);
164void diff_q(struct diff_queue_struct *, struct diff_filepair *);
Junio C Hamanof7c15122005-05-22 21:26:09 -0700165
Elijah Newrenfb529382021-03-13 22:22:03 +0000166/* dir_rename_relevance: the reason we want rename information for a dir */
167enum dir_rename_relevance {
168 NOT_RELEVANT = 0,
169 RELEVANT_FOR_ANCESTOR = 1,
170 RELEVANT_FOR_SELF = 2
171};
Elijah Newrenec59da62021-03-13 22:22:07 +0000172/* file_rename_relevance: the reason(s) we want rename information for a file */
173enum file_rename_relevance {
174 RELEVANT_NO_MORE = 0, /* i.e. NOT relevant */
175 RELEVANT_CONTENT = 1,
176 RELEVANT_LOCATION = 2
177};
Elijah Newrenfb529382021-03-13 22:22:03 +0000178
Elijah Newrencd52e002021-02-27 00:30:43 +0000179void partial_clear_dir_rename_count(struct strmap *dir_rename_count);
180
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200181void diffcore_break(struct repository *, int);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200182void diffcore_rename(struct diff_options *);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000183void diffcore_rename_extended(struct diff_options *options,
Elijah Newrenf239fff2021-07-30 11:47:42 +0000184 struct mem_pool *pool,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000185 struct strintmap *relevant_sources,
186 struct strintmap *dirs_removed,
Elijah Newren25e65b62021-05-20 06:09:41 +0000187 struct strmap *dir_rename_count,
188 struct strmap *cached_pairs);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200189void diffcore_merge_broken(void);
190void diffcore_pickaxe(struct diff_options *);
191void diffcore_order(const char *orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -0800192void diffcore_rotate(struct diff_options *);
Junio C Hamanoce240672005-06-03 01:36:43 -0700193
Kirill Smelkov1df43202014-01-20 20:20:38 +0400194/* low-level interface to diffcore_order */
195struct obj_order {
196 void *obj; /* setup by caller */
197
198 /* setup/used by order_objects() */
199 int orig_order;
200 int order;
201};
202
203typedef const char *(*obj_path_fn_t)(void *obj);
204
205void order_objects(const char *orderfile, obj_path_fn_t obj_path,
206 struct obj_order *objs, int nr);
207
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700208#define DIFF_DEBUG 0
209#if DIFF_DEBUG
210void diff_debug_filespec(struct diff_filespec *, int, const char *);
211void diff_debug_filepair(const struct diff_filepair *, int);
212void diff_debug_queue(const char *, struct diff_queue_struct *);
213#else
Jonathan Nieder98746062010-08-12 17:11:15 -0500214#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 Hamano25d5ea42005-05-24 01:10:48 -0700217#endif
218
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200219int diffcore_count_changes(struct repository *r,
220 struct diff_filespec *src,
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200221 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 Hamano65416752006-02-28 16:01:36 -0800226
Jonathan Tan95acf112020-04-07 15:11:43 -0700227/*
228 * If filespec contains an OID and if that object is missing from the given
229 * repository, add that OID to to_fetch.
230 */
231void diff_add_if_missing(struct repository *r,
232 struct oid_array *to_fetch,
233 const struct diff_filespec *filespec);
234
Junio C Hamano427dcb42005-05-21 02:39:09 -0700235#endif