blob: c1592bcd0135ec6066b6c04bb94bc818c1ae1cc8 [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;
11struct userdiff_driver;
Elijah Newrenef3ca952018-08-15 10:54:05 -070012
Junio C Hamano427dcb42005-05-21 02:39:09 -070013/* This header file is internal between diff.c and its diff transformers
14 * (e.g. diffcore-rename, diffcore-pickaxe). Never include this header
15 * in anything else.
16 */
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070017
18/* We internally use unsigned short as the score value,
19 * and rely on an int capable to hold 32-bits. -B can take
20 * -Bmerge_score/break_score format and the two scores are
21 * passed around in one int (high 16-bit for merge and low 16-bit
22 * for break).
23 */
Junio C Hamanoee3d2992006-01-15 21:08:42 -080024#define MAX_SCORE 60000.0
Junio C Hamanof345b0a2005-05-30 00:08:37 -070025#define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */
Junio C Hamano4d0f39c2006-03-04 01:03:53 -080026#define DEFAULT_BREAK_SCORE 30000 /* minimum for break to happen (50%) */
Matthieu Moycf958af2010-08-05 18:14:25 +020027#define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen (60%) */
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070028
29#define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
Junio C Hamano427dcb42005-05-21 02:39:09 -070030
Heba Waly13c4d7e2019-11-17 21:04:40 +000031/**
32 * the internal representation for a single file (blob). It records the blob
33 * object name (if known -- for a work tree file it typically is a NUL SHA-1),
34 * filemode and pathname. This is what the `diff_addremove()`, `diff_change()`
35 * and `diff_unmerge()` synthesize and feed `diff_queue()` function with.
36 */
Junio C Hamano427dcb42005-05-21 02:39:09 -070037struct diff_filespec {
brian m. carlsona0d12c42016-06-24 23:09:23 +000038 struct object_id oid;
Junio C Hamano427dcb42005-05-21 02:39:09 -070039 char *path;
40 void *data;
Junio C Hamanoc06c7962006-03-12 03:22:10 -080041 void *cnt_data;
Junio C Hamano427dcb42005-05-21 02:39:09 -070042 unsigned long size;
Linus Torvalds9fb88412007-10-25 11:19:10 -070043 int count; /* Reference count */
Linus Torvalds64479712007-10-25 11:20:56 -070044 int rename_used; /* Count of rename users */
Junio C Hamano427dcb42005-05-21 02:39:09 -070045 unsigned short mode; /* file mode */
brian m. carlson41c95602016-06-24 23:09:24 +000046 unsigned oid_valid : 1; /* if true, use oid and trust mode;
Junio C Hamano427dcb42005-05-21 02:39:09 -070047 * if false, use the name and read from
48 * the filesystem.
49 */
Junio C Hamanodc7090e2005-06-12 17:23:15 -070050#define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -070051 unsigned should_free : 1; /* data should be free()'ed */
52 unsigned should_munmap : 1; /* data should be munmap()'ed */
Jens Lehmannc7e1a732010-03-04 22:20:33 +010053 unsigned dirty_submodule : 2; /* For submodules: its work tree is dirty */
54#define DIRTY_SUBMODULE_UNTRACKED 1
55#define DIRTY_SUBMODULE_MODIFIED 2
Jeff Kingb837f5d2014-01-16 20:19:46 -050056 unsigned is_stdin : 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -070057 unsigned has_more_entries : 1; /* only appear in combined diff */
Jeff King122aa6f2008-10-05 17:43:36 -040058 /* data should be considered "binary"; -1 means "don't know yet" */
Richard Lowe7d0a9a72014-02-23 19:54:47 -050059 signed int is_binary : 2;
Jeff Kingb38f70a2014-01-16 20:22:56 -050060 struct userdiff_driver *driver;
Junio C Hamano427dcb42005-05-21 02:39:09 -070061};
62
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +020063struct diff_filespec *alloc_filespec(const char *);
64void free_filespec(struct diff_filespec *);
65void fill_filespec(struct diff_filespec *, const struct object_id *,
66 int, unsigned short);
Junio C Hamano427dcb42005-05-21 02:39:09 -070067
Jonathan Tan95acf112020-04-07 15:11:43 -070068/*
69 * Prefetch the entries in diff_queued_diff. The parameter is a pointer to a
70 * struct repository.
71 */
72void diff_queued_diff_prefetch(void *repository);
73
Jonathan Tan1c37e862020-04-07 15:11:41 -070074struct diff_populate_filespec_options {
75 unsigned check_size_only : 1;
76 unsigned check_binary : 1;
Jonathan Tan95acf112020-04-07 15:11:43 -070077
78 /*
79 * If an object is missing, diff_populate_filespec() will invoke this
80 * callback before attempting to read that object again.
81 */
82 void (*missing_object_cb)(void *);
83 void *missing_object_data;
Jonathan Tan1c37e862020-04-07 15:11:41 -070084};
85int diff_populate_filespec(struct repository *, struct diff_filespec *,
86 const struct diff_populate_filespec_options *);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +020087void diff_free_filespec_data(struct diff_filespec *);
88void diff_free_filespec_blob(struct diff_filespec *);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +020089int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
Junio C Hamano427dcb42005-05-21 02:39:09 -070090
Heba Waly13c4d7e2019-11-17 21:04:40 +000091/**
92 * This records a pair of `struct diff_filespec`; the filespec for a file in
93 * the "old" set (i.e. preimage) is called `one`, and the filespec for a file
94 * in the "new" set (i.e. postimage) is called `two`. A change that represents
95 * file creation has NULL in `one`, and file deletion has NULL in `two`.
96 *
97 * A `filepair` starts pointing at `one` and `two` that are from the same
98 * filename, but `diffcore_std()` can break pairs and match component filespecs
99 * with other filespecs from a different filepair to form new filepair. This is
100 * called 'rename detection'.
101 */
Junio C Hamano52e95782005-05-21 02:40:01 -0700102struct diff_filepair {
Junio C Hamano427dcb42005-05-21 02:39:09 -0700103 struct diff_filespec *one;
104 struct diff_filespec *two;
Junio C Hamano01c4e702005-05-29 16:56:48 -0700105 unsigned short int score;
Yann Dirsona5a323f2008-11-02 14:37:28 +0100106 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 -0700107 unsigned broken_pair : 1;
Junio C Hamanoef677682006-08-03 12:01:01 -0700108 unsigned renamed_pair : 1;
Junio C Hamanoe9c84092007-01-05 01:25:18 -0800109 unsigned is_unmerged : 1;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +0700110 unsigned done_skip_stat_unmatch : 1;
111 unsigned skip_stat_unmatch_result : 1;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700112};
Heba Waly13c4d7e2019-11-17 21:04:40 +0000113
Junio C Hamanoe9c84092007-01-05 01:25:18 -0800114#define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700115
Junio C Hamanoef677682006-08-03 12:01:01 -0700116#define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
Junio C Hamano01c4e702005-05-29 16:56:48 -0700117
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700118#define DIFF_PAIR_BROKEN(p) \
119 ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \
120 ((p)->broken_pair != 0) )
121
Junio C Hamano96716a12005-05-25 15:07:08 -0700122#define DIFF_PAIR_TYPE_CHANGED(p) \
123 ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode))
124
Junio C Hamano4130b992005-05-26 02:24:30 -0700125#define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
126
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200127void diff_free_filepair(struct diff_filepair *);
Junio C Hamano226406f2005-05-27 15:50:30 -0700128
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200129int diff_unmodified_pair(struct diff_filepair *);
Junio C Hamanof7c15122005-05-22 21:26:09 -0700130
Heba Waly13c4d7e2019-11-17 21:04:40 +0000131/**
132 * This is a collection of filepairs. Notable members are:
133 *
134 * - `queue`:
135 * An array of pointers to `struct diff_filepair`. This dynamically grows as
136 * you add filepairs;
137 *
138 * - `alloc`:
139 * The allocated size of the `queue` array;
140 *
141 * - `nr`:
142 * The number of elements in the `queue` array.
143 */
Junio C Hamano427dcb42005-05-21 02:39:09 -0700144struct diff_queue_struct {
Junio C Hamano52e95782005-05-21 02:40:01 -0700145 struct diff_filepair **queue;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700146 int alloc;
147 int nr;
148};
Heba Waly13c4d7e2019-11-17 21:04:40 +0000149
Bo Yang9ca5df92010-05-06 21:52:27 -0700150#define DIFF_QUEUE_CLEAR(q) \
151 do { \
152 (q)->queue = NULL; \
153 (q)->nr = (q)->alloc = 0; \
Jonathan Nieder98746062010-08-12 17:11:15 -0500154 } while (0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700155
Junio C Hamano38c6f782005-05-21 19:40:36 -0700156extern struct diff_queue_struct diff_queued_diff;
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200157struct diff_filepair *diff_queue(struct diff_queue_struct *,
158 struct diff_filespec *,
159 struct diff_filespec *);
160void diff_q(struct diff_queue_struct *, struct diff_filepair *);
Junio C Hamanof7c15122005-05-22 21:26:09 -0700161
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200162void diffcore_break(struct repository *, int);
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200163void diffcore_rename(struct diff_options *);
164void diffcore_merge_broken(void);
165void diffcore_pickaxe(struct diff_options *);
166void diffcore_order(const char *orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -0800167void diffcore_rotate(struct diff_options *);
Junio C Hamanoce240672005-06-03 01:36:43 -0700168
Kirill Smelkov1df43202014-01-20 20:20:38 +0400169/* low-level interface to diffcore_order */
170struct obj_order {
171 void *obj; /* setup by caller */
172
173 /* setup/used by order_objects() */
174 int orig_order;
175 int order;
176};
177
178typedef const char *(*obj_path_fn_t)(void *obj);
179
180void order_objects(const char *orderfile, obj_path_fn_t obj_path,
181 struct obj_order *objs, int nr);
182
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700183#define DIFF_DEBUG 0
184#if DIFF_DEBUG
185void diff_debug_filespec(struct diff_filespec *, int, const char *);
186void diff_debug_filepair(const struct diff_filepair *, int);
187void diff_debug_queue(const char *, struct diff_queue_struct *);
188#else
Jonathan Nieder98746062010-08-12 17:11:15 -0500189#define diff_debug_filespec(a,b,c) do { /* nothing */ } while (0)
190#define diff_debug_filepair(a,b) do { /* nothing */ } while (0)
191#define diff_debug_queue(a,b) do { /* nothing */ } while (0)
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700192#endif
193
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200194int diffcore_count_changes(struct repository *r,
195 struct diff_filespec *src,
Nguyễn Thái Ngọc Duy78d70d92018-06-30 11:20:25 +0200196 struct diff_filespec *dst,
197 void **src_count_p,
198 void **dst_count_p,
199 unsigned long *src_copied,
200 unsigned long *literal_added);
Junio C Hamano65416752006-02-28 16:01:36 -0800201
Jonathan Tan95acf112020-04-07 15:11:43 -0700202/*
203 * If filespec contains an OID and if that object is missing from the given
204 * repository, add that OID to to_fetch.
205 */
206void diff_add_if_missing(struct repository *r,
207 struct oid_array *to_fetch,
208 const struct diff_filespec *filespec);
209
Junio C Hamano427dcb42005-05-21 02:39:09 -0700210#endif