Skip to contentSkip to conversation

Add testcase for ammending and fixing author in git commit.

PATCH In review Kristian Høgsberg 7 participants 28 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Add testcase for ammending and fixing author in git commit.

---
 t/t7501-commit.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
t/t7501-commit.sh +10 -0
index b151b51..3f2112a 100644
@@ -163,4 +163,14 @@ test_expect_success 'partial commit that involves removal (3)' '
 
 '
 
+author="The Real Author <[email protected]>"
+test_expect_success 'amend commit to fix author' '
+
+	git reset --hard
+	git cat-file -p HEAD | sed -e "s/author.*>/author $author/" > expected &&
+	git commit --amend --author="$author" &&
+	git cat-file -p HEAD > current &&
+	diff expected current
+	
+'
 test_done
Signature · 1 line
1.5.3.4.206.g58ba4
#1 lore raw

PATCH 2/4 Remove unecessary hard-coding of EDITOR=':' VISUAL=':' in some test suites.

---
 t/t3501-revert-cherry-pick.sh |    4 ++--
 t/t3901-i18n-patch.sh         |    8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
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
 '
Signature · 1 line
1.5.3.4.206.g58ba4
#2 lore raw

PATCH 3/4 Export launch_editor() and make it accept ':' as a no-op editor.

---
 builtin-tag.c |    5 ++++-
 strbuf.h      |    1 +
 2 files changed, 5 insertions(+), 1 deletions(-)
builtin-tag.c +4 -1
index 66e5a58..bcb3a7a 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;
@@ -42,6 +42,9 @@ static 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;
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.206.g58ba4
#3 lore raw

PATCH 4/4 Implement git commit and status as a builtin commands.

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

---
 Makefile                                        |    9 +-
 builtin-commit.c                                |  614 +++++++++++++++++++++++
 builtin.h                                       |    3 +-
 git-commit.sh => contrib/examples/git-commit.sh |    0 
 git.c                                           |    3 +-
 5 files changed, 621 insertions(+), 8 deletions(-)
 create mode 100644 builtin-commit.c
 rename git-commit.sh => contrib/examples/git-commit.sh (100%)
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 +614 -0

614 lines not on this page · raw message

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 renamed from git-commit.sh +0 -0
similarity index 100%
rename from git-commit.sh
rename to contrib/examples/git-commit.sh
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 },
Signature · 1 line
1.5.3.4.206.g58ba4
#4 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

builtin-commit.c line 389

Quoted 2 lines · Kristian Høgsberg
builtin-commit.c new file +2 -0
+	if (all && interactive)
+		die("Cannot use -a, --interactive or -i at the same time.");

Shouldn't that be "if (all && (interactive || also))"?

Björn

#5 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 5 lines · Björn Steinbrink

builtin-commit.c line 389

Quoted 2 lines · Kristian Høgsberg
builtin-commit.c new file +2 -0
+	if (all && interactive)
+		die("Cannot use -a, --interactive or -i at the same time.");

Shouldn't that be "if (all && (interactive || also))"?

The shell script just has

case "$all,$interactive,$also,$#" in
*t,*t,*)

        die "Cannot use -a, --interactive or -i at the same time." ;;

which doesn't seem to care about the value of $also. As far as I understand git commit, it doesn't make sense to pass any of -a, -i, -o or --interactive at the same time so I guess I could join the checks

        if (also && only)
                die("Only one of --include/--only can be used.");
        if (all && interactive)
                die("Cannot use -a, --interactive or -i at the same

time.");

into something like

	if (also + only + all + interactive > 1)              
		die("Only one of --include/--only/--all/--interactive can be used.");

Does that sound right?

Kristian

#6 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 6 lines · Kristian Høgsberg
Quoted 5 lines · Björn Steinbrink

builtin-commit.c line 389

Quoted 2 lines · Kristian Høgsberg
builtin-commit.c new file +2 -0
+	if (all && interactive)
+		die("Cannot use -a, --interactive or -i at the same time.");

Shouldn't that be "if (all && (interactive || also))"?

Ah, damn. I (obviously) misread the error message as:
foo and (bar or bar2)

IOW "and" instead of the first comma.

Actually it should be:

	if ((all && (interactive || also)) || (interactive && also))

(or whatever more simple version you come up with)

Quoted 9 lines

The shell script just has

case "$all,$interactive,$also,$#" in
*t,*t,*)

        die "Cannot use -a, --interactive or -i at the same time." ;;

