Git 2.44
[alt-git.git] / resolve-undo.c
blobcd02dc99289997e5bf3ac57da3a0c5d482a8301d
1 #include "git-compat-util.h"
2 #include "dir.h"
3 #include "hash.h"
4 #include "read-cache.h"
5 #include "resolve-undo.h"
6 #include "sparse-index.h"
7 #include "string-list.h"
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)
12 struct string_list_item *lost;
13 struct resolve_undo_info *ui;
14 struct string_list *resolve_undo;
15 int stage = ce_stage(ce);
17 if (!stage)
18 return;
20 if (!istate->resolve_undo) {
21 CALLOC_ARRAY(resolve_undo, 1);
22 resolve_undo->strdup_strings = 1;
23 istate->resolve_undo = resolve_undo;
25 resolve_undo = istate->resolve_undo;
26 lost = string_list_insert(resolve_undo, ce->name);
27 if (!lost->util)
28 lost->util = xcalloc(1, sizeof(*ui));
29 ui = lost->util;
30 oidcpy(&ui->oid[stage - 1], &ce->oid);
31 ui->mode[stage - 1] = ce->ce_mode;
34 void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo)
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;
41 if (!ui)
42 continue;
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;
50 strbuf_add(sb, ui->oid[i].hash, the_hash_algo->rawsz);
55 struct string_list *resolve_undo_read(const char *data, unsigned long size)
57 struct string_list *resolve_undo;
58 size_t len;
59 char *endptr;
60 int i;
61 const unsigned rawsz = the_hash_algo->rawsz;
63 CALLOC_ARRAY(resolve_undo, 1);
64 resolve_undo->strdup_strings = 1;
66 while (size) {
67 struct string_list_item *lost;
68 struct resolve_undo_info *ui;
70 len = strlen(data) + 1;
71 if (size <= len)
72 goto error;
73 lost = string_list_insert(resolve_undo, data);
74 if (!lost->util)
75 lost->util = xcalloc(1, sizeof(*ui));
76 ui = lost->util;
77 size -= len;
78 data += len;
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;
91 for (i = 0; i < 3; i++) {
92 if (!ui->mode[i])
93 continue;
94 if (size < rawsz)
95 goto error;
96 oidread(&ui->oid[i], (const unsigned char *)data);
97 size -= rawsz;
98 data += rawsz;
101 return resolve_undo;
103 error:
104 string_list_clear(resolve_undo, 1);
105 error("Index records invalid resolve-undo information");
106 return NULL;
109 void resolve_undo_clear_index(struct index_state *istate)
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;
117 istate->cache_changed |= RESOLVE_UNDO_CHANGED;
120 int unmerge_index_entry(struct index_state *istate, const char *path,
121 struct resolve_undo_info *ru, unsigned ce_flags)
123 int i = index_name_pos(istate, path, strlen(path));
125 if (i < 0) {
126 /* unmerged? */
127 i = -i - 1;
128 if (i < istate->cache_nr &&
129 !strcmp(istate->cache[i]->name, path))
130 /* yes, it is already unmerged */
131 return 0;
132 /* fallthru: resolved to removal */
133 } else {
134 /* merged - remove it to replace it with unmerged entries */
135 remove_index_entry_at(istate, i);
138 for (i = 0; i < 3; i++) {
139 struct cache_entry *ce;
140 if (!ru->mode[i])
141 continue;
142 ce = make_cache_entry(istate, ru->mode[i], &ru->oid[i],
143 path, i + 1, 0);
144 ce->ce_flags |= ce_flags;
145 if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD))
146 return error("cannot unmerge '%s'", path);
148 return 0;
151 void unmerge_index(struct index_state *istate, const struct pathspec *pathspec,
152 unsigned ce_flags)
154 struct string_list_item *item;
156 if (!istate->resolve_undo)
157 return;
159 /* TODO: audit for interaction with sparse-index. */
160 ensure_full_index(istate);
162 for_each_string_list_item(item, istate->resolve_undo) {
163 const char *path = item->string;
164 struct resolve_undo_info *ru = item->util;
165 if (!item->util)
166 continue;
167 if (!match_pathspec(istate, pathspec,
168 item->string, strlen(item->string),
169 0, NULL, 0))
170 continue;
171 unmerge_index_entry(istate, path, ru, ce_flags);
172 free(ru);
173 item->util = NULL;