4 #include "resolve-undo.h"
5 #include "string-list.h"
7 /* The only error case is to run out of memory in string-list */
8 void record_resolve_undo(struct index_state
*istate
, struct cache_entry
*ce
)
10 struct string_list_item
*lost
;
11 struct resolve_undo_info
*ui
;
12 struct string_list
*resolve_undo
;
13 int stage
= ce_stage(ce
);
18 if (!istate
->resolve_undo
) {
19 CALLOC_ARRAY(resolve_undo
, 1);
20 resolve_undo
->strdup_strings
= 1;
21 istate
->resolve_undo
= resolve_undo
;
23 resolve_undo
= istate
->resolve_undo
;
24 lost
= string_list_insert(resolve_undo
, ce
->name
);
26 lost
->util
= xcalloc(1, sizeof(*ui
));
28 oidcpy(&ui
->oid
[stage
- 1], &ce
->oid
);
29 ui
->mode
[stage
- 1] = ce
->ce_mode
;
32 void resolve_undo_write(struct strbuf
*sb
, struct string_list
*resolve_undo
)
34 struct string_list_item
*item
;
35 for_each_string_list_item(item
, resolve_undo
) {
36 struct resolve_undo_info
*ui
= item
->util
;
41 strbuf_addstr(sb
, item
->string
);
43 for (i
= 0; i
< 3; i
++)
44 strbuf_addf(sb
, "%o%c", ui
->mode
[i
], 0);
45 for (i
= 0; i
< 3; i
++) {
48 strbuf_add(sb
, ui
->oid
[i
].hash
, the_hash_algo
->rawsz
);
53 struct string_list
*resolve_undo_read(const char *data
, unsigned long size
)
55 struct string_list
*resolve_undo
;
59 const unsigned rawsz
= the_hash_algo
->rawsz
;
61 CALLOC_ARRAY(resolve_undo
, 1);
62 resolve_undo
->strdup_strings
= 1;
65 struct string_list_item
*lost
;
66 struct resolve_undo_info
*ui
;
68 len
= strlen(data
) + 1;
71 lost
= string_list_insert(resolve_undo
, data
);
73 lost
->util
= xcalloc(1, sizeof(*ui
));
78 for (i
= 0; i
< 3; i
++) {
79 ui
->mode
[i
] = strtoul(data
, &endptr
, 8);
80 if (!endptr
|| endptr
== data
|| *endptr
)
82 len
= (endptr
+ 1) - (char*)data
;
89 for (i
= 0; i
< 3; i
++) {
94 oidread(&ui
->oid
[i
], (const unsigned char *)data
);
102 string_list_clear(resolve_undo
, 1);
103 error("Index records invalid resolve-undo information");
107 void resolve_undo_clear_index(struct index_state
*istate
)
109 struct string_list
*resolve_undo
= istate
->resolve_undo
;
112 string_list_clear(resolve_undo
, 1);
114 istate
->resolve_undo
= NULL
;
115 istate
->cache_changed
|= RESOLVE_UNDO_CHANGED
;
118 int unmerge_index_entry_at(struct index_state
*istate
, int pos
)
120 const struct cache_entry
*ce
;
121 struct string_list_item
*item
;
122 struct resolve_undo_info
*ru
;
123 int i
, err
= 0, matched
;
126 if (!istate
->resolve_undo
)
129 ce
= istate
->cache
[pos
];
131 /* already unmerged */
132 while ((pos
< istate
->cache_nr
) &&
133 ! strcmp(istate
->cache
[pos
]->name
, ce
->name
))
135 return pos
- 1; /* return the last entry processed */
137 item
= string_list_lookup(istate
->resolve_undo
, ce
->name
);
143 matched
= ce
->ce_flags
& CE_MATCHED
;
144 name
= xstrdup(ce
->name
);
145 remove_index_entry_at(istate
, pos
);
146 for (i
= 0; i
< 3; i
++) {
147 struct cache_entry
*nce
;
150 nce
= make_cache_entry(istate
,
155 nce
->ce_flags
|= CE_MATCHED
;
156 if (add_index_entry(istate
, nce
, ADD_CACHE_OK_TO_ADD
)) {
158 error("cannot unmerge '%s'", name
);
166 return unmerge_index_entry_at(istate
, pos
);
169 void unmerge_marked_index(struct index_state
*istate
)
173 if (!istate
->resolve_undo
)
176 /* TODO: audit for interaction with sparse-index. */
177 ensure_full_index(istate
);
178 for (i
= 0; i
< istate
->cache_nr
; i
++) {
179 const struct cache_entry
*ce
= istate
->cache
[i
];
180 if (ce
->ce_flags
& CE_MATCHED
)
181 i
= unmerge_index_entry_at(istate
, i
);
185 void unmerge_index(struct index_state
*istate
, const struct pathspec
*pathspec
)
189 if (!istate
->resolve_undo
)
192 /* TODO: audit for interaction with sparse-index. */
193 ensure_full_index(istate
);
194 for (i
= 0; i
< istate
->cache_nr
; i
++) {
195 const struct cache_entry
*ce
= istate
->cache
[i
];
196 if (!ce_path_match(istate
, ce
, pathspec
, NULL
))
198 i
= unmerge_index_entry_at(istate
, i
);