Skip to contentSkip to conversation

Build in merge

PATCH In review Miklos Vajna 3 participants 10 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Build in merge

Hello,

This is a work in progress patch, reflecting the current status of builtin-merge I have. As far as I know all the tests pass, but there are still some work to do. In general, a few problems I'm aware of:

1) There are still TODOs/FIXMEs in the code.

2) It should be possible to avoid some exec() calls, like write-tree, show-branch and fmt-merge-msg.

3) Minor memory leaks.

Other than that, comments are welcome.

Miklos Vajna (5):
Move reset_index_file() to a new reset.c Move split_cmdline() to libgit
Make verbosity configurable in reset_index_file() Move commit_list_count() to commit.c Build in merge

 Makefile                                      |    4 +-
 alias.c                                       |   54 ++
 builtin-merge-recursive.c                     |    8 -
 builtin-merge.c                               | 1117 +++++++++++++++++++++++++
 builtin-reset.c                               |   19 +-
 builtin.h                                     |    1 +
 cache.h                                       |    1 +
 commit.c                                      |    8 +
 commit.h                                      |    1 +
 git-merge.sh => contrib/examples/git-merge.sh |    0 
 git.c                                         |   54 +--
 reset.c                                       |   29 +
 reset.h                                       |    6 +
 13 files changed, 1223 insertions(+), 79 deletions(-)
 create mode 100644 builtin-merge.c
 rename git-merge.sh => contrib/examples/git-merge.sh (100%)
 create mode 100644 reset.c
 create mode 100644 reset.h
#1 lore raw

PATCH 1/5 Move reset_index_file() to a new reset.c

This is needed as we want to use it later in builtin-merge.

---
 Makefile        |    2 ++
 builtin-reset.c |   17 +----------------
 reset.c         |   28 ++++++++++++++++++++++++++++
 reset.h         |    6 ++++++
 4 files changed, 37 insertions(+), 16 deletions(-)
 create mode 100644 reset.c
 create mode 100644 reset.h
Makefile +2 -0
index a2de075..91d938a 100644
@@ -364,6 +364,7 @@ LIB_H += quote.h
 LIB_H += reflog-walk.h
 LIB_H += refs.h
 LIB_H += remote.h
+LIB_H += reset.h
 LIB_H += revision.h
 LIB_H += run-command.h
 LIB_H += sha1-lookup.h
@@ -443,6 +444,7 @@ LIB_OBJS += read-cache.o
 LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
 LIB_OBJS += remote.o
+LIB_OBJS += reset.o
 LIB_OBJS += revision.o
 LIB_OBJS += run-command.o
 LIB_OBJS += server-info.o
builtin-reset.c +1 -16
index 79424bb..6e6e168 100644
@@ -18,6 +18,7 @@
 #include "tree.h"
 #include "branch.h"
 #include "parse-options.h"
+#include "reset.h"
 
 static const char * const git_reset_usage[] = {
 	"git-reset [--mixed | --soft | --hard] [-q] [<commit>]",
@@ -49,22 +50,6 @@ static inline int is_merge(void)
 	return !access(git_path("MERGE_HEAD"), F_OK);
 }
 
