Skip to contentSkip to conversation

log: Handle new repository case.

PATCH In review Leila Muhtasib 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 log: Handle new repository case.

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.

---
 builtin/log.c |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)
builtin/log.c +19 -5
index 4f1b42a..1685946 100644
@@ -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;
Signature · 1 line
1.7.7.5 (Apple Git-26)
#1 lore raw

builtin/log.c int cmd_log(int argc, const char **argv, const char *prefix) · line 572

Quoted 13 lines · Leila Muhtasib
builtin/log.c +6 -1
@@ -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?

builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1145

Quoted 6 lines
builtin/log.c +1 -1
@@ -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?

author
#2 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Leila Muhtasib
builtin/log.c +3 -1
@@ -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);
+
Quoted 8 lines

builtin/log.c int cmd_log(int argc, const char **argv, const char *prefix) · line 572

Quoted 4 lines
builtin/log.c +3 -0
+     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.

Quoted 12 lines

builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1145

Quoted 7 lines
builtin/log.c +1 -1
@@ -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.

#3 lore raw
Quoted 8 lines · Leila
Quoted 4 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?