which doesn't seem to care about the value of $also. As far as I understand git commit, it doesn't make sense to pass any of -a, -i, -o or --interactive at the same time so I guess I could join the checks

Note that there are only two commas. The asterisks catch everything and $# won't be "t", so that catches anything with at least two t's.

Also note, that the two checks after that one in git-commit.sh as of the current master (140dd77a5cb2e61dcb942e245a2474fae95e42a5) are broken (I'll send a patch in a separate mail).

Quoted 4 lines
        if (also && only)
                die("Only one of --include/--only can be used.");
        if (all && interactive)
                die("Cannot use -a, --interactive or -i at the same
Quoted 8 lines

time.");

into something like

	if (also + only + all + interactive > 1)              
		die("Only one of --include/--only/--all/--interactive can be used.");

Does that sound right?

--include is not in the manpage, and I only glanced over git-commit.sh, but yeah, at least to me, that sounds right.

Björn

#7 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Hi,

Quoted 8 lines · Bj?rn Steinbrink
Quoted 6 lines · Kristian H?gsberg

The shell script just has

case "$all,$interactive,$also,$#" in
*t,*t,*)

        die "Cannot use -a, --interactive or -i at the same time." ;;
Quoted 6 lines
Quoted 3 lines

which doesn't seem to care about the value of $also. As far as I understand git commit, it doesn't make sense to pass any of -a, -i, -o or --interactive at the same time so I guess I could join the checks

Note that there are only two commas. The asterisks catch everything and $# won't be "t", so that catches anything with at least two t's.

So shouldn't it be

	if (!!all + !!interactive + !!also > 1)

Hmm?

Ciao,
Dscho

#8 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 16 lines · Johannes Schindelin

Hi,

Quoted 12 lines · Bj?rn Steinbrink
Quoted 9 lines · Kristian H?gsberg

The shell script just has

case "$all,$interactive,$also,$#" in
*t,*t,*)

        die "Cannot use -a, --interactive or -i at the same time." ;;

which doesn't seem to care about the value of $also. As far as I understand git commit, it doesn't make sense to pass any of -a, -i, -o or --interactive at the same time so I guess I could join the checks

Quoted 8 lines
Quoted 2 lines

Note that there are only two commas. The asterisks catch everything and $# won't be "t", so that catches anything with at least two t's.

So shouldn't it be

	if (!!all + !!interactive + !!also > 1)

Hmm?

Ah, yeah, that's the short and sweet version, I always forget about the conversion to bool giving you 0/1 values... ;-)

Note though, that Kristian had a similar check at the end of his email, that included "only" (but lacked the bool conversion). The original reason why I thought that it would be better was that for example "git commit --all --only foo" didn't care about "only" at all. But that actually was because the --all + paths usage check was broken. So the fixed version actually refuses to use accept that, but with a (IMHO) not so good error message:

$ git commit -a -o file
Paths with -a does not make sense.

Given that some people are used to just pass -a all the time, they might just automatically pass it together with -o. And I think that we actually want to tell them that -a + -o makes no sense instead. Just like we do for -a + -i, which is kind of the complementary usage error.

So I'd go for a correct version of Kristian's suggestion:

if (!!also + !!only + !!all + !!interactive > 1)

	die("Only one of --include/--only/--all/--interactive can be used.");

Björn

#9 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

On Tue, 2007-11-06 at 07:59 +0100, Björn Steinbrink wrote:
...

Quoted 16 lines

Note though, that Kristian had a similar check at the end of his email, that included "only" (but lacked the bool conversion). The original reason why I thought that it would be better was that for example "git commit --all --only foo" didn't care about "only" at all. But that actually was because the --all + paths usage check was broken. So the fixed version actually refuses to use accept that, but with a (IMHO) not so good error message:

$ git commit -a -o file
Paths with -a does not make sense.

Given that some people are used to just pass -a all the time, they might just automatically pass it together with -o. And I think that we actually want to tell them that -a + -o makes no sense instead. Just like we do for -a + -i, which is kind of the complementary usage error.

Quoted 4 lines

So I'd go for a correct version of Kristian's suggestion:

if (!!also + !!only + !!all + !!interactive > 1)

	die("Only one of --include/--only/--all/--interactive can be used.");