-static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
-{
-	int i = 0;
-	const char *args[6];
-
-	args[i++] = "read-tree";
-	args[i++] = "-v";
-	args[i++] = "--reset";
-	if (is_hard_reset)
-		args[i++] = "-u";
-	args[i++] = sha1_to_hex(sha1);
-	args[i] = NULL;
-
-	return run_command_v_opt(args, RUN_GIT_CMD);
-}
-
 static void print_new_head_line(struct commit *commit)
 {
 	const char *hex, *body;
reset.c new file +28 -0
new file mode 100644
index 0000000..a75fec6
@@ -0,0 +1,28 @@
+/*
+ * generic fucntion(s) from the "git reset" builtin command
+ *
+ * Copyright (c) 2007 Carlos Rica
+ *
+ * Based on git-reset.sh, which is
+ *
+ * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
+ */
+
+#include "cache.h"
+#include "run-command.h"
+
+int reset_index_file(const unsigned char *sha1, int is_hard_reset)
+{
+	int i = 0;
+	const char *args[6];
+
+	args[i++] = "read-tree";
+	args[i++] = "-v";
+	args[i++] = "--reset";
+	if (is_hard_reset)
+		args[i++] = "-u";
+	args[i++] = sha1_to_hex(sha1);
+	args[i] = NULL;
+
+	return run_command_v_opt(args, RUN_GIT_CMD);
+}
reset.h new file +6 -0
new file mode 100644
index 0000000..4ae235b
@@ -0,0 +1,6 @@
+#ifndef RESET_H
+#define RESET_H
+
+int reset_index_file(const unsigned char *sha1, int is_hard_reset);
+
+#endif
Signature · 1 line
1.5.5.1.357.g1af8b.dirty
#2 lore raw

PATCH 2/5 Move split_cmdline() to libgit

split_cmdline() is currently used for aliases only, but later it can be useful for other builtins as well. Move it to alias.c for now, indicating that originally it's for aliases, but we'll have it in libgit this way.

---
 alias.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cache.h |    1 +
 git.c   |   53 -----------------------------------------------------
 3 files changed, 55 insertions(+), 53 deletions(-)
alias.c +54 -0
index 116cac8..75bc48a 100644
@@ -20,3 +20,57 @@ char *alias_lookup(const char *alias)
 	git_config(alias_lookup_cb);
 	return alias_val;
 }
+
+int split_cmdline(char *cmdline, const char ***argv)
+{
+	int src, dst, count = 0, size = 16;
+	char quoted = 0;
+
+	*argv = xmalloc(sizeof(char*) * size);
+
+	/* split alias_string */
+	(*argv)[count++] = cmdline;
+	for (src = dst = 0; cmdline[src];) {
+		char c = cmdline[src];
+		if (!quoted && isspace(c)) {
+			cmdline[dst++] = 0;
+			while (cmdline[++src]
+					&& isspace(cmdline[src]))
+				; /* skip */
+			if (count >= size) {
+				size += 16;
+				*argv = xrealloc(*argv, sizeof(char*) * size);
+			}
+			(*argv)[count++] = cmdline + dst;
+		} else if (!quoted && (c == '\'' || c == '"')) {
+			quoted = c;
+			src++;
+		} else if (c == quoted) {
+			quoted = 0;
+			src++;
+		} else {
+			if (c == '\\' && quoted != '\'') {
+				src++;
+				c = cmdline[src];
+				if (!c) {
+					free(*argv);
+					*argv = NULL;
+					return error("cmdline ends with \\");
+				}
+			}
+			cmdline[dst++] = c;
+			src++;
+		}
+	}
+
+	cmdline[dst] = 0;
+
+	if (quoted) {
+		free(*argv);
+		*argv = NULL;
+		return error("unclosed quote");
+	}
+
+	return count;
+}
+
cache.h +1 -0
index 093f04c..5e22b1e 100644
@@ -814,5 +814,6 @@ int report_path_error(const char *ps_matched, const char **pathspec, int prefix_
 void overlay_tree_on_cache(const char *tree_name, const char *prefix);
 
 char *alias_lookup(const char *alias);
+int split_cmdline(char *cmdline, const char ***argv);
 
 #endif /* CACHE_H */
git.c +0 -53

59 lines not on this page · raw message

Signature · 1 line
1.5.5.1.357.g1af8b.dirty
#3 lore raw

PATCH 3/5 Make verbosity configurable in reset_index_file()

Till now reset_index_file() was always verbose. Add a new argument to be able to disable this behaviour.

---
 builtin-reset.c |    2 +-
 reset.c         |    5 +++--
 reset.h         |    2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
builtin-reset.c +1 -1
index 6e6e168..179c59c 100644
@@ -216,7 +216,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 		if (is_merge() || read_cache() < 0 || unmerged_cache())
 			die("Cannot do a soft reset in the middle of a merge.");
 	}
