1 #include "git-compat-util.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
);
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
);
28 lost
->util
= xcalloc(1, sizeof(*ui
));
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
;
43 strbuf_addstr(sb
, item
->string
);
45 for (i
= 0; i
< 3; i
++)
46 strbuf_addf(sb
, "%o%c", ui
->mode
[i
], 0);
47 for (i
= 0; i
< 3; i
++) {
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
;
61 const unsigned rawsz
= the_hash_algo
->rawsz
;
63 CALLOC_ARRAY(resolve_undo
, 1);
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(resolve_undo
, data
);
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 oidread(&ui
->oid
[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
|= RESOLVE_UNDO_CHANGED
;
120 int unmerge_index_entry_at(struct index_state
*istate
, int pos
)
122 const struct cache_entry
*ce
;
123 struct string_list_item
*item
;
124 struct resolve_undo_info
*ru
;
125 int i
, err
= 0, matched
;
128 if (!istate
->resolve_undo
)
131 ce
= istate
->cache
[pos
];
133 /* already unmerged */
134 while ((pos
< istate
->cache_nr
) &&
135 ! strcmp(istate
->cache
[pos
]->name
, ce
->name
))
137 return pos
- 1; /* return the last entry processed */
139 item
= string_list_lookup(istate
->resolve_undo
, ce
->name
);
145 matched
= ce
->ce_flags
& CE_MATCHED
;
146 name
= xstrdup(ce
->name
);
147 remove_index_entry_at(istate
, pos
);
148 for (i
= 0; i
< 3; i
++) {
149 struct cache_entry
*nce
;
152 nce
= make_cache_entry(istate
,
157 nce
->ce_flags
|= CE_MATCHED
;
158 if (add_index_entry(istate
, nce
, ADD_CACHE_OK_TO_ADD
)) {
160 error("cannot unmerge '%s'", name
);
168 return unmerge_index_entry_at(istate
, pos
);
171 void unmerge_marked_index(struct index_state
*istate
)
175 if (!istate
->resolve_undo
)
178 /* TODO: audit for interaction with sparse-index. */
179 ensure_full_index(istate
);
180 for (i
= 0; i
< istate
->cache_nr
; i
++) {
181 const struct cache_entry
*ce
= istate
->cache
[i
];
182 if (ce
->ce_flags
& CE_MATCHED
)
183 i
= unmerge_index_entry_at(istate
, i
);
187 void unmerge_index(struct index_state
*istate
, const struct pathspec
*pathspec
)
191 if (!istate
->resolve_undo
)
194 /* TODO: audit for interaction with sparse-index. */
195 ensure_full_index(istate
);
196 for (i
= 0; i
< istate
->cache_nr
; i
++) {
197 const struct cache_entry
*ce
= istate
->cache
[i
];
198 if (!ce_path_match(istate
, ce
, pathspec
, NULL
))
200 i
= unmerge_index_entry_at(istate
, i
);