Good points, I will use that in the next version of the patch. Just a note about the !! idiom (which I can't stand, fwiw): my version just added the variables, which were all integers, initialized to zero and incremented by the option parser when it sees the corresponding option. So what I had would work too, with the extra check that:

  $ git commit -a -a

etc would give the error

Only one of --include/--only/--all/--interactive can be used.

which is acutally accurate.

cheers,
Kristian

#10 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 29 lines · Kristian Høgsberg

On Tue, 2007-11-06 at 07:59 +0100, Björn Steinbrink wrote:
...

Quoted 16 lines

Note though, that Kristian had a similar check at the end of his email, that included "only" (but lacked the bool conversion). The original reason why I thought that it would be better was that for example "git commit --all --only foo" didn't care about "only" at all. But that actually was because the --all + paths usage check was broken. So the fixed version actually refuses to use accept that, but with a (IMHO) not so good error message:

$ git commit -a -o file
Paths with -a does not make sense.

Given that some people are used to just pass -a all the time, they might just automatically pass it together with -o. And I think that we actually want to tell them that -a + -o makes no sense instead. Just like we do for -a + -i, which is kind of the complementary usage error.

Quoted 4 lines

So I'd go for a correct version of Kristian's suggestion:

if (!!also + !!only + !!all + !!interactive > 1)

	die("Only one of --include/--only/--all/--interactive can be used.");

Good points, I will use that in the next version of the patch. Just a note about the !! idiom (which I can't stand, fwiw): my version just added the variables, which were all integers, initialized to zero and incremented by the option parser when it sees the corresponding option. So what I had would work too, with the extra check that:

Quoted 7 lines
  $ git commit -a -a

etc would give the error

Only one of --include/--only/--all/--interactive can be used.

which is acutally accurate.

Hm, why? The user used only one of them. The error message does not say that you cannot pass the same one multiple times. And I don't think that passing the same boolean flag twice should be treated as an usage error either. There's no contradiction in wanting all files and, well, all files to be committed.

Sidenote: The "or mask" stuff in the option parser would probably prevent you from catching "-a -a" anyway, because the flag becomes truly boolean ;-)

Björn

#11 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

In reply to Johannes Schindelin
Quoted 16 lines · Johannes Schindelin

Hi,

Quoted 12 lines · Bj?rn Steinbrink
Quoted 9 lines · Kristian H?gsberg

The shell script just has

case "$all,$interactive,$also,$#" in
*t,*t,*)

        die "Cannot use -a, --interactive or -i at the same time." ;;

which doesn't seem to care about the value of $also. As far as I understand git commit, it doesn't make sense to pass any of -a, -i, -o or --interactive at the same time so I guess I could join the checks

Quoted 6 lines
Quoted 2 lines

Note that there are only two commas. The asterisks catch everything and $# won't be "t", so that catches anything with at least two t's.

So shouldn't it be

	if (!!all + !!interactive + !!also > 1)

Btw, I'm starting to work slowly on the diff_opt_parse conversion to the macro we discussed, and the need for new option parsing callbacks arised, and I've created a:

parse_opt_mask_{or,and,xor} commands that you declare this way:

    OPT_MASK_OR('a', "all",         &mode, "...", MASK_ALL),
    OPT_MASK_OR('i', "interactive", &mode, "...", MASK_INTERACTIVE),
    ...

And if you chose MASK_ALL/INTERACTIVE/.. to be single bits,

    if (!!all + !!interactive ... > 1)

becomes[0]:

    if (mode & (mode - 1)) {

    }

I've not read your patch thoroughly, but if you feel such a thing would help you, I could send a preliminar set of patches to enable this feature for people that may need it. Though you can look at my WIP on my git public repository[1].

For those who care, this need arised because parse_diff_opts have a _lot_ of single bit options, and that expansing it on many many full blown integers looked like a regression, so I took the option to have a flags member with explicit masks, and those masks will be used from the parse-option callbacks[2]

[0] for those who don't already know it, (i & (i - 1)) == 0 iff i is 0 or a power of 2.

[1] the patch[3]:

      http://git.madism.org/?p=git.git;a=commitdiff;h=9d75b0a00915fa81657934f36318c1c0f5bac96b
      example of use:
      http://git.madism.org/?p=git.git;a=commitdiff;h=74aacc487579d0cbd638adf883e743caeaee0f76
      http://git.madism.org/?p=git.git;a=commitdiff;h=af15793dde94119faa1577c9eec7e839ae628011

[2] http://git.madism.org/?p=git.git;a=commitdiff;h=86032204f1bdf5da2fee555ec917709b3e6f662c#patch10

[3] oh boy gitweb urls are really way too long :/

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#12 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 28 lines · Pierre Habouzit
Quoted 16 lines · Johannes Schindelin

Hi,

Quoted 12 lines · Bj?rn Steinbrink
Quoted 9 lines · Kristian H?gsberg