-	else if (reset_index_file(sha1, (reset_type == HARD)))
+	else if (reset_index_file(sha1, (reset_type == HARD), 1))
 		die("Could not reset index file to revision '%s'.", rev);
 
 	/* Any resets update HEAD to the head being switched to,
reset.c +3 -2
index a75fec6..baae947 100644
@@ -11,13 +11,14 @@
 #include "cache.h"
 #include "run-command.h"
 
-int reset_index_file(const unsigned char *sha1, int is_hard_reset)
+int reset_index_file(const unsigned char *sha1, int is_hard_reset, int verbose)
 {
 	int i = 0;
 	const char *args[6];
 
 	args[i++] = "read-tree";
-	args[i++] = "-v";
+	if (verbose)
+		args[i++] = "-v";
 	args[i++] = "--reset";
 	if (is_hard_reset)
 		args[i++] = "-u";
reset.h +1 -1
index 4ae235b..c43ae22 100644
@@ -1,6 +1,6 @@
 #ifndef RESET_H
 #define RESET_H
 
-int reset_index_file(const unsigned char *sha1, int is_hard_reset);
+int reset_index_file(const unsigned char *sha1, int is_hard_reset, int verbose);
 
 #endif
Signature · 1 line
1.5.5.1.357.g1af8b.dirty
#4 lore raw

PATCH 4/5 Move commit_list_count() to commit.c

This function is useful outside builtin-merge-recursive, for example in builtin-merge.

---
 builtin-merge-recursive.c |    8 --------
 commit.c                  |    8 ++++++++
 commit.h                  |    1 +
 3 files changed, 9 insertions(+), 8 deletions(-)
builtin-merge-recursive.c +0 -8
index 46e636f..7aaa215 100644
@@ -42,14 +42,6 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two)
  * - *(int *)commit->object.sha1 set to the virtual id.
  */
 
-static unsigned commit_list_count(const struct commit_list *l)
-{
-	unsigned c = 0;
-	for (; l; l = l->next )
-		c++;
-	return c;
-}
-
 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
 {
 	struct commit *commit = xcalloc(1, sizeof(struct commit));
commit.c +8 -0
index 94d5b3d..b45ec9b 100644
@@ -331,6 +331,14 @@ struct commit_list *commit_list_insert(struct commit *item, struct commit_list *
 	return new_list;
 }
 
+unsigned commit_list_count(const struct commit_list *l)
+{
+	unsigned c = 0;
+	for (; l; l = l->next )
+		c++;
+	return c;
+}
+
 void free_commit_list(struct commit_list *list)
 {
 	while (list) {
commit.h +1 -0
index 2d94d41..7f8c5ee 100644
@@ -41,6 +41,7 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
 int parse_commit(struct commit *item);
 
 struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p);
+unsigned commit_list_count(const struct commit_list *l);
 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list);
 
 void free_commit_list(struct commit_list *list);
Signature · 1 line
1.5.5.1.357.g1af8b.dirty
#5 lore raw

PATCH 5/5 Build in merge

---
 Makefile                                      |    2 +-
 builtin-merge.c                               | 1117 +++++++++++++++++++++++++
 builtin.h                                     |    1 +
 git-merge.sh => contrib/examples/git-merge.sh |    0 
 git.c                                         |    1 +
 5 files changed, 1120 insertions(+), 1 deletions(-)
 create mode 100644 builtin-merge.c
 rename git-merge.sh => contrib/examples/git-merge.sh (100%)
Makefile +1 -1
index 91d938a..9820932 100644
@@ -241,7 +241,6 @@ SCRIPT_SH += git-lost-found.sh
 SCRIPT_SH += git-merge-octopus.sh
 SCRIPT_SH += git-merge-one-file.sh
 SCRIPT_SH += git-merge-resolve.sh
-SCRIPT_SH += git-merge.sh
 SCRIPT_SH += git-merge-stupid.sh
 SCRIPT_SH += git-mergetool.sh
 SCRIPT_SH += git-parse-remote.sh
@@ -509,6 +508,7 @@ BUILTIN_OBJS += builtin-ls-remote.o
 BUILTIN_OBJS += builtin-ls-tree.o
 BUILTIN_OBJS += builtin-mailinfo.o
 BUILTIN_OBJS += builtin-mailsplit.o
+BUILTIN_OBJS += builtin-merge.o
 BUILTIN_OBJS += builtin-merge-base.o
 BUILTIN_OBJS += builtin-merge-file.o
 BUILTIN_OBJS += builtin-merge-ours.o
builtin-merge.c new file +1117 -0

1,117 lines not on this page · raw message

builtin.h +1 -0
index 95126fd..7c26d75 100644
@@ -54,6 +54,7 @@ extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_ls_remote(int argc, const char **argv, const char *prefix);
 extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
 extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
+extern int cmd_merge(int argc, const char **argv, const char *prefix);
 extern int cmd_merge_base(int argc, const char **argv, const char *prefix);
 extern int cmd_merge_ours(int argc, const char **argv, const char *prefix);
 extern int cmd_merge_file(int argc, const char **argv, const char *prefix);
contrib/examples/git-merge.sh renamed from git-merge.sh +0 -0
similarity index 100%
rename from git-merge.sh
rename to contrib/examples/git-merge.sh
git.c +1 -0
index b8df19e..fced435 100644
@@ -267,6 +267,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "ls-remote", cmd_ls_remote },
 		{ "mailinfo", cmd_mailinfo },
 		{ "mailsplit", cmd_mailsplit },
