git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] builtin/commit.c: memoize git-path for COMMIT_EDITMSG

From
Junio C Hamano <gitster@pobox.com>
Date
May 23, 2016, 19:16 UTC
Message-ID
<xmqq7feka8kk.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1464027390-1512-1-git-send-email-pranit.bauva@gmail.com>
Pranit Bauva <pranit.bauva@gmail.com> writes:
Show 8 quoted lines
> This is a follow up commit for f932729c (memoize common git-path
> "constant" files, 10-Aug-2015).
>
> It serves two purposes:
>   1. It reduces the number of calls to git_path() .
>
>   2. It serves the benefits of using GIT_PATH_FUNC as mentioned in the
>      commit message of f932729c.
All of that is a good idea, but I have huge doubts about its use.
Show 13 quoted lines
> diff --git a/builtin/commit.c b/builtin/commit.c
> index 391126e..ffa242c 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -92,8 +92,10 @@ N_("If you wish to skip this commit, use:\n"
>  "Then \"git cherry-pick --continue\" will resume cherry-picking\n"
>  "the remaining commits.\n");
>  
> +static GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
> +
>  static const char *use_message_buffer;
> -static const char commit_editmsg[] = "COMMIT_EDITMSG";
> +static const char commit_editmsg_path[] = git_path_commit_editmsg();
The function defined with the macro looks like
	const char *git_path_commit_editmsg(void)
        {
		static char *ret;
                if (!ret)
                	ret = git_pathdup("COMMIT_EDITMSG");
		return ret;
	}

so receiving its result to "const char v[]" looks somewhat suspicious.

More importantly, when is this function evaluated and returned value used to fill commit_editmsg_path[]? In order for git_pathdup() to produce a meaningful result, it needs to know where .git/ directory is, which (roughly) means setup_git_dir() must have been called from a callchain from main() somewhere already.

But I do not think the linker knows that fact.
> @@ -771,9 +773,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  		hook_arg2 = "";
>  	}
>  

Instead, what you could do is to call git_path_commit_editmsg() when you refer to that global variable whose initialization is suspect.

> -	s->fp = fopen_for_writing(git_path(commit_editmsg));
> +	s->fp = fopen_for_writing(commit_editmsg_path);
i.e.
	s->fp = fopen_for_writing(git_path_commit_editmsg());

As you can see in its definition, when the original code used to call git_path(), it is safe to call git_path_commit_editmsg(), because for the original git_path() to be correct, the code should already have established where $GIT_DIR is, so it is safe to call git_pathdup(), too. Also, as you can see in its definition, calling the function many times would not cause git_path() called many times. The first invocation will keep its value that is constant within the program that works with a constant $GIT_DIR.

And you do not free its return value.
Previous: Pranit BauvaNext: Pranit Bauva
Message 2 of 12 in “builtin/commit.c: memoize git-path for COMMIT_EDITMSG”
  1. builtin/commit.c: memoize git-path for COMMIT_EDITMSGPranit Bauva, May 23, 2016
  2. Junio C HamanoMay 23, 2016
  3. Pranit BauvaMay 24, 2016
  4. Pranit BauvaMay 24, 2016
  5. Matthieu MoyMay 24, 2016
  6. Pranit BauvaMay 24, 2016
  7. Junio C HamanoMay 24, 2016
  8. builtin/commit.c: memoize git-path for COMMIT_EDITMSGPranit Bauva, May 24, 2016
  9. Pranit BauvaJun 7, 2016
  10. Jeff KingJun 9, 2016
  11. Pranit BauvaJun 9, 2016
  12. Junio C HamanoJun 9, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.