The shell script just has

case "$all,$interactive,$also,$#" in
*t,*t,*)

        die "Cannot use -a, --interactive or -i at the same time." ;;

which doesn't seem to care about the value of $also. As far as I understand git commit, it doesn't make sense to pass any of -a, -i, -o or --interactive at the same time so I guess I could join the checks

Quoted 6 lines
Quoted 2 lines

Note that there are only two commas. The asterisks catch everything and $# won't be "t", so that catches anything with at least two t's.

So shouldn't it be

	if (!!all + !!interactive + !!also > 1)

Btw, I'm starting to work slowly on the diff_opt_parse conversion to the macro we discussed, and the need for new option parsing callbacks arised, and I've created a:

Quoted 5 lines

parse_opt_mask_{or,and,xor} commands that you declare this way:

    OPT_MASK_OR('a', "all",         &mode, "...", MASK_ALL),
    OPT_MASK_OR('i', "interactive", &mode, "...", MASK_INTERACTIVE),
    ...

That's useful here, and it should be useful in a lot of other places using parse_options() where we end up or'ing flags into a mask based on boolean options.

cheers,
Kristian

#13 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

In reply to Pierre Habouzit
Quoted 12 lines · Pierre Habouzit

Btw, I'm starting to work slowly on the diff_opt_parse conversion to the macro we discussed, and the need for new option parsing callbacks arised, and I've created a:

parse_opt_mask_{or,and,xor} commands that you declare this way:

    OPT_MASK_OR('a', "all",         &mode, "...", MASK_ALL),
    OPT_MASK_OR('i', "interactive", &mode, "...", MASK_INTERACTIVE),
    ...

And if you chose MASK_ALL/INTERACTIVE/.. to be single bits,

Quoted 7 lines
    if (!!all + !!interactive ... > 1)

becomes[0]:

    if (mode & (mode - 1)) {

    }

This goes too far, IMHO. That's unnecessary cleverness/microoptimization at the expense of readability.

-- Hannes

#14 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 14 lines · Johannes Sixt
Quoted 4 lines · Pierre Habouzit

Btw, I'm starting to work slowly on the diff_opt_parse conversion to the macro we discussed, and the need for new option parsing callbacks arised, and I've created a:
parse_opt_mask_{or,and,xor} commands that you declare this way:

Quoted 8 lines
   OPT_MASK_OR('a', "all",         &mode, "...", MASK_ALL),
   OPT_MASK_OR('i', "interactive", &mode, "...", MASK_INTERACTIVE),
   ...
And if you chose MASK_ALL/INTERACTIVE/.. to be single bits,
   if (!!all + !!interactive ... > 1)
becomes[0]:
   if (mode & (mode - 1)) {
   }
Quoted 2 lines

This goes too far, IMHO. That's unnecessary cleverness/microoptimization at the expense of readability.

The reason why I did that is not to be able to do mode & (mode - 1). Have a look at diff.c, imagine the insane amount of intermediate variables we would need, and now understand why I introduced that :)

I'm not sure it's useful for git-commit, I was mentioning it just in case.

Note: the fact that an OPT_BOOLEAN when you repeat it increments the
value isn't always a good thing, and is the reason why you need the (quite ugly) double bangs.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#15 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 14 lines · Pierre Habouzit
Quoted 13 lines · Johannes Sixt
Quoted 12 lines · Pierre Habouzit

Btw, I'm starting to work slowly on the diff_opt_parse conversion to the macro we discussed, and the need for new option parsing callbacks arised, and I've created a:
parse_opt_mask_{or,and,xor} commands that you declare this way:

   OPT_MASK_OR('a', "all",         &mode, "...", MASK_ALL),
   OPT_MASK_OR('i', "interactive", &mode, "...", MASK_INTERACTIVE),
   ...
And if you chose MASK_ALL/INTERACTIVE/.. to be single bits,
   if (!!all + !!interactive ... > 1)
becomes[0]:
   if (mode & (mode - 1)) {
   }
Quoted 3 lines
Quoted 1 line

This goes too far, IMHO. That's unnecessary cleverness/microoptimization at the expense of readability.

The reason why I did that is not to be able to do mode & (mode - 1).

With a macro along the lines of

#define IS_MULTI_FLAGGED(x) (x & (x - 1))

at least this particular construct becomes a lot more readable.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#16 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

In reply to Kristian Høgsberg

Hi,