+		{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
 		{ "merge-base", cmd_merge_base, RUN_SETUP },
 		{ "merge-file", cmd_merge_file },
 		{ "merge-ours", cmd_merge_ours, RUN_SETUP },
Signature · 1 line
1.5.5.1.357.g1af8b.dirty
#8 lore raw

Re: [PATCH 1/5] Move reset_index_file() to a new reset.c

In reply to Miklos Vajna
Quoted 91 lines · Miklos Vajna

This is needed as we want to use it later in builtin-merge.

---
 Makefile        |    2 ++
 builtin-reset.c |   17 +----------------
 reset.c         |   28 ++++++++++++++++++++++++++++
 reset.h         |    6 ++++++
 4 files changed, 37 insertions(+), 16 deletions(-)
 create mode 100644 reset.c
 create mode 100644 reset.h
Makefile +2 -0
index a2de075..91d938a 100644
@@ -364,6 +364,7 @@ LIB_H += quote.h
 LIB_H += reflog-walk.h
 LIB_H += refs.h
 LIB_H += remote.h
+LIB_H += reset.h
 LIB_H += revision.h
 LIB_H += run-command.h
 LIB_H += sha1-lookup.h
@@ -443,6 +444,7 @@ LIB_OBJS += read-cache.o
 LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
 LIB_OBJS += remote.o
+LIB_OBJS += reset.o
 LIB_OBJS += revision.o
 LIB_OBJS += run-command.o
 LIB_OBJS += server-info.o
builtin-reset.c +1 -16
index 79424bb..6e6e168 100644
@@ -18,6 +18,7 @@
 #include "tree.h"
 #include "branch.h"
 #include "parse-options.h"
+#include "reset.h"
 
 static const char * const git_reset_usage[] = {
 	"git-reset [--mixed | --soft | --hard] [-q] [<commit>]",
@@ -49,22 +50,6 @@ static inline int is_merge(void)
 	return !access(git_path("MERGE_HEAD"), F_OK);
 }
 
-static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
-{
-	int i = 0;
-	const char *args[6];
-
-	args[i++] = "read-tree";
-	args[i++] = "-v";
-	args[i++] = "--reset";
-	if (is_hard_reset)
-		args[i++] = "-u";
-	args[i++] = sha1_to_hex(sha1);
-	args[i] = NULL;
-
-	return run_command_v_opt(args, RUN_GIT_CMD);
-}
-
 static void print_new_head_line(struct commit *commit)
 {
 	const char *hex, *body;
reset.c new file +18 -0
new file mode 100644
index 0000000..a75fec6
@@ -0,0 +1,28 @@
+/*
+ * generic fucntion(s) from the "git reset" builtin command
+ *
+ * Copyright (c) 2007 Carlos Rica
+ *
+ * Based on git-reset.sh, which is
+ *
+ * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
+ */
+
+#include "cache.h"
+#include "run-command.h"
+
+int reset_index_file(const unsigned char *sha1, int is_hard_reset)
+{
+	int i = 0;
+	const char *args[6];
+

reset.c line 28

Quoted 10 lines
reset.c new file +10 -0
+	args[i++] = "read-tree";
+	args[i++] = "-v";
+	args[i++] = "--reset";
+	if (is_hard_reset)
+		args[i++] = "-u";
+	args[i++] = sha1_to_hex(sha1);
+	args[i] = NULL;
+
+	return run_command_v_opt(args, RUN_GIT_CMD);
+}

You might as well use unpack_trees() directly here, instead of running a separate command. In fact, you might skip making a function here, and just call unpack_trees() where you'd use it. (And maybe have a function to initialize a struct unpack_tree_options to some common defaults to make it easy to see what's being done.)

-Daniel
*This .sig left intentionally blank*

#9 lore raw

Re: [PATCH 1/5] Move reset_index_file() to a new reset.c

Quoted 5 lines · Daniel Barkalow

You might as well use unpack_trees() directly here, instead of running a separate command. In fact, you might skip making a function here, and just call unpack_trees() where you'd use it. (And maybe have a function to initialize a struct unpack_tree_options to some common defaults to make it easy to see what's being done.)

