Skip to content

Commit 94a5728

Browse files
iabervongitster
authored andcommitted
Library function to check for unmerged index entries
It's small, but it was in three places already, so it should be in the library. Signed-off-by: Daniel Barkalow <[email protected]>
1 parent 922d87f commit 94a5728

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

builtin-merge-recursive.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,11 @@ static int git_merge_trees(int index_only,
223223
return rc;
224224
}
225225

226-
static int unmerged_index(void)
227-
{
228-
int i;
229-
for (i = 0; i < active_nr; i++) {
230-
struct cache_entry *ce = active_cache[i];
231-
if (ce_stage(ce))
232-
return 1;
233-
}
234-
return 0;
235-
}
236-
237226
struct tree *write_tree_from_memory(void)
238227
{
239228
struct tree *result = NULL;
240229

241-
if (unmerged_index()) {
230+
if (unmerged_cache()) {
242231
int i;
243232
output(0, "There are unmerged index entries:");
244233
for (i = 0; i < active_nr; i++) {
@@ -1524,7 +1513,7 @@ int merge_trees(struct tree *head,
15241513
sha1_to_hex(head->object.sha1),
15251514
sha1_to_hex(merge->object.sha1));
15261515

1527-
if (unmerged_index()) {
1516+
if (unmerged_cache()) {
15281517
struct path_list *entries, *re_head, *re_merge;
15291518
int i;
15301519
path_list_clear(&current_file_set, 1);

builtin-reset.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ static inline int is_merge(void)
4444
return !access(git_path("MERGE_HEAD"), F_OK);
4545
}
4646

47-
static int unmerged_files(void)
48-
{
49-
int i;
50-
read_cache();
51-
for (i = 0; i < active_nr; i++) {
52-
struct cache_entry *ce = active_cache[i];
53-
if (ce_stage(ce))
54-
return 1;
55-
}
56-
return 0;
57-
}
58-
5947
static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
6048
{
6149
int i = 0;
@@ -250,7 +238,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
250238
* at all, but requires them in a good order. Other resets reset
251239
* the index file to the tree object we are switching to. */
252240
if (reset_type == SOFT) {
253-
if (is_merge() || unmerged_files())
241+
if (is_merge() || read_cache() < 0 || unmerged_cache())
254242
die("Cannot do a soft reset in the middle of a merge.");
255243
}
256244
else if (reset_index_file(sha1, (reset_type == HARD)))

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ extern struct index_state the_index;
208208
#define read_cache_from(path) read_index_from(&the_index, (path))
209209
#define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
210210
#define discard_cache() discard_index(&the_index)
211+
#define unmerged_cache() unmerged_index(&the_index)
211212
#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
212213
#define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
213214
#define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
@@ -302,6 +303,7 @@ extern int read_index(struct index_state *);
302303
extern int read_index_from(struct index_state *, const char *path);
303304
extern int write_index(struct index_state *, int newfd);
304305
extern int discard_index(struct index_state *);
306+
extern int unmerged_index(struct index_state *);
305307
extern int verify_path(const char *path);
306308
extern int index_name_exists(struct index_state *istate, const char *name, int namelen);
307309
extern int index_name_pos(struct index_state *, const char *name, int namelen);

read-cache.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,16 @@ int discard_index(struct index_state *istate)
11761176
return 0;
11771177
}
11781178

1179+
int unmerged_index(struct index_state *istate)
1180+
{
1181+
int i;
1182+
for (i = 0; i < istate->cache_nr; i++) {
1183+
if (ce_stage(istate->cache[i]))
1184+
return 1;
1185+
}
1186+
return 0;
1187+
}
1188+
11791189
#define WRITE_BUFFER_SIZE 8192
11801190
static unsigned char write_buffer[WRITE_BUFFER_SIZE];
11811191
static unsigned long write_buffer_len;

0 commit comments

Comments
 (0)