Quoted 12 lines · Kristian Høgsberg
builtin-commit.c new file +12 -0
+static char *
+prepare_index(const char **files, const char *prefix)
+{
+	int fd;
+	struct tree *tree;
+	struct lock_file *next_index_lock;
+
+	fd = hold_locked_index(&lock_file, 1);
+	if (read_cache() < 0)
+		die("index file corrupt");
+
+	if (all) {

builtin-commit.c line 90

Quoted 10 lines
builtin-commit.c new file +10 -0
+		add_files_to_cache(verbose, NULL, files);
+		if (write_cache(fd, active_cache, active_nr) || close(fd))
+			die("unable to write new_index file");
+		return lock_file.filename;
+	} else if (also) {
+		add_files_to_cache(verbose, prefix, files);
+		if (write_cache(fd, active_cache, active_nr) || close(fd))
+			die("unable to write new_index file");
+		return lock_file.filename;
+	}

Unless something slips by my mind, this could be written as

	if (all || also) {
		add_files_to_cache(verbose, also ? prefix : NULL, files);
		...
	}

builtin-commit.c line 99

Quoted 9 lines
builtin-commit.c new file +9 -0
+
+	if (interactive)
+		interactive_add();
+
+	if (*files == NULL) {
+		/* Commit index as-is. */
+		rollback_lock_file(&lock_file);
+		return get_index_file();
+	}

Would an interactive add not conflict with this rollback? And indeed with the locked index to begin with?

builtin-commit.c line 104

Quoted 4 lines
builtin-commit.c new file +4 -0
+	/* update the user index file */
+	add_files_to_cache(verbose, prefix, files);
+	if (write_cache(fd, active_cache, active_nr) || close(fd))
+		die("unable to write new_index file");

Does that mean that the index is _always_ written? Even when not specifying and paths on the command line?

builtin-commit.c line 114

Quoted 1 line
builtin-commit.c new file +1 -0
+	/* Uh oh, abusing lock_file to create a garbage collected file */

It's not that bad. But I would mention that it is a temporary index which you are building.

Quoted 1 line

+static const char sign_off_header[] = "Signed-off-by: ";

Funny, I thought it was a footer ;-)

builtin-commit.c line 178

