Skip to contentSkip to conversation

Implement git commit as a builtin command.

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement git commit as a builtin command.

Move git-commit.sh to contrib/examples.

---

Here's the builtin-commit patch again, this time updated to build against the in-tree strbuf and option parser. Now that Pierre has done all the work of getting those pieces upstream, I notice that the C version ends up 20 lines shorter than the shell script :)

There's a couple of changes to the test suite at the end, which removes some EDITOR=: VISUAL=: hard-coding in a few tests and then sets the default editor to /bin/true. This is to address the problem that the C version of launch editor doesn't support any kind of shell qouting or built-ins. Should we just use system(3) here?

cheers,
Kristian

 Makefile                       |    9 +-
 builtin-commit.c               |  608 ++++++++++++++++++++++++++++++++++++++
 builtin-tag.c                  |    2 +-
 builtin.h                      |    3 +-
 contrib/examples/git-commit.sh |  628 ++++++++++++++++++++++++++++++++++++++++
 git-commit.sh                  |  628 ----------------------------------------
 git.c                          |    3 +-
 strbuf.h                       |    1 +
 t/t3501-revert-cherry-pick.sh  |    4 +-
 t/t3901-i18n-patch.sh          |    8 +-
 t/test-lib.sh                  |    4 +-
 11 files changed, 1253 insertions(+), 645 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 042f79e..689eb33 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 \
@@ -364,7 +365,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 \
@@ -830,9 +830,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 +608 -0

608 lines not on this page · raw message

builtin-tag.c +1 -1
index 66e5a58..e6bae69 100644
@@ -17,7 +17,7 @@ static const char builtin_tag_usage[] =
 
 static char signingkey[1000];
 
