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

4 messages from 2012-06-25 to 2012-06-26. Participants: Leila Muhtasib, Junio C Hamano, Leila.
Thread: https://gitlist.dev/t/30899

## Leila Muhtasib, 2012-06-25 22:53

Subject: [PATCH v2] log: Handle new repository case.
Message-ID: <1340664813-96891-1-git-send-email-muhtasib@gmail.com>
URL: https://gitlist.dev/e/1340664813-96891-1-git-send-email-muhtasib%40gmail.com

```
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(-)

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, 2012-06-25 23:13

Subject: Re: [PATCH v2] log: Handle new repository case.
Message-ID: <7vobo7dlyl.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vobo7dlyl.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1340664813-96891-1-git-send-email-muhtasib@gmail.com>

```
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?

> @@ -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, 2012-06-26 02:42

Subject: Re: [PATCH v2] log: Handle new repository case.
Message-ID: <CAA3EhHK25rLU-gQNUG0cmJ4E1g+Ce6AHcMF6jzcco5QaTJE+jA@mail.gmail.com>
URL: https://gitlist.dev/e/CAA3EhHK25rLU-gQNUG0cmJ4E1g%2BCe6AHcMF6jzcco5QaTJE%2BjA%40mail.gmail.com
In-Reply-To: <7vobo7dlyl.fsf@alter.siamese.dyndns.org>

```
On Mon, Jun 25, 2012 at 7:13 PM, Junio C Hamano <gitster@pobox.com> wrote:
> 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.

>
>> @@ -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, 2012-06-26 04:00

Subject: Re: [PATCH v2] log: Handle new repository case.
Message-ID: <7v7guuen8m.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7guuen8m.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CAA3EhHK25rLU-gQNUG0cmJ4E1g+Ce6AHcMF6jzcco5QaTJE+jA@mail.gmail.com>

```
Leila <muhtasib@gmail.com> writes:

>> 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?

```