Quoted 1 line
builtin-commit.c new file +1 -0
+	} else if (!stat(template_file, &statbuf)) {

Should this not test "if (template_file && " first?

Quoted 7 lines
builtin-commit.c new file +7 -0
+/* Find out if the message starting at position 'start' in the strbuf
+ * contains only whitespace and Signed-off-by lines. */
+static int message_is_empty(struct strbuf *sb, int start)
+{
+	struct strbuf tmpl;
+	const char *nl;
+	int eol, i;

builtin-commit.c line 247

Quoted 10 lines
builtin-commit.c new file +10 -0
+
+	/* See if the template is just a prefix of the message. */
+	strbuf_init(&tmpl, 0);
+	if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
+		stripspace(&tmpl, 1);
+		if (start + tmpl.len <= sb->len &&
+		    memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
+			start += tmpl.len;
+	}
+	strbuf_release(&tmpl);

The release could go inside the if block, no?

Quoted 1 line

+static int run_hook(const char *index_file, const char *name, const char *arg)

Would this function not prefer to live in run-command.c?

Quoted 4 lines
builtin-commit.c new file +4 -0
+{
+	struct child_process hook;
+	const char *argv[3], *env[2];
+	char index[PATH_MAX];

builtin-commit.c line 430

Quoted 10 lines
builtin-commit.c new file +10 -0
+
+	argv[0] = git_path("hooks/%s", name);
+	argv[1] = arg;
+	argv[2] = NULL;
+	snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
+	env[0] = index;
+	env[1] = NULL;
+
+	if (access(argv[0], X_OK) < 0)
+		return 0;

That check logically belongs 6 lines higher...

builtin-commit.c line 462

Quoted 8 lines
builtin-commit.c new file +8 -0
+	rev.abbrev = 0;
+	rev.diff = 1;
+	rev.diffopt.output_format =
+		DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
+
+	rev.verbose_header = 1;
+	rev.show_root_diff = 1;
+	rev.commit_format = get_commit_format("format:%h: %s");

That's interesting. Wouldn't have thought of that. Reusing the log_tree machinery to output the summary. Cute.

Note that one relatively low-hanging fruit will be to teach builtin-commit to use a cheaper "no changes?" check when no_edit = 1.

Thanks,
Dscho

#17 lore raw

Re: [PATCH 4/4] Implement git commit and status as a builtin commands.

Quoted 1 line · Johannes Schindelin

Hi,

Hi, wanted to send this out earlier, but I punted it and a couple of days went by...

Quoted 27 lines · Kristian Høgsberg
Quoted 13 lines
builtin-commit.c new file +12 -0
+static char *
+prepare_index(const char **files, const char *prefix)
+{
+	int fd;
+	struct tree *tree;
+	struct lock_file *next_index_lock;
+
+	fd = hold_locked_index(&lock_file, 1);
+	if (read_cache() < 0)
+		die("index file corrupt");
+
+	if (all) {

builtin-commit.c line 90

Quoted 10 lines
builtin-commit.c new file +10 -0
+		add_files_to_cache(verbose, NULL, files);
+		if (write_cache(fd, active_cache, active_nr) || close(fd))
+			die("unable to write new_index file");
+		return lock_file.filename;
+	} else if (also) {
+		add_files_to_cache(verbose, prefix, files);
+		if (write_cache(fd, active_cache, active_nr) || close(fd))
+			die("unable to write new_index file");
+		return lock_file.filename;
+	}

Unless something slips by my mind, this could be written as

Quoted 4 lines
	if (all || also) {
		add_files_to_cache(verbose, also ? prefix : NULL, files);
		...
	}

Yup, that looks right.

Quoted 4 lines
Quoted 4 lines
builtin-commit.c new file +4 -0
+
+	if (interactive)
+		interactive_add();
+
Quoted 8 lines

builtin-commit.c line 99

Quoted 5 lines
builtin-commit.c new file +5 -0
+	if (*files == NULL) {
+		/* Commit index as-is. */
+		rollback_lock_file(&lock_file);
+		return get_index_file();
+	}

Would an interactive add not conflict with this rollback? And indeed with the locked index to begin with?

Yes, I've moved the interactive case up to the beginning of prepare_index() and made it return get_index_file() after running interactive_add().

Quoted 7 lines

builtin-commit.c line 104

Quoted 4 lines
builtin-commit.c new file +4 -0
+	/* update the user index file */
+	add_files_to_cache(verbose, prefix, files);
+	if (write_cache(fd, active_cache, active_nr) || close(fd))
+		die("unable to write new_index file");

Does that mean that the index is _always_ written? Even when not specifying and paths on the command line?

Do you mean "not specifying any options and paths..."? In that case we add the files to the index and create a temporary index from HEAD and add the files there too, which we then commit. So we have to write the index in that case. If there are no files on the command line, we roll back the lock and bail out just above the part you quoted.

Quoted 4 lines

builtin-commit.c line 114

Quoted 1 line
builtin-commit.c new file +1 -0
+	/* Uh oh, abusing lock_file to create a garbage collected file */

It's not that bad. But I would mention that it is a temporary index which you are building.

Heh, yeah, I toned it down a bit :)

Quoted 7 lines
Quoted 1 line

+static const char sign_off_header[] = "Signed-off-by: ";

Funny, I thought it was a footer ;-)

builtin-commit.c line 178

Quoted 1 line
builtin-commit.c new file +1 -0
+	} else if (!stat(template_file, &statbuf)) {

Should this not test "if (template_file && " first?

Yup, added.

Quoted 11 lines
Quoted 11 lines
builtin-commit.c new file +11 -0
+/* Find out if the message starting at position 'start' in the strbuf
+ * contains only whitespace and Signed-off-by lines. */
+static int message_is_empty(struct strbuf *sb, int start)
+{
+	struct strbuf tmpl;
+	const char *nl;
+	int eol, i;
+
+	/* See if the template is just a prefix of the message. */
+	strbuf_init(&tmpl, 0);
+	if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
Quoted 8 lines

builtin-commit.c line 247

Quoted 6 lines
builtin-commit.c new file +6 -0
+		stripspace(&tmpl, 1);
+		if (start + tmpl.len <= sb->len &&
+		    memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
+			start += tmpl.len;
+	}
+	strbuf_release(&tmpl);

The release could go inside the if block, no?

Sure, not a big deal, though.

Quoted 3 lines
Quoted 1 line

+static int run_hook(const char *index_file, const char *name, const char *arg)

Would this function not prefer to live in run-command.c?

Yeah, we can move it later, though.

Quoted 8 lines
Quoted 8 lines
builtin-commit.c new file +8 -0
+{
+	struct child_process hook;
+	const char *argv[3], *env[2];
+	char index[PATH_MAX];
+
+	argv[0] = git_path("hooks/%s", name);
+	argv[1] = arg;
+	argv[2] = NULL;
Quoted 8 lines

builtin-commit.c line 430

Quoted 6 lines
builtin-commit.c new file +6 -0
+	snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
+	env[0] = index;
+	env[1] = NULL;
+
+	if (access(argv[0], X_OK) < 0)
+		return 0;

That check logically belongs 6 lines higher...

You mean you want to do it right after the argv[0] assignment? I'd like to keep the block of assignments together so it's easy to see how the array is set up. Don't tell me you worry about performance here... ;)

Quoted 11 lines

builtin-commit.c line 462

Quoted 8 lines
builtin-commit.c new file +8 -0
+	rev.abbrev = 0;
+	rev.diff = 1;
+	rev.diffopt.output_format =
+		DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
+
+	rev.verbose_header = 1;
+	rev.show_root_diff = 1;
+	rev.commit_format = get_commit_format("format:%h: %s");

That's interesting. Wouldn't have thought of that. Reusing the log_tree machinery to output the summary. Cute.

Heh, I just did what the shell script did. It uses git diff-tree for this, and the above is just the relevant bits from builtin-diff-tree.c.

cheers,
Kristian

#18 lore raw

Re: [PATCH 3/4] Export launch_editor() and make it accept ':' as a no-op editor.

In reply to Kristian Høgsberg

builtin-tag.c static void launch_editor(const char *path, struct strbuf *buffer) · line 47

Quoted 4 lines · Kristian Høgsberg
builtin-tag.c +3 -0
+	if (!strcmp(editor, ":"))
+		return;
+

Doesn't this make the change in 2/4 superfluous?

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#19 lore raw

Re: [PATCH 3/4] Export launch_editor() and make it accept ':' as a no-op editor.

Quoted 7 lines · Andreas Ericsson

builtin-tag.c static void launch_editor(const char *path, struct strbuf *buffer) · line 47

Quoted 4 lines · Kristian Høgsberg
builtin-tag.c +3 -0
+	if (!strcmp(editor, ":"))
+		return;
+

Doesn't this make the change in 2/4 superfluous?

Yeah, but it's still a nice cleanup, I think.

Kristian

#20 lore raw

Re: [PATCH 2/4] Remove unecessary hard-coding of EDITOR=':' VISUAL=':' in some test suites.

In reply to Kristian Høgsberg
Quoted 5 lines · Kristian Høgsberg
---
 t/t3501-revert-cherry-pick.sh |    4 ++--
 t/t3901-i18n-patch.sh         |    8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

This is a good patch, but a real commit message that says why they are unnecessary is lacking. Something like...

Sourcing of ./test-lib.sh at the very beginning of test scripts already define and export them.

#21 lore raw
In reply to Kristian Høgsberg
Quoted 14 lines · Kristian Høgsberg
---
 t/t7501-commit.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
t/t7501-commit.sh +1 -0
index b151b51..3f2112a 100644
@@ -163,4 +163,14 @@ test_expect_success 'partial commit that involves removal (3)' '
 
 '
 
+author="The Real Author <[email protected]>"

t/t7501-commit.sh test_expect_success 'partial commit that involves removal (3)' ' · line 176

Quoted 10 lines
t/t7501-commit.sh +9 -0
+test_expect_success 'amend commit to fix author' '
+
+	git reset --hard
+	git cat-file -p HEAD | sed -e "s/author.*>/author $author/" > expected &&
+	git commit --amend --author="$author" &&
+	git cat-file -p HEAD > current &&
+	diff expected current
+	
+'
 test_done

This can't be right. How are you ignoring the differences in committer dates?

By the way, I _think_ git-commit.sh allows fixing author name/email without molesting the author timestamp (i.e. takes it from the amended commit). That should probably be checked with the test as well.

#22 lore raw
Quoted 15 lines · Junio C Hamano
Quoted 13 lines · Kristian Høgsberg
---
 t/t7501-commit.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
t/t7501-commit.sh +0 -0
index b151b51..3f2112a 100644
@@ -163,4 +163,14 @@ test_expect_success 'partial commit that involves removal (3)' '
 
 '
 
Quoted 14 lines

t/t7501-commit.sh test_expect_success 'partial commit that involves removal (3)' ' · line 176

Quoted 11 lines
t/t7501-commit.sh +10 -0
+author="The Real Author <[email protected]>"
+test_expect_success 'amend commit to fix author' '
+
+	git reset --hard
+	git cat-file -p HEAD | sed -e "s/author.*>/author $author/" > expected &&
+	git commit --amend --author="$author" &&
+	git cat-file -p HEAD > current &&
+	diff expected current
+	
+'
 test_done

This can't be right. How are you ignoring the differences in committer dates?

t/test-lib.sh fixes GIT_COMMITTER_DATE so all commits have the date

committer C O Mitter <[email protected]> 1112911993 -0700

unless you use test_tick.

Quoted 4 lines

By the way, I _think_ git-commit.sh allows fixing author name/email without molesting the author timestamp (i.e. takes it from the amended commit). That should probably be checked with the test as well.

You're right, I need to pick GIT_AUTHOR_DATE from the ammended commit. Ok, I'll need to rewrite determine_author_info() a little bit. I might get an update patch out this weekend.

cheers,
Kristian

#23 lore raw
Quoted 8 lines · Kristian Høgsberg
Quoted 2 lines

This can't be right. How are you ignoring the differences in committer dates?

t/test-lib.sh fixes GIT_COMMITTER_DATE so all commits have the date

committer C O Mitter <[email protected]> 1112911993 -0700

unless you use test_tick.

Heh, that was actually what I was getting at. By not doing test_tick, you are not making sure that the commit gets the timestamp of the time --amend is run.

Quoted 8 lines
Quoted 4 lines

By the way, I _think_ git-commit.sh allows fixing author name/email without molesting the author timestamp (i.e. takes it from the amended commit). That should probably be checked with the test as well.

You're right, I need to pick GIT_AUTHOR_DATE from the ammended commit. Ok, I'll need to rewrite determine_author_info() a little bit. I might get an update patch out this weekend.

#24 lore raw

PATCH 1/1 t7501-commit.sh: Add test case for fixing author in amend commit.

In reply to Junio C Hamano
---
 t/t7501-commit.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
t/t7501-commit.sh +16 -0
index b151b51..7a37c13 100644
@@ -151,6 +151,7 @@ test_expect_success 'partial commit that involves removal (2)' '
 	diff expected current
 
 '
+test_tick
 
 test_expect_success 'partial commit that involves removal (3)' '
 
@@ -163,4 +164,19 @@ test_expect_success 'partial commit that involves removal (3)' '
 
 '
 
+oldtick=$GIT_AUTHOR_DATE
+test_tick
+
+author="The Real Author <[email protected]>"
+committer="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE"
+
+test_expect_success 'amend commit to fix author' '
+
+	git reset --hard
+	git cat-file -p HEAD | sed -e "s/author.*/author $author $oldtick/" -e "s/committer.*/committer $committer/" > expected &&
+	git commit --amend --author="$author" &&
+	git cat-file -p HEAD > current &&
+	diff expected current
+	
+'
 test_done
Signature · 1 line
1.5.3.5.1527.g6161-dirty
#25 lore raw

Re: [PATCH] t7501-commit.sh: Add test case for fixing author in amend commit.

Hi,

Quoted 3 lines · Kristian Høgsberg

+test_tick

test_expect_success 'partial commit that involves removal (3)' '

We usually put the test_tick into the test case. IOW

	test_expect_success 'message' '
		test_tick &&
		...

t/t7501-commit.sh test_expect_success 'partial commit that involves removal (3)' ' · line 173

Quoted 9 lines

'

t/t7501-commit.sh +7 -0
+oldtick=$GIT_AUTHOR_DATE
+test_tick
+
+author="The Real Author <[email protected]>"
+committer="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE"
+
+test_expect_success 'amend commit to fix author' '

Same here.

BTW: is this committer mangling really necessary? I thought only GIT_COMMITTER_DATE was relevant. And that is easily replaced by

	sed -e "s/^\(committer.* \)[0-9][0-9]*$/\1$GIT_COMMITTER_DATE/"

Ciao,
Dscho

#26 lore raw

PATCH 1/1 t7501-commit.sh: Add test case for fixing author in amend commit.

---

How about this?

 t/t7501-commit.sh |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
t/t7501-commit.sh +15 -0
index b151b51..c5d122f 100644
@@ -163,4 +163,19 @@ test_expect_success 'partial commit that involves removal (3)' '
 
 '
 
+author="The Real Author <[email protected]>"
+test_expect_success 'amend commit to fix author' '
+
+	oldtick=$GIT_AUTHOR_DATE &&
+	test_tick &&
+	git reset --hard &&
+	git cat-file -p HEAD |
+	sed -e "s/author.*/author $author $oldtick/" \
+		-e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
+		expected &&
+	git commit --amend --author="$author" &&
+	git cat-file -p HEAD > current &&
+	diff expected current
+	
+'
 test_done
Signature · 1 line
1.5.3.4