threads / patch / 30899

v2log: Handle new repository case.

Subject: [PATCH v2] log: Handle new repository case.

## tl;dr

4 messages between Jun 25, 2012 and Jun 26, 2012. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Leila Muhtasib· Jun 25, 2012, 22:53 UTC · lore

Currently, we display an error message if 'git log' is run on a new repo with no commits. This is a corner case and shouldn't return an error. Also, updated other functions to use new 'default_to_head_if_exists()' function introduced.

Signed-off-by: Leila Muhtasib <muhtasib@gmail.com>
---
 builtin/log.c |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)
Show changes to builtin/log.c +19 −5
diff --git a/builtin/log.c b/builtin/log.c
index 4f1b42a..1685946 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -355,6 +355,15 @@ static int git_log_config(const char *var, const char *value, void *cb)
 	return git_diff_ui_config(var, value, cb);
 }
 
+static const char *default_to_head_if_exists(void)
+{
+	unsigned char sha1[20];
+	if (resolve_ref_unsafe("HEAD", sha1, 1, NULL))
+		return "HEAD";
+	else
+		return NULL;
+}
+
 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info rev;
@@ -366,7 +375,7 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
 	rev.diff = 1;
 	rev.simplify_history = 0;
 	memset(&opt, 0, sizeof(opt));
-	opt.def = "HEAD";
+	opt.def = default_to_head_if_exists();
 	cmd_log_init(argc, argv, prefix, &rev, &opt);
 	if (!rev.diffopt.output_format)
 		rev.diffopt.output_format = DIFF_FORMAT_RAW;
@@ -459,7 +468,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
 	rev.diffopt.stat_width = -1; 	/* Scale to real terminal size */
 
 	memset(&opt, 0, sizeof(opt));
-	opt.def = "HEAD";
+	opt.def = default_to_head_if_exists();
 	opt.tweak = show_rev_tweak_rev;
 	cmd_log_init(argc, argv, prefix, &rev, &opt);
 
@@ -532,7 +541,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
 	init_reflog_walk(&rev.reflog_info);
 	rev.verbose_header = 1;
 	memset(&opt, 0, sizeof(opt));
-	opt.def = "HEAD";
+	opt.def = default_to_head_if_exists();
 	cmd_log_init_defaults(&rev);
 	rev.abbrev_commit = 1;
 	rev.commit_format = CMIT_FMT_ONELINE;
@@ -553,8 +562,13 @@ int cmd_log(int argc, const char **argv, const char *prefix)
 	init_revisions(&rev, prefix);
 	rev.always_show_header = 1;
 	memset(&opt, 0, sizeof(opt));
-	opt.def = "HEAD";
+	opt.def = default_to_head_if_exists();
+
 	cmd_log_init(argc, argv, prefix, &rev, &opt);
+
+	if (!opt.def && !rev.cmdline.nr)
+		return 0;
+
 	return cmd_log_walk(&rev);
 }
 
@@ -1128,7 +1142,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
 	rev.subject_prefix = fmt_patch_subject_prefix;
 	memset(&s_r_opt, 0, sizeof(s_r_opt));
-	s_r_opt.def = "HEAD";
+	s_r_opt.def = default_to_head_if_exists();
 
 	if (default_attach) {
 		rev.mime_boundary = default_attach;
-- 
1.7.7.5 (Apple Git-26)
Junio C Hamano· Jun 25, 2012, 23:13 UTC · re: Leila Muhtasib · lore

Re: [PATCH v2] log: Handle new repository case.

Leila Muhtasib <muhtasib@gmail.com> writes:
Show 13 quoted lines
> @@ -553,8 +562,13 @@ int cmd_log(int argc, const char **argv, const char *prefix)
>  	init_revisions(&rev, prefix);
>  	rev.always_show_header = 1;
>  	memset(&opt, 0, sizeof(opt));
> -	opt.def = "HEAD";
> +	opt.def = default_to_head_if_exists();
> +
>  	cmd_log_init(argc, argv, prefix, &rev, &opt);
> +
> +	if (!opt.def && !rev.cmdline.nr)
> +		return 0;
> +
>  	return cmd_log_walk(&rev);

Do you even need these four new lines if you do not error out? Doesn't log_walk() return successfully if there is nothing in the queue anyway?

Show 6 quoted lines
> @@ -1128,7 +1142,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>  	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
>  	rev.subject_prefix = fmt_patch_subject_prefix;
>  	memset(&s_r_opt, 0, sizeof(s_r_opt));
> -	s_r_opt.def = "HEAD";
> +	s_r_opt.def = default_to_head_if_exists();

I didn't think about other commands, but I do not think format-patch wants to silently succeed without doing anything---it is definitely a user error, no?

Leila· Jun 26, 2012, 02:42 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] log: Handle new repository case.

On Mon, Jun 25, 2012 at 7:13 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 19 quoted lines
> Leila Muhtasib <muhtasib@gmail.com> writes:
>
>> @@ -553,8 +562,13 @@ int cmd_log(int argc, const char **argv, const char *prefix)
>>       init_revisions(&rev, prefix);
>>       rev.always_show_header = 1;
>>       memset(&opt, 0, sizeof(opt));
>> -     opt.def = "HEAD";
>> +     opt.def = default_to_head_if_exists();
>> +
>>       cmd_log_init(argc, argv, prefix, &rev, &opt);
>> +
>> +     if (!opt.def && !rev.cmdline.nr)
>> +             return 0;
>> +
>>       return cmd_log_walk(&rev);
>
> Do you even need these four new lines if you do not error out?
> Doesn't log_walk() return successfully if there is nothing in the
> queue anyway?

Actually, yes. This was left behind because I was printing a success message "No commit(s) to display". Thanks for catching this.

Show 12 quoted lines
>
>> @@ -1128,7 +1142,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>>       DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
>>       rev.subject_prefix = fmt_patch_subject_prefix;
>>       memset(&s_r_opt, 0, sizeof(s_r_opt));
>> -     s_r_opt.def = "HEAD";
>> +     s_r_opt.def = default_to_head_if_exists();
>
> I didn't think about other commands, but I do not think format-patch
> wants to silently succeed without doing anything---it is definitely
> a user error, no?
>

The other changed commands are git show, git whatchanged, git reflog. And I think for the initial repo case, they can just silently succeed vs display that error message.

'git format-patch' on a regular repo with commits, did nothing for me. That's why I had originally included it. Though I think I'm changing my mind about this because it's about generating a patch -- not just displaying info.

Junio C Hamano· Jun 26, 2012, 04:00 UTC · re: Leila · lore

Re: [PATCH v2] log: Handle new repository case.

Leila <muhtasib@gmail.com> writes:
Show 8 quoted lines
>> I didn't think about other commands, but I do not think format-patch
>> wants to silently succeed without doing anything---it is definitely
>> a user error, no?
>>
>
> The other changed commands are git show, git whatchanged, git reflog.
> And I think for the initial repo case, they can just silently succeed
> vs display that error message.

What should happen if you say "git log/show/whatchanged" immediately after running "git checkout --orphan foo"? Should these commands succeed silently?

← back to recent threads