-static void launch_editor(const char *path, struct strbuf *buffer)
+void launch_editor(const char *path, struct strbuf *buffer)
 {
 	const char *editor, *terminal;
 	struct child_process child;
builtin.h +2 -1
index 9a6213a..0f8456c 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);
@@ -68,10 +69,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_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 4e10581..8522095 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 },
@@ -346,10 +347,10 @@ 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 },
 		{ "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 */
t/t3501-revert-cherry-pick.sh +2 -2
index 552af1c..2dbe04f 100755
@@ -44,7 +44,7 @@ test_expect_success setup '
 test_expect_success 'cherry-pick after renaming branch' '
 
 	git checkout rename2 &&
-	EDITOR=: VISUAL=: git cherry-pick added &&
+	git cherry-pick added &&
 	test -f opos &&
 	grep "Add extra line at the end" opos
 
@@ -53,7 +53,7 @@ test_expect_success 'cherry-pick after renaming branch' '
 test_expect_success 'revert after renaming branch' '
 
 	git checkout rename1 &&
-	EDITOR=: VISUAL=: git revert added &&
+	git revert added &&
 	test -f spoo &&
 	! grep "Add extra line at the end" spoo
 
t/t3901-i18n-patch.sh +4 -4
index 28e9e37..235f372 100755
@@ -154,7 +154,7 @@ test_expect_success 'cherry-pick(U/U)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3
 '
@@ -169,7 +169,7 @@ test_expect_success 'cherry-pick(L/L)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3 8859
 '
@@ -184,7 +184,7 @@ test_expect_success 'cherry-pick(U/L)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3
 '
@@ -200,7 +200,7 @@ test_expect_success 'cherry-pick(L/U)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3 8859
 '
t/test-lib.sh +2 -2
index 714de6e..51cbcc1 100644
@@ -9,8 +9,8 @@ LC_ALL=C
 PAGER=cat
 TZ=UTC
 export LANG LC_ALL PAGER TZ
-EDITOR=:
-VISUAL=:
+EDITOR=/bin/true
+VISUAL=/bin/true
 unset GIT_EDITOR
 unset AUTHOR_DATE
 unset AUTHOR_EMAIL
Signature · 1 line
1.5.3.4.206.g58ba4
#1 lore raw

Makefile BUILTIN_OBJS = \ · line 370

Quoted 8 lines · Kristian Høgsberg
Makefile +0 -1
@@ -364,7 +365,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 \

I did not read in the commit log that runstatus is gone...

Quoted 8 lines
builtin-commit.c new file +2 -0
new file mode 100644
index 0000000..56c7427
@@ -0,0 +1,608 @@
+/*
+ * Builtin "git commit"

builtin-commit.c line 12

Quoted 10 lines
builtin-commit.c new file +10 -0
+ *
+ * Copyright (c) 2007 Kristian Høgsberg <[email protected]>
+ * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "cache.h"

The system header files on some systems have a nasty habit of changing the behaviour depending on the order they are included. Since 85023577a8f4b540aa64aa37f6f44578c0c305a3 (simplify inclusion of system header files) in late 2006, we have a few rules for system-header inclusion to avoid the portability issues:

(1) sources under compat/, platform sha-1 implementations, and

     xdelta code are exempt from the following rules;

(2) the first #include must be "git-compat-util.h" or one of our own header file that includes it first (e.g. config.h, builtin.h, pkt-line.h);

(3) system headers that are included in "git-compat-util.h" need not be included in individual C source files.

(4) "git-compat-util.h" does not have to include subsystem specific header files (e.g. expat.h).

Quoted 8 lines
builtin-commit.c new file +8 -0
+static void determine_author_info(struct strbuf *sb)
+{
+	char *p, *eol;
+	char *name = NULL, *email = NULL;
+
+	if (use_message) {
+		p = strstr(use_message_buffer, "\nauthor");
+		if (!p)

builtin-commit.c line 289

Quoted 10 lines
builtin-commit.c new file +10 -0
+			die("invalid commit: %s\n", use_message);
+		p++;
+		eol = strchr(p, '\n');
+		if (!eol)
+			die("invalid commit: %s\n", use_message);
+
+		strbuf_add(sb, p, eol + 1 - p);
+	} else if (force_author) {
+		const char *eoname = strstr(force_author, " <");
+		const char *eomail = strchr(force_author, '>');

Doesn't this break:

    $ git commit --amend --author='A U Thor <[email protected]>'

to fix a misattribution?

Quoted 19 lines
builtin-commit.c new file +18 -0
+static int parse_and_validate_options(int argc, const char *argv[])
+{
...
+	if (use_message) {
+		unsigned char sha1[20];
+		static char utf8[] = "UTF-8";
+		const char *out_enc;
+		char *enc, *end;
+		struct commit *commit;
+
+		if (get_sha1(use_message, sha1))
+			die("could not lookup commit %s", use_message);
+		commit = lookup_commit(sha1);
+		if (!commit || parse_commit(commit))
+			die("could not parse commit %s", use_message);
+
+		enc = strstr(commit->buffer, "\nencoding");
+		if (enc) {
+			end = strchr(enc + 10, '\n');

builtin-commit.c line 368

Quoted 10 lines
builtin-commit.c new file +10 -0
+			enc = xstrndup(enc + 10, end - (enc + 10));
+		} else {
+			enc = utf8;
+		}
+		out_enc = git_commit_encoding ? git_commit_encoding : utf8;
+
+		use_message_buffer =
+			reencode_string(commit->buffer, out_enc, enc);
+		if (enc != utf8)
+			free(enc);

A few issues.

* When use_message is set because of --amend that wanted to amend a commit message that was recorded in a corrupt encoding, and iconv() in reencode_string() fails, saying "I cannot convert that completely", most of the message can still be salvageable. This part should allow looser reencoding if the message will be eyeballed and edited by the user.

* We would want to avoid reencoding when !strcmp(out_enc, enc). Both builtin-mailinfo.c and commit.c skip the call to the function at the calling site, but it might make sense to teach reencode_string() about such an optimization.

#2 lore raw

On Thu, 2007-11-01 at 16:51 -0700, Junio C Hamano wrote:

Hi Junio,

Thanks for the review (again). I've split the patch up in a couple of test suite patches (a patch to accept ':' as a no-op editor, fixing hard-coding of editor, and a test case for the commit --amend --author case), exporting launch_editor() and a reworked version of the commit patch that addresses your comments.

Quoted 12 lines

Makefile BUILTIN_OBJS = \ · line 370

Quoted 8 lines · Kristian Høgsberg
Makefile +0 -1
@@ -364,7 +365,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 \

I did not read in the commit log that runstatus is gone...

True, that should be documented. Added in the following patch.

Quoted 34 lines
Quoted 8 lines
builtin-commit.c new file +2 -0
new file mode 100644
index 0000000..56c7427
@@ -0,0 +1,608 @@
+/*
+ * Builtin "git commit"

builtin-commit.c line 12

Quoted 10 lines
builtin-commit.c new file +10 -0
+ *
+ * Copyright (c) 2007 Kristian Høgsberg <[email protected]>
+ * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "cache.h"

The system header files on some systems have a nasty habit of changing the behaviour depending on the order they are included. Since 85023577a8f4b540aa64aa37f6f44578c0c305a3 (simplify inclusion of system header files) in late 2006, we have a few rules for system-header inclusion to avoid the portability issues:

(1) sources under compat/, platform sha-1 implementations, and

     xdelta code are exempt from the following rules;

(2) the first #include must be "git-compat-util.h" or one of our own header file that includes it first (e.g. config.h, builtin.h, pkt-line.h);

Quoted 5 lines

(3) system headers that are included in "git-compat-util.h" need not be included in individual C source files.

(4) "git-compat-util.h" does not have to include subsystem specific header files (e.g. expat.h).

Ah, yes, this has been pointed out to me before, sorry. Patch updated.

Quoted 21 lines
Quoted 8 lines
builtin-commit.c new file +8 -0
+static void determine_author_info(struct strbuf *sb)
+{
+	char *p, *eol;
+	char *name = NULL, *email = NULL;
+
+	if (use_message) {
+		p = strstr(use_message_buffer, "\nauthor");
+		if (!p)

builtin-commit.c line 289

Quoted 10 lines
builtin-commit.c new file +10 -0
+			die("invalid commit: %s\n", use_message);
+		p++;
+		eol = strchr(p, '\n');
+		if (!eol)
+			die("invalid commit: %s\n", use_message);
+
+		strbuf_add(sb, p, eol + 1 - p);
+	} else if (force_author) {
+		const char *eoname = strstr(force_author, " <");
+		const char *eomail = strchr(force_author, '>');

Doesn't this break:

Quoted 3 lines
    $ git commit --amend --author='A U Thor <[email protected]>'

to fix a misattribution?

Dang, good catch. I've fixed it by swapping the (use_message) and (force_author) clauses and the new patch adds a test case for this.

Quoted 32 lines
Quoted 19 lines
builtin-commit.c new file +18 -0
+static int parse_and_validate_options(int argc, const char *argv[])
+{
...
+	if (use_message) {
+		unsigned char sha1[20];
+		static char utf8[] = "UTF-8";
+		const char *out_enc;
+		char *enc, *end;
+		struct commit *commit;
+
+		if (get_sha1(use_message, sha1))
+			die("could not lookup commit %s", use_message);
+		commit = lookup_commit(sha1);
+		if (!commit || parse_commit(commit))
+			die("could not parse commit %s", use_message);
+
+		enc = strstr(commit->buffer, "\nencoding");
+		if (enc) {
+			end = strchr(enc + 10, '\n');

builtin-commit.c line 368

Quoted 10 lines
builtin-commit.c new file +10 -0
+			enc = xstrndup(enc + 10, end - (enc + 10));
+		} else {
+			enc = utf8;
+		}
+		out_enc = git_commit_encoding ? git_commit_encoding : utf8;
+
+		use_message_buffer =
+			reencode_string(commit->buffer, out_enc, enc);
+		if (enc != utf8)
+			free(enc);

A few issues.

Quoted 7 lines

* When use_message is set because of --amend that wanted to amend a commit message that was recorded in a corrupt encoding, and iconv() in reencode_string() fails, saying "I cannot convert that completely", most of the message can still be salvageable. This part should allow looser reencoding if the message will be eyeballed and edited by the user.

So in this case we just want to copy the old message byte for byte, right? That's what I have in the updated patch.

Quoted 4 lines

* We would want to avoid reencoding when !strcmp(out_enc, enc). Both builtin-mailinfo.c and commit.c skip the call to the function at the calling site, but it might make sense to teach reencode_string() about such an optimization.

Right. I just added the call-site optimization for builtin-commit for now, but I would expect iconv() to be smart in case input and output encodings are the same.

cheers,
Kristian

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

Move git-commit.sh to contrib/examples.

---

Here's the builtin-commit patch again, this time updated to build against the in-tree strbuf and option parser. Now that Pierre has done all the work of getting those pieces upstream, I notice that the C version ends up 20 lines shorter than the shell script :)

Quoted 5 lines

There's a couple of changes to the test suite at the end, which removes some EDITOR=: VISUAL=: hard-coding in a few tests and then sets the default editor to /bin/true. This is to address the problem that the C version of launch editor doesn't support any kind of shell qouting or built-ins. Should we just use system(3) here?

I do not care much about _how_ it is done, but one of the most often typed command in my .history is

EDITOR=: git commit

I'd be somewhat unhappy to be forced to retrain my fingers ;-)

#4 lore raw
In reply to Kristian Høgsberg
Quoted 1 line · Kristian H?gsberg

Move git-commit.sh to contrib/examples.

Just a note: you might want to use "git format-patch -M".

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#5 lore raw
Quoted 5 lines · Jakub Narebski
Quoted 1 line · Kristian H?gsberg

Move git-commit.sh to contrib/examples.

Just a note: you might want to use "git format-patch -M".

I wonder if it would be such a bad idea to make this the default. It seems like _everyone_ who should be using it forget it. And the number of patches made for human or git consumption is probably vastly larger than the number of patches made for consumption by other tools.

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#6 lore raw
Quoted 4 lines · Karl Hasselström
Quoted 2 lines · Jakub Narebski

Kristian H?gsberg wrote:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Move git-commit.sh to contrib/examples.

Just a note: you might want to use "git format-patch -M".

I wonder if it would be such a bad idea to make this the default. It seems like _everyone_ who should be using it forget it. And the number of patches made for human or git consumption is probably vastly larger than the number of patches made for consumption by other tools.

On this mailing list, we can pretty much assume that renaming diff can be applied by people. But outside world is a different story.

#7 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 5 lines · Karl Hasselström

I wonder if it would be such a bad idea to make this the default. It seems like _everyone_ who should be using it forget it. And the number of patches made for human or git consumption is probably vastly larger than the number of patches made for consumption by other tools.

On this mailing list, we can pretty much assume that renaming diff can be applied by people. But outside world is a different story.

So I guess a prerequisite for this is to get git patch format support into GNU patch, then? :-)

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle