1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "read-cache.h"
7 #include "resolve-undo.h"
8 #include "sparse-index.h"
9 #include "string-list.h"
11 /* The only error case is to run out of memory in string-list */
12 void record_resolve_undo(struct index_state
*istate
, struct cache_entry
*ce
)
14 struct string_list_item
*lost
;
15 struct resolve_undo_info
*ui
;
16 struct string_list
*resolve_undo
;
17 int stage
= ce_stage(ce
);
22 if (!istate
->resolve_undo
) {
23 CALLOC_ARRAY(resolve_undo
, 1);
24 resolve_undo
->strdup_strings
= 1;
25 istate
->resolve_undo
= resolve_undo
;
27 resolve_undo
= istate
->resolve_undo
;
28 lost
= string_list_insert(resolve_undo
, ce
->name
);
30 lost
->util
= xcalloc(1, sizeof(*ui
));
32 oidcpy(&ui
->oid
[stage
- 1], &ce
->oid
);
33 ui
->mode
[stage
- 1] = ce
->ce_mode
;
36 void resolve_undo_write(struct strbuf
*sb
, struct string_list
*resolve_undo
)
38 struct string_list_item
*item
;
39 for_each_string_list_item(item
, resolve_undo
) {
40 struct resolve_undo_info
*ui
= item
->util
;
45 strbuf_addstr(sb
, item
->string
);
47 for (i
= 0; i
< 3; i
++)
48 strbuf_addf(sb
, "%o%c", ui
->mode
[i
], 0);
49 for (i
= 0; i
< 3; i
++) {
52 strbuf_add(sb
, ui
->oid
[i
].hash
, the_hash_algo
->rawsz
);
57 struct string_list
*resolve_undo_read(const char *data
, unsigned long size
)
59 struct string_list
*resolve_undo
;
63 const unsigned rawsz
= the_hash_algo
->rawsz
;
65 CALLOC_ARRAY(resolve_undo
, 1);
66 resolve_undo
->strdup_strings
= 1;
69 struct string_list_item
*lost
;
70 struct resolve_undo_info
*ui
;
72 len
= strlen(data
) + 1;
75 lost
= string_list_insert(resolve_undo
, data
);
77 lost
->util
= xcalloc(1, sizeof(*ui
));
82 for (i
= 0; i
< 3; i
++) {
83 ui
->mode
[i
] = strtoul(data
, &endptr
, 8);
84 if (!endptr
|| endptr
== data
|| *endptr
)
86 len
= (endptr
+ 1) - (char*)data
;
93 for (i
= 0; i
< 3; i
++) {
98 oidread(&ui
->oid
[i
], (const unsigned char *)data
,
99 the_repository
->hash_algo
);
107 string_list_clear(resolve_undo
, 1);
108 error("Index records invalid resolve-undo information");
112 void resolve_undo_clear_index(struct index_state
*istate
)
114 struct string_list
*resolve_undo
= istate
->resolve_undo
;
117 string_list_clear(resolve_undo
, 1);
119 istate
->resolve_undo
= NULL
;
120 istate
->cache_changed
|= RESOLVE_UNDO_CHANGED
;
123 int unmerge_index_entry(struct index_state
*istate
, const char *path
,
124 struct resolve_undo_info
*ru
, unsigned ce_flags
)
126 int i
= index_name_pos(istate
, path
, strlen(path
));
131 if (i
< istate
->cache_nr
&&
132 !strcmp(istate
->cache
[i
]->name
, path
))
133 /* yes, it is already unmerged */
135 /* fallthru: resolved to removal */
137 /* merged - remove it to replace it with unmerged entries */
138 remove_index_entry_at(istate
, i
);
141 for (i
= 0; i
< 3; i
++) {
142 struct cache_entry
*ce
;
145 ce
= make_cache_entry(istate
, ru
->mode
[i
], &ru
->oid
[i
],
147 ce
->ce_flags
|= ce_flags
;
148 if (add_index_entry(istate
, ce
, ADD_CACHE_OK_TO_ADD
))
149 return error("cannot unmerge '%s'", path
);
154 void unmerge_index(struct index_state
*istate
, const struct pathspec
*pathspec
,
157 struct string_list_item
*item
;
159 if (!istate
->resolve_undo
)
162 /* TODO: audit for interaction with sparse-index. */
163 ensure_full_index(istate
);
165 for_each_string_list_item(item
, istate
->resolve_undo
) {
166 const char *path
= item
->string
;
167 struct resolve_undo_info
*ru
= item
->util
;
170 if (!match_pathspec(istate
, pathspec
,
171 item
->string
, strlen(item
->string
),
174 unmerge_index_entry(istate
, path
, ru
, ce_flags
);