Thanks for the suggestion, I did so; as a result now patch #1 and #3 are no longer necessary.

(I haven't sent out a new series yet, but it's in
git://repo.or.cz/git/vmiklos.git builtin-merge.)

Maybe builtin-reset could use unpack_trees() directly as well? I don't know if it's just a question of "nobody did it yet", or there is some reason for not using unpack_trees() there.

#6 lore raw

Re: [PATCH 3/5] Make verbosity configurable in reset_index_file()

In reply to Miklos Vajna
Quoted 28 lines · Miklos Vajna

Till now reset_index_file() was always verbose. Add a new argument to be able to disable this behaviour.

---
 builtin-reset.c |    2 +-
 reset.c         |    5 +++--
 reset.h         |    2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
builtin-reset.c +1 -1
index 6e6e168..179c59c 100644
@@ -216,7 +216,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 		if (is_merge() || read_cache() < 0 || unmerged_cache())
 			die("Cannot do a soft reset in the middle of a merge.");
 	}
-	else if (reset_index_file(sha1, (reset_type == HARD)))
+	else if (reset_index_file(sha1, (reset_type == HARD), 1))
 		die("Could not reset index file to revision '%s'.", rev);
 
 	/* Any resets update HEAD to the head being switched to,
reset.c +0 -0
index a75fec6..baae947 100644

reset.c line 14

Quoted 5 lines
reset.c +1 -1
@@ -11,13 +11,14 @@
 #include "cache.h"
 #include "run-command.h"
 
-int reset_index_file(const unsigned char *sha1, int is_hard_reset)
+int reset_index_file(const unsigned char *sha1, int is_hard_reset, int verbose)

I would suggest using flags here instead of a bunch of boolean args. Consider the readability of

    reset_index_file(sha1, 1, 1);

vs

    reset_index_file(sha1, RESET_HARD | RESET_VERBOSE);

cheers,
Kristian

#7 lore raw

Re: [PATCH 3/5] Make verbosity configurable in reset_index_file()

Quoted 11 lines · Kristian H?gsberg

reset.c line 14

Quoted 2 lines
reset.c +1 -1
-int reset_index_file(const unsigned char *sha1, int is_hard_reset)
+int reset_index_file(const unsigned char *sha1, int is_hard_reset, int verbose)

I would suggest using flags here instead of a bunch of boolean args. Consider the readability of

    reset_index_file(sha1, 1, 1);

vs

    reset_index_file(sha1, RESET_HARD | RESET_VERBOSE);

Thanks for the suggestion, but - as Daniel already pointed out - the same functionality can be achieved by using the unpack_trees() API directly, so the patch is useless.