]> git.ipfire.org Git - thirdparty/git.git/blame - resolve-undo.c
reftable: read reftable files
[thirdparty/git.git] / resolve-undo.c
CommitLineData
cfc5789a 1#include "cache.h"
4421a823 2#include "dir.h"
cfc5789a
JH
3#include "resolve-undo.h"
4#include "string-list.h"
5
6/* The only error case is to run out of memory in string-list */
7void record_resolve_undo(struct index_state *istate, struct cache_entry *ce)
8{
9 struct string_list_item *lost;
10 struct resolve_undo_info *ui;
11 struct string_list *resolve_undo;
12 int stage = ce_stage(ce);
13
14 if (!stage)
15 return;
16
17 if (!istate->resolve_undo) {
ca56dadb 18 CALLOC_ARRAY(resolve_undo, 1);
cfc5789a
JH
19 resolve_undo->strdup_strings = 1;
20 istate->resolve_undo = resolve_undo;
21 }
22 resolve_undo = istate->resolve_undo;
78a395d3 23 lost = string_list_insert(resolve_undo, ce->name);
cfc5789a
JH
24 if (!lost->util)
25 lost->util = xcalloc(1, sizeof(*ui));
26 ui = lost->util;
5ac913c6 27 oidcpy(&ui->oid[stage - 1], &ce->oid);
cfc5789a
JH
28 ui->mode[stage - 1] = ce->ce_mode;
29}
30
8a57c6e9 31void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo)
cfc5789a 32{
8a57c6e9
AR
33 struct string_list_item *item;
34 for_each_string_list_item(item, resolve_undo) {
35 struct resolve_undo_info *ui = item->util;
36 int i;
cfc5789a 37
8a57c6e9 38 if (!ui)
cfc5789a 39 continue;
8a57c6e9
AR
40 strbuf_addstr(sb, item->string);
41 strbuf_addch(sb, 0);
42 for (i = 0; i < 3; i++)
43 strbuf_addf(sb, "%o%c", ui->mode[i], 0);
44 for (i = 0; i < 3; i++) {
45 if (!ui->mode[i])
46 continue;
5ac913c6 47 strbuf_add(sb, ui->oid[i].hash, the_hash_algo->rawsz);
8a57c6e9 48 }
cfc5789a 49 }
cfc5789a
JH
50}
51
b8bba419 52struct string_list *resolve_undo_read(const char *data, unsigned long size)
cfc5789a
JH
53{
54 struct string_list *resolve_undo;
55 size_t len;
56 char *endptr;
57 int i;
5ac913c6 58 const unsigned rawsz = the_hash_algo->rawsz;
cfc5789a 59
ca56dadb 60 CALLOC_ARRAY(resolve_undo, 1);
cfc5789a
JH
61 resolve_undo->strdup_strings = 1;
62
63 while (size) {
64 struct string_list_item *lost;
65 struct resolve_undo_info *ui;
66
67 len = strlen(data) + 1;
68 if (size <= len)
69 goto error;
78a395d3 70 lost = string_list_insert(resolve_undo, data);
cfc5789a
JH
71 if (!lost->util)
72 lost->util = xcalloc(1, sizeof(*ui));
73 ui = lost->util;
74 size -= len;
75 data += len;
76
77 for (i = 0; i < 3; i++) {
78 ui->mode[i] = strtoul(data, &endptr, 8);
79 if (!endptr || endptr == data || *endptr)
80 goto error;
81 len = (endptr + 1) - (char*)data;
82 if (size <= len)
83 goto error;
84 size -= len;
85 data += len;
86 }
87
88 for (i = 0; i < 3; i++) {
89 if (!ui->mode[i])
90 continue;
5ac913c6 91 if (size < rawsz)
cfc5789a 92 goto error;
69d12425 93 oidread(&ui->oid[i], (const unsigned char *)data);
5ac913c6 94 size -= rawsz;
95 data += rawsz;
cfc5789a
JH
96 }
97 }
98 return resolve_undo;
99
100error:
101 string_list_clear(resolve_undo, 1);
102 error("Index records invalid resolve-undo information");
103 return NULL;
104}
105
106void resolve_undo_clear_index(struct index_state *istate)
107{
108 struct string_list *resolve_undo = istate->resolve_undo;
109 if (!resolve_undo)
110 return;
111 string_list_clear(resolve_undo, 1);
112 free(resolve_undo);
113 istate->resolve_undo = NULL;
6c306a34 114 istate->cache_changed |= RESOLVE_UNDO_CHANGED;
cfc5789a 115}
4421a823
JH
116
117int unmerge_index_entry_at(struct index_state *istate, int pos)
118{
9c5e6c80 119 const struct cache_entry *ce;
4421a823
JH
120 struct string_list_item *item;
121 struct resolve_undo_info *ru;
e721c154 122 int i, err = 0, matched;
5699d17e 123 char *name;
4421a823
JH
124
125 if (!istate->resolve_undo)
126 return pos;
127
128 ce = istate->cache[pos];
129 if (ce_stage(ce)) {
130 /* already unmerged */
131 while ((pos < istate->cache_nr) &&
132 ! strcmp(istate->cache[pos]->name, ce->name))
133 pos++;
134 return pos - 1; /* return the last entry processed */
135 }
e8c8b713 136 item = string_list_lookup(istate->resolve_undo, ce->name);
4421a823
JH
137 if (!item)
138 return pos;
139 ru = item->util;
140 if (!ru)
141 return pos;
e721c154 142 matched = ce->ce_flags & CE_MATCHED;
5699d17e 143 name = xstrdup(ce->name);
4421a823
JH
144 remove_index_entry_at(istate, pos);
145 for (i = 0; i < 3; i++) {
146 struct cache_entry *nce;
147 if (!ru->mode[i])
148 continue;
a849735b
JM
149 nce = make_cache_entry(istate,
150 ru->mode[i],
151 &ru->oid[i],
5699d17e 152 name, i + 1, 0);
e721c154
NTND
153 if (matched)
154 nce->ce_flags |= CE_MATCHED;
4421a823
JH
155 if (add_index_entry(istate, nce, ADD_CACHE_OK_TO_ADD)) {
156 err = 1;
5699d17e 157 error("cannot unmerge '%s'", name);
4421a823
JH
158 }
159 }
5699d17e 160 free(name);
4421a823
JH
161 if (err)
162 return pos;
163 free(ru);
164 item->util = NULL;
165 return unmerge_index_entry_at(istate, pos);
166}
167
e721c154
NTND
168void unmerge_marked_index(struct index_state *istate)
169{
170 int i;
171
172 if (!istate->resolve_undo)
173 return;
174
dc26b23e
DS
175 /* TODO: audit for interaction with sparse-index. */
176 ensure_full_index(istate);
e721c154 177 for (i = 0; i < istate->cache_nr; i++) {
9c5e6c80 178 const struct cache_entry *ce = istate->cache[i];
e721c154
NTND
179 if (ce->ce_flags & CE_MATCHED)
180 i = unmerge_index_entry_at(istate, i);
181 }
182}
183
5ab06518 184void unmerge_index(struct index_state *istate, const struct pathspec *pathspec)
4421a823
JH
185{
186 int i;
187
188 if (!istate->resolve_undo)
189 return;
190
dc26b23e
DS
191 /* TODO: audit for interaction with sparse-index. */
192 ensure_full_index(istate);
4421a823 193 for (i = 0; i < istate->cache_nr; i++) {
9c5e6c80 194 const struct cache_entry *ce = istate->cache[i];
ff82d126 195 if (!ce_path_match(istate, ce, pathspec, NULL))
4421a823
JH
196 continue;
197 i = unmerge_index_entry_at(istate, i);
198 }
199}