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

Re: [PATCH 1/2] get_sha1_oneline: allow to input commit_list

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 13, 2010, 00:29 UTC
Message-ID
<7vsjy27bxp.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1292151419-30678-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 31 quoted lines
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  sha1_name.c |   13 ++++++++++---
>  1 files changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/sha1_name.c b/sha1_name.c
> index 2c3a5fb..c298285 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -690,7 +690,9 @@ static int handle_one_ref(const char *path,
>  	return 0;
>  }
>  
> -static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
> +static int get_sha1_oneline(const char *prefix,
> +			    unsigned char *sha1,
> +			    struct commit_list *original_list)
>  {
>  	struct commit_list *list = NULL, *backup = NULL, *l;
>  	int retval = -1;
> @@ -706,7 +708,12 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
>  	if (regcomp(&regex, prefix, REG_EXTENDED))
>  		die("Invalid search pattern: %s", prefix);
>  
> -	for_each_ref(handle_one_ref, &list);
> +	for (l = original_list; l; l = l->next) {
> +		commit_list_insert(l->item, &list);
> +		l->item->object.flags |= ONELINE_SEEN;
> +	}
> +	if (!list)
> +		for_each_ref(handle_one_ref, &list);
Two-and-half yucks.
 (1) "We work on the list you give us, if you give us one, but we work on
     a list we come up with outselves in a magic way otherwise" is an API
     designed with a bad taste.  Why not make the original caller run
     for-each-ref before calling this function?
 (2) Why do you have to copy the list, using commit-list-insert, here?
 (3) Even if the above extra copy turns out to be needed, do you need yet
     another copy in "backup"?
Instead of this patch, I would suggest to go this route:
 * Remove local varaible "list" and make it an input parameter.
 * Stop calling for-each-ref from this function.  Instead, have the
   current caller run for-each-ref to prepare list and feed it to you;
 * Stop marking commits with "ONELINE_SEEN" from handle-one-ref.  Instead,
   have the loop to copy list to backup do the marking.

That way, you make the purpose of the function much more clear. It gets a list of one or more commits that are in date-order, and finds the most recent commit reachable from them that match the given string, using ONELINE_SEEN bit as a scratchpad (shouldn't we be using TMP_MARK here, by the way?). The caller is responsible for giving you a sorted list, but the caller shouldn't care about ONELINE_SEEN bit.

Previous: Jonathan Nieder
Message 5 of 5 in “get_sha1_oneline: allow to input commit_list”
  1. 1/2 get_sha1_oneline: allow to input commit_listNguyễn Thái Ngọc Duy, Dec 12, 2010
  2. 2/2 get_sha1: support ref^{/regex} syntaxNguyễn Thái Ngọc Duy, Dec 12, 2010
  3. Jonathan NiederDec 12, 2010
  4. Jonathan NiederDec 12, 2010
  5. Junio C HamanoDec 13, 2010

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.