Skip to contentSkip to conversation

Implement git commit as a builtin.

PATCH In review Kristian Høgsberg 3 participants 4 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement git commit as a builtin.

---
Here's another update on the work in progress. At this point, the C version is almost complete, there's only a few issues left (look for FIXME in builtin-commit.c). I've added a commit test case which should be split out in a patch on its own, but the good news is that it successfully exercises most of the command line options and the C version passes.

My plan for the remainder of the work is still to wrap up the last few pieces of functionality and then start taking this big patch apart in a number of more manageable pieces. However, the bulk of this work will still be a big patch that removes git-commit.sh and adds builtin-commit in one swoop.

Kristian

 Makefile              |    9 +-
 builtin-add.c         |   14 +-
 builtin-commit-tree.c |  120 +++++---
 builtin-commit.c      |  746 +++++++++++++++++++++++++++++++++++++++++++++++++
 builtin.h             |    3 +-
 cache.h               |    3 +-
 color.c               |   18 +-
 color.h               |    4 +-
 commit.h              |    9 +
 git-commit.sh         |  658 -------------------------------------------
 git.c                 |    3 +-
 mktag.c               |    8 +-
 sha1_file.c           |   44 ++-
 t/t7800-commit.sh     |  126 +++++++++
 wt-status.c           |   86 +++---
 wt-status.h           |    4 +
 16 files changed, 1066 insertions(+), 789 deletions(-)
 create mode 100644 builtin-commit.c
 delete mode 100755 git-commit.sh
 create mode 100644 t/t7800-commit.sh
Makefile +3 -6
index 0f75955..967d5a5 100644
@@ -198,7 +198,7 @@ BASIC_LDFLAGS =
 
 SCRIPT_SH = \
 	git-bisect.sh git-checkout.sh \
-	git-clean.sh git-clone.sh git-commit.sh \
+	git-clean.sh git-clone.sh \
 	git-fetch.sh \
 	git-ls-remote.sh \
 	git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
@@ -257,7 +257,7 @@ EXTRA_PROGRAMS =
 BUILT_INS = \
 	git-format-patch$X git-show$X git-whatchanged$X git-cherry$X \
 	git-get-tar-commit-id$X git-init$X git-repo-config$X \
-	git-fsck-objects$X git-cherry-pick$X \
+	git-fsck-objects$X git-cherry-pick$X git-status$X\
 	$(patsubst builtin-%.o,git-%$X,$(BUILTIN_OBJS))
 
 # what 'all' will build and 'install' will install, in gitexecdir
@@ -332,6 +332,7 @@ BUILTIN_OBJS = \
 	builtin-check-attr.o \
 	builtin-checkout-index.o \
 	builtin-check-ref-format.o \
+	builtin-commit.o \
 	builtin-commit-tree.o \
 	builtin-count-objects.o \
 	builtin-describe.o \
@@ -367,7 +368,6 @@ BUILTIN_OBJS = \
 	builtin-rev-parse.o \
 	builtin-revert.o \
 	builtin-rm.o \
-	builtin-runstatus.o \
 	builtin-shortlog.o \
 	builtin-show-branch.o \
 	builtin-stripspace.o \
@@ -791,9 +791,6 @@ $(patsubst %.perl,%,$(SCRIPT_PERL)): % : %.perl
 	chmod +x $@+ && \
 	mv $@+ $@
 
-git-status: git-commit
-	$(QUIET_GEN)cp $< $@+ && mv $@+ $@
-
 gitweb/gitweb.cgi: gitweb/gitweb.perl
 	$(QUIET_GEN)rm -f $@ $@+ && \
 	sed -e '1s|#!.*perl|#!$(PERL_PATH_SQ)|' \
builtin-add.c +10 -4
index 1591171..bcd796d 100644
@@ -8,6 +8,7 @@
 #include "dir.h"
 #include "exec_cmd.h"
 #include "cache-tree.h"
+#include "run-command.h"
 #include "diff.h"
 #include "diffcore.h"
 #include "commit.h"
@@ -148,6 +149,13 @@ static int git_add_config(const char *var, const char *value)
 	return git_default_config(var, value);
 }
 
+int interactive_add(void)
+{
+	const char *argv[2] = { "add--interactive", NULL };
+
+	return run_command_v_opt(argv, RUN_GIT_CMD);
+}
+
 static struct lock_file lock_file;
 
 static const char ignore_warning[] =
@@ -167,11 +175,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 			add_interactive++;
 	}
 	if (add_interactive) {
-		const char *args[] = { "add--interactive", NULL };
-
-		if (add_interactive != 1 || argc != 2)
+		if (argc != 2)
 			die("add --interactive does not take any parameters");
-		execv_git_cmd(args);
+		interactive_add();
 		exit(1);
 	}
 
builtin-commit-tree.c +74 -46

163 lines not on this page · raw message

builtin-commit.c new file +746 -0

746 lines not on this page · raw message

