@@ -49,26 +49,37 @@ static void print_help(void)
4949 cli_opt_help_fprint (stdout , opts );
5050}
5151
52- static int hash_buf (git_odb * odb , git_str * buf , git_object_t type )
52+ static int hash_buf (
53+ git_odb * odb ,
54+ git_str * buf ,
55+ git_object_t object_type ,
56+ git_oid_t oid_type )
5357{
5458 git_oid oid ;
5559
5660 if (!literally ) {
5761 int valid = 0 ;
5862
59- if (git_object_rawcontent_is_valid (& valid , buf -> ptr , buf -> size , type ) < 0 || !valid )
63+ #ifdef GIT_EXPERIMENTAL_SHA256
64+ if (git_object_rawcontent_is_valid (& valid , buf -> ptr , buf -> size , object_type , oid_type ) < 0 || !valid )
65+ return cli_error_git ();
66+ #else
67+ GIT_UNUSED (oid_type );
68+
69+ if (git_object_rawcontent_is_valid (& valid , buf -> ptr , buf -> size , object_type ) < 0 || !valid )
6070 return cli_error_git ();
71+ #endif
6172 }
6273
6374 if (write_object ) {
64- if (git_odb_write (& oid , odb , buf -> ptr , buf -> size , type ) < 0 )
75+ if (git_odb_write (& oid , odb , buf -> ptr , buf -> size , object_type ) < 0 )
6576 return cli_error_git ();
6677 } else {
6778#ifdef GIT_EXPERIMENTAL_SHA256
68- if (git_odb_hash (& oid , buf -> ptr , buf -> size , type , GIT_OID_SHA1 ) < 0 )
79+ if (git_odb_hash (& oid , buf -> ptr , buf -> size , object_type , GIT_OID_SHA1 ) < 0 )
6980 return cli_error_git ();
7081#else
71- if (git_odb_hash (& oid , buf -> ptr , buf -> size , type ) < 0 )
82+ if (git_odb_hash (& oid , buf -> ptr , buf -> size , object_type ) < 0 )
7283 return cli_error_git ();
7384#endif
7485 }
@@ -83,9 +94,10 @@ int cmd_hash_object(int argc, char **argv)
8394{
8495 git_repository * repo = NULL ;
8596 git_odb * odb = NULL ;
97+ git_oid_t oid_type ;
8698 git_str buf = GIT_STR_INIT ;
8799 cli_opt invalid_opt ;
88- git_object_t type = GIT_OBJECT_BLOB ;
100+ git_object_t object_type = GIT_OBJECT_BLOB ;
89101 char * * filename ;
90102 int ret = 0 ;
91103
@@ -97,7 +109,7 @@ int cmd_hash_object(int argc, char **argv)
97109 return 0 ;
98110 }
99111
100- if (type_name && (type = git_object_string2type (type_name )) == GIT_OBJECT_INVALID )
112+ if (type_name && (object_type = git_object_string2type (type_name )) == GIT_OBJECT_INVALID )
101113 return cli_error_usage ("invalid object type '%s'" , type_name );
102114
103115 if (write_object &&
@@ -107,6 +119,8 @@ int cmd_hash_object(int argc, char **argv)
107119 goto done ;
108120 }
109121
122+ oid_type = git_repository_oid_type (repo );
123+
110124 /*
111125 * TODO: we're reading blobs, we shouldn't pull them all into main
112126 * memory, we should just stream them into the odb instead.
@@ -118,7 +132,7 @@ int cmd_hash_object(int argc, char **argv)
118132 goto done ;
119133 }
120134
121- if ((ret = hash_buf (odb , & buf , type )) != 0 )
135+ if ((ret = hash_buf (odb , & buf , object_type , oid_type )) != 0 )
122136 goto done ;
123137 } else {
124138 for (filename = filenames ; * filename ; filename ++ ) {
@@ -127,7 +141,7 @@ int cmd_hash_object(int argc, char **argv)
127141 goto done ;
128142 }
129143
130- if ((ret = hash_buf (odb , & buf , type )) != 0 )
144+ if ((ret = hash_buf (odb , & buf , object_type , oid_type )) != 0 )
131145 goto done ;
132146 }
133147 }
0 commit comments