Skip to content

Commit 4674ab6

Browse files
committed
apply: fix uninitialized hash function
"git apply" can work outside a repository as a better "GNU patch", but when it does so, it still assumed that it can access the_hash_algo, which is no longer true in the new world order. Make sure we explicitly fall back to SHA-1 algorithm for backward compatibility. It is of dubious value to make this configurable to other hash algorithms, as the code does not use the_hash_algo for hashing purposes when working outside a repository (which is how the_hash_algo is left to NULL)---it is only used to learn the max length of the hash when parsing the object names on the "index" line, but failing to parse the "index" line is not a hard failure, and the program does not support operations like applying binary patches and --3way fallback that requires object access outside a repository. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d058b8 commit 4674ab6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

builtin/apply.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "builtin.h"
22
#include "gettext.h"
33
#include "repository.h"
4+
#include "hash.h"
45
#include "apply.h"
56

67
static const char * const apply_usage[] = {
@@ -18,6 +19,15 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
1819
if (init_apply_state(&state, the_repository, prefix))
1920
exit(128);
2021

22+
/*
23+
* We could to redo the "apply.c" machinery to make this
24+
* arbitrary fallback unnecessary, but it is dubious that it
25+
* is worth the effort.
26+
* cf. https://lore.kernel.org/git/[email protected]/
27+
*/
28+
if (!the_hash_algo)
29+
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
30+
2131
argc = apply_parse_options(argc, argv,
2232
&state, &force_apply, &options,
2333
apply_usage);

t/t1517-outside-repo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test_expect_success 'hash-object outside repository (uses SHA-1)' '
3737
test_cmp hash.expect hash.actual
3838
'
3939

40-
test_expect_failure 'apply a patch outside repository' '
40+
test_expect_success 'apply a patch outside repository' '
4141
(
4242
cd non-repo &&
4343
cp ../nums.old nums &&

0 commit comments

Comments
 (0)