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 oidcpy(&ui
->oid
[stage
- 1], &ce
->oid
);
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
->oid
[i
].hash
, the_hash_algo
->rawsz
);
52 struct string_list
*resolve_undo_read(const char *data
, unsigned long size
)
54 struct string_list
*resolve_undo
;
58 const unsigned rawsz
= the_hash_algo
->rawsz
;
60 resolve_undo
= xcalloc(1, sizeof(*resolve_undo
));
61 resolve_undo
->strdup_strings
= 1;
64 struct string_list_item
*lost
;
65 struct resolve_undo_info
*ui
;
67 len
= strlen(data
) + 1;
70 lost
= string_list_insert(resolve_undo
, data
);
72 lost
->util
= xcalloc(1, sizeof(*ui
));
77 for (i
= 0; i
< 3; i
++) {
78 ui
->mode
[i
] = strtoul(data
, &endptr
, 8);
79 if (!endptr
|| endptr
== data
|| *endptr
)
81 len
= (endptr
+ 1) - (char*)data
;
88 for (i
= 0; i
< 3; i
++) {
93 oidread(&ui
->oid
[i
], (const unsigned char *)data
);
101 string_list_clear(resolve_undo
, 1);
102 error("Index records invalid resolve-undo information");
106 void resolve_undo_clear_index(struct index_state
*istate
)
108 struct string_list
*resolve_undo
= istate
->resolve_undo
;
111 string_list_clear(resolve_undo
, 1);
113 istate
->resolve_undo
= NULL
;
114 istate
->cache_changed
|= RESOLVE_UNDO_CHANGED
;
117 int unmerge_index_entry_at(struct index_state
*istate
, int pos
)
119 const struct cache_entry
*ce
;
120 struct string_list_item
*item
;
121 struct resolve_undo_info
*ru
;
122 int i
, err
= 0, matched
;
125 if (!istate
->resolve_undo
)
128 ce
= istate
->cache
[pos
];
130 /* already unmerged */
131 while ((pos
< istate
->cache_nr
) &&
132 ! strcmp(istate
->cache
[pos
]->name
, ce
->name
))
134 return pos
- 1; /* return the last entry processed */
136 item
= string_list_lookup(istate
->resolve_undo
, ce
->name
);
142 matched
= ce
->ce_flags
& CE_MATCHED
;
143 name
= xstrdup(ce
->name
);
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
->oid
[i
].hash
,
152 nce
->ce_flags
|= CE_MATCHED
;
153 if (add_index_entry(istate
, nce
, ADD_CACHE_OK_TO_ADD
)) {
155 error("cannot unmerge '%s'", name
);
163 return unmerge_index_entry_at(istate
, pos
);
166 void unmerge_marked_index(struct index_state
*istate
)
170 if (!istate
->resolve_undo
)
173 for (i
= 0; i
< istate
->cache_nr
; i
++) {
174 const struct cache_entry
*ce
= istate
->cache
[i
];
175 if (ce
->ce_flags
& CE_MATCHED
)
176 i
= unmerge_index_entry_at(istate
, i
);
180 void unmerge_index(struct index_state
*istate
, const struct pathspec
*pathspec
)
184 if (!istate
->resolve_undo
)
187 for (i
= 0; i
< istate
->cache_nr
; i
++) {
188 const struct cache_entry
*ce
= istate
->cache
[i
];
189 if (!ce_path_match(ce
, pathspec
, NULL
))
191 i
= unmerge_index_entry_at(istate
, i
);