builtin/mktree: remove USE_THE_REPOSITORY_VARIABLE
The 'cmd_mktree()' function already receives a 'struct repository *repo' pointer, but it was previously marked as UNUSED. Pass the 'repo' pointer down to 'mktree_line()' and 'write_tree()'. Consequently, remove the 'USE_THE_REPOSITORY_VARIABLE' macro, replace usages of 'the_repository', and swap 'parse_oid_hex()' with its context-aware version 'parse_oid_hex_algop()'. This refactoring is safe because 'cmd_mktree()' is registered with the 'RUN_SETUP' flag in 'git.c', which guarantees that the command is executed within a initialized repository, ensuring that the passed 'repo' pointer is never 'NULL'. Signed-off-by: Tian Yuchen <cat@malon.dev> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tian Yuchen committed
Mar 13, 2026 at 00:42 UTC
78827970ecf1cb853fc2c9059c180f91fa154c97
1 file changed
+9
-10
builtin/mktree.c
+9
-10
@@ -3,7 +3,6 @@
3
*
4
* Copyright (c) Junio C Hamano, 2006, 2009
5
*/
6
-#define USE_THE_REPOSITORY_VARIABLE
6
#include "builtin.h"
7
#include "gettext.h"
8
#include "hex.h"
@@ -46,7 +45,7 @@ static int ent_compare(const void *a_, const void *b_)
45
b->name, b->len, b->mode);
46
}
47
49
-static void write_tree(struct object_id *oid)
48
+static void write_tree(struct repository *repo, struct object_id *oid)
49
{
50
struct strbuf buf;
51
size_t size;
@@ -60,10 +59,10 @@ static void write_tree(struct object_id *oid)
59
for (i = 0; i < used; i++) {
60
struct treeent *ent = entries[i];
61
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
63
- strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
62
+ strbuf_add(&buf, ent->oid.hash, repo->hash_algo->rawsz);
63
}
64
66
- odb_write_object(the_repository->objects, buf.buf, buf.len, OBJ_TREE, oid);
65
+ odb_write_object(repo->objects, buf.buf, buf.len, OBJ_TREE, oid);
66
strbuf_release(&buf);
67
}
68
@@ -72,7 +71,7 @@ static const char *const mktree_usage[] = {
71
NULL
72
};
73
75
-static void mktree_line(char *buf, int nul_term_line, int allow_missing)
74
+static void mktree_line(struct repository *repo, char *buf, int nul_term_line, int allow_missing)
75
{
76
char *ptr, *ntr;
77
const char *p;
@@ -93,7 +92,7 @@ static void mktree_line(char *buf, int nul_term_line, int allow_missing)
92
die("input format error: %s", buf);
93
ptr = ntr + 1; /* type */
94
ntr = strchr(ptr, ' ');
96
- if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
95
+ if (!ntr || parse_oid_hex_algop(ntr + 1, &oid, &p, repo->hash_algo) ||
96
*p != '\t')
97
die("input format error: %s", buf);
98
@@ -124,7 +123,7 @@ static void mktree_line(char *buf, int nul_term_line, int allow_missing)
123
124
/* Check the type of object identified by oid without fetching objects */
125
oi.typep = &obj_type;
127
- if (odb_read_object_info_extended(the_repository->objects, &oid, &oi,
126
+ if (odb_read_object_info_extended(repo->objects, &oid, &oi,
127
OBJECT_INFO_LOOKUP_REPLACE |
128
OBJECT_INFO_QUICK |
129
OBJECT_INFO_SKIP_FETCH_OBJECT) < 0)
@@ -155,7 +154,7 @@ static void mktree_line(char *buf, int nul_term_line, int allow_missing)
154
int cmd_mktree(int ac,
155
const char **av,
156
const char *prefix,
158
- struct repository *repo UNUSED)
157
+ struct repository *repo)
158
{
159
struct strbuf sb = STRBUF_INIT;
160
struct object_id oid;
@@ -187,7 +186,7 @@ int cmd_mktree(int ac,
186
break;
187
die("input format error: (blank line only valid in batch mode)");
188
}
190
- mktree_line(sb.buf, nul_term_line, allow_missing);
189
+ mktree_line(repo, sb.buf, nul_term_line, allow_missing);
190
}
191
if (is_batch_mode && got_eof && used < 1) {
192
/*
@@ -197,7 +196,7 @@ int cmd_mktree(int ac,
196
*/
197
; /* skip creating an empty tree */
198
} else {
200
- write_tree(&oid);
199
+ write_tree(repo, &oid);
200
puts(oid_to_hex(&oid));
201
fflush(stdout);
202
}