3 #include "resolve-undo.h"
4 #include "string-list.h"
6 /* The only error case is to run out of memory in string-list */
7 void record_resolve_undo(struct index_state *istate, struct cache_entry *ce)
9 struct string_list_item *lost;
10 struct resolve_undo_info *ui;
11 struct string_list *resolve_undo;
12 int stage = ce_stage(ce);
17 if (!istate->resolve_undo) {
18 resolve_undo = xcalloc(1, sizeof(*resolve_undo));
19 resolve_undo->strdup_strings = 1;
20 istate->resolve_undo = resolve_undo;
22 resolve_undo = istate->resolve_undo;
23 lost = string_list_insert(ce->name, resolve_undo);
25 lost->util = xcalloc(1, sizeof(*ui));
27 hashcpy(ui->sha1[stage - 1], ce->sha1);
28 ui->mode[stage - 1] = ce->ce_mode;
31 static int write_one(struct string_list_item *item, void *cbdata)
33 struct strbuf *sb = cbdata;
34 struct resolve_undo_info *ui = item->util;
39 strbuf_addstr(sb, item->string);
41 for (i = 0; i < 3; i++)
42 strbuf_addf(sb, "%o%c", ui->mode[i], 0);
43 for (i = 0; i < 3; i++) {
46 strbuf_add(sb, ui->sha1[i], 20);
51 void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo)
53 for_each_string_list(write_one, resolve_undo, sb);
56 struct string_list *resolve_undo_read(const char *data, unsigned long size)
58 struct string_list *resolve_undo;
63 resolve_undo = xcalloc(1, sizeof(*resolve_undo));
64 resolve_undo->strdup_strings = 1;
67 struct string_list_item *lost;
68 struct resolve_undo_info *ui;
70 len = strlen(data) + 1;
73 lost = string_list_insert(data, resolve_undo);
75 lost->util = xcalloc(1, sizeof(*ui));
80 for (i = 0; i < 3; i++) {
81 ui->mode[i] = strtoul(data, &endptr, 8);
82 if (!endptr || endptr == data || *endptr)
84 len = (endptr + 1) - (char*)data;
91 for (i = 0; i < 3; i++) {
96 hashcpy(ui->sha1[i], (const unsigned char *)data);
104 string_list_clear(resolve_undo, 1);
105 error("Index records invalid resolve-undo information");
109 void resolve_undo_clear_index(struct index_state *istate)
111 struct string_list *resolve_undo = istate->resolve_undo;
114 string_list_clear(resolve_undo, 1);
116 istate->resolve_undo = NULL;
117 istate->cache_changed = 1;
120 int unmerge_index_entry_at(struct index_state *istate, int pos)
122 struct cache_entry *ce;
123 struct string_list_item *item;
124 struct resolve_undo_info *ru;
127 if (!istate->resolve_undo)
130 ce = istate->cache[pos];
132 /* already unmerged */
133 while ((pos < istate->cache_nr) &&
134 ! strcmp(istate->cache[pos]->name, ce->name))
136 return pos - 1; /* return the last entry processed */
138 item = string_list_lookup(ce->name, istate->resolve_undo);
144 remove_index_entry_at(istate, pos);
145 for (i = 0; i < 3; i++) {
146 struct cache_entry *nce;
149 nce = make_cache_entry(ru->mode[i], ru->sha1[i],
151 if (add_index_entry(istate, nce, ADD_CACHE_OK_TO_ADD)) {
153 error("cannot unmerge '%s'", ce->name);
160 return unmerge_index_entry_at(istate, pos);
163 void unmerge_index(struct index_state *istate, const char **pathspec)
167 if (!istate->resolve_undo)
170 for (i = 0; i < istate->cache_nr; i++) {
171 struct cache_entry *ce = istate->cache[i];
172 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL))
174 i = unmerge_index_entry_at(istate, i);