Skip to contentSkip to conversation

Port git commit to C.

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Port git commit to C.

This makes git commit a builtin and moves git-commit.sh to contrib/examples. This also removes the git-runstatus helper, which was mostly just a git-status.sh implementation detail.

---

This has Johannes' updates and Bjoerns command line parsing fixes. Also fixes the author date problem on amend commits, so this replaces Johannes' recent [PATCH 3/3] author fix.

cheers,
Kristian

 Makefile                       |    9 +-
 builtin-commit.c               |  615 +++++++++++++++++++++++++++++++++++++++
 builtin.h                      |    3 +-
 contrib/examples/git-commit.sh |  628 ++++++++++++++++++++++++++++++++++++++++
 git-commit.sh                  |  628 ----------------------------------------
 git.c                          |    3 +-
 strbuf.h                       |    1 +
 7 files changed, 1251 insertions(+), 636 deletions(-)
 create mode 100644 builtin-commit.c
 create mode 100755 contrib/examples/git-commit.sh
 delete mode 100755 git-commit.sh
Makefile +3 -6
index 3ec1876..e4c51c6 100644
@@ -209,7 +209,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-ls-remote.sh \
 	git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
 	git-pull.sh git-rebase.sh git-rebase--interactive.sh \
@@ -256,7 +256,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
@@ -326,6 +326,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 \
@@ -366,7 +367,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 \
@@ -832,9 +832,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) $@ $@+ && \
 	sed -e '1s|#!.*perl|#!$(PERL_PATH_SQ)|' \
builtin-commit.c new file +615 -0

615 lines not on this page · raw message

