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

Re: [PATCH v2 01/10] Define a structure for object IDs.

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 12, 2015, 00:26 UTC
Message-ID
<xmqqr3sv3vsf.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20150311220825.GB46326@vauxhall.crustytoothpaste.net>
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> Michael Haggerty recommended that I call the structure element sha1
> instead of oid in case we want to turn this into a union if we decide to
> go the additional hash route.
I'd advise against it.
As I wrote in $gmane/265337 in response to Michael:
    > 4. We continue to support working with SHA-1s declared to be (unsigned
    > char *) in some performance-critical code, even as we migrate most other
    > code to using SHA-1s embedded within a (struct object_id). This will
    > cost some duplication of code. To accept this approach, we would need an
    > idea of *how much* code duplication would be needed. E.g., how many
    > functions will need both (unsigned char *) versions and (struct
    > object_id *) versions?
    ...
    I do not know what kind of code duplication you are worried about,
    though.  If a callee needs "unsigned char *", the caller that has a
    "struct object_id *o" should pass o->hash to the callee.

And that would break the abstraction effort if you start calling the field with a name that is specific to the underlying hash function. The caller has to change o->sha1 to o->sha256 instead of keeping that as o->oid and letting the callee handle the implementation details when calling

        if (!hashcmp(o1->oid, o2->oid))
                ; /* they are the same */
        else
                ; /* they are different */

The only folks that need to _know_ what hash function is used or how long the field is are the ones that have raw bytes of the hash obtained from files (e.g. from the index) and they would do something like this to implement a function that checks the file records an object name that is expected by the caller:

        void check_oid(int fd, struct object_id *expected)
        {
                unsigned char object_name[GIT_HASH_RAWSZ];
                ...
                read(fd, object_name, GIT_HASH_RAWSZ);
                if (hashcmp(object_name, expected->oid))
                        die("object name mismatch???");
        }
or when they do know they are using a specific function, do:
        void compute_object_name(struct object_id *id,
                                unsignd char*data,
                                size_t len)
        {
                git_SHA_CTX c;
                unsigned char *sha1 = &(id->oid);
                /* if we are paranoid... */
                assert(sizeof(id->oid) >= 20);
                ...
                git_SHA1_Init(&c);
                git_SHA1_Update(&c, data, len);
                ...
                git_SHA1_Final(sha1, &c);
        }

Even the latter would not be helped if the field to store the hash were named id->sha1 very much, I would think.

Previous: brian m. carlsonNext: brian m. carlson
Message 24 of 36 in “Use a structure for object IDs.”
  1. 00/10 Use a structure for object IDs.brian m. carlson, Mar 7, 2015
  2. 01/10 Define a structure for object IDs.brian m. carlson, Mar 7, 2015
  3. 02/10 Define utility functions for object IDs.brian m. carlson, Mar 7, 2015
  4. Duy NguyenMar 8, 2015
  5. brian m. carlsonMar 8, 2015
  6. Michael HaggertyMar 11, 2015
  7. 03/10 bisect.c: convert leaf functions to use struct object_idbrian m. carlson, Mar 7, 2015
  8. 04/10 archive.c: convert to use struct object_idbrian m. carlson, Mar 7, 2015
  9. Michael HaggertyMar 11, 2015
  10. brian m. carlsonMar 11, 2015
  11. 05/10 zip: use GIT_SHA1_HEXSZ for trailersbrian m. carlson, Mar 7, 2015
  12. 06/10 bulk-checkin.c: convert to use struct object_idbrian m. carlson, Mar 7, 2015
  13. 07/10 diff: convert struct combine_diff_path to object_idbrian m. carlson, Mar 7, 2015
  14. 08/10 commit: convert parts to struct object_idbrian m. carlson, Mar 7, 2015
  15. Michael HaggertyMar 11, 2015
  16. 09/10 patch-id: convert to use struct object_idbrian m. carlson, Mar 7, 2015
  17. 10/10 apply: convert threeway_stage to object_idbrian m. carlson, Mar 7, 2015
  18. Junio C HamanoMar 8, 2015
  19. Kyle J. McKayMar 11, 2015
  20. Michael HaggertyMar 11, 2015
  21. Junio C HamanoMar 11, 2015
  22. brian m. carlsonMar 13, 2015
  23. brian m. carlsonMar 11, 2015
  24. Junio C HamanoMar 12, 2015
  25. brian m. carlsonMar 12, 2015
  26. Michael HaggertyMar 12, 2015
  27. brian m. carlsonMar 12, 2015
  28. Duy NguyenMar 12, 2015
  29. Junio C HamanoMar 12, 2015
  30. Junio C HamanoMar 12, 2015
  31. Duy NguyenMar 13, 2015
  32. Junio C HamanoMar 13, 2015
  33. Duy NguyenMar 14, 2015
  34. Junio C HamanoMar 14, 2015
  35. Duy NguyenMar 15, 2015
  36. Junio C HamanoMar 15, 2015

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.