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

Re: [PATCH v4 03/19] builtin/diff-tree: convert to struct object_id

From
Jeff King <peff@peff.net>
Date
Feb 20, 2017, 08:09 UTC
Message-ID
<20170220080902.vkexezd5solnhrhb@sigill.intra.peff.net>
In-Reply-To
<20170220001031.559931-4-sandals@crustytoothpaste.net>
On Mon, Feb 20, 2017 at 12:10:15AM +0000, brian m. carlson wrote:
Show 26 quoted lines
>  /* Diff one or more commits. */
> -static int stdin_diff_commit(struct commit *commit, char *line, int len)
> +static int stdin_diff_commit(struct commit *commit, const char *p)
>  {
> -	unsigned char sha1[20];
> -	if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
> +	struct object_id oid;
> +	if (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
>  		/* Graft the fake parents locally to the commit */
> -		int pos = 41;
>  		struct commit_list **pptr;
>  
>  		/* Free the real parent list */
>  		free_commit_list(commit->parents);
>  		commit->parents = NULL;
>  		pptr = &(commit->parents);
> -		while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
> -			struct commit *parent = lookup_commit(sha1);
> +		while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
> +			struct commit *parent = lookup_commit(oid.hash);
>  			if (parent) {
>  				pptr = &commit_list_insert(parent, pptr)->next;
>  			}
> -			pos += 41;
>  		}
>  	}

Are you sure this is right? The first "if" will advance the "p" pointer, and we'll miss it in the inner loop.

IOW, the original looked something like:
  1. see if we have any parents after the initial commit sha1
  2. if so, then free the original parent list, so we can parse the new
     ones
  3. starting at pos 41 (the same one we parsed in the conditional!),
     loop and parse each parent sha1

The conditional in step 1 can't advance our pointer, or we miss the first parent in step 3.

It's silly to parse the same sha1 twice, though. You could solve it by adding the first "oid" from the conditional to the new parent list. In my "something like this" patch, I solved it by dropping the conditional, and just having the inner loop. It lazily drops the old parent list on the first iteration.

It's a little disturbing that we do not seem to have even a basic test of:

  git rev-list --parents HEAD | git diff-tree --stdin
which would exercise this code.
-Peff
Previous: brian m. carlsonNext: brian m. carlson
Message 21 of 24 in “object_id part 6”
  1. 00/19 object_id part 6brian m. carlson, Feb 20, 2017
  2. 16/19 sha1_file: introduce an nth_packed_object_oid functionbrian m. carlson, Feb 20, 2017
  3. 15/19 refs: simplify parsing of reflog entriesbrian m. carlson, Feb 20, 2017
  4. 05/19 builtin/fast-export: convert to struct object_idbrian m. carlson, Feb 20, 2017
  5. 09/19 builtin/clone: convert to struct object_idbrian m. carlson, Feb 20, 2017
  6. 18/19 builtin/merge-base: convert to struct object_idbrian m. carlson, Feb 20, 2017
  7. 01/19 hex: introduce parse_oid_hexbrian m. carlson, Feb 20, 2017
  8. 12/19 builtin/replace: convert to struct object_idbrian m. carlson, Feb 20, 2017
  9. 14/19 refs: convert each_reflog_ent_fn to struct object_idbrian m. carlson, Feb 20, 2017
  10. 19/19 wt-status: convert to struct object_idbrian m. carlson, Feb 20, 2017
  11. 08/19 builtin/branch: convert to struct object_idbrian m. carlson, Feb 20, 2017
  12. 17/19 Convert object iteration callbacks to struct object_idbrian m. carlson, Feb 20, 2017
  13. 06/19 builtin/fmt-merge-message: convert to struct object_idbrian m. carlson, Feb 20, 2017
  14. 07/19 builtin/grep: convert to struct object_idbrian m. carlson, Feb 20, 2017
  15. 13/19 reflog-walk: convert struct reflog_info to struct object_idbrian m. carlson, Feb 20, 2017
  16. 11/19 Convert remaining callers of resolve_refdup to object_idbrian m. carlson, Feb 20, 2017
  17. 10/19 builtin/merge: convert to struct object_idbrian m. carlson, Feb 20, 2017
  18. 02/19 builtin/commit: convert to struct object_idbrian m. carlson, Feb 20, 2017
  19. 04/19 builtin/describe: convert to struct object_idbrian m. carlson, Feb 20, 2017
  20. 03/19 builtin/diff-tree: convert to struct object_idbrian m. carlson, Feb 20, 2017
  21. Jeff KingFeb 20, 2017
  22. brian m. carlsonFeb 21, 2017
  23. Jeff KingFeb 21, 2017
  24. brian m. carlsonFeb 21, 2017

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.