23
#include "merge-recursive.h"
24
#include "dir.h"
25
#include "submodule.h"
26
+#include "revision.h"
27
28
struct path_hashmap_entry {
29
struct hashmap_entry e;
978
return merge_status;
979
}
980
981
+static int find_first_merges(struct object_array *result, const char *path,
982
+ struct commit *a, struct commit *b)
983
+{
984
+ int i, j;
985
+ struct object_array merges = OBJECT_ARRAY_INIT;
986
+ struct commit *commit;
987
+ int contains_another;
988
+
989
+ char merged_revision[42];
990
+ const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
991
+ "--all", merged_revision, NULL };
992
+ struct rev_info revs;
993
+ struct setup_revision_opt rev_opts;
994
+
995
+ memset(result, 0, sizeof(struct object_array));
996
+ memset(&rev_opts, 0, sizeof(rev_opts));
997
+
998
+ /* get all revisions that merge commit a */
999
+ xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1000
+ oid_to_hex(&a->object.oid));
1001
+ init_revisions(&revs, NULL);
1002
+ rev_opts.submodule = path;
1003
+ /* FIXME: can't handle linked worktrees in submodules yet */
1004
+ revs.single_worktree = path != NULL;
1005
+ setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1006
+
1007
+ /* save all revisions from the above list that contain b */
1008
+ if (prepare_revision_walk(&revs))
1009
+ die("revision walk setup failed");
1010
+ while ((commit = get_revision(&revs)) != NULL) {
1011
+ struct object *o = &(commit->object);
1012
+ if (in_merge_bases(b, commit))
1013
+ add_object_array(o, NULL, &merges);
1014
+ }
1015
+ reset_revision_walk();
1016
+
1017
+ /* Now we've got all merges that contain a and b. Prune all
1018
+ * merges that contain another found merge and save them in
1019
+ * result.
1020
+ */
1021
+ for (i = 0; i < merges.nr; i++) {
1022
+ struct commit *m1 = (struct commit *) merges.objects[i].item;
1023
+
1024
+ contains_another = 0;
1025
+ for (j = 0; j < merges.nr; j++) {
1026
+ struct commit *m2 = (struct commit *) merges.objects[j].item;
1027
+ if (i != j && in_merge_bases(m2, m1)) {
1028
+ contains_another = 1;
1029
+ break;
1030
+ }
1031
+ }
1032
+
1033
+ if (!contains_another)
1034
+ add_object_array(merges.objects[i].item, NULL, result);
1035
+ }
1036
+
1037
+ object_array_clear(&merges);
1038
+ return result->nr;
1039
+}
1040
+
1041
+static void print_commit(struct commit *commit)
1042
+{
1043
+ struct strbuf sb = STRBUF_INIT;
1044
+ struct pretty_print_context ctx = {0};
1045
+ ctx.date_mode.type = DATE_NORMAL;
1046
+ format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1047
+ fprintf(stderr, "%s\n", sb.buf);
1048
+ strbuf_release(&sb);
1049
+}
1050
+
1051
+#define MERGE_WARNING(path, msg) \
1052
+ warning("Failed to merge submodule %s (%s)", path, msg);
1053
+
1054
+static int merge_submodule(struct object_id *result, const char *path,
1055
+ const struct object_id *base, const struct object_id *a,
1056
+ const struct object_id *b, int search)
1057
+{
1058
+ struct commit *commit_base, *commit_a, *commit_b;
1059
+ int parent_count;
1060
+ struct object_array merges;
1061
+
1062
+ int i;
1063
+
1064
+ /* store a in result in case we fail */
1065
+ oidcpy(result, a);
1066
+
1067
+ /* we can not handle deletion conflicts */
1068
+ if (is_null_oid(base))
1069
+ return 0;
1070
+ if (is_null_oid(a))
1071
+ return 0;
1072
+ if (is_null_oid(b))
1073
+ return 0;
1074
+
1075
+ if (add_submodule_odb(path)) {
1076
+ MERGE_WARNING(path, "not checked out");
1077
+ return 0;
1078
+ }
1079
+
1080
+ if (!(commit_base = lookup_commit_reference(base)) ||
1081
+ !(commit_a = lookup_commit_reference(a)) ||
1082
+ !(commit_b = lookup_commit_reference(b))) {
1083
+ MERGE_WARNING(path, "commits not present");
1084
+ return 0;
1085
+ }
1086
+
1087
+ /* check whether both changes are forward */
1088
+ if (!in_merge_bases(commit_base, commit_a) ||
1089
+ !in_merge_bases(commit_base, commit_b)) {
1090
+ MERGE_WARNING(path, "commits don't follow merge-base");
1091
+ return 0;
1092
+ }
1093
+
1094
+ /* Case #1: a is contained in b or vice versa */
1095
+ if (in_merge_bases(commit_a, commit_b)) {
1096
+ oidcpy(result, b);
1097
+ return 1;
1098
+ }
1099
+ if (in_merge_bases(commit_b, commit_a)) {
1100
+ oidcpy(result, a);
1101
+ return 1;
1102
+ }
1103
+
1104
+ /*
1105
+ * Case #2: There are one or more merges that contain a and b in
1106
+ * the submodule. If there is only one, then present it as a
1107
+ * suggestion to the user, but leave it marked unmerged so the
1108
+ * user needs to confirm the resolution.
1109
+ */
1110
+
1111
+ /* Skip the search if makes no sense to the calling context. */
1112
+ if (!search)
1113
+ return 0;
1114
+
1115
+ /* find commit which merges them */
1116
+ parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1117
+ switch (parent_count) {
1118
+ case 0:
1119
+ MERGE_WARNING(path, "merge following commits not found");
1120
+ break;
1121
+
1122
+ case 1:
1123
+ MERGE_WARNING(path, "not fast-forward");
1124
+ fprintf(stderr, "Found a possible merge resolution "
1125
+ "for the submodule:\n");
1126
+ print_commit((struct commit *) merges.objects[0].item);
1127
+ fprintf(stderr,
1128
+ "If this is correct simply add it to the index "
1129
+ "for example\n"
1130
+ "by using:\n\n"
1131
+ " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1132
+ "which will accept this suggestion.\n",
1133
+ oid_to_hex(&merges.objects[0].item->oid), path);
1134
+ break;
1135
+
1136
+ default:
1137
+ MERGE_WARNING(path, "multiple merges found");
1138
+ for (i = 0; i < merges.nr; i++)
1139
+ print_commit((struct commit *) merges.objects[i].item);
1140
+ }
1141
+
1142
+ object_array_clear(&merges);
1143
+ return 0;
1144
+}
1145
+
1146
static int merge_file_1(struct merge_options *o,
1147
const struct diff_filespec *one,
1148
const struct diff_filespec *a,