git_futils_readbuffer: don't compute sha-1 Don't compute the sha-1 in `git_futils_readbuffer_updated` unless the checksum was requested. This means that `git_futils_readbuffer` will not calculate the checksum unnecessarily.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
diff --git a/src/fileops.c b/src/fileops.c
index 57dea8f..acb3b87 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -196,28 +196,29 @@ int git_futils_readbuffer_updated(
p_close(fd);
- if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size)) < 0) {
- git_buf_free(&buf);
- return error;
- }
+ if (checksum) {
+ if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size)) < 0) {
+ git_buf_free(&buf);
+ return error;
+ }
- /*
- * If we were given a checksum, we only want to use it if it's different
- */
- if (checksum && !git_oid__cmp(checksum, &checksum_new)) {
- git_buf_free(&buf);
- if (updated)
- *updated = 0;
+ /*
+ * If we were given a checksum, we only want to use it if it's different
+ */
+ if (!git_oid__cmp(checksum, &checksum_new)) {
+ git_buf_free(&buf);
+ if (updated)
+ *updated = 0;
- return 0;
+ return 0;
+ }
+
+ git_oid_cpy(checksum, &checksum_new);
}
/*
* If we're here, the file did change, or the user didn't have an old version
*/
- if (checksum)
- git_oid_cpy(checksum, &checksum_new);
-
if (updated != NULL)
*updated = 1;