builtin.h +2 -1
index da4834c..7f395da 100644
@@ -23,6 +23,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
+extern int cmd_commit(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_describe(int argc, const char **argv, const char *prefix);
@@ -63,10 +64,10 @@ extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
 extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
 extern int cmd_revert(int argc, const char **argv, const char *prefix);
 extern int cmd_rm(int argc, const char **argv, const char *prefix);
-extern int cmd_runstatus(int argc, const char **argv, const char *prefix);
 extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
 extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_status(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
 extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
cache.h +2 -1
index 5e7381e..0403ada 100644
@@ -245,7 +245,8 @@ extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat
 extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);
 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
-extern int read_pipe(int fd, char** return_buf, unsigned long* return_size);
+extern int read_fd(int fd, char** return_buf, unsigned long* return_size);
+extern int read_path(const char *path, char** return_buf, unsigned long* return_size);
 extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
color.c +9 -9
index 09d82ee..124ba33 100644
@@ -135,39 +135,39 @@ int git_config_colorbool(const char *var, const char *value)
 	return git_config_bool(var, value);
 }
 
-static int color_vprintf(const char *color, const char *fmt,
+static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
 		va_list args, const char *trail)
 {
 	int r = 0;
 
 	if (*color)
-		r += printf("%s", color);
-	r += vprintf(fmt, args);
+		r += fprintf(fp, "%s", color);
+	r += vfprintf(fp, fmt, args);
 	if (*color)
-		r += printf("%s", COLOR_RESET);
+		r += fprintf(fp, "%s", COLOR_RESET);
 	if (trail)
-		r += printf("%s", trail);
+		r += fprintf(fp, "%s", trail);
 	return r;
 }
 
 
 
-int color_printf(const char *color, const char *fmt, ...)
+int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
 {
 	va_list args;
 	int r;
 	va_start(args, fmt);
-	r = color_vprintf(color, fmt, args, NULL);
+	r = color_vfprintf(fp, color, fmt, args, NULL);
 	va_end(args);
 	return r;
 }
 
-int color_printf_ln(const char *color, const char *fmt, ...)
+int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
 {
 	va_list args;
 	int r;
 	va_start(args, fmt);
-	r = color_vprintf(color, fmt, args, "\n");
+	r = color_vfprintf(fp, color, fmt, args, "\n");
 	va_end(args);
 	return r;
 }
color.h +2 -2
index 88bb8ff..6809800 100644
@@ -6,7 +6,7 @@
 
 int git_config_colorbool(const char *var, const char *value);
 void color_parse(const char *var, const char *value, char *dst);
-int color_printf(const char *color, const char *fmt, ...);
-int color_printf_ln(const char *color, const char *fmt, ...);
+int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
+int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
 
 #endif /* COLOR_H */
commit.h +9 -0
index a313b53..a07e379 100644
@@ -122,4 +122,13 @@ extern struct commit_list *get_shallow_commits(struct object_array *heads,
 		int depth, int shallow_flag, int not_shallow_flag);
 
 int in_merge_bases(struct commit *, struct commit **, int);
+
+const unsigned char *
+create_commit(const unsigned char *tree_sha1,
+	      unsigned char parent_sha1[][20], int parents,
+	      const char *author_info, const char *committer_info,
+	      const char *message, int length);
+
+int interactive_add(void);
+
 #endif /* COMMIT_H */
git-commit.sh deleted +0 -658

658 lines not on this page · raw message

git.c +2 -1
index 29b55a1..4018e3c 100644
@@ -237,6 +237,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
 		{ "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE },
 		{ "cherry", cmd_cherry, RUN_SETUP },
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE },
+		{ "commit", cmd_commit, RUN_SETUP },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config },
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
@@ -279,10 +280,10 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
 		{ "rev-parse", cmd_rev_parse, RUN_SETUP },
 		{ "revert", cmd_revert, RUN_SETUP | NOT_BARE },
 		{ "rm", cmd_rm, RUN_SETUP | NOT_BARE },
