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(resolve_undo
, ce
->name
);
25 lost
->util
= xcalloc(1, sizeof(*ui
));
27 hashcpy(ui
->sha1
[stage
- 1], ce
->sha1
);
28 ui
->mode
[stage
- 1] = ce
->ce_mode
;
31 void resolve_undo_write(struct strbuf
*sb
, struct string_list
*resolve_undo
)
33 struct string_list_item
*item
;
34 for_each_string_list_item(item
, resolve_undo
) {
35 struct resolve_undo_info
*ui
= item
->util
;
40 strbuf_addstr(sb
, item
->string
);
42 for (i
= 0; i
< 3; i
++)
43 strbuf_addf(sb
, "%o%c", ui
->mode
[i
], 0);
44 for (i
= 0; i
< 3; i
++) {
47 strbuf_add(sb
, ui
->sha1
[i
], 20);
52 struct string_list
*resolve_undo_read(const char *data
, unsigned long size
)
54 struct string_list
*resolve_undo
;
59 resolve_undo
= xcalloc(1, sizeof(*resolve_undo
));
60 resolve_undo
->strdup_strings
= 1;
63 struct string_list_item
*lost
;
64 struct resolve_undo_info
*ui
;
66 len
= strlen(data
) + 1;
69 lost
= string_list_insert(resolve_undo
, data
);
71 lost
->util
= xcalloc(1, sizeof(*ui
));
76 for (i
= 0; i
< 3; i
++) {
77 ui
->mode
[i
] = strtoul(data
, &endptr
, 8);
78 if (!endptr
|| endptr
== data
|| *endptr
)
80 len
= (endptr
+ 1) - (char*)data
;
87 for (i
= 0; i
< 3; i
++) {
92 hashcpy(ui
->sha1
[i
], (const unsigned char *)data
);
100 string_list_clear(resolve_undo
, 1);
101 error("Index records invalid resolve-undo information");
105 void resolve_undo_clear_index(struct index_state
*istate
)
107 struct string_list
*resolve_undo
= istate
->resolve_undo
;
110 string_list_clear(resolve_undo
, 1);
112 istate
->resolve_undo
= NULL
;
113 istate
->cache_changed
= 1;
116 int unmerge_index_entry_at(struct index_state
*istate
, int pos
)
118 const struct cache_entry
*ce
;
119 struct string_list_item
*item
;
120 struct resolve_undo_info
*ru
;
121 int i
, err
= 0, matched
;
124 if (!istate
->resolve_undo
)
127 ce
= istate
->cache
[pos
];
129 /* already unmerged */
130 while ((pos
< istate
->cache_nr
) &&
131 ! strcmp(istate
->cache
[pos
]->name
, ce
->name
))
133 return pos
- 1; /* return the last entry processed */
135 item
= string_list_lookup(istate
->resolve_undo
, ce
->name
);
141 matched
= ce
->ce_flags
& CE_MATCHED
;
142 name
= xstrdup(ce
->name
);
143 remove_index_entry_at(istate
, pos
);
144 for (i
= 0; i
< 3; i
++) {
145 struct cache_entry
*nce
;
148 nce
= make_cache_entry(ru
->mode
[i
], ru
->sha1
[i
],
151 nce
->ce_flags
|= CE_MATCHED
;
152 if (add_index_entry(istate
, nce
, ADD_CACHE_OK_TO_ADD
)) {
154 error("cannot unmerge '%s'", name
);
162 return unmerge_index_entry_at(istate
, pos
);
165 void unmerge_marked_index(struct index_state
*istate
)
169 if (!istate
->resolve_undo
)
172 for (i
= 0; i
< istate
->cache_nr
; i
++) {
173 const struct cache_entry
*ce
= istate
->cache
[i
];
174 if (ce
->ce_flags
& CE_MATCHED
)
175 i
= unmerge_index_entry_at(istate
, i
);
179 void unmerge_index(struct index_state
*istate
, const struct pathspec
*pathspec
)
183 if (!istate
->resolve_undo
)
186 for (i
= 0; i
< istate
->cache_nr
; i
++) {
187 const struct cache_entry
*ce
= istate
->cache
[i
];
188 if (!ce_path_match(ce
, pathspec
, NULL
))
190 i
= unmerge_index_entry_at(istate
, i
);