builtin.h +2 -1
index 2335c01..3d2de27 100644
@@ -24,6 +24,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);
@@ -69,11 +70,11 @@ 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_send_pack(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_tag(int argc, const char **argv, const char *prefix);
contrib/examples/git-commit.sh new file +628 -0

628 lines not on this page · raw message

git-commit.sh deleted +0 -628

628 lines not on this page · raw message

git.c +2 -1
index 19a2172..1016e04 100644
@@ -298,6 +298,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
 		{ "cherry", cmd_cherry, RUN_SETUP },
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config },
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
@@ -347,11 +348,11 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "rev-parse", cmd_rev_parse, RUN_SETUP },
 		{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
 		{ "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
-		{ "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
 		{ "send-pack", cmd_send_pack, RUN_SETUP },
 		{ "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 | NEED_WORK_TREE },
 		{ "stripspace", cmd_stripspace },
 		{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
 		{ "tag", cmd_tag, RUN_SETUP },
strbuf.h +1 -0
index 9b9e861..9720826 100644
@@ -113,5 +113,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 
 extern void stripspace(struct strbuf *buf, int skip_comments);
+extern void launch_editor(const char *path, struct strbuf *buffer);
 
 #endif /* STRBUF_H */
Signature · 1 line
1.5.3.4
#1 lore raw

Kristian Høgsberg, Thu, Nov 08, 2007 17:59:00 +0100:

Quoted 3 lines

This makes git commit a builtin and moves git-commit.sh to contrib/examples. This also removes the git-runstatus helper, which was mostly just a git-status.sh implementation detail.

Applied instead of 00c8febf563da on Junio's pu it breaks t1400:

* expecting success: echo TEST >F &&

     git add F &&
         GIT_AUTHOR_DATE="2005-05-26 23:30" \
         GIT_COMMITTER_DATE="2005-05-26 23:30" git-commit -m add -a &&
         h_TEST=$(git rev-parse --verify HEAD)
         echo The other day this did not work. >M &&
         echo And then Bob told me how to fix it. >>M &&
         echo OTHER >F &&
         GIT_AUTHOR_DATE="2005-05-26 23:41" \
         GIT_COMMITTER_DATE="2005-05-26 23:41" git-commit -F M -a &&
         h_OTHER=$(git rev-parse --verify HEAD) &&
         GIT_AUTHOR_DATE="2005-05-26 23:44" \
         GIT_COMMITTER_DATE="2005-05-26 23:44" git-commit --amend &&
         h_FIXED=$(git rev-parse --verify HEAD) &&
         echo Merged initial commit and a later commit. >M &&
         echo $h_TEST >.git/MERGE_HEAD &&
         GIT_AUTHOR_DATE="2005-05-26 23:45" \
         GIT_COMMITTER_DATE="2005-05-26 23:45" git-commit -F M &&
         h_MERGED=$(git rev-parse --verify HEAD)
         rm -f M
Created initial commit 2bc82dd: add
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 F
Created commit d244b72: The other day this did not work.
 1 files changed, 1 insertions(+), 1 deletions(-)

launching editor, log (null)
fatal: * no commit message? aborting commit.
* ok 28: creating initial files

* expecting success: diff expect .git/logs/refs/heads/master
3,4d2
< d244b725ca2c5f8aaacd9df2468b890499380862 C O Mitter <[email protected]> 1117151040 +0000 commit (amend): The other day this did not work.
< C O Mitter <[email protected]> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
* FAIL 29: git-commit logged updates
diff expect .git/logs/refs/heads/master

Which is not the test actually failed. The failed one is 28, but the last "rm -f M" killed the error because of missed "&&" before it.

I believe you need something like this to fix the test:

t/t1400-update-ref.sh +1 -1
index ce045b2..a90824b 100755
@@ -205,7 +205,7 @@ test_expect_success \
 	 echo $h_TEST >.git/MERGE_HEAD &&
 	 GIT_AUTHOR_DATE="2005-05-26 23:45" \
 	 GIT_COMMITTER_DATE="2005-05-26 23:45" git-commit -F M &&
-	 h_MERGED=$(git rev-parse --verify HEAD)
+	 h_MERGED=$(git rev-parse --verify HEAD) &&
 	 rm -f M'
 
 cat >expect <<EOF

and something like this to refill the strbuf of commit message with the text read from the amended commit (the failed test was a "git commit --amend"). The patch is on top of yours "Export launch_editor() and make it accept ':' as a no-op editor":

builtin-tag.c +11 -11
index c3b76da..8ca9ffb 100644
@@ -42,17 +42,17 @@ void launch_editor(const char *path, struct strbuf *buffer)
 	if (!editor)
 		editor = "vi";
 
-	if (!strcmp(editor, ":"))
-		return;
-
-	memset(&child, 0, sizeof(child));
-	child.argv = args;
-	args[0] = editor;
-	args[1] = path;
-	args[2] = NULL;
-
-	if (run_command(&child))
-		die("There was a problem with the editor %s.", editor);
+	if (strcmp(editor, ":"))
+	{
+		memset(&child, 0, sizeof(child));
+		child.argv = args;
+		args[0] = editor;
+		args[1] = path;
+		args[2] = NULL;
+
+		if (run_command(&child))
+			die("There was a problem with the editor %s.", editor);
+	}
 
 	if (strbuf_read_file(buffer, path, 0) < 0)
 		die("could not read message file '%s': %s",
#2 lore raw

PATCH 1/1 stop t1400 hiding errors in tests

The last rm in the test was lacking an "&&" before it, which caused the errors in the commands be silently hidden.

---
Alex Riesen, Fri, Nov 09, 2007 00:27:52 +0100:

Quoted 11 lines

Kristian Høgsberg, Thu, Nov 08, 2007 17:59:00 +0100:

Quoted 1 line

This makes git commit a builtin and moves git-commit.sh to

Applied instead of 00c8febf563da on Junio's pu it breaks t1400:

* FAIL 29: git-commit logged updates
diff expect .git/logs/refs/heads/master

Which is not the test actually failed. The failed one is 28, but the last "rm -f M" killed the error because of missed "&&" before it.

just separated the fix into a patch of its own. Johannes already took care of the problem with launch_editor not reading the message file.

 t/t1400-update-ref.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
t/t1400-update-ref.sh +1 -1
index ce045b2..a90824b 100755
@@ -205,7 +205,7 @@ test_expect_success \
 	 echo $h_TEST >.git/MERGE_HEAD &&
 	 GIT_AUTHOR_DATE="2005-05-26 23:45" \
 	 GIT_COMMITTER_DATE="2005-05-26 23:45" git-commit -F M &&
-	 h_MERGED=$(git rev-parse --verify HEAD)
+	 h_MERGED=$(git rev-parse --verify HEAD) &&
 	 rm -f M'
 
 cat >expect <<EOF
Signature · 1 line
1.5.3.5.626.g8f33b
#3 lore raw
In reply to Alex Riesen
Quoted 6 lines · Alex Riesen

Kristian Høgsberg, Thu, Nov 08, 2007 17:59:00 +0100:

Quoted 3 lines

This makes git commit a builtin and moves git-commit.sh to contrib/examples. This also removes the git-runstatus helper, which was mostly just a git-status.sh implementation detail.

Applied instead of 00c8febf563da on Junio's pu it breaks t1400:

Don't worry. It will pass with two of the Dscho's patches.

The racy-git issue is still there, though.

#4 lore raw
In reply to Kristian Høgsberg

A huge diff.

I'll quote the difference between this version and the version previously parked on 'pu', and comment, after re-reverting what you reverted from the parked version (which has minor tweaks such as removing git-runstatus from .gitignore I made previously).

Quoted 8 lines
 builtin-commit.c |   88 +++++++++++++++++++++++++++---------------------------
 1 files changed, 44 insertions(+), 44 deletions(-)
builtin-commit.c +0 -0
index f108e90..669cc6b 100644

builtin-commit.c static char *prepare_index(const char **files, const char *prefix) · line 77

Quoted 8 lines
builtin-commit.c +5 -0
@@ -70,25 +70,22 @@ static char *prepare_index(const char **files, const char *prefix)
 	struct tree *tree;
 	struct lock_file *next_index_lock;
 
+	if (interactive) {
+		interactive_add();
+		return get_index_file();
+	}
+

So interactive bypasses everything else, which makes sense.

What happens when the user aborts interactive_add, wishing to abort the whole commit process?

Quoted 24 lines
builtin-commit.c +4 -14
@@ -267,36 +264,41 @@ static int message_is_empty(struct strbuf *sb, int start)
 
 static void determine_author_info(struct strbuf *sb)
 {
-	char *p, *eol;
-	char *name = NULL, *email = NULL;
+	char *name, *email, *date;
 
-	if (force_author) {
-		const char *eoname = strstr(force_author, " <");
-		const char *eomail = strchr(force_author, '>');
+	name = getenv("GIT_AUTHOR_NAME");
+	email = getenv("GIT_AUTHOR_EMAIL");
+	date = getenv("GIT_AUTHOR_DATE");
 
-		if (!eoname || !eomail)
-			die("malformed --author parameter\n");
-		name = xstrndup(force_author, eoname - force_author);
-		email = xstrndup(eoname + 2, eomail - eoname - 2);
-		/* REVIEW: drops author date from amended commit on --amend --author=<author> */
-		strbuf_addf(sb, "author %s\n",
-			    fmt_ident(name, email,
-				      getenv("GIT_AUTHOR_DATE"), 1));
-		free(name);

builtin-commit.c static int message_is_empty(struct strbuf *sb, int start) · line 278

Quoted 10 lines
builtin-commit.c +5 -4
-		free(email);
-	} else if (use_message) {
-		p = strstr(use_message_buffer, "\nauthor");
-		if (!p)
+	if (use_message) {
+		const char *a, *lb, *rb, *eol;
+
+		a = strstr(use_message_buffer, "\nauthor ");
+		if (!a)
 			die("invalid commit: %s\n", use_message);

You know in what sense it is invalid here and ...

builtin-commit.c

Quoted 9 lines
builtin-commit.c +5 -3
-		p++;
-		eol = strchr(p, '\n');
-		if (!eol)
+
+		lb = strstr(a + 8, " <");
+		rb = strstr(a + 8, "> ");
+		eol = strchr(a + 8, '\n');
+		if (!lb || !rb || !eol)
 			die("invalid commit: %s\n", use_message);

... here. Would it be more helpful to say "No author line", and "Cannot parse author line", I wonder. Probably too much.

Quoted 9 lines
builtin-commit.c +5 -3
-		strbuf_add(sb, p, eol + 1 - p);
-	} else {
-		strbuf_addf(sb, "author %s\n", git_author_info(1));
+		name = xstrndup(a + 8, lb - (a + 8));
+		email = xstrndup(lb + 2, rb - (lb + 2));
+		date = xstrndup(rb + 2, eol - (rb + 2));
 	}
+
+	if (force_author) {

builtin-commit.c line 303

Quoted 10 lines
builtin-commit.c +10 -0
+		const char *lb = strstr(force_author, " <");
+		const char *rb = strchr(force_author, '>');
+
+		if (!lb || !rb)
+			die("malformed --author parameter\n");
+		name = xstrndup(force_author, lb - force_author);
+		email = xstrndup(lb + 2, rb - (lb + 2));
+	}
+
+	strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, 1));

I see a slight leak here of name/email/date depending on how they are obtained. Probably it is too much hassle to deal with for too little gain.