rugged 0.25.0b5 → 0.25.0b6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/ext/rugged/rugged_patch.c +77 -17
  3. data/lib/rugged/version.rb +1 -1
  4. data/vendor/libgit2/CMakeLists.txt +8 -6
  5. data/vendor/libgit2/include/git2/checkout.h +7 -0
  6. data/vendor/libgit2/include/git2/common.h +1 -0
  7. data/vendor/libgit2/include/git2/diff.h +54 -7
  8. data/vendor/libgit2/include/git2/errors.h +2 -1
  9. data/vendor/libgit2/include/git2/index.h +25 -0
  10. data/vendor/libgit2/include/git2/pack.h +4 -4
  11. data/vendor/libgit2/include/git2/repository.h +20 -1
  12. data/vendor/libgit2/include/git2/submodule.h +11 -3
  13. data/vendor/libgit2/include/git2/sys/odb_backend.h +11 -0
  14. data/vendor/libgit2/src/apply.c +369 -0
  15. data/vendor/libgit2/src/apply.h +21 -0
  16. data/vendor/libgit2/src/array.h +0 -1
  17. data/vendor/libgit2/src/blame_git.c +18 -8
  18. data/vendor/libgit2/src/buffer.c +252 -20
  19. data/vendor/libgit2/src/buffer.h +8 -0
  20. data/vendor/libgit2/src/checkout.c +7 -1
  21. data/vendor/libgit2/src/clone.c +0 -8
  22. data/vendor/libgit2/src/common.h +1 -1
  23. data/vendor/libgit2/src/crlf.c +1 -0
  24. data/vendor/libgit2/src/delta.c +238 -62
  25. data/vendor/libgit2/src/delta.h +79 -58
  26. data/vendor/libgit2/src/diff.c +32 -1547
  27. data/vendor/libgit2/src/diff.h +12 -122
  28. data/vendor/libgit2/src/diff_driver.c +1 -2
  29. data/vendor/libgit2/src/diff_file.c +3 -0
  30. data/vendor/libgit2/src/diff_generate.c +1611 -0
  31. data/vendor/libgit2/src/diff_generate.h +123 -0
  32. data/vendor/libgit2/src/diff_parse.c +105 -0
  33. data/vendor/libgit2/src/diff_print.c +259 -142
  34. data/vendor/libgit2/src/diff_stats.c +3 -2
  35. data/vendor/libgit2/src/diff_tform.c +1 -0
  36. data/vendor/libgit2/src/diff_tform.h +22 -0
  37. data/vendor/libgit2/src/diff_xdiff.c +9 -9
  38. data/vendor/libgit2/src/diff_xdiff.h +5 -5
  39. data/vendor/libgit2/src/fileops.c +13 -0
  40. data/vendor/libgit2/src/fileops.h +6 -0
  41. data/vendor/libgit2/src/global.c +9 -8
  42. data/vendor/libgit2/src/ignore.c +45 -16
  43. data/vendor/libgit2/src/index.c +161 -47
  44. data/vendor/libgit2/src/index.h +2 -0
  45. data/vendor/libgit2/src/merge.c +2 -0
  46. data/vendor/libgit2/src/mwindow.c +8 -19
  47. data/vendor/libgit2/src/mwindow.h +1 -2
  48. data/vendor/libgit2/src/odb.c +46 -10
  49. data/vendor/libgit2/src/odb_loose.c +19 -1
  50. data/vendor/libgit2/src/odb_pack.c +27 -4
  51. data/vendor/libgit2/src/pack-objects.c +106 -77
  52. data/vendor/libgit2/src/pack-objects.h +13 -12
  53. data/vendor/libgit2/src/pack.c +4 -3
  54. data/vendor/libgit2/src/pack.h +2 -0
  55. data/vendor/libgit2/src/patch.c +207 -0
  56. data/vendor/libgit2/src/patch.h +66 -0
  57. data/vendor/libgit2/src/{diff_patch.c → patch_generate.c} +171 -359
  58. data/vendor/libgit2/src/patch_generate.h +66 -0
  59. data/vendor/libgit2/src/patch_parse.c +1121 -0
  60. data/vendor/libgit2/src/patch_parse.h +54 -0
  61. data/vendor/libgit2/src/path.c +19 -0
  62. data/vendor/libgit2/src/path.h +6 -0
  63. data/vendor/libgit2/src/pool.h +5 -0
  64. data/vendor/libgit2/src/remote.c +15 -80
  65. data/vendor/libgit2/src/repository.c +227 -39
  66. data/vendor/libgit2/src/settings.c +11 -5
  67. data/vendor/libgit2/src/stash.c +1 -0
  68. data/vendor/libgit2/src/status.c +1 -0
  69. data/vendor/libgit2/src/stransport_stream.c +14 -9
  70. data/vendor/libgit2/src/submodule.c +16 -4
  71. data/vendor/libgit2/src/sysdir.c +41 -47
  72. data/vendor/libgit2/src/sysdir.h +0 -5
  73. data/vendor/libgit2/src/thread-utils.h +5 -51
  74. data/vendor/libgit2/src/transport.c +3 -5
  75. data/vendor/libgit2/src/transports/http.c +2 -3
  76. data/vendor/libgit2/src/transports/smart_pkt.c +1 -0
  77. data/vendor/libgit2/src/transports/smart_protocol.c +11 -0
  78. data/vendor/libgit2/src/transports/winhttp.c +16 -2
  79. data/vendor/libgit2/src/unix/pthread.h +54 -0
  80. data/vendor/libgit2/src/util.c +23 -5
  81. data/vendor/libgit2/src/util.h +10 -0
  82. data/vendor/libgit2/src/varint.c +44 -0
  83. data/vendor/libgit2/src/varint.h +15 -0
  84. data/vendor/libgit2/src/vector.c +42 -0
  85. data/vendor/libgit2/src/vector.h +3 -0
  86. data/vendor/libgit2/src/win32/precompiled.h +1 -1
  87. data/vendor/libgit2/src/win32/{pthread.c → thread.c} +50 -80
  88. data/vendor/libgit2/src/win32/thread.h +62 -0
  89. data/vendor/libgit2/src/zstream.c +36 -7
  90. data/vendor/libgit2/src/zstream.h +8 -1
  91. metadata +20 -9
  92. data/vendor/libgit2/src/delta-apply.c +0 -166
  93. data/vendor/libgit2/src/delta-apply.h +0 -62
  94. data/vendor/libgit2/src/diff_patch.h +0 -83
  95. data/vendor/libgit2/src/win32/pthread.h +0 -92
@@ -22,67 +22,30 @@
22
22
  #define DIFF_OLD_PREFIX_DEFAULT "a/"
23
23
  #define DIFF_NEW_PREFIX_DEFAULT "b/"
24
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
- };
32
-
33
- #define DIFF_FLAGS_KNOWN_BINARY (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
34
- #define DIFF_FLAGS_NOT_BINARY (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
35
-
36
- enum {
37
- GIT_DIFF_FLAG__FREE_PATH = (1 << 7), /* `path` is allocated memory */
38
- GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */
39
- GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */
40
- GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */
41
- GIT_DIFF_FLAG__FREE_BLOB = (1 << 11), /* release the blob when done */
42
- GIT_DIFF_FLAG__LOADED = (1 << 12), /* file data has been loaded */
43
-
44
- GIT_DIFF_FLAG__TO_DELETE = (1 << 16), /* delete entry during rename det. */
45
- GIT_DIFF_FLAG__TO_SPLIT = (1 << 17), /* split entry during rename det. */
46
- GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18),
47
- GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19),
48
- GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20),
49
- };
50
-
51
- #define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF)
52
-
53
- #define GIT_DIFF__VERBOSE (1 << 30)
25
+ typedef enum {
26
+ GIT_DIFF_TYPE_UNKNOWN = 0,
27
+ GIT_DIFF_TYPE_GENERATED = 1,
28
+ GIT_DIFF_TYPE_PARSED = 2,
29
+ } git_diff_origin_t;
54
30
 
55
31
  struct git_diff {
56
32
  git_refcount rc;
57
33
  git_repository *repo;
34
+ git_diff_origin_t type;
58
35
  git_diff_options opts;
59
- git_vector pathspec;
60
36
  git_vector deltas; /* vector of git_diff_delta */
61
37
  git_pool pool;
62
38
  git_iterator_type_t old_src;
63
39
  git_iterator_type_t new_src;
64
- uint32_t diffcaps;
65
40
  git_diff_perfdata perf;
66
- bool index_updated;
67
41
 
68
42
  int (*strcomp)(const char *, const char *);
69
43
  int (*strncomp)(const char *, const char *, size_t);
70
44
  int (*pfxcomp)(const char *str, const char *pfx);
71
45
  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
46
 
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);
47
+ void (*free_fn)(git_diff *diff);
48
+ };
86
49
 
