Skip to content

Commit b101793

Browse files
newrengitster
authored andcommitted
move index_has_changes() from builtin/am.c to merge.c for reuse
index_has_changes() is a function we want to reuse outside of just am, making it also available for merge-recursive and merge-ort. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eab3f28 commit b101793

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

builtin/am.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,43 +1142,6 @@ static void refresh_and_write_cache(void)
11421142
die(_("unable to write index file"));
11431143
}
11441144

1145-
/**
1146-
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
1147-
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
1148-
* strbuf is provided, the space-separated list of files that differ will be
1149-
* appended to it.
1150-
*/
1151-
static int index_has_changes(struct strbuf *sb)
1152-
{
1153-
struct object_id head;
1154-
int i;
1155-
1156-
if (!get_oid_tree("HEAD", &head)) {
1157-
struct diff_options opt;
1158-
1159-
diff_setup(&opt);
1160-
DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
1161-
if (!sb)
1162-
DIFF_OPT_SET(&opt, QUICK);
1163-
do_diff_cache(&head, &opt);
1164-
diffcore_std(&opt);
1165-
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
1166-
if (i)
1167-
strbuf_addch(sb, ' ');
1168-
strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
1169-
}
1170-
diff_flush(&opt);
1171-
return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
1172-
} else {
1173-
for (i = 0; sb && i < active_nr; i++) {
1174-
if (i)
1175-
strbuf_addch(sb, ' ');
1176-
strbuf_addstr(sb, active_cache[i]->name);
1177-
}
1178-
return !!active_nr;
1179-
}
1180-
}
1181-
11821145
/**
11831146
* Dies with a user-friendly message on how to proceed after resolving the
11841147
* problem. This message can be overridden with state->resolvemsg.

cache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,15 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
608608
extern int discard_index(struct index_state *);
609609
extern void move_index_extensions(struct index_state *dst, struct index_state *src);
610610
extern int unmerged_index(const struct index_state *);
611+
612+
/**
613+
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
614+
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
615+
* strbuf is provided, the space-separated list of files that differ will be
616+
* appended to it.
617+
*/
618+
extern int index_has_changes(struct strbuf *sb);
619+
611620
extern int verify_path(const char *path);
612621
extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
613622
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);

merge.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "cache.h"
2+
#include "diff.h"
3+
#include "diffcore.h"
24
#include "lockfile.h"
35
#include "commit.h"
46
#include "run-command.h"
@@ -15,6 +17,37 @@ static const char *merge_argument(struct commit *commit)
1517
return EMPTY_TREE_SHA1_HEX;
1618
}
1719

20+
int index_has_changes(struct strbuf *sb)
21+
{
22+
struct object_id head;
23+
int i;
24+
25+
if (!get_oid_tree("HEAD", &head)) {
26+
struct diff_options opt;
27+
28+
diff_setup(&opt);
29+
DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
30+
if (!sb)
31+
DIFF_OPT_SET(&opt, QUICK);
32+
do_diff_cache(&head, &opt);
33+
diffcore_std(&opt);
34+
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
35+
if (i)
36+
strbuf_addch(sb, ' ');
37+
strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
38+
}
39+
diff_flush(&opt);
40+
return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
41+
} else {
42+
for (i = 0; sb && i < active_nr; i++) {
43+
if (i)
44+
strbuf_addch(sb, ' ');
45+
strbuf_addstr(sb, active_cache[i]->name);
46+
}
47+
return !!active_nr;
48+
}
49+
}
50+
1851
int try_merge_command(const char *strategy, size_t xopts_nr,
1952
const char **xopts, struct commit_list *common,
2053
const char *head_arg, struct commit_list *remotes)

0 commit comments

Comments
 (0)