csum-file: use idempotent git_hash_discard()

Now that it is safe to call git_hash_discard() even after finalizing it, we can simplify our cleanup logic a bit. This is mostly undoing a few bits of 64337aecde (csum-file: always finalize or discard hash, 2026-07-02): - We no longer need a separate free_hashfile_memory() function for finalize_hashfile(). It can just call free_hashfile(), which will now discard (or not) the hash as appropriate. - When f->skip_hash is set, we don't need to discard; we can rely on free_hashfile() to do it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 7, 2026 at 23:53 UTC 6728cfba89aa77b38d08154404eda65c1461bcd3
1 file changed +5 -12
csum-file.c
+5 -12
@@ -55,19 +55,14 @@ void hashflush(struct hashfile *f)
55 }
56 }
57
58 -static void free_hashfile_memory(struct hashfile *f)
58 +void free_hashfile(struct hashfile *f)
59 {
60 + git_hash_discard(&f->ctx);
61 free(f->buffer);
62 free(f->check_buffer);
63 free(f);
64 }
65
65 -void free_hashfile(struct hashfile *f)
66 -{
67 - git_hash_discard(&f->ctx);
68 - free_hashfile_memory(f);
69 -}
70 -
66 int finalize_hashfile(struct hashfile *f, unsigned char *result,
67 enum fsync_component component, unsigned int flags)
68 {
@@ -75,12 +70,10 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
70
71 hashflush(f);
72
78 - if (f->skip_hash) {
79 - git_hash_discard(&f->ctx);
73 + if (f->skip_hash)
74 hashclr(f->buffer, f->algop);
81 - } else {
75 + else
76 git_hash_final(f->buffer, &f->ctx);
83 - }
77
78 if (result)
79 hashcpy(result, f->buffer, f->algop);
@@ -105,7 +98,7 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
98 if (close(f->check_fd))
99 die_errno("%s: sha1 file error on close", f->name);
100 }
108 - free_hashfile_memory(f);
101 + free_hashfile(f);
102 return fd;
103 }
104