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

Add builtin merge-file, a minimal replacement for RCS merge

merge-file has the same syntax as RCS merge, but supports only the "-L" option. For good measure, a test is added, which is quite minimal, though. [jc: further fix for compliation errors included.] Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Dec 6, 2006 at 16:26 UTC ba1f5f353775ddbf97bc0d543888783630693023
5 files changed +191
Makefile
+1
@@ -275,6 +275,7 @@ BUILTIN_OBJS = \
275 builtin-ls-tree.o \
276 builtin-mailinfo.o \
277 builtin-mailsplit.o \
278 + builtin-merge-file.o \
279 builtin-mv.o \
280 builtin-name-rev.o \
281 builtin-pack-objects.o \
builtin-merge-file.c new
+72
@@ -0,0 +1,72 @@
1 +#include "cache.h"
2 +#include "xdiff/xdiff.h"
3 +
4 +static const char merge_file_usage[] =
5 +"git merge-file [-L name1 [-L orig [-L name2]]] file1 orig_file file2";
6 +
7 +static int read_file(mmfile_t *ptr, const char *filename)
8 +{
9 + struct stat st;
10 + FILE *f;
11 +
12 + if (stat(filename, &st))
13 + return error("Could not stat %s", filename);
14 + if ((f = fopen(filename, "rb")) == NULL)
15 + return error("Could not open %s", filename);
16 + ptr->ptr = xmalloc(st.st_size);
17 + if (fread(ptr->ptr, st.st_size, 1, f) != 1)
18 + return error("Could not read %s", filename);
19 + fclose(f);
20 + ptr->size = st.st_size;
21 + return 0;
22 +}
23 +
24 +int cmd_merge_file(int argc, char **argv, char **envp)
25 +{
26 + char *names[3];
27 + mmfile_t mmfs[3];
28 + mmbuffer_t result = {NULL, 0};
29 + xpparam_t xpp = {XDF_NEED_MINIMAL};
30 + int ret = 0, i = 0;
31 +
32 + while (argc > 4) {
33 + if (!strcmp(argv[1], "-L")) {
34 + names[i++] = argv[2];
35 + argc -= 2;
36 + argv += 2;
37 + continue;
38 + }
39 + usage(merge_file_usage);
40 + }
41 +
42 + if (argc != 4)
43 + usage(merge_file_usage);
44 +
45 + for (; i < 3; i++)
46 + names[i] = argv[i + 1];
47 +
48 + for (i = 0; i < 3; i++)
49 + if (read_file(mmfs + i, argv[i + 1]))
50 + return -1;
51 +
52 + ret = xdl_merge(mmfs + 1, mmfs + 0, names[0], mmfs + 2, names[2],
53 + &xpp, XDL_MERGE_ZEALOUS, &result);
54 +
55 + for (i = 0; i < 3; i++)
56 + free(mmfs[i].ptr);
57 +
58 + if (ret >= 0) {
59 + char *filename = argv[1];
60 + FILE *f = fopen(filename, "wb");
61 +
62 + if (!f)
63 + ret = error("Could not open %s for writing", filename);
64 + else if (fwrite(result.ptr, result.size, 1, f) != 1)
65 + ret = error("Could not write to %s", filename);
66 + else if (fclose(f))
67 + ret = error("Could not close %s", filename);
68 + free(result.ptr);
69 + }
70 +
71 + return ret;
72 +}
builtin.h
+1
@@ -42,6 +42,7 @@ extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
42 extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
43 extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
44 extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
45 +extern int cmd_merge_file(int argc, const char **argv, const char *prefix);
46 extern int cmd_mv(int argc, const char **argv, const char *prefix);
47 extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
48 extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
git.c
+1
@@ -247,6 +247,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
247 { "ls-tree", cmd_ls_tree, RUN_SETUP },
248 { "mailinfo", cmd_mailinfo },
249 { "mailsplit", cmd_mailsplit },
250 + { "merge-file", cmd_merge_file },
251 { "mv", cmd_mv, RUN_SETUP },
252 { "name-rev", cmd_name_rev, RUN_SETUP },
253 { "pack-objects", cmd_pack_objects, RUN_SETUP },
t/t6023-merge-file.sh new
+116
@@ -0,0 +1,116 @@
1 +#!/bin/sh
2 +
3 +test_description='RCS merge replacement: merge-file'
4 +. ./test-lib.sh
5 +
6 +cat > orig.txt << EOF
7 +Dominus regit me,
8 +et nihil mihi deerit.
9 +In loco pascuae ibi me collocavit,
10 +super aquam refectionis educavit me;
11 +animam meam convertit,
12 +deduxit me super semitas jusitiae,
13 +propter nomen suum.
14 +EOF
15 +
16 +cat > new1.txt << EOF
17 +Dominus regit me,
18 +et nihil mihi deerit.
19 +In loco pascuae ibi me collocavit,
20 +super aquam refectionis educavit me;
21 +animam meam convertit,
22 +deduxit me super semitas jusitiae,
23 +propter nomen suum.
24 +Nam et si ambulavero in medio umbrae mortis,
25 +non timebo mala, quoniam tu mecum es:
26 +virga tua et baculus tuus ipsa me consolata sunt.
27 +EOF
28 +
29 +cat > new2.txt << EOF
30 +Dominus regit me, et nihil mihi deerit.
31 +In loco pascuae ibi me collocavit,
32 +super aquam refectionis educavit me;
33 +animam meam convertit,
34 +deduxit me super semitas jusitiae,
35 +propter nomen suum.
36 +EOF
37 +
38 +cat > new3.txt << EOF
39 +DOMINUS regit me,
40 +et nihil mihi deerit.
41 +In loco pascuae ibi me collocavit,
42 +super aquam refectionis educavit me;
43 +animam meam convertit,
44 +deduxit me super semitas jusitiae,
45 +propter nomen suum.
46 +EOF
47 +
48 +cat > new4.txt << EOF
49 +Dominus regit me, et nihil mihi deerit.
50 +In loco pascuae ibi me collocavit,
51 +super aquam refectionis educavit me;
52 +animam meam convertit,
53 +deduxit me super semitas jusitiae,
54 +EOF
55 +echo -n "propter nomen suum." >> new4.txt
56 +
57 +cp new1.txt test.txt
58 +test_expect_success "merge without conflict" \
59 + "git-merge-file test.txt orig.txt new2.txt"
60 +
61 +cp new1.txt test2.txt
62 +test_expect_success "merge without conflict (missing LF at EOF)" \
63 + "git-merge-file test2.txt orig.txt new2.txt"
64 +
65 +test_expect_success "merge result added missing LF" \
66 + "diff -u test.txt test2.txt"
67 +
68 +cp test.txt backup.txt
69 +test_expect_failure "merge with conflicts" \
70 + "git-merge-file test.txt orig.txt new3.txt"
71 +
72 +cat > expect.txt << EOF
73 +<<<<<<< test.txt
74 +Dominus regit me, et nihil mihi deerit.
75 +=======
76 +DOMINUS regit me,
77 +et nihil mihi deerit.
78 +>>>>>>> new3.txt
79 +In loco pascuae ibi me collocavit,
80 +super aquam refectionis educavit me;
81 +animam meam convertit,
82 +deduxit me super semitas jusitiae,
83 +propter nomen suum.
84 +Nam et si ambulavero in medio umbrae mortis,
85 +non timebo mala, quoniam tu mecum es:
86 +virga tua et baculus tuus ipsa me consolata sunt.
87 +EOF
88 +
89 +test_expect_success "expected conflict markers" "diff -u test.txt expect.txt"
90 +
91 +cp backup.txt test.txt
92 +test_expect_failure "merge with conflicts, using -L" \
93 + "git-merge-file -L 1 -L 2 test.txt orig.txt new3.txt"
94 +
95 +cat > expect.txt << EOF
96 +<<<<<<< 1
97 +Dominus regit me, et nihil mihi deerit.
98 +=======
99 +DOMINUS regit me,
100 +et nihil mihi deerit.
101 +>>>>>>> new3.txt
102 +In loco pascuae ibi me collocavit,
103 +super aquam refectionis educavit me;
104 +animam meam convertit,
105 +deduxit me super semitas jusitiae,
106 +propter nomen suum.
107 +Nam et si ambulavero in medio umbrae mortis,
108 +non timebo mala, quoniam tu mecum es:
109 +virga tua et baculus tuus ipsa me consolata sunt.
110 +EOF
111 +
112 +test_expect_success "expected conflict markers, with -L" \
113 + "diff -u test.txt expect.txt"
114 +
115 +test_done
116 +