-		{ "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
 		{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
 		{ "show-branch", cmd_show_branch, RUN_SETUP },
 		{ "show", cmd_show, RUN_SETUP | USE_PAGER },
+		{ "status", cmd_status, RUN_SETUP | NOT_BARE },
 		{ "stripspace", cmd_stripspace },
 		{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
 		{ "tar-tree", cmd_tar_tree },
mktag.c +3 -5
index b82e377..26b9ebf 100644
@@ -111,8 +111,8 @@ static int verify_tag(char *buffer, unsigned long size)
 
 int main(int argc, char **argv)
 {
-	unsigned long size = 4096;
-	char *buffer = xmalloc(size);
+	unsigned long size;
+	char *buffer;
 	unsigned char result_sha1[20];
 
 	if (argc != 1)
@@ -120,10 +120,8 @@ int main(int argc, char **argv)
 
 	setup_git_directory();
 
-	if (read_pipe(0, &buffer, &size)) {
-		free(buffer);
+	if (read_fd(0, &buffer, &size))
 		die("could not read from stdin");
-	}
 
 	/* Verify it for some basic sanity: it needs to start with
 	   "object <sha1>\ntype\ntagger " */
sha1_file.c +30 -14

69 lines not on this page · raw message

t/t7800-commit.sh new file +126 -0

126 lines not on this page · raw message

wt-status.c +44 -42

197 lines not on this page · raw message

wt-status.h +4 -0
index cfea4ae..7744932 100644
@@ -1,6 +1,8 @@
 #ifndef STATUS_H
 #define STATUS_H
 
+#include <stdio.h>
+
 enum color_wt_status {
 	WT_STATUS_HEADER,
 	WT_STATUS_UPDATED,
@@ -19,6 +21,8 @@ struct wt_status {
 	int commitable;
 	int workdir_dirty;
 	int workdir_untracked;
+	const char *index_file;
+	FILE *fp;
 };
 
 int git_status_config(const char *var, const char *value);
Signature · 1 line
1.5.2.GIT
#1 lore raw

2007/7/18, Kristian Høgsberg <[email protected]>:

Quoted 7 lines
builtin-commit.c new file +7 -0
+       if (buffer) {
+               len = strip_lines(buffer, len);
+
+               if (fwrite(buffer, 1, len, fp) < len)
+                       die("could not write commit template: %s\n",
+                           strerror(errno));
+       }

....

builtin-commit.c line 673

Quoted 5 lines
builtin-commit.c new file +5 -0
+       len = strip_lines(buffer, len);
+
+       if (message_is_empty(buffer, len))
+               die("* no commit message?  aborting commit.");
+

Hi Kristian, you could call to the new stripspace() function in builtin-stripspace.c, to reduce code in your file. The only thing you should consider is that the new stripspace() removes always the last '\n' in the file (if any), so you have to add it when you need.

I sent a patch to change the name of read_pipe with read_fd (not accepted yet), and to terminate the buffer with NUL (although perhaps you don't need that), and to allow the function to receive buffer NULL or size 0.
Your version was a lot different, therefore you would need to set buffer and size before calling it
and free the buffer if reading failed after that:
http://article.gmane.org/gmane.comp.version-control.git/52835

Now I need to create the new file editor.c to group at least three functions related with reading and editing text. Those also will reduce the size of your code and also can be reused from builtin-tag.c: launch_editor, read_path (as read_file someone said), and stripspace.

The first problem now should be how to write a version for launch_editor() which both builtins (yours and mine) could share, because the references in your launch_editor() related to commits, removed already in my version for git-tag.

I did this job trying to make easier your porting by moving those parts shared with builtin-tag.c outside your code and generating discussion on them, so you
can concentrate just in the specific parts and reuse the rest. I hope those can help you.

---
Carlos Rica

#2 lore raw
Quoted 9 lines · Carlos Rica

2007/7/18, Kristian Høgsberg <[email protected]>:

Quoted 7 lines
builtin-commit.c new file +7 -0
+       if (buffer) {
+               len = strip_lines(buffer, len);
+
+               if (fwrite(buffer, 1, len, fp) < len)
+                       die("could not write commit template: %s\n",
+                           strerror(errno));
+       }

....

Quoted 11 lines

builtin-commit.c line 673

Quoted 5 lines
builtin-commit.c new file +5 -0
+       len = strip_lines(buffer, len);
+
+       if (message_is_empty(buffer, len))
+               die("* no commit message?  aborting commit.");
+

Hi Kristian, you could call to the new stripspace() function in builtin-stripspace.c, to reduce code in your file. The only thing you should consider is that the new stripspace() removes always the last '\n' in the file (if any), so you have to add it when you need.

Yup, I'm keeping an eye on all your changes, and it looks like it's all falling in place nicely. Once I have the last few pieces of functionality ported over I'll rebase and update builtin-commit to use the new functionality you've landed.

A couple of things that might be interesting right now is: 1) pick out the test case I made for commit and get it upstream, and 2) take the changes I did to wt-status.c (making it able to run on a different index and output to a specified FILE pointer) and get that upstream.

cheers,
Kristian

#3 lore raw
In reply to Kristian Høgsberg
Quoted 8 lines · Kristian Høgsberg

---
Here's another update on the work in progress. At this point, the C version is almost complete, there's only a few issues left (look for FIXME in builtin-commit.c). I've added a commit test case which should be split out in a patch on its own, but the good news is that it successfully exercises most of the command line options and the C version passes.

Quoted 5 lines

My plan for the remainder of the work is still to wrap up the last few pieces of functionality and then start taking this big patch apart in a number of more manageable pieces. However, the bulk of this work will still be a big patch that removes git-commit.sh and adds builtin-commit in one swoop.

Please move git-commit.sh to contrib/examples instead of deleting it.

Nicolas