@matheusbsbs / matheus-bagliano / commits / b7fa51da9b
Fork 0

merge-recursive: get rid of the index_only global variable

struct merge_options already has a call_depth member, and index_only global variable always equals to !!call_depth. We always use index_only as a condition, so we can just use call_depth instead of index_only. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>

Miklos Vajna committed Sep 2, 2008 at 23:53 UTC b7fa51da9be3cee9e2ca31af4edfec18b8ecbce7
1 file changed +67 -73
merge-recursive.c
+67 -73
@@ -165,17 +165,6 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
165 return add_cache_entry(ce, options);
166 }
167
168 -/*
169 - * This is a global variable which is used in a number of places but
170 - * only written to in the 'merge' function.
171 - *
172 - * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
173 - * don't update the working directory.
174 - * 0 => Leave unmerged entries in the cache and update
175 - * the working directory.
176 - */
177 -static int index_only = 0;
178 -
168 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
169 {
170 parse_tree(tree);
@@ -428,10 +417,11 @@ static int remove_path(const char *name)
417 return ret;
418 }
419
431 -static int remove_file(int clean, const char *path, int no_wd)
420 +static int remove_file(struct merge_options *o, int clean,
421 + const char *path, int no_wd)
422 {
433 - int update_cache = index_only || clean;
434 - int update_working_directory = !index_only && !no_wd;
423 + int update_cache = o->call_depth || clean;
424 + int update_working_directory = !o->call_depth && !no_wd;
425
426 if (update_cache) {
427 if (remove_file_from_cache(path))
@@ -509,13 +499,14 @@ static int make_room_for_path(const char *path)
499 return error(msg, path, ": perhaps a D/F conflict?");
500 }
501
512 -static void update_file_flags(const unsigned char *sha,
502 +static void update_file_flags(struct merge_options *o,
503 + const unsigned char *sha,
504 unsigned mode,
505 const char *path,
506 int update_cache,
507 int update_wd)
508 {
518 - if (index_only)
509 + if (o->call_depth)
510 update_wd = 0;
511
512 if (update_wd) {
@@ -574,12 +565,13 @@ static void update_file_flags(const unsigned char *sha,
565 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
566 }
567
577 -static void update_file(int clean,
568 +static void update_file(struct merge_options *o,
569 + int clean,
570 const unsigned char *sha,
571 unsigned mode,
572 const char *path)
573 {
582 - update_file_flags(sha, mode, path, index_only || clean, !index_only);
574 + update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
575 }
576
577 /* Low level file merging, update and removal */
@@ -609,8 +601,9 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
601 mm->size = size;
602 }
603
612 -static int merge_3way(mmbuffer_t *result_buf,
613 - struct diff_filespec *o,
604 +static int merge_3way(struct merge_options *o,
605 + mmbuffer_t *result_buf,
606 + struct diff_filespec *one,
607 struct diff_filespec *a,
608 struct diff_filespec *b,
609 const char *branch1,
@@ -623,13 +616,13 @@ static int merge_3way(mmbuffer_t *result_buf,
616 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
617 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
618
626 - fill_mm(o->sha1, &orig);
619 + fill_mm(one->sha1, &orig);
620 fill_mm(a->sha1, &src1);
621 fill_mm(b->sha1, &src2);
622
623 merge_status = ll_merge(result_buf, a->path, &orig,
624 &src1, name1, &src2, name2,
632 - index_only);
625 + o->call_depth);
626
627 free(name1);
628 free(name2);
@@ -639,9 +632,12 @@ static int merge_3way(mmbuffer_t *result_buf,
632 return merge_status;
633 }
634
642 -static struct merge_file_info merge_file(struct diff_filespec *o,
643 - struct diff_filespec *a, struct diff_filespec *b,
644 - const char *branch1, const char *branch2)
635 +static struct merge_file_info merge_file(struct merge_options *o,
636 + struct diff_filespec *one,
637 + struct diff_filespec *a,
638 + struct diff_filespec *b,
639 + const char *branch1,
640 + const char *branch2)
641 {
642 struct merge_file_info result;
643 result.merge = 0;
@@ -657,31 +653,31 @@ static struct merge_file_info merge_file(struct diff_filespec *o,
653 hashcpy(result.sha, b->sha1);
654 }
655 } else {
660 - if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
656 + if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
657 result.merge = 1;
658
659 /*
660 * Merge modes
661 */
666 - if (a->mode == b->mode || a->mode == o->mode)
662 + if (a->mode == b->mode || a->mode == one->mode)
663 result.mode = b->mode;
664 else {
665 result.mode = a->mode;
670 - if (b->mode != o->mode) {
666 + if (b->mode != one->mode) {
667 result.clean = 0;
668 result.merge = 1;
669 }
670 }
671
676 - if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, o->sha1))
672 + if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
673 hashcpy(result.sha, b->sha1);
678 - else if (sha_eq(b->sha1, o->sha1))
674 + else if (sha_eq(b->sha1, one->sha1))
675 hashcpy(result.sha, a->sha1);
676 else if (S_ISREG(a->mode)) {
677 mmbuffer_t result_buf;
678 int merge_status;
679
684 - merge_status = merge_3way(&result_buf, o, a, b,
680 + merge_status = merge_3way(o, &result_buf, one, a, b,
681 branch1, branch2);
682
683 if ((merge_status < 0) || !result_buf.ptr)
@@ -726,22 +722,22 @@ static void conflict_rename_rename(struct merge_options *o,
722 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
723 output(o, 1, "%s is a directory in %s adding as %s instead",
724 ren1_dst, branch2, dst_name1);
729 - remove_file(0, ren1_dst, 0);
725 + remove_file(o, 0, ren1_dst, 0);
726 }
727 if (string_list_has_string(&current_directory_set, ren2_dst)) {
728 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
729 output(o, 1, "%s is a directory in %s adding as %s instead",
730 ren2_dst, branch1, dst_name2);
735 - remove_file(0, ren2_dst, 0);
731 + remove_file(o, 0, ren2_dst, 0);
732 }
737 - if (index_only) {
733 + if (o->call_depth) {
734 remove_file_from_cache(dst_name1);
735 remove_file_from_cache(dst_name2);
736 /*
737 * Uncomment to leave the conflicting names in the resulting tree
738 *
743 - * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
744 - * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
739 + * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
740 + * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
741 */
742 } else {
743 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
@@ -757,8 +753,8 @@ static void conflict_rename_dir(struct merge_options *o,
753 {
754 char *new_path = unique_path(ren1->pair->two->path, branch1);
755 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
760 - remove_file(0, ren1->pair->two->path, 0);
761 - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
756 + remove_file(o, 0, ren1->pair->two->path, 0);
757 + update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
758 free(new_path);
759 }
760
@@ -773,9 +769,9 @@ static void conflict_rename_rename_2(struct merge_options *o,
769 output(o, 1, "Renaming %s to %s and %s to %s instead",
770 ren1->pair->one->path, new_path1,
771 ren2->pair->one->path, new_path2);
776 - remove_file(0, ren1->pair->two->path, 0);
777 - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
778 - update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
772 + remove_file(o, 0, ren1->pair->two->path, 0);
773 + update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
774 + update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
775 free(new_path2);
776 free(new_path1);
777 }
@@ -867,17 +863,18 @@ static int process_renames(struct merge_options *o,
863 "rename \"%s\"->\"%s\" in \"%s\"%s",
864 src, ren1_dst, branch1,
865 src, ren2_dst, branch2,
870 - index_only ? " (left unresolved)": "");
871 - if (index_only) {
866 + o->call_depth ? " (left unresolved)": "");
867 + if (o->call_depth) {
868 remove_file_from_cache(src);
873 - update_file(0, ren1->pair->one->sha1,
869 + update_file(o, 0, ren1->pair->one->sha1,
870 ren1->pair->one->mode, src);
871 }
872 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
873 } else {
874 struct merge_file_info mfi;
879 - remove_file(1, ren1_src, 1);
880 - mfi = merge_file(ren1->pair->one,
875 + remove_file(o, 1, ren1_src, 1);
876 + mfi = merge_file(o,
877 + ren1->pair->one,
878 ren1->pair->two,
879 ren2->pair->two,
880 branch1,
@@ -893,14 +890,14 @@ static int process_renames(struct merge_options *o,
890 ren1_dst);
891 clean_merge = 0;
892
896 - if (!index_only)
893 + if (!o->call_depth)
894 update_stages(ren1_dst,
895 ren1->pair->one,
896 ren1->pair->two,
897 ren2->pair->two,
898 1 /* clear */);
899 }
903 - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
900 + update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
901 }
902 } else {
903 /* Renamed in 1, maybe changed in 2 */
@@ -909,7 +906,7 @@ static int process_renames(struct merge_options *o,
906 struct diff_filespec src_other, dst_other;
907 int try_merge, stage = a_renames == renames1 ? 3: 2;
908
912 - remove_file(1, ren1_src, index_only || stage == 3);
909 + remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
910
911 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
912 src_other.mode = ren1->src_entry->stages[stage].mode;
@@ -931,7 +928,7 @@ static int process_renames(struct merge_options *o,
928 "and deleted in %s",
929 ren1_src, ren1_dst, branch1,
930 branch2);
934 - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
931 + update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
932 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
933 const char *new_path;
934 clean_merge = 0;
@@ -942,7 +939,7 @@ static int process_renames(struct merge_options *o,
939 ren1_dst, branch2);
940 new_path = unique_path(ren1_dst, branch2);
941 output(o, 1, "Adding as %s instead", new_path);
945 - update_file(0, dst_other.sha1, dst_other.mode, new_path);
942 + update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
943 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
944 ren2 = item->util;
945 clean_merge = 0;
@@ -969,7 +966,7 @@ static int process_renames(struct merge_options *o,
966 b = ren1->pair->two;
967 a = &src_other;
968 }
972 - mfi = merge_file(one, a, b,
969 + mfi = merge_file(o, one, a, b,
970 o->branch1, o->branch2);
971
972 if (mfi.clean &&
@@ -991,11 +988,11 @@ static int process_renames(struct merge_options *o,
988 ren1_dst);
989 clean_merge = 0;
990
994 - if (!index_only)
991 + if (!o->call_depth)
992 update_stages(ren1_dst,
993 one, a, b, 1);
994 }
998 - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
995 + update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
996 }
997 }
998 }
@@ -1037,7 +1034,7 @@ static int process_entry(struct merge_options *o,
1034 if (a_sha)
1035 output(o, 2, "Removing %s", path);
1036 /* do not touch working file if it did not exist */
1040 - remove_file(1, path, !a_sha);
1037 + remove_file(o, 1, path, !a_sha);
1038 } else {
1039 /* Deleted in one and changed in the other */
1040 clean_merge = 0;
@@ -1046,13 +1043,13 @@ static int process_entry(struct merge_options *o,
1043 "and modified in %s. Version %s of %s left in tree.",
1044 path, o->branch1,
1045 o->branch2, o->branch2, path);
1049 - update_file(0, b_sha, b_mode, path);
1046 + update_file(o, 0, b_sha, b_mode, path);
1047 } else {
1048 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1049 "and modified in %s. Version %s of %s left in tree.",
1050 path, o->branch2,
1051 o->branch1, o->branch1, path);
1055 - update_file(0, a_sha, a_mode, path);
1052 + update_file(o, 0, a_sha, a_mode, path);
1053 }
1054 }
1055
@@ -1084,11 +1081,11 @@ static int process_entry(struct merge_options *o,
1081 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1082 "Adding %s as %s",
1083 conf, path, other_branch, path, new_path);
1087 - remove_file(0, path, 0);
1088 - update_file(0, sha, mode, new_path);
1084 + remove_file(o, 0, path, 0);
1085 + update_file(o, 0, sha, mode, new_path);
1086 } else {
1087 output(o, 2, "Adding %s", path);
1091 - update_file(1, sha, mode, path);
1088 + update_file(o, 1, sha, mode, path);
1089 }
1090 } else if (a_sha && b_sha) {
1091 /* Case C: Added in both (check for same permissions) and */
@@ -1110,12 +1107,12 @@ static int process_entry(struct merge_options *o,
1107 hashcpy(b.sha1, b_sha);
1108 b.mode = b_mode;
1109
1113 - mfi = merge_file(&one, &a, &b,
1110 + mfi = merge_file(o, &one, &a, &b,
1111 o->branch1, o->branch2);
1112
1113 clean_merge = mfi.clean;
1114 if (mfi.clean)
1118 - update_file(1, mfi.sha, mfi.mode, path);
1115 + update_file(o, 1, mfi.sha, mfi.mode, path);
1116 else if (S_ISGITLINK(mfi.mode))
1117 output(o, 1, "CONFLICT (submodule): Merge conflict in %s "
1118 "- needs %s", path, sha1_to_hex(b.sha1));
@@ -1123,10 +1120,10 @@ static int process_entry(struct merge_options *o,
1120 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1121 reason, path);
1122
1126 - if (index_only)
1127 - update_file(0, mfi.sha, mfi.mode, path);
1123 + if (o->call_depth)
1124 + update_file(o, 0, mfi.sha, mfi.mode, path);
1125 else
1129 - update_file_flags(mfi.sha, mfi.mode, path,
1126 + update_file_flags(o, mfi.sha, mfi.mode, path,
1127 0 /* update_cache */, 1 /* update_working_directory */);
1128 }
1129 } else if (!o_sha && !a_sha && !b_sha) {
@@ -1134,7 +1131,7 @@ static int process_entry(struct merge_options *o,
1131 * this entry was deleted altogether. a_mode == 0 means
1132 * we had that path and want to actively remove it.
1133 */
1137 - remove_file(1, path, !a_mode);
1134 + remove_file(o, 1, path, !a_mode);
1135 } else
1136 die("Fatal merge failure, shouldn't happen.");
1137
@@ -1160,7 +1157,7 @@ int merge_trees(struct merge_options *o,
1157 return 1;
1158 }
1159
1163 - code = git_merge_trees(index_only, common, head, merge);
1160 + code = git_merge_trees(o->call_depth, common, head, merge);
1161
1162 if (code != 0)
1163 die("merging of trees %s and %s failed",
@@ -1195,7 +1192,7 @@ int merge_trees(struct merge_options *o,
1192 else
1193 clean = 1;
1194
1198 - if (index_only)
1195 + if (o->call_depth)
1196 *result = write_tree_from_memory(o);
1197
1198 return clean;
@@ -1281,16 +1278,13 @@ int merge_recursive(struct merge_options *o,
1278 }
1279
1280 discard_cache();
1284 - if (!o->call_depth) {
1281 + if (!o->call_depth)
1282 read_cache();
1286 - index_only = 0;
1287 - } else
1288 - index_only = 1;
1283
1284 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1285 &mrtree);
1286
1293 - if (index_only) {
1287 + if (o->call_depth) {
1288 *result = make_virtual_commit(mrtree, "merged tree");
1289 commit_list_insert(h1, &(*result)->parents);
1290 commit_list_insert(h2, &(*result)->parents->next);