87
50
  extern int git_diff_delta__format_file_header(
88
51
  git_buf *out,
@@ -91,84 +54,11 @@ extern int git_diff_delta__format_file_header(
91
54
  const char *newpfx,
92
55
  int oid_strlen);
93
56
 
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
- extern git_diff_delta *git_diff__merge_like_cgit(
137
- const git_diff_delta *a,
138
- const git_diff_delta *b,
139
- git_pool *pool);
140
-
141
- /* Duplicate a `git_diff_delta` out of the `git_pool` */
142
- extern git_diff_delta *git_diff__delta_dup(
143
- const git_diff_delta *d, git_pool *pool);
144
-
145
- /*
146
- * Sometimes a git_diff_file will have a zero size; this attempts to
147
- * fill in the size without loading the blob if possible. If that is
148
- * not possible, then it will return the git_odb_object that had to be
149
- * loaded and the caller can use it or dispose of it as needed.
150
- */
151
- GIT_INLINE(int) git_diff_file__resolve_zero_size(
152
- git_diff_file *file, git_odb_object **odb_obj, git_repository *repo)
153
- {
154
- int error;
155
- git_odb *odb;
156
- size_t len;
157
- git_otype type;
158
-
159
- if ((error = git_repository_odb(&odb, repo)) < 0)
160
- return error;
161
-
162
- error = git_odb__read_header_or_object(
163
- odb_obj, &len, &type, odb, &file->id);
164
-
165
- git_odb_free(odb);
166
-
167
- if (!error)
168
- file->size = (git_off_t)len;
57
+ extern int git_diff_delta__cmp(const void *a, const void *b);
58
+ extern int git_diff_delta__casecmp(const void *a, const void *b);
169
59
 
170
- return error;
171
- }
60
+ extern int git_diff__entry_cmp(const void *a, const void *b);
61
+ extern int git_diff__entry_icmp(const void *a, const void *b);
172
62
 
173
63
  #endif
174
64
 
@@ -9,7 +9,6 @@
9
9
  #include "git2/attr.h"
10
10
 
11
11
  #include "diff.h"
12
- #include "diff_patch.h"
13
12
  #include "diff_driver.h"
14
13
  #include "strmap.h"
15
14
  #include "map.h"
@@ -59,7 +58,7 @@ static git_diff_driver global_drivers[3] = {
59
58
  { DIFF_DRIVER_TEXT, GIT_DIFF_FORCE_TEXT, 0 },
60
59
  };
61
60
 
62
- git_diff_driver_registry *git_diff_driver_registry_new()
61
+ git_diff_driver_registry *git_diff_driver_registry_new(void)
63
62
  {
64
63
  git_diff_driver_registry *reg =
65
64
  git__calloc(1, sizeof(git_diff_driver_registry));
@@ -8,6 +8,7 @@
8
8
  #include "git2/blob.h"
9
9
  #include "git2/submodule.h"
10
10
  #include "diff.h"
11
+ #include "diff_generate.h"
11
12
  #include "diff_file.h"
12
13
  #include "odb.h"
13
14
  #include "fileops.h"
@@ -149,12 +150,14 @@ int git_diff_file_content__init_from_src(
149
150
  if (src->blob) {
150
151
  fc->file->size = git_blob_rawsize(src->blob);
151
152
  git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
153
+ fc->file->id_abbrev = GIT_OID_HEXSZ;
152
154
 
153
155
  fc->map.len = (size_t)fc->file->size;
154
156
  fc->map.data = (char *)git_blob_rawcontent(src->blob);
155
157
  } else {
156
158
  fc->file->size = src->buflen;
157
159
  git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
160
+ fc->file->id_abbrev = GIT_OID_HEXSZ;
158
161
 
159
162
  fc->map.len = src->buflen;
160
163
  fc->map.data = (char *)src->buf;
@@ -0,0 +1,1611 @@
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
+ #include "common.h"
8
+ #include "diff.h"
9
+ #include "diff_generate.h"
10
+ #include "fileops.h"
11
+ #include "config.h"
12
+ #include "attr_file.h"
13
+ #include "filter.h"
14
+ #include "pathspec.h"
15
+ #include "index.h"
16
+ #include "odb.h"
17
+ #include "submodule.h"
18
+
19
+ #define DIFF_FLAG_IS_SET(DIFF,FLAG) \
20
+ (((DIFF)->base.opts.flags & (FLAG)) != 0)
21
+ #define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
22
+ (((DIFF)->base.opts.flags & (FLAG)) == 0)
23
+ #define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->base.opts.flags = \
24
+ (VAL) ? ((DIFF)->base.opts.flags | (FLAG)) : \
25
+ ((DIFF)->base.opts.flags & ~(VAL))
26
+
27
+ typedef struct {
28
+ struct git_diff base;
29
+
30
+ git_vector pathspec;
31
+
32
+ uint32_t diffcaps;
33
+ bool index_updated;
34
+ } git_diff_generated;
35
+
36
+ static git_diff_delta *diff_delta__alloc(
37
+ git_diff_generated *diff,
38
+ git_delta_t status,
39
+ const char *path)
40
+ {
41
+ git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
42
+ if (!delta)
43
+ return NULL;
44
+
45
+ delta->old_file.path = git_pool_strdup(&diff->base.pool, path);
46
+ if (delta->old_file.path == NULL) {
47
+ git__free(delta);
48
+ return NULL;
49
+ }
50
+
51
+ delta->new_file.path = delta->old_file.path;
52
+
53
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
54
+ switch (status) {
55
+ case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
56
+ case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
57
+ default: break; /* leave other status values alone */
58
+ }
59
+ }
60
+ delta->status = status;
61
+
62
+ return delta;
63
+ }
64
+
65
+ static int diff_insert_delta(
66
+ git_diff_generated *diff,
67
+ git_diff_delta *delta,
68
+ const char *matched_pathspec)
69
+ {
70
+ int error = 0;
71
+
72
+ if (diff->base.opts.notify_cb) {
73
+ error = diff->base.opts.notify_cb(
74
+ &diff->base, delta, matched_pathspec, diff->base.opts.payload);
75
+
76
+ if (error) {
77
+ git__free(delta);
78
+
79
+ if (error > 0) /* positive value means to skip this delta */
80
+ return 0;
81
+ else /* negative value means to cancel diff */
82
+ return giterr_set_after_callback_function(error, "git_diff");
83
+ }
84
+ }
85
+
86
+ if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0)
87
+ git__free(delta);
88
+
89
+ return error;
90
+ }
91
+
92
+ static bool diff_pathspec_match(
93
+ const char **matched_pathspec,
94
+ git_diff_generated *diff,
95
+ const git_index_entry *entry)
96
+ {
97
+ bool disable_pathspec_match =
98
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
99
+
100
+ /* If we're disabling fnmatch, then the iterator has already applied
101
+ * the filters to the files for us and we don't have to do anything.
102
+ * However, this only applies to *files* - the iterator will include
103
+ * directories that we need to recurse into when not autoexpanding,
104
+ * so we still need to apply the pathspec match to directories.
105
+ */
106
+ if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
107
+ disable_pathspec_match) {
108
+ *matched_pathspec = entry->path;
109
+ return true;
110
+ }
111
+
112
+ return git_pathspec__match(
113
+ &diff->pathspec, entry->path, disable_pathspec_match,
114
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
115
+ matched_pathspec, NULL);
116
+ }
117
+
118
+ static int diff_delta__from_one(
119
+ git_diff_generated *diff,
120
+ git_delta_t status,
121
+ const git_index_entry *oitem,
122
+ const git_index_entry *nitem)
123
+ {
124
+ const git_index_entry *entry = nitem;
125
+ bool has_old = false;
126
+ git_diff_delta *delta;
127
+ const char *matched_pathspec;
128
+
129
+ assert((oitem != NULL) ^ (nitem != NULL));
130
+
131
+ if (oitem) {
132
+ entry = oitem;
133
+ has_old = true;
134
+ }
135
+
136
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
137
+ has_old = !has_old;
138
+
139
+ if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
140
+ return 0;
141
+
142
+ if (status == GIT_DELTA_IGNORED &&
143
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
144
+ return 0;
145
+
146
+ if (status == GIT_DELTA_UNTRACKED &&
147
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
148
+ return 0;
149
+
150
+ if (status == GIT_DELTA_UNREADABLE &&
151
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
152
+ return 0;
153
+
154
+ if (!diff_pathspec_match(&matched_pathspec, diff, entry))
155
+ return 0;
156
+
157
+ delta = diff_delta__alloc(diff, status, entry->path);
158
+ GITERR_CHECK_ALLOC(delta);
159
+
160
+ /* This fn is just for single-sided diffs */
161
+ assert(status != GIT_DELTA_MODIFIED);
162
+ delta->nfiles = 1;
163
+
164
+ if (has_old) {
165
+ delta->old_file.mode = entry->mode;
166
+ delta->old_file.size = entry->file_size;
167
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
168
+ git_oid_cpy(&delta->old_file.id, &entry->id);
169
+ delta->old_file.id_abbrev = GIT_OID_HEXSZ;
170
+ } else /* ADDED, IGNORED, UNTRACKED */ {
171
+ delta->new_file.mode = entry->mode;
172
+ delta->new_file.size = entry->file_size;
173
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
174
+ git_oid_cpy(&delta->new_file.id, &entry->id);
175
+ delta->new_file.id_abbrev = GIT_OID_HEXSZ;
176
+ }
177
+
178
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
179
+
180
+ if (has_old || !git_oid_iszero(&delta->new_file.id))
181
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
182
+
183
+ return diff_insert_delta(diff, delta, matched_pathspec);
184
+ }
185
+
186
+ static int diff_delta__from_two(
187
+ git_diff_generated *diff,
188
+ git_delta_t status,
189
+ const git_index_entry *old_entry,
190
+ uint32_t old_mode,
191
+ const git_index_entry *new_entry,
192
+ uint32_t new_mode,
193
+ const git_oid *new_id,
194
+ const char *matched_pathspec)
195
+ {
196
+ const git_oid *old_id = &old_entry->id;
197
+ git_diff_delta *delta;
198
+ const char *canonical_path = old_entry->path;
199
+
200
+ if (status == GIT_DELTA_UNMODIFIED &&
201
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
202
+ return 0;
203
+
204
+ if (!new_id)
205
+ new_id = &new_entry->id;
206
+
207
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
208
+ uint32_t temp_mode = old_mode;
209
+ const git_index_entry *temp_entry = old_entry;
210
+ const git_oid *temp_id = old_id;
211
+
212
+ old_entry = new_entry;
213
+ new_entry = temp_entry;
214
+ old_mode = new_mode;
215
+ new_mode = temp_mode;
216
+ old_id = new_id;
217
+ new_id = temp_id;
218
+ }
219
+
220
+ delta = diff_delta__alloc(diff, status, canonical_path);
221
+ GITERR_CHECK_ALLOC(delta);
222
+ delta->nfiles = 2;
223
+
224
+ if (!git_index_entry_is_conflict(old_entry)) {
225
+ delta->old_file.size = old_entry->file_size;
226
+ delta->old_file.mode = old_mode;
227
+ git_oid_cpy(&delta->old_file.id, old_id);
228
+ delta->old_file.id_abbrev = GIT_OID_HEXSZ;
229
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
230
+ GIT_DIFF_FLAG_EXISTS;
231
+ }
232
+
233
+ if (!git_index_entry_is_conflict(new_entry)) {
234
+ git_oid_cpy(&delta->new_file.id, new_id);
235
+ delta->new_file.id_abbrev = GIT_OID_HEXSZ;
236
+ delta->new_file.size = new_entry->file_size;
237
+ delta->new_file.mode = new_mode;
238
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
239
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
240
+
241
+ if (!git_oid_iszero(&new_entry->id))
242
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
243
+ }
244
+
245
+ return diff_insert_delta(diff, delta, matched_pathspec);
246
+ }
247
+
248
+ static git_diff_delta *diff_delta__last_for_item(
249
+ git_diff_generated *diff,
250
+ const git_index_entry *item)
251
+ {
252
+ git_diff_delta *delta = git_vector_last(&diff->base.deltas);
253
+ if (!delta)
254
+ return NULL;
255
+
256
+ switch (delta->status) {
257
+ case GIT_DELTA_UNMODIFIED:
258
+ case GIT_DELTA_DELETED:
259
+ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
260
+ return delta;
261
+ break;
262
+ case GIT_DELTA_ADDED:
263
+ if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
264
+ return delta;
265
+ break;
266
+ case GIT_DELTA_UNREADABLE:
267
+ case GIT_DELTA_UNTRACKED:
268
+ if (diff->base.strcomp(delta->new_file.path, item->path) == 0 &&
269
+ git_oid__cmp(&delta->new_file.id, &item->id) == 0)
270
+ return delta;
271
+ break;
272
+ case GIT_DELTA_MODIFIED:
273
+ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
274
+ git_oid__cmp(&delta->new_file.id, &item->id) == 0)
275
+ return delta;
276
+ break;
277
+ default:
278
+ break;
279
+ }
280
+
281
+ return NULL;
282
+ }
283
+
284
+ static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
285
+ {
286
+ size_t len = strlen(prefix);
287
+
288
+ /* append '/' at end if needed */
289
+ if (len > 0 && prefix[len - 1] != '/')
290
+ return git_pool_strcat(pool, prefix, "/");
291
+ else
292
+ return git_pool_strndup(pool, prefix, len + 1);
293
+ }
294
+
295
+ GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
296
+ {
297
+ return delta->old_file.path ?
298
+ delta->old_file.path : delta->new_file.path;
299
+ }
300
+
301
+ int git_diff_delta__i2w_cmp(const void *a, const void *b)
302
+ {
303
+ const git_diff_delta *da = a, *db = b;
304
+ int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
305
+ return val ? val : ((int)da->status - (int)db->status);
306
+ }
307
+
308
+ int git_diff_delta__i2w_casecmp(const void *a, const void *b)
309
+ {
310
+ const git_diff_delta *da = a, *db = b;
311
+ int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
312
+ return val ? val : ((int)da->status - (int)db->status);
313
+ }
314
+
315
+ bool git_diff_delta__should_skip(
316
+ const git_diff_options *opts, const git_diff_delta *delta)
317
+ {
318
+ uint32_t flags = opts ? opts->flags : 0;
319
+
320
+ if (delta->status == GIT_DELTA_UNMODIFIED &&
321
+ (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
322
+ return true;
323
+
324
+ if (delta->status == GIT_DELTA_IGNORED &&
325
+ (flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
326
+ return true;
327
+
328
+ if (delta->status == GIT_DELTA_UNTRACKED &&
329
+ (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
330
+ return true;
331
+
332
+ if (delta->status == GIT_DELTA_UNREADABLE &&
333
+ (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
334
+ return true;
335
+
336
+ return false;
337
+ }
338
+
339
+
340
+ static const char *diff_mnemonic_prefix(
341
+ git_iterator_type_t type, bool left_side)
342
+ {
343
+ const char *pfx = "";
344
+
345
+ switch (type) {
346
+ case GIT_ITERATOR_TYPE_EMPTY: pfx = "c"; break;
347
+ case GIT_ITERATOR_TYPE_TREE: pfx = "c"; break;
348
+ case GIT_ITERATOR_TYPE_INDEX: pfx = "i"; break;
349
+ case GIT_ITERATOR_TYPE_WORKDIR: pfx = "w"; break;
350
+ case GIT_ITERATOR_TYPE_FS: pfx = left_side ? "1" : "2"; break;
351
+ default: break;
352
+ }
353
+
354
+ /* note: without a deeper look at pathspecs, there is no easy way
355
+ * to get the (o)bject / (w)ork tree mnemonics working...
356
+ */
357
+
358
+ return pfx;
359
+ }
360
+
361
+ void git_diff__set_ignore_case(git_diff *diff, bool ignore_case)
362
+ {
363
+ if (!ignore_case) {
364
+ diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
365
+
366
+ diff->strcomp = git__strcmp;
367
+ diff->strncomp = git__strncmp;
368
+ diff->pfxcomp = git__prefixcmp;
369
+ diff->entrycomp = git_diff__entry_cmp;
370
+
371
+ git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
372
+ } else {
373
+ diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
374
+
375
+ diff->strcomp = git__strcasecmp;
376
+ diff->strncomp = git__strncasecmp;
377
+ diff->pfxcomp = git__prefixcmp_icase;
378
+ diff->entrycomp = git_diff__entry_icmp;
379
+
380
+ git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
381
+ }
382
+
383
+ git_vector_sort(&diff->deltas);
384
+ }
385
+
386
+ static void diff_generated_free(git_diff *d)
387
+ {
388
+ git_diff_generated *diff = (git_diff_generated *)d;
389
+
390
+ git_vector_free_deep(&diff->base.deltas);
391
+
392
+ git_pathspec__vfree(&diff->pathspec);
393
+ git_pool_clear(&diff->base.pool);
394
+
395
+ git__memzero(diff, sizeof(*diff));
396
+ git__free(diff);
397
+ }
398
+
399
+ static git_diff_generated *diff_generated_alloc(
400
+ git_repository *repo,
401
+ git_iterator *old_iter,
402
+ git_iterator *new_iter)
403
+ {
404
+ git_diff_generated *diff;
405
+ git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
406
+
407
+ assert(repo && old_iter && new_iter);
408
+
409
+ if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL)
410
+ return NULL;
411
+
412
+ GIT_REFCOUNT_INC(diff);
413
+ diff->base.type = GIT_DIFF_TYPE_GENERATED;
414
+ diff->base.repo = repo;
415
+ diff->base.old_src = old_iter->type;
416
+ diff->base.new_src = new_iter->type;
417
+ diff->base.free_fn = diff_generated_free;
418
+ memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
419
+
420
+ git_pool_init(&diff->base.pool, 1);
421
+
422
+ if (git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) {
423
+ git_diff_free(&diff->base);
424
+ return NULL;
425
+ }
426
+
427
+ /* Use case-insensitive compare if either iterator has
428
+ * the ignore_case bit set */
429
+ git_diff__set_ignore_case(
430
+ &diff->base,
431
+ git_iterator_ignore_case(old_iter) ||
432
+ git_iterator_ignore_case(new_iter));
433
+
434
+ return diff;
435
+ }
436
+
437
+ static int diff_generated_apply_options(
438
+ git_diff_generated *diff,
439
+ const git_diff_options *opts)
440
+ {
441
+ git_config *cfg = NULL;
442
+ git_repository *repo = diff->base.repo;
443
+ git_pool *pool = &diff->base.pool;
444
+ int val;
445
+
446
+ if (opts) {
447
+ /* copy user options (except case sensitivity info from iterators) */
448
+ bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
449
+ memcpy(&diff->base.opts, opts, sizeof(diff->base.opts));
450
+ DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
451
+
452
+ /* initialize pathspec from options */
453
+ if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
454
+ return -1;
455
+ }
456
+
457
+ /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
458
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
459
+ diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
460
+
461
+ /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
462
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
463
+ diff->base.opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
464
+
465
+ /* load config values that affect diff behavior */
466
+ if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
467
+ return val;
468
+
469
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val)
470
+ diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS;
471
+
472
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val)
473
+ diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT;
474
+
475
+ if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
476
+ !git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val)
477
+ diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS;
478
+
479
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val)
480
+ diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME;
481
+
482
+ /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
483
+
484
+ /* If not given explicit `opts`, check `diff.xyz` configs */
485
+ if (!opts) {
486
+ int context = git_config__get_int_force(cfg, "diff.context", 3);
487
+ diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3;
488
+
489
+ /* add other defaults here */
490
+ }
491
+
492
+ /* Reverse src info if diff is reversed */
493
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
494
+ git_iterator_type_t tmp_src = diff->base.old_src;
495
+ diff->base.old_src = diff->base.new_src;
496
+ diff->base.new_src = tmp_src;
497
+ }
498
+
499
+ /* Unset UPDATE_INDEX unless diffing workdir and index */
500
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
501
+ (!(diff->base.old_src == GIT_ITERATOR_TYPE_WORKDIR ||
502
+ diff->base.new_src == GIT_ITERATOR_TYPE_WORKDIR) ||
503
+ !(diff->base.old_src == GIT_ITERATOR_TYPE_INDEX ||
504
+ diff->base.new_src == GIT_ITERATOR_TYPE_INDEX)))
505
+ diff->base.opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
506
+
507
+ /* if ignore_submodules not explicitly set, check diff config */
508
+ if (diff->base.opts.ignore_submodules <= 0) {
509
+ git_config_entry *entry;
510
+ git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
511
+
512
+ if (entry && git_submodule_parse_ignore(
513
+ &diff->base.opts.ignore_submodules, entry->value) < 0)
514
+ giterr_clear();
515
+ git_config_entry_free(entry);
516
+ }
517
+
518
+ /* if either prefix is not set, figure out appropriate value */
519
+ if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) {
520
+ const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
521
+ const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
522
+
523
+ if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
524
+ use_old = use_new = "";
525
+ else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
526
+ use_old = diff_mnemonic_prefix(diff->base.old_src, true);
527
+ use_new = diff_mnemonic_prefix(diff->base.new_src, false);
528
+ }
529
+
530
+ if (!diff->base.opts.old_prefix)
531
+ diff->base.opts.old_prefix = use_old;
532
+ if (!diff->base.opts.new_prefix)
533
+ diff->base.opts.new_prefix = use_new;
534
+ }
535
+
536
+ /* strdup prefix from pool so we're not dependent on external data */
537
+ diff->base.opts.old_prefix = diff_strdup_prefix(pool, diff->base.opts.old_prefix);
538
+ diff->base.opts.new_prefix = diff_strdup_prefix(pool, diff->base.opts.new_prefix);
539
+
540
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
541
+ const char *tmp_prefix = diff->base.opts.old_prefix;
542
+ diff->base.opts.old_prefix = diff->base.opts.new_prefix;
543
+ diff->base.opts.new_prefix = tmp_prefix;
544
+ }
545
+
546
+ git_config_free(cfg);
547
+
548
+ /* check strdup results for error */
549
+ return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0;
550
+ }
551
+
552
+ int git_diff__oid_for_file(
553
+ git_oid *out,
554
+ git_diff *diff,
555
+ const char *path,
556
+ uint16_t mode,
557
+ git_off_t size)
558
+ {
559
+ git_index_entry entry;
560
+
561
+ memset(&entry, 0, sizeof(entry));
562
+ entry.mode = mode;
563
+ entry.file_size = size;
564
+ entry.path = (char *)path;
565
+
566
+ return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
567
+ }
568
+
569
+ int git_diff__oid_for_entry(
570
+ git_oid *out,
571
+ git_diff *d,
572
+ const git_index_entry *src,
573
+ uint16_t mode,
574
+ const git_oid *update_match)
575
+ {
576
+ git_diff_generated *diff;
577
+ git_buf full_path = GIT_BUF_INIT;
578
+ git_index_entry entry = *src;
579
+ git_filter_list *fl = NULL;
580
+ int error = 0;
581
+
582
+ assert(d->type == GIT_DIFF_TYPE_GENERATED);
583
+ diff = (git_diff_generated *)d;
584
+
585
+ memset(out, 0, sizeof(*out));
586
+
587
+ if (git_buf_joinpath(&full_path,
588
+ git_repository_workdir(diff->base.repo), entry.path) < 0)
589
+ return -1;
590
+
591
+ if (!mode) {
592
+ struct stat st;
593
+
594
+ diff->base.perf.stat_calls++;
595
+
596
+ if (p_stat(full_path.ptr, &st) < 0) {
597
+ error = git_path_set_error(errno, entry.path, "stat");
598
+ git_buf_free(&full_path);
599
+ return error;
600
+ }
601
+
602
+ git_index_entry__init_from_stat(&entry,
603
+ &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
604
+ }
605
+
606
+ /* calculate OID for file if possible */
607
+ if (S_ISGITLINK(mode)) {
608
+ git_submodule *sm;
609
+
610
+ if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) {
611
+ const git_oid *sm_oid = git_submodule_wd_id(sm);
612
+ if (sm_oid)
613
+ git_oid_cpy(out, sm_oid);
614
+ git_submodule_free(sm);
615
+ } else {
616
+ /* if submodule lookup failed probably just in an intermediate
617
+ * state where some init hasn't happened, so ignore the error
618
+ */
619
+ giterr_clear();
620
+ }
621
+ } else if (S_ISLNK(mode)) {
622
+ error = git_odb__hashlink(out, full_path.ptr);
623
+ diff->base.perf.oid_calculations++;
624
+ } else if (!git__is_sizet(entry.file_size)) {
625
+ giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'",
626
+ entry.path);
627
+ error = -1;
628
+ } else if (!(error = git_filter_list_load(&fl,
629
+ diff->base.repo, NULL, entry.path,
630
+ GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
631
+ {
632
+ int fd = git_futils_open_ro(full_path.ptr);
633
+ if (fd < 0)
634
+ error = fd;
635
+ else {
636
+ error = git_odb__hashfd_filtered(
637
+ out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
638
+ p_close(fd);
639
+ diff->base.perf.oid_calculations++;
640
+ }
641
+
642
+ git_filter_list_free(fl);
643
+ }
644
+
645
+ /* update index for entry if requested */
646
+ if (!error && update_match && git_oid_equal(out, update_match)) {
647
+ git_index *idx;
648
+ git_index_entry updated_entry;
649
+
650
+ memcpy(&updated_entry, &entry, sizeof(git_index_entry));
651
+ updated_entry.mode = mode;
652
+ git_oid_cpy(&updated_entry.id, out);
653
+
654
+ if (!(error = git_repository_index__weakptr(&idx,
655
+ diff->base.repo))) {
656
+ error = git_index_add(idx, &updated_entry);
657
+ diff->index_updated = true;
658
+ }
659
+ }
660
+
661
+ git_buf_free(&full_path);
662
+ return error;
663
+ }
664
+
665
+ typedef struct {
666
+ git_repository *repo;
667
+ git_iterator *old_iter;
668
+ git_iterator *new_iter;
669
+ const git_index_entry *oitem;
670
+ const git_index_entry *nitem;
671
+ } diff_in_progress;
672
+
673
+ #define MODE_BITS_MASK 0000777
674
+
675
+ static int maybe_modified_submodule(
676
+ git_delta_t *status,
677
+ git_oid *found_oid,
678
+ git_diff_generated *diff,
679
+ diff_in_progress *info)
680
+ {
681
+ int error = 0;
682
+ git_submodule *sub;
683
+ unsigned int sm_status = 0;
684
+ git_submodule_ignore_t ign = diff->base.opts.ignore_submodules;
685
+
686
+ *status = GIT_DELTA_UNMODIFIED;
687
+
688
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
689
+ ign == GIT_SUBMODULE_IGNORE_ALL)
690
+ return 0;
691
+
692
+ if ((error = git_submodule_lookup(
693
+ &sub, diff->base.repo, info->nitem->path)) < 0) {
694
+
695
+ /* GIT_EEXISTS means dir with .git in it was found - ignore it */
696
+ if (error == GIT_EEXISTS) {
697
+ giterr_clear();
698
+ error = 0;
699
+ }
700
+ return error;
701
+ }
702
+
703
+ if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
704
+ /* ignore it */;
705
+ else if ((error = git_submodule__status(
706
+ &sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
707
+ /* return error below */;
708
+
709
+ /* check IS_WD_UNMODIFIED because this case is only used
710
+ * when the new side of the diff is the working directory
711
+ */
712
+ else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
713
+ *status = GIT_DELTA_MODIFIED;
714
+
715
+ /* now that we have a HEAD OID, check if HEAD moved */
716
+ else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
717
+ !git_oid_equal(&info->oitem->id, found_oid))
718
+ *status = GIT_DELTA_MODIFIED;
719
+
720
+ git_submodule_free(sub);
721
+ return error;
722
+ }
723
+
724
+ static int maybe_modified(
725
+ git_diff_generated *diff,
726
+ diff_in_progress *info)
727
+ {
728
+ git_oid noid;
729
+ git_delta_t status = GIT_DELTA_MODIFIED;
730
+ const git_index_entry *oitem = info->oitem;
731
+ const git_index_entry *nitem = info->nitem;
732
+ unsigned int omode = oitem->mode;
733
+ unsigned int nmode = nitem->mode;
734
+ bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
735
+ bool modified_uncertain = false;
736
+ const char *matched_pathspec;
737
+ int error = 0;
738
+
739
+ if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
740
+ return 0;
741
+
742
+ memset(&noid, 0, sizeof(noid));
743
+
744
+ /* on platforms with no symlinks, preserve mode of existing symlinks */
745
+ if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
746
+ !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
747
+ nmode = omode;
748
+
749
+ /* on platforms with no execmode, just preserve old mode */
750
+ if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
751
+ (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
752
+ new_is_workdir)
753
+ nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
754
+
755
+ /* if one side is a conflict, mark the whole delta as conflicted */
756
+ if (git_index_entry_is_conflict(oitem) ||
757
+ git_index_entry_is_conflict(nitem)) {
758
+ status = GIT_DELTA_CONFLICTED;
759
+
760
+ /* support "assume unchanged" (poorly, b/c we still stat everything) */
761
+ } else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
762
+ status = GIT_DELTA_UNMODIFIED;
763
+
764
+ /* support "skip worktree" index bit */
765
+ } else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
766
+ status = GIT_DELTA_UNMODIFIED;
767
+
768
+ /* if basic type of file changed, then split into delete and add */
769
+ } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
770
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
771
+ status = GIT_DELTA_TYPECHANGE;
772
+ }
773
+
774
+ else if (nmode == GIT_FILEMODE_UNREADABLE) {
775
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
776
+ error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
777
+ return error;
778
+ }
779
+
780
+ else {
781
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
782
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
783
+ return error;
784
+ }
785
+
786
+ /* if oids and modes match (and are valid), then file is unmodified */
787
+ } else if (git_oid_equal(&oitem->id, &nitem->id) &&
788
+ omode == nmode &&
789
+ !git_oid_iszero(&oitem->id)) {
790
+ status = GIT_DELTA_UNMODIFIED;
791
+
792
+ /* if we have an unknown OID and a workdir iterator, then check some
793
+ * circumstances that can accelerate things or need special handling
794
+ */
795
+ } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
796
+ bool use_ctime =
797
+ ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
798
+ git_index *index = git_iterator_index(info->new_iter);
799
+
800
+ status = GIT_DELTA_UNMODIFIED;
801
+
802
+ if (S_ISGITLINK(nmode)) {
803
+ if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
804
+ return error;
805
+ }
806
+
807
+ /* if the stat data looks different, then mark modified - this just
808
+ * means that the OID will be recalculated below to confirm change
809
+ */
810
+ else if (omode != nmode || oitem->file_size != nitem->file_size) {
811
+ status = GIT_DELTA_MODIFIED;
812
+ modified_uncertain =
813
+ (oitem->file_size <= 0 && nitem->file_size > 0);
814
+ }
815
+ else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) ||
816
+ (use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) ||
817
+ oitem->ino != nitem->ino ||
818
+ oitem->uid != nitem->uid ||
819
+ oitem->gid != nitem->gid ||
820
+ git_index_entry_newer_than_index(nitem, index))
821
+ {
822
+ status = GIT_DELTA_MODIFIED;
823
+ modified_uncertain = true;
824
+ }
825
+
826
+ /* if mode is GITLINK and submodules are ignored, then skip */
827
+ } else if (S_ISGITLINK(nmode) &&
828
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
829
+ status = GIT_DELTA_UNMODIFIED;
830
+ }
831
+
832
+ /* if we got here and decided that the files are modified, but we
833
+ * haven't calculated the OID of the new item, then calculate it now
834
+ */
835
+ if (modified_uncertain && git_oid_iszero(&nitem->id)) {
836
+ const git_oid *update_check =
837
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
838
+ &oitem->id : NULL;
839
+
840
+ if ((error = git_diff__oid_for_entry(
841
+ &noid, &diff->base, nitem, nmode, update_check)) < 0)
842
+ return error;
843
+
844
+ /* if oid matches, then mark unmodified (except submodules, where
845
+ * the filesystem content may be modified even if the oid still
846
+ * matches between the index and the workdir HEAD)
847
+ */
848
+ if (omode == nmode && !S_ISGITLINK(omode) &&
849
+ git_oid_equal(&oitem->id, &noid))
850
+ status = GIT_DELTA_UNMODIFIED;
851
+ }
852
+
853
+ /* If we want case changes, then break this into a delete of the old
854
+ * and an add of the new so that consumers can act accordingly (eg,
855
+ * checkout will update the case on disk.)
856
+ */
857
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
858
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
859
+ strcmp(oitem->path, nitem->path) != 0) {
860
+
861
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
862
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
863
+
864
+ return error;
865
+ }
866
+
867
+ return diff_delta__from_two(
868
+ diff, status, oitem, omode, nitem, nmode,
869
+ git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec);
870
+ }
871
+
872
+ static bool entry_is_prefixed(
873
+ git_diff_generated *diff,
874
+ const git_index_entry *item,
875
+ const git_index_entry *prefix_item)
876
+ {
877
+ size_t pathlen;
878
+
879
+ if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0)
880
+ return false;
881
+
882
+ pathlen = strlen(prefix_item->path);
883
+
884
+ return (prefix_item->path[pathlen - 1] == '/' ||
885
+ item->path[pathlen] == '\0' ||
886
+ item->path[pathlen] == '/');
887
+ }
888
+
889
+ static int iterator_current(
890
+ const git_index_entry **entry,
891
+ git_iterator *iterator)
892
+ {
893
+ int error;
894
+
895
+ if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
896
+ *entry = NULL;
897
+ error = 0;
898
+ }
899
+
900
+ return error;
901
+ }
902
+
903
+ static int iterator_advance(
904
+ const git_index_entry **entry,
905
+ git_iterator *iterator)
906
+ {
907
+ const git_index_entry *prev_entry = *entry;
908
+ int cmp, error;
909
+
910
+ /* if we're looking for conflicts, we only want to report
911
+ * one conflict for each file, instead of all three sides.
912
+ * so if this entry is a conflict for this file, and the
913
+ * previous one was a conflict for the same file, skip it.
914
+ */
915
+ while ((error = git_iterator_advance(entry, iterator)) == 0) {
916
+ if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
917
+ !git_index_entry_is_conflict(prev_entry) ||
918
+ !git_index_entry_is_conflict(*entry))
919
+ break;
920
+
921
+ cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
922
+ strcasecmp(prev_entry->path, (*entry)->path) :
923
+ strcmp(prev_entry->path, (*entry)->path);
924
+
925
+ if (cmp)
926
+ break;
927
+ }
928
+
929
+ if (error == GIT_ITEROVER) {
930
+ *entry = NULL;
931
+ error = 0;
932
+ }
933
+
934
+ return error;
935
+ }
936
+
937
+ static int iterator_advance_into(
938
+ const git_index_entry **entry,
939
+ git_iterator *iterator)
940
+ {
941
+ int error;
942
+
943
+ if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
944
+ *entry = NULL;
945
+ error = 0;
946
+ }
947
+
948
+ return error;
949
+ }
950
+
951
+ static int iterator_advance_over(
952
+ const git_index_entry **entry,
953
+ git_iterator_status_t *status,
954
+ git_iterator *iterator)
955
+ {
956
+ int error = git_iterator_advance_over(entry, status, iterator);
957
+
958
+ if (error == GIT_ITEROVER) {
959
+ *entry = NULL;
960
+ error = 0;
961
+ }
962
+
963
+ return error;
964
+ }
965
+
966
+ static int handle_unmatched_new_item(
967
+ git_diff_generated *diff, diff_in_progress *info)
968
+ {
969
+ int error = 0;
970
+ const git_index_entry *nitem = info->nitem;
971
+ git_delta_t delta_type = GIT_DELTA_UNTRACKED;
972
+ bool contains_oitem;
973
+
974
+ /* check if this is a prefix of the other side */
975
+ contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
976
+
977
+ /* update delta_type if this item is conflicted */
978
+ if (git_index_entry_is_conflict(nitem))
979
+ delta_type = GIT_DELTA_CONFLICTED;
980
+
981
+ /* update delta_type if this item is ignored */
982
+ else if (git_iterator_current_is_ignored(info->new_iter))
983
+ delta_type = GIT_DELTA_IGNORED;
984
+
985
+ if (nitem->mode == GIT_FILEMODE_TREE) {
986
+ bool recurse_into_dir = contains_oitem;
987
+
988
+ /* check if user requests recursion into this type of dir */
989
+ recurse_into_dir = contains_oitem ||
990
+ (delta_type == GIT_DELTA_UNTRACKED &&
991
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
992
+ (delta_type == GIT_DELTA_IGNORED &&
993
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
994
+
995
+ /* do not advance into directories that contain a .git file */
996
+ if (recurse_into_dir && !contains_oitem) {
997
+ git_buf *full = NULL;
998
+ if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
999
+ return -1;
1000
+ if (full && git_path_contains(full, DOT_GIT)) {
1001
+ /* TODO: warning if not a valid git repository */
1002
+ recurse_into_dir = false;
1003
+ }
1004
+ }
1005
+
1006
+ /* still have to look into untracked directories to match core git -
1007
+ * with no untracked files, directory is treated as ignored
1008
+ */
1009
+ if (!recurse_into_dir &&
1010
+ delta_type == GIT_DELTA_UNTRACKED &&
1011
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
1012
+ {
1013
+ git_diff_delta *last;
1014
+ git_iterator_status_t untracked_state;
1015
+
1016
+ /* attempt to insert record for this directory */
1017
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
1018
+ return error;
1019
+
1020
+ /* if delta wasn't created (because of rules), just skip ahead */
1021
+ last = diff_delta__last_for_item(diff, nitem);
1022
+ if (!last)
1023
+ return iterator_advance(&info->nitem, info->new_iter);
1024
+
1025
+ /* iterate into dir looking for an actual untracked file */
1026
+ if ((error = iterator_advance_over(
1027
+ &info->nitem, &untracked_state, info->new_iter)) < 0)
1028
+ return error;
1029
+
1030
+ /* if we found nothing that matched our pathlist filter, exclude */
1031
+ if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
1032
+ git_vector_pop(&diff->base.deltas);
1033
+ git__free(last);
1034
+ }
1035
+
1036
+ /* if we found nothing or just ignored items, update the record */
1037
+ if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
1038
+ untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
1039
+ last->status = GIT_DELTA_IGNORED;
1040
+
1041
+ /* remove the record if we don't want ignored records */
1042
+ if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
1043
+ git_vector_pop(&diff->base.deltas);
1044
+ git__free(last);
1045
+ }
1046
+ }
1047
+
1048
+ return 0;
1049
+ }
1050
+
1051
+ /* try to advance into directory if necessary */
1052
+ if (recurse_into_dir) {
1053
+ error = iterator_advance_into(&info->nitem, info->new_iter);
1054
+
1055
+ /* if directory is empty, can't advance into it, so skip it */
1056
+ if (error == GIT_ENOTFOUND) {
1057
+ giterr_clear();
1058
+ error = iterator_advance(&info->nitem, info->new_iter);
1059
+ }
1060
+
1061
+ return error;
1062
+ }
1063
+ }
1064
+
1065
+ else if (delta_type == GIT_DELTA_IGNORED &&
1066
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
1067
+ git_iterator_current_tree_is_ignored(info->new_iter))
1068
+ /* item contained in ignored directory, so skip over it */
1069
+ return iterator_advance(&info->nitem, info->new_iter);
1070
+
1071
+ else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) {
1072
+ if (delta_type != GIT_DELTA_CONFLICTED)
1073
+ delta_type = GIT_DELTA_ADDED;
1074
+ }
1075
+
1076
+ else if (nitem->mode == GIT_FILEMODE_COMMIT) {
1077
+ /* ignore things that are not actual submodules */
1078
+ if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
1079
+ giterr_clear();
1080
+ delta_type = GIT_DELTA_IGNORED;
1081
+
1082
+ /* if this contains a tracked item, treat as normal TREE */
1083
+ if (contains_oitem) {
1084
+ error = iterator_advance_into(&info->nitem, info->new_iter);
1085
+ if (error != GIT_ENOTFOUND)
1086
+ return error;
1087
+
1088
+ giterr_clear();
1089
+ return iterator_advance(&info->nitem, info->new_iter);
1090
+ }
1091
+ }
1092
+ }
1093
+
1094
+ else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
1095
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
1096
+ delta_type = GIT_DELTA_UNTRACKED;
1097
+ else
1098
+ delta_type = GIT_DELTA_UNREADABLE;
1099
+ }
1100
+
1101
+ /* Actually create the record for this item if necessary */
1102
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
1103
+ return error;
1104
+
1105
+ /* If user requested TYPECHANGE records, then check for that instead of
1106
+ * just generating an ADDED/UNTRACKED record
1107
+ */
1108
+ if (delta_type != GIT_DELTA_IGNORED &&
1109
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1110
+ contains_oitem)
1111
+ {
1112
+ /* this entry was prefixed with a tree - make TYPECHANGE */
1113
+ git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
1114
+ if (last) {
1115
+ last->status = GIT_DELTA_TYPECHANGE;
1116
+ last->old_file.mode = GIT_FILEMODE_TREE;
1117
+ }
1118
+ }
1119
+
1120
+ return iterator_advance(&info->nitem, info->new_iter);
1121
+ }
1122
+
1123
+ static int handle_unmatched_old_item(
1124
+ git_diff_generated *diff, diff_in_progress *info)
1125
+ {
1126
+ git_delta_t delta_type = GIT_DELTA_DELETED;
1127
+ int error;
1128
+
1129
+ /* update delta_type if this item is conflicted */
1130
+ if (git_index_entry_is_conflict(info->oitem))
1131
+ delta_type = GIT_DELTA_CONFLICTED;
1132
+
1133
+ if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
1134
+ return error;
1135
+
1136
+ /* if we are generating TYPECHANGE records then check for that
1137
+ * instead of just generating a DELETE record
1138
+ */
1139
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1140
+ entry_is_prefixed(diff, info->nitem, info->oitem))
1141
+ {
1142
+ /* this entry has become a tree! convert to TYPECHANGE */
1143
+ git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
1144
+ if (last) {
1145
+ last->status = GIT_DELTA_TYPECHANGE;
1146
+ last->new_file.mode = GIT_FILEMODE_TREE;
1147
+ }
1148
+
1149
+ /* If new_iter is a workdir iterator, then this situation
1150
+ * will certainly be followed by a series of untracked items.
1151
+ * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
1152
+ */
1153
+ if (S_ISDIR(info->nitem->mode) &&
1154
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
1155
+ return iterator_advance(&info->nitem, info->new_iter);
1156
+ }
1157
+
1158
+ return iterator_advance(&info->oitem, info->old_iter);
1159
+ }
1160
+
1161
+ static int handle_matched_item(
1162
+ git_diff_generated *diff, diff_in_progress *info)
1163
+ {
1164
+ int error = 0;
1165
+
1166
+ if ((error = maybe_modified(diff, info)) < 0)
1167
+ return error;
1168
+
1169
+ if (!(error = iterator_advance(&info->oitem, info->old_iter)))
1170
+ error = iterator_advance(&info->nitem, info->new_iter);
1171
+
1172
+ return error;
1173
+ }
1174
+
1175
+ int git_diff__from_iterators(
1176
+ git_diff **out,
1177
+ git_repository *repo,
1178
+ git_iterator *old_iter,
1179
+ git_iterator *new_iter,
1180
+ const git_diff_options *opts)
1181
+ {
1182
+ git_diff_generated *diff;
1183
+ diff_in_progress info;
1184
+ int error = 0;
1185
+
1186
+ *out = NULL;
1187
+
1188
+ diff = diff_generated_alloc(repo, old_iter, new_iter);
1189
+ GITERR_CHECK_ALLOC(diff);
1190
+
1191
+ info.repo = repo;
1192
+ info.old_iter = old_iter;
1193
+ info.new_iter = new_iter;
1194
+
1195
+ /* make iterators have matching icase behavior */
1196
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
1197
+ git_iterator_set_ignore_case(old_iter, true);
1198
+ git_iterator_set_ignore_case(new_iter, true);
1199
+ }
1200
+
1201
+ /* finish initialization */
1202
+ if ((error = diff_generated_apply_options(diff, opts)) < 0)
1203
+ goto cleanup;
1204
+
1205
+ if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
1206
+ (error = iterator_current(&info.nitem, new_iter)) < 0)
1207
+ goto cleanup;
1208
+
1209
+ /* run iterators building diffs */
1210
+ while (!error && (info.oitem || info.nitem)) {
1211
+ int cmp;
1212
+
1213
+ /* report progress */
1214
+ if (opts && opts->progress_cb) {
1215
+ if ((error = opts->progress_cb(&diff->base,
1216
+ info.oitem ? info.oitem->path : NULL,
1217
+ info.nitem ? info.nitem->path : NULL,
1218
+ opts->payload)))
1219
+ break;
1220
+ }
1221
+
1222
+ cmp = info.oitem ?
1223
+ (info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1;
1224
+
1225
+ /* create DELETED records for old items not matched in new */
1226
+ if (cmp < 0)
1227
+ error = handle_unmatched_old_item(diff, &info);
1228
+
1229
+ /* create ADDED, TRACKED, or IGNORED records for new items not
1230
+ * matched in old (and/or descend into directories as needed)
1231
+ */
1232
+ else if (cmp > 0)
1233
+ error = handle_unmatched_new_item(diff, &info);
1234
+
1235
+ /* otherwise item paths match, so create MODIFIED record
1236
+ * (or ADDED and DELETED pair if type changed)
1237
+ */
1238
+ else
1239
+ error = handle_matched_item(diff, &info);
1240
+ }
1241
+
1242
+ diff->base.perf.stat_calls +=
1243
+ old_iter->stat_calls + new_iter->stat_calls;
1244
+
1245
+ cleanup:
1246
+ if (!error)
1247
+ *out = &diff->base;
1248
+ else
1249
+ git_diff_free(&diff->base);
1250
+
1251
+ return error;
1252
+ }
1253
+
1254
+ #define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
1255
+ git_iterator *a = NULL, *b = NULL; \
1256
+ char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
1257
+ git_pathspec_prefix(&opts->pathspec) : NULL; \
1258
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
1259
+ b_opts = GIT_ITERATOR_OPTIONS_INIT; \
1260
+ a_opts.flags = FLAGS_FIRST; \
1261
+ a_opts.start = pfx; \
1262
+ a_opts.end = pfx; \
1263
+ b_opts.flags = FLAGS_SECOND; \
1264
+ b_opts.start = pfx; \
1265
+ b_opts.end = pfx; \
1266
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
1267
+ if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
1268
+ a_opts.pathlist.strings = opts->pathspec.strings; \
1269
+ a_opts.pathlist.count = opts->pathspec.count; \
1270
+ b_opts.pathlist.strings = opts->pathspec.strings; \
1271
+ b_opts.pathlist.count = opts->pathspec.count; \
1272
+ } \
1273
+ if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
1274
+ error = git_diff__from_iterators(&diff, repo, a, b, opts); \
1275
+ git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
1276
+ } while (0)
1277
+
1278
+ int git_diff_tree_to_tree(
1279
+ git_diff **out,
1280
+ git_repository *repo,
1281
+ git_tree *old_tree,
1282
+ git_tree *new_tree,
1283
+ const git_diff_options *opts)
1284
+ {
1285
+ git_diff *diff = NULL;
1286
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
1287
+ int error = 0;
1288
+
1289
+ assert(out && repo);
1290
+
1291
+ *out = NULL;
1292
+
1293
+ /* for tree to tree diff, be case sensitive even if the index is
1294
+ * currently case insensitive, unless the user explicitly asked
1295
+ * for case insensitivity
1296
+ */
1297
+ if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
1298
+ iflag = GIT_ITERATOR_IGNORE_CASE;
1299
+
1300
+ DIFF_FROM_ITERATORS(
1301
+ git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
1302
+ git_iterator_for_tree(&b, new_tree, &b_opts), iflag
1303
+ );
1304
+
1305
+ if (!error)
1306
+ *out = diff;
1307
+
1308
+ return error;
1309
+ }
1310
+
1311
+ static int diff_load_index(git_index **index, git_repository *repo)
1312
+ {
1313
+ int error = git_repository_index__weakptr(index, repo);
1314
+
1315
+ /* reload the repository index when user did not pass one in */
1316
+ if (!error && git_index_read(*index, false) < 0)
1317
+ giterr_clear();
1318
+
1319
+ return error;
1320
+ }
1321
+
1322
+ int git_diff_tree_to_index(
1323
+ git_diff **out,
1324
+ git_repository *repo,
1325
+ git_tree *old_tree,
1326
+ git_index *index,
1327
+ const git_diff_options *opts)
1328
+ {
1329
+ git_diff *diff = NULL;
1330
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
1331
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
1332
+ bool index_ignore_case = false;
1333
+ int error = 0;
1334
+
1335
+ assert(out && repo);
1336
+
1337
+ *out = NULL;
1338
+
1339
+ if (!index && (error = diff_load_index(&index, repo)) < 0)
1340
+ return error;
1341
+
1342
+ index_ignore_case = index->ignore_case;
1343
+
1344
+ DIFF_FROM_ITERATORS(
1345
+ git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
1346
+ git_iterator_for_index(&b, repo, index, &b_opts), iflag
1347
+ );
1348
+
1349
+ /* if index is in case-insensitive order, re-sort deltas to match */
1350
+ if (!error && index_ignore_case)
1351
+ git_diff__set_ignore_case(diff, true);
1352
+
1353
+ if (!error)
1354
+ *out = diff;
1355
+
1356
+ return error;
1357
+ }
1358
+
1359
+ int git_diff_index_to_workdir(
1360
+ git_diff **out,
1361
+ git_repository *repo,
1362
+ git_index *index,
1363
+ const git_diff_options *opts)
1364
+ {
1365
+ git_diff *diff = NULL;
1366
+ int error = 0;
1367
+
1368
+ assert(out && repo);
1369
+
1370
+ *out = NULL;
1371
+
1372
+ if (!index && (error = diff_load_index(&index, repo)) < 0)
1373
+ return error;
1374
+
1375
+ DIFF_FROM_ITERATORS(
1376
+ git_iterator_for_index(&a, repo, index, &a_opts),
1377
+ GIT_ITERATOR_INCLUDE_CONFLICTS,
1378
+
1379
+ git_iterator_for_workdir(&b, repo, index, NULL, &b_opts),
1380
+ GIT_ITERATOR_DONT_AUTOEXPAND
1381
+ );
1382
+
1383
+ if (!error && (diff->opts.flags & GIT_DIFF_UPDATE_INDEX) != 0 &&
1384
+ ((git_diff_generated *)diff)->index_updated)
1385
+ error = git_index_write(index);
1386
+
1387
+ if (!error)
1388
+ *out = diff;
1389
+
1390
+ return error;
1391
+ }
1392
+
1393
+ int git_diff_tree_to_workdir(
1394
+ git_diff **out,
1395
+ git_repository *repo,
1396
+ git_tree *old_tree,
1397
+ const git_diff_options *opts)
1398
+ {
1399
+ git_diff *diff = NULL;
1400
+ git_index *index;
1401
+ int error = 0;
1402
+
1403
+ assert(out && repo);
1404
+
1405
+ *out = NULL;
1406
+
1407
+ if ((error = git_repository_index__weakptr(&index, repo)))
1408
+ return error;
1409
+
1410
+ DIFF_FROM_ITERATORS(
1411
+ git_iterator_for_tree(&a, old_tree, &a_opts), 0,
1412
+ git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND
1413
+ );
1414
+
1415
+ if (!error)
1416
+ *out = diff;
1417
+
1418
+ return error;
1419
+ }
1420
+
1421
+ int git_diff_tree_to_workdir_with_index(
1422
+ git_diff **out,
1423
+ git_repository *repo,
1424
+ git_tree *tree,
1425
+ const git_diff_options *opts)
1426
+ {
1427
+ git_diff *d1 = NULL, *d2 = NULL;
1428
+ git_index *index = NULL;
1429
+ int error = 0;
1430
+
1431
+ assert(out && repo);
1432
+
1433
+ *out = NULL;
1434
+
1435
+ if ((error = diff_load_index(&index, repo)) < 0)
1436
+ return error;
1437
+
1438
+ if (!(error = git_diff_tree_to_index(&d1, repo, tree, index, opts)) &&
1439
+ !(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
1440
+ error = git_diff_merge(d1, d2);
1441
+
1442
+ git_diff_free(d2);
1443
+
1444
+ if (error) {
1445
+ git_diff_free(d1);
1446
+ d1 = NULL;
1447
+ }
1448
+
1449
+ *out = d1;
1450
+ return error;
1451
+ }
1452
+
1453
+ int git_diff_index_to_index(
1454
+ git_diff **out,
1455
+ git_repository *repo,
1456
+ git_index *old_index,
1457
+ git_index *new_index,
1458
+ const git_diff_options *opts)
1459
+ {
1460
+ git_diff *diff;
1461
+ int error = 0;
1462
+
1463
+ assert(out && old_index && new_index);
1464
+
1465
+ *out = NULL;
1466
+
1467
+ DIFF_FROM_ITERATORS(
1468
+ git_iterator_for_index(&a, repo, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE,
1469
+ git_iterator_for_index(&b, repo, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE
1470
+ );
1471
+
1472
+ /* if index is in case-insensitive order, re-sort deltas to match */
1473
+ if (!error && (old_index->ignore_case || new_index->ignore_case))
1474
+ git_diff__set_ignore_case(diff, true);
1475
+
1476
+ if (!error)
1477
+ *out = diff;
1478
+
1479
+ return error;
1480
+ }
1481
+
1482
+ int git_diff__paired_foreach(
1483
+ git_diff *head2idx,
1484
+ git_diff *idx2wd,
1485
+ int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
1486
+ void *payload)
1487
+ {
1488
+ int cmp, error = 0;
1489
+ git_diff_delta *h2i, *i2w;
1490
+ size_t i, j, i_max, j_max;
1491
+ int (*strcomp)(const char *, const char *) = git__strcmp;
1492
+ bool h2i_icase, i2w_icase, icase_mismatch;
1493
+
1494
+ i_max = head2idx ? head2idx->deltas.length : 0;
1495
+ j_max = idx2wd ? idx2wd->deltas.length : 0;
1496
+ if (!i_max && !j_max)
1497
+ return 0;
1498
+
1499
+ /* At some point, tree-to-index diffs will probably never ignore case,
1500
+ * even if that isn't true now. Index-to-workdir diffs may or may not
1501
+ * ignore case, but the index filename for the idx2wd diff should
1502
+ * still be using the canonical case-preserving name.
1503
+ *
1504
+ * Therefore the main thing we need to do here is make sure the diffs
1505
+ * are traversed in a compatible order. To do this, we temporarily
1506
+ * resort a mismatched diff to get the order correct.
1507
+ *
1508
+ * In order to traverse renames in the index->workdir, we need to
1509
+ * ensure that we compare the index name on both sides, so we
1510
+ * always sort by the old name in the i2w list.
1511
+ */
1512
+ h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx);
1513
+ i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd);
1514
+
1515
+ icase_mismatch =
1516
+ (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
1517
+
1518
+ if (icase_mismatch && h2i_icase) {
1519
+ git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
1520
+ git_vector_sort(&head2idx->deltas);
1521
+ }
1522
+
1523
+ if (i2w_icase && !icase_mismatch) {
1524
+ strcomp = git__strcasecmp;
1525
+
1526
+ git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_casecmp);
1527
+ git_vector_sort(&idx2wd->deltas);
1528
+ } else if (idx2wd != NULL) {
1529
+ git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_cmp);
1530
+ git_vector_sort(&idx2wd->deltas);
1531
+ }
1532
+
1533
+ for (i = 0, j = 0; i < i_max || j < j_max; ) {
1534
+ h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
1535
+ i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
1536
+
1537
+ cmp = !i2w ? -1 : !h2i ? 1 :
1538
+ strcomp(h2i->new_file.path, i2w->old_file.path);
1539
+
1540
+ if (cmp < 0) {
1541
+ i++; i2w = NULL;
1542
+ } else if (cmp > 0) {
1543
+ j++; h2i = NULL;
1544
+ } else {
1545
+ i++; j++;
1546
+ }
1547
+
1548
+ if ((error = cb(h2i, i2w, payload)) != 0) {
1549
+ giterr_set_after_callback(error);
1550
+ break;
1551
+ }
1552
+ }
1553
+
1554
+ /* restore case-insensitive delta sort */
1555
+ if (icase_mismatch && h2i_icase) {
1556
+ git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
1557
+ git_vector_sort(&head2idx->deltas);
1558
+ }
1559
+
1560
+ /* restore idx2wd sort by new path */
1561
+ if (idx2wd != NULL) {
1562
+ git_vector_set_cmp(&idx2wd->deltas,
1563
+ i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
1564
+ git_vector_sort(&idx2wd->deltas);
1565
+ }
1566
+
1567
+ return error;
1568
+ }
1569
+
1570
+ int git_diff__commit(
1571
+ git_diff **out,
1572
+ git_repository *repo,
1573
+ const git_commit *commit,
1574
+ const git_diff_options *opts)
1575
+ {
1576
+ git_commit *parent = NULL;
1577
+ git_diff *commit_diff = NULL;
1578
+ git_tree *old_tree = NULL, *new_tree = NULL;
1579
+ size_t parents;
1580
+ int error = 0;
1581
+
1582
+ *out = NULL;
1583
+
1584
+ if ((parents = git_commit_parentcount(commit)) > 1) {
1585
+ char commit_oidstr[GIT_OID_HEXSZ + 1];
1586
+
1587
+ error = -1;
1588
+ giterr_set(GITERR_INVALID, "Commit %s is a merge commit",
1589
+ git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
1590
+ goto on_error;
1591
+ }
1592
+
1593
+ if (parents > 0)
1594
+ if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
1595
+ (error = git_commit_tree(&old_tree, parent)) < 0)
1596
+ goto on_error;
1597
+
1598
+ if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
1599
+ (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
1600
+ goto on_error;
1601
+
1602
+ *out = commit_diff;
1603
+
1604
+ on_error:
1605
+ git_tree_free(new_tree);
1606
+ git_tree_free(old_tree);
1607
+ git_commit_free(parent);
1608
+
1609
+ return error;
1610
+ }
1611
+