range-diff: avoid segfault with -I
[git/debian.git] / diffcore.h
blob533b30e21e7fe283350f8c6c408d002da1aad08d
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #ifndef DIFFCORE_H
5 #define DIFFCORE_H
7 #include "cache.h"
9 struct diff_options;
10 struct repository;
11 struct strintmap;
12 struct strmap;
13 struct userdiff_driver;
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.
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).
26 #define MAX_SCORE 60000.0
27 #define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */
28 #define DEFAULT_BREAK_SCORE 30000 /* minimum for break to happen (50%) */
29 #define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen (60%) */
31 #define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
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.
39 struct diff_filespec {
40 struct object_id oid;
41 char *path;
42 void *data;
43 void *cnt_data;
44 unsigned long size;
45 int count; /* Reference count */
46 int rename_used; /* Count of rename users */
47 unsigned short mode; /* file mode */
48 unsigned oid_valid : 1; /* if true, use oid and trust mode;
49 * if false, use the name and read from
50 * the filesystem.
52 #define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
53 unsigned should_free : 1; /* data should be free()'ed */
54 unsigned should_munmap : 1; /* data should be munmap()'ed */
55 unsigned dirty_submodule : 2; /* For submodules: its work tree is dirty */
56 #define DIRTY_SUBMODULE_UNTRACKED 1
57 #define DIRTY_SUBMODULE_MODIFIED 2
58 unsigned is_stdin : 1;
59 unsigned has_more_entries : 1; /* only appear in combined diff */
60 /* data should be considered "binary"; -1 means "don't know yet" */
61 signed int is_binary : 2;
62 struct userdiff_driver *driver;
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);
71 * Prefetch the entries in diff_queued_diff. The parameter is a pointer to a
72 * struct repository.
74 void diff_queued_diff_prefetch(void *repository);
76 struct diff_populate_filespec_options {
77 unsigned check_size_only : 1;
78 unsigned check_binary : 1;
81 * If an object is missing, diff_populate_filespec() will invoke this
82 * callback before attempting to read that object again.
84 void (*missing_object_cb)(void *);
85 void *missing_object_data;
87 int diff_populate_filespec(struct repository *, struct diff_filespec *,
88 const struct diff_populate_filespec_options *);
89 void diff_free_filespec_data(struct diff_filespec *);
90 void diff_free_filespec_blob(struct diff_filespec *);
91 int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
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`.
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'.
104 struct diff_filepair {
105 struct diff_filespec *one;
106 struct diff_filespec *two;
107 unsigned short int score;
108 char status; /* M C R A D U etc. (see Documentation/diff-format.txt or DIFF_STATUS_* in diff.h) */
109 unsigned broken_pair : 1;
110 unsigned renamed_pair : 1;
111 unsigned is_unmerged : 1;
112 unsigned done_skip_stat_unmatch : 1;
113 unsigned skip_stat_unmatch_result : 1;
116 #define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
118 #define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
120 #define DIFF_PAIR_BROKEN(p) \
121 ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \
122 ((p)->broken_pair != 0) )
124 #define DIFF_PAIR_TYPE_CHANGED(p) \
125 ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode))
127 #define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
129 void diff_free_filepair(struct diff_filepair *);
131 int diff_unmodified_pair(struct diff_filepair *);
134 * This is a collection of filepairs. Notable members are:
136 * - `queue`:
137 * An array of pointers to `struct diff_filepair`. This dynamically grows as
138 * you add filepairs;
140 * - `alloc`:
141 * The allocated size of the `queue` array;
143 * - `nr`:
144 * The number of elements in the `queue` array.
146 struct diff_queue_struct {
147 struct diff_filepair **queue;
148 int alloc;
149 int nr;
152 #define DIFF_QUEUE_CLEAR(q) \
153 do { \
154 (q)->queue = NULL; \
155 (q)->nr = (q)->alloc = 0; \
156 } while (0)
158 extern struct diff_queue_struct diff_queued_diff;
159 struct diff_filepair *diff_queue(struct diff_queue_struct *,
160 struct diff_filespec *,
161 struct diff_filespec *);
162 void diff_q(struct diff_queue_struct *, struct diff_filepair *);
164 /* dir_rename_relevance: the reason we want rename information for a dir */
165 enum dir_rename_relevance {
166 NOT_RELEVANT = 0,
167 RELEVANT_FOR_ANCESTOR = 1,
168 RELEVANT_FOR_SELF = 2
170 /* file_rename_relevance: the reason(s) we want rename information for a file */
171 enum file_rename_relevance {
172 RELEVANT_NO_MORE = 0, /* i.e. NOT relevant */
173 RELEVANT_CONTENT = 1,
174 RELEVANT_LOCATION = 2
177 void partial_clear_dir_rename_count(struct strmap *dir_rename_count);
179 void diffcore_break(struct repository *, int);
180 void diffcore_rename(struct diff_options *);
181 void diffcore_rename_extended(struct diff_options *options,
182 struct strintmap *relevant_sources,
183 struct strintmap *dirs_removed,
184 struct strmap *dir_rename_count,
185 struct strmap *cached_pairs);
186 void diffcore_merge_broken(void);
187 void diffcore_pickaxe(struct diff_options *);
188 void diffcore_order(const char *orderfile);
189 void diffcore_rotate(struct diff_options *);
191 /* low-level interface to diffcore_order */
192 struct obj_order {
193 void *obj; /* setup by caller */
195 /* setup/used by order_objects() */
196 int orig_order;
197 int order;
200 typedef const char *(*obj_path_fn_t)(void *obj);
202 void order_objects(const char *orderfile, obj_path_fn_t obj_path,
203 struct obj_order *objs, int nr);
205 #define DIFF_DEBUG 0
206 #if DIFF_DEBUG
207 void diff_debug_filespec(struct diff_filespec *, int, const char *);
208 void diff_debug_filepair(const struct diff_filepair *, int);
209 void diff_debug_queue(const char *, struct diff_queue_struct *);
210 #else
211 #define diff_debug_filespec(a,b,c) do { /* nothing */ } while (0)
212 #define diff_debug_filepair(a,b) do { /* nothing */ } while (0)
213 #define diff_debug_queue(a,b) do { /* nothing */ } while (0)
214 #endif
216 int diffcore_count_changes(struct repository *r,
217 struct diff_filespec *src,
218 struct diff_filespec *dst,
219 void **src_count_p,
220 void **dst_count_p,
221 unsigned long *src_copied,
222 unsigned long *literal_added);
225 * If filespec contains an OID and if that object is missing from the given
226 * repository, add that OID to to_fetch.
228 void diff_add_if_missing(struct repository *r,
229 struct oid_array *to_fetch,
230 const struct diff_filespec *filespec);
232 #endif