]> git.proxmox.com Git - libgit2.git/blob - src/diff.h
git_diff__merge: allow pluggable diff merges
[libgit2.git] / src / diff.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_diff_h__
8 #define INCLUDE_diff_h__
9
10 #include "git2/diff.h"
11 #include "git2/sys/diff.h"
12 #include "git2/oid.h"
13
14 #include <stdio.h>
15 #include "vector.h"
16 #include "buffer.h"
17 #include "iterator.h"
18 #include "repository.h"
19 #include "pool.h"
20 #include "odb.h"
21
22 #define DIFF_OLD_PREFIX_DEFAULT "a/"
23 #define DIFF_NEW_PREFIX_DEFAULT "b/"
24
25 enum {
26 GIT_DIFFCAPS_HAS_SYMLINKS = (1 << 0), /* symlinks on platform? */
27 GIT_DIFFCAPS_IGNORE_STAT = (1 << 1), /* use stat? */
28 GIT_DIFFCAPS_TRUST_MODE_BITS = (1 << 2), /* use st_mode? */
29 GIT_DIFFCAPS_TRUST_CTIME = (1 << 3), /* use st_ctime? */
30 GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */
31 GIT_DIFFCAPS_TRUST_NANOSECS = (1 << 5), /* use stat time nanoseconds */
32 };
33
34 #define DIFF_FLAGS_KNOWN_BINARY (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
35 #define DIFF_FLAGS_NOT_BINARY (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
36
37 enum {
38 GIT_DIFF_FLAG__FREE_PATH = (1 << 7), /* `path` is allocated memory */
39 GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */
40 GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */
41 GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */
42 GIT_DIFF_FLAG__FREE_BLOB = (1 << 11), /* release the blob when done */
43 GIT_DIFF_FLAG__LOADED = (1 << 12), /* file data has been loaded */
44
45 GIT_DIFF_FLAG__TO_DELETE = (1 << 16), /* delete entry during rename det. */
46 GIT_DIFF_FLAG__TO_SPLIT = (1 << 17), /* split entry during rename det. */
47 GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18),
48 GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19),
49 GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20),
50 };
51
52 #define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF)
53
54 #define GIT_DIFF__VERBOSE (1 << 30)
55
56 struct git_diff {
57 git_refcount rc;
58 git_repository *repo;
59 git_diff_options opts;
60 git_vector pathspec;
61 git_vector deltas; /* vector of git_diff_delta */
62 git_pool pool;
63 git_iterator_type_t old_src;
64 git_iterator_type_t new_src;
65 uint32_t diffcaps;
66 git_diff_perfdata perf;
67
68 int (*strcomp)(const char *, const char *);
69 int (*strncomp)(const char *, const char *, size_t);
70 int (*pfxcomp)(const char *str, const char *pfx);
71 int (*entrycomp)(const void *a, const void *b);
72 };
73
74 extern void git_diff__cleanup_modes(
75 uint32_t diffcaps, uint32_t *omode, uint32_t *nmode);
76
77 extern void git_diff_addref(git_diff *diff);
78
79 extern int git_diff_delta__cmp(const void *a, const void *b);
80 extern int git_diff_delta__casecmp(const void *a, const void *b);
81
82 extern const char *git_diff_delta__path(const git_diff_delta *delta);
83
84 extern bool git_diff_delta__should_skip(
85 const git_diff_options *opts, const git_diff_delta *delta);
86
87 extern int git_diff_delta__format_file_header(
88 git_buf *out,
89 const git_diff_delta *delta,
90 const char *oldpfx,
91 const char *newpfx,
92 int oid_strlen);
93
94 extern int git_diff__oid_for_file(
95 git_oid *out, git_diff *, const char *, uint16_t, git_off_t);
96 extern int git_diff__oid_for_entry(
97 git_oid *out, git_diff *, const git_index_entry *, uint16_t, const git_oid *update);
98
99 extern int git_diff__from_iterators(
100 git_diff **diff_ptr,
101 git_repository *repo,
102 git_iterator *old_iter,
103 git_iterator *new_iter,
104 const git_diff_options *opts);
105
106 extern int git_diff__paired_foreach(
107 git_diff *idx2head,
108 git_diff *wd2idx,
109 int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
110 void *payload);
111
112 extern int git_diff_find_similar__hashsig_for_file(
113 void **out, const git_diff_file *f, const char *path, void *p);
114
115 extern int git_diff_find_similar__hashsig_for_buf(
116 void **out, const git_diff_file *f, const char *buf, size_t len, void *p);
117
118 extern void git_diff_find_similar__hashsig_free(void *sig, void *payload);
119
120 extern int git_diff_find_similar__calc_similarity(
121 int *score, void *siga, void *sigb, void *payload);
122
123 extern int git_diff__commit(
124 git_diff **diff, git_repository *repo, const git_commit *commit, const git_diff_options *opts);
125
126 /* Merge two `git_diff`s according to the callback given by `cb`. */
127
128 typedef git_diff_delta *(*git_diff__merge_cb)(
129 const git_diff_delta *left,
130 const git_diff_delta *right,
131 git_pool *pool);
132
133 extern int git_diff__merge(
134 git_diff *onto, const git_diff *from, git_diff__merge_cb cb);
135
136 /*
137 * Sometimes a git_diff_file will have a zero size; this attempts to
138 * fill in the size without loading the blob if possible. If that is
139 * not possible, then it will return the git_odb_object that had to be
140 * loaded and the caller can use it or dispose of it as needed.
141 */
142 GIT_INLINE(int) git_diff_file__resolve_zero_size(
143 git_diff_file *file, git_odb_object **odb_obj, git_repository *repo)
144 {
145 int error;
146 git_odb *odb;
147 size_t len;
148 git_otype type;
149
150 if ((error = git_repository_odb(&odb, repo)) < 0)
151 return error;
152
153 error = git_odb__read_header_or_object(
154 odb_obj, &len, &type, odb, &file->id);
155
156 git_odb_free(odb);
157
158 if (!error)
159 file->size = (git_off_t)len;
160
161 return error;
162 }
163
164 #endif
165