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