{"thread":{"id":"30899","subject":"[PATCH v2] log: Handle new repository case.","startedAt":"2012-06-25T22:53:33Z","lastAt":"2012-06-26T04:00:25Z","messageCount":4,"participants":["Leila Muhtasib","Junio C Hamano","Leila"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"194234","messageId":"1340664813-96891-1-git-send-email-muhtasib@gmail.com","threadId":"30899","inReplyTo":null,"subject":"[PATCH v2] log: Handle new repository case.","fromName":"Leila Muhtasib","fromEmail":"muhtasib@gmail.com","sentAt":"2012-06-25T22:53:33Z","receivedAt":"2012-06-25T22:53:33Z","isPatch":true,"sender":{"key":"muhtasib@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1618875?v=4"},"body":"Currently, we display an error message if 'git log' is run on a new repo with no commits.\nThis is a corner case and shouldn't return an error.\nAlso, updated other functions to use new 'default_to_head_if_exists()' function introduced.\n\nSigned-off-by: Leila Muhtasib <muhtasib@gmail.com>\n---\n builtin/log.c |   24 +++++++++++++++++++-----\n 1 files changed, 19 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/log.c b/builtin/log.c\nindex 4f1b42a..1685946 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -355,6 +355,15 @@ static int git_log_config(const char *var, const char *value, void *cb)\n \treturn git_diff_ui_config(var, value, cb);\n }\n \n+static const char *default_to_head_if_exists(void)\n+{\n+\tunsigned char sha1[20];\n+\tif (resolve_ref_unsafe(\"HEAD\", sha1, 1, NULL))\n+\t\treturn \"HEAD\";\n+\telse\n+\t\treturn NULL;\n+}\n+\n int cmd_whatchanged(int argc, const char **argv, const char *prefix)\n {\n \tstruct rev_info rev;\n@@ -366,7 +375,7 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)\n \trev.diff = 1;\n \trev.simplify_history = 0;\n \tmemset(&opt, 0, sizeof(opt));\n-\topt.def = \"HEAD\";\n+\topt.def = default_to_head_if_exists();\n \tcmd_log_init(argc, argv, prefix, &rev, &opt);\n \tif (!rev.diffopt.output_format)\n \t\trev.diffopt.output_format = DIFF_FORMAT_RAW;\n@@ -459,7 +468,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)\n \trev.diffopt.stat_width = -1; \t/* Scale to real terminal size */\n \n \tmemset(&opt, 0, sizeof(opt));\n-\topt.def = \"HEAD\";\n+\topt.def = default_to_head_if_exists();\n \topt.tweak = show_rev_tweak_rev;\n \tcmd_log_init(argc, argv, prefix, &rev, &opt);\n \n@@ -532,7 +541,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)\n \tinit_reflog_walk(&rev.reflog_info);\n \trev.verbose_header = 1;\n \tmemset(&opt, 0, sizeof(opt));\n-\topt.def = \"HEAD\";\n+\topt.def = default_to_head_if_exists();\n \tcmd_log_init_defaults(&rev);\n \trev.abbrev_commit = 1;\n \trev.commit_format = CMIT_FMT_ONELINE;\n@@ -553,8 +562,13 @@ int cmd_log(int argc, const char **argv, const char *prefix)\n \tinit_revisions(&rev, prefix);\n \trev.always_show_header = 1;\n \tmemset(&opt, 0, sizeof(opt));\n-\topt.def = \"HEAD\";\n+\topt.def = default_to_head_if_exists();\n+\n \tcmd_log_init(argc, argv, prefix, &rev, &opt);\n+\n+\tif (!opt.def && !rev.cmdline.nr)\n+\t\treturn 0;\n+\n \treturn cmd_log_walk(&rev);\n }\n \n@@ -1128,7 +1142,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)\n \tDIFF_OPT_SET(&rev.diffopt, RECURSIVE);\n \trev.subject_prefix = fmt_patch_subject_prefix;\n \tmemset(&s_r_opt, 0, sizeof(s_r_opt));\n-\ts_r_opt.def = \"HEAD\";\n+\ts_r_opt.def = default_to_head_if_exists();\n \n \tif (default_attach) {\n \t\trev.mime_boundary = default_attach;\n-- \n1.7.7.5 (Apple Git-26)\n"},{"id":"194237","messageId":"7vobo7dlyl.fsf@alter.siamese.dyndns.org","threadId":"30899","inReplyTo":"1340664813-96891-1-git-send-email-muhtasib@gmail.com","subject":"Re: [PATCH v2] log: Handle new repository case.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-06-25T23:13:22Z","receivedAt":"2012-06-25T23:13:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Leila Muhtasib <muhtasib@gmail.com> writes:\n\n> @@ -553,8 +562,13 @@ int cmd_log(int argc, const char **argv, const char *prefix)\n>  \tinit_revisions(&rev, prefix);\n>  \trev.always_show_header = 1;\n>  \tmemset(&opt, 0, sizeof(opt));\n> -\topt.def = \"HEAD\";\n> +\topt.def = default_to_head_if_exists();\n> +\n>  \tcmd_log_init(argc, argv, prefix, &rev, &opt);\n> +\n> +\tif (!opt.def && !rev.cmdline.nr)\n> +\t\treturn 0;\n> +\n>  \treturn cmd_log_walk(&rev);\n\nDo you even need these four new lines if you do not error out?\nDoesn't log_walk() return successfully if there is nothing in the\nqueue anyway?\n\n> @@ -1128,7 +1142,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)\n>  \tDIFF_OPT_SET(&rev.diffopt, RECURSIVE);\n>  \trev.subject_prefix = fmt_patch_subject_prefix;\n>  \tmemset(&s_r_opt, 0, sizeof(s_r_opt));\n> -\ts_r_opt.def = \"HEAD\";\n> +\ts_r_opt.def = default_to_head_if_exists();\n\nI didn't think about other commands, but I do not think format-patch\nwants to silently succeed without doing anything---it is definitely\na user error, no?\n"},{"id":"194251","messageId":"CAA3EhHK25rLU-gQNUG0cmJ4E1g+Ce6AHcMF6jzcco5QaTJE+jA@mail.gmail.com","threadId":"30899","inReplyTo":"7vobo7dlyl.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v2] log: Handle new repository case.","fromName":"Leila","fromEmail":"muhtasib@gmail.com","sentAt":"2012-06-26T02:42:11Z","receivedAt":"2012-06-26T02:42:11Z","isPatch":true,"sender":{"key":"muhtasib@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1618875?v=4"},"body":"On Mon, Jun 25, 2012 at 7:13 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Leila Muhtasib <muhtasib@gmail.com> writes:\n>\n>> @@ -553,8 +562,13 @@ int cmd_log(int argc, const char **argv, const char *prefix)\n>>       init_revisions(&rev, prefix);\n>>       rev.always_show_header = 1;\n>>       memset(&opt, 0, sizeof(opt));\n>> -     opt.def = \"HEAD\";\n>> +     opt.def = default_to_head_if_exists();\n>> +\n>>       cmd_log_init(argc, argv, prefix, &rev, &opt);\n>> +\n>> +     if (!opt.def && !rev.cmdline.nr)\n>> +             return 0;\n>> +\n>>       return cmd_log_walk(&rev);\n>\n> Do you even need these four new lines if you do not error out?\n> Doesn't log_walk() return successfully if there is nothing in the\n> queue anyway?\n\nActually, yes. This was left behind because I was printing a success\nmessage \"No commit(s) to display\". Thanks for catching this.\n\n>\n>> @@ -1128,7 +1142,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)\n>>       DIFF_OPT_SET(&rev.diffopt, RECURSIVE);\n>>       rev.subject_prefix = fmt_patch_subject_prefix;\n>>       memset(&s_r_opt, 0, sizeof(s_r_opt));\n>> -     s_r_opt.def = \"HEAD\";\n>> +     s_r_opt.def = default_to_head_if_exists();\n>\n> I didn't think about other commands, but I do not think format-patch\n> wants to silently succeed without doing anything---it is definitely\n> a user error, no?\n>\n\nThe other changed commands are git show, git whatchanged, git reflog.\nAnd I think for the initial repo case, they can just silently succeed\nvs display that error message.\n\n'git format-patch' on a regular repo with commits, did nothing for me.\nThat's why I had originally included it. Though I think I'm changing\nmy mind about this because it's about generating a patch -- not just\ndisplaying info.\n"},{"id":"194257","messageId":"7v7guuen8m.fsf@alter.siamese.dyndns.org","threadId":"30899","inReplyTo":"CAA3EhHK25rLU-gQNUG0cmJ4E1g+Ce6AHcMF6jzcco5QaTJE+jA@mail.gmail.com","subject":"Re: [PATCH v2] log: Handle new repository case.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-06-26T04:00:25Z","receivedAt":"2012-06-26T04:00:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Leila <muhtasib@gmail.com> writes:\n\n>> I didn't think about other commands, but I do not think format-patch\n>> wants to silently succeed without doing anything---it is definitely\n>> a user error, no?\n>>\n>\n> The other changed commands are git show, git whatchanged, git reflog.\n> And I think for the initial repo case, they can just silently succeed\n> vs display that error message.\n\nWhat should happen if you say \"git log/show/whatchanged\" immediately\nafter running \"git checkout --orphan foo\"?  Should these commands\nsucceed silently?\n"}]}