Skip to content

Commit 0b179f3

Browse files
committed
Merge branch 'nd/sha1-name-c-wo-the-repository'
Further code clean-up to allow the lowest level of name-to-object mapping layer to work with a passed-in repository other than the default one. * nd/sha1-name-c-wo-the-repository: (34 commits) sha1-name.c: remove the_repo from get_oid_mb() sha1-name.c: remove the_repo from other get_oid_* sha1-name.c: remove the_repo from maybe_die_on_misspelt_object_name submodule-config.c: use repo_get_oid for reading .gitmodules sha1-name.c: add repo_get_oid() sha1-name.c: remove the_repo from get_oid_with_context_1() sha1-name.c: remove the_repo from resolve_relative_path() sha1-name.c: remove the_repo from diagnose_invalid_index_path() sha1-name.c: remove the_repo from handle_one_ref() sha1-name.c: remove the_repo from get_oid_1() sha1-name.c: remove the_repo from get_oid_basic() sha1-name.c: remove the_repo from get_describe_name() sha1-name.c: remove the_repo from get_oid_oneline() sha1-name.c: add repo_interpret_branch_name() sha1-name.c: remove the_repo from interpret_branch_mark() sha1-name.c: remove the_repo from interpret_nth_prior_checkout() sha1-name.c: remove the_repo from get_short_oid() sha1-name.c: add repo_for_each_abbrev() sha1-name.c: store and use repo in struct disambiguate_state sha1-name.c: add repo_find_unique_abbrev_r() ...
2 parents ce2a18f + 0daf7ff commit 0b179f3

19 files changed

+408
-242
lines changed

builtin/rebase.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15681568
branch_name = options.head_name;
15691569

15701570
} else {
1571-
free(options.head_name);
1572-
options.head_name = NULL;
1571+
FREE_AND_NULL(options.head_name);
15731572
branch_name = "HEAD";
15741573
}
15751574
if (get_oid("HEAD", &options.orig_head))
@@ -1769,7 +1768,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17691768
* we just fast-forwarded.
17701769
*/
17711770
strbuf_reset(&msg);
1772-
if (!oidcmp(&merge_base, &options.orig_head)) {
1771+
if (oideq(&merge_base, &options.orig_head)) {
17731772
printf(_("Fast-forwarded %s to %s.\n"),
17741773
branch_name, options.onto_name);
17751774
strbuf_addf(&msg, "rebase finished: %s onto %s",

builtin/show-branch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
753753
/* Ah, that is a date spec... */
754754
timestamp_t at;
755755
at = approxidate(reflog_base);
756-
read_ref_at(ref, flags, at, -1, &oid, NULL,
756+
read_ref_at(get_main_ref_store(the_repository),
757+
ref, flags, at, -1, &oid, NULL,
757758
NULL, NULL, &base);
758759
}
759760
}
@@ -765,7 +766,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
765766
timestamp_t timestamp;
766767
int tz;
767768

768-
if (read_ref_at(ref, flags, 0, base + i, &oid, &logmsg,
769+
if (read_ref_at(get_main_ref_store(the_repository),
770+
ref, flags, 0, base + i, &oid, &logmsg,
769771
&timestamp, &tz, NULL)) {
770772
reflog = i;
771773
break;

cache.h

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,10 @@ extern void check_repository_format(void);
10471047
* Note that while this version avoids the static buffer, it is not fully
10481048
* reentrant, as it calls into other non-reentrant git code.
10491049
*/
1050-
extern const char *find_unique_abbrev(const struct object_id *oid, int len);
1051-
extern int find_unique_abbrev_r(char *hex, const struct object_id *oid, int len);
1050+
const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
1051+
#define find_unique_abbrev(oid, len) repo_find_unique_abbrev(the_repository, oid, len)
1052+
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
1053+
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)
10521054

10531055
extern const unsigned char null_sha1[GIT_MAX_RAWSZ];
10541056
extern const struct object_id null_oid;
@@ -1380,20 +1382,32 @@ enum get_oid_result {
13801382
*/
13811383
};
13821384

1383-
extern int get_oid(const char *str, struct object_id *oid);
1384-
extern int get_oidf(struct object_id *oid, const char *fmt, ...);
1385-
extern int get_oid_commit(const char *str, struct object_id *oid);
1386-
extern int get_oid_committish(const char *str, struct object_id *oid);
1387-
extern int get_oid_tree(const char *str, struct object_id *oid);
1388-
extern int get_oid_treeish(const char *str, struct object_id *oid);
1389-
extern int get_oid_blob(const char *str, struct object_id *oid);
1390-
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
1385+
int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
1386+
int get_oidf(struct object_id *oid, const char *fmt, ...);
1387+
int repo_get_oid_commit(struct repository *r, const char *str, struct object_id *oid);
1388+
int repo_get_oid_committish(struct repository *r, const char *str, struct object_id *oid);
1389+
int repo_get_oid_tree(struct repository *r, const char *str, struct object_id *oid);
1390+
int repo_get_oid_treeish(struct repository *r, const char *str, struct object_id *oid);
1391+
int repo_get_oid_blob(struct repository *r, const char *str, struct object_id *oid);
1392+
int repo_get_oid_mb(struct repository *r, const char *str, struct object_id *oid);
1393+
void maybe_die_on_misspelt_object_name(struct repository *repo,
1394+
const char *name,
1395+
const char *prefix);
13911396
extern enum get_oid_result get_oid_with_context(struct repository *repo, const char *str,
13921397
unsigned flags, struct object_id *oid,
13931398
struct object_context *oc);
13941399

1400+
#define get_oid(str, oid) repo_get_oid(the_repository, str, oid)
1401+
#define get_oid_commit(str, oid) repo_get_oid_commit(the_repository, str, oid)
1402+
#define get_oid_committish(str, oid) repo_get_oid_committish(the_repository, str, oid)
1403+
#define get_oid_tree(str, oid) repo_get_oid_tree(the_repository, str, oid)
1404+
#define get_oid_treeish(str, oid) repo_get_oid_treeish(the_repository, str, oid)
1405+
#define get_oid_blob(str, oid) repo_get_oid_blob(the_repository, str, oid)
1406+
#define get_oid_mb(str, oid) repo_get_oid_mb(the_repository, str, oid)
1407+
13951408
typedef int each_abbrev_fn(const struct object_id *oid, void *);
1396-
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
1409+
int repo_for_each_abbrev(struct repository *r, const char *prefix, each_abbrev_fn, void *);
1410+
#define for_each_abbrev(prefix, fn, data) repo_for_each_abbrev(the_repository, prefix, fn, data)
13971411

13981412
extern int set_disambiguate_hint_config(const char *var, const char *value);
13991413

@@ -1471,9 +1485,12 @@ extern int parse_oid_hex(const char *hex, struct object_id *oid, const char **en
14711485
#define INTERPRET_BRANCH_LOCAL (1<<0)
14721486
#define INTERPRET_BRANCH_REMOTE (1<<1)
14731487
#define INTERPRET_BRANCH_HEAD (1<<2)
1474-
extern int interpret_branch_name(const char *str, int len, struct strbuf *,
1475-
unsigned allowed);
1476-
extern int get_oid_mb(const char *str, struct object_id *oid);
1488+
int repo_interpret_branch_name(struct repository *r,
1489+
const char *str, int len,
1490+
struct strbuf *buf,
1491+
unsigned allowed);
1492+
#define interpret_branch_name(str, len, buf, allowed) \
1493+
repo_interpret_branch_name(the_repository, str, len, buf, allowed)
14771494

14781495
extern int validate_headref(const char *ref);
14791496

@@ -1487,8 +1504,11 @@ extern void *read_object_with_reference(const struct object_id *oid,
14871504
unsigned long *size,
14881505
struct object_id *oid_ret);
14891506

1490-
extern struct object *peel_to_type(const char *name, int namelen,
1491-
struct object *o, enum object_type);
1507+
struct object *repo_peel_to_type(struct repository *r,
1508+
const char *name, int namelen,
1509+
struct object *o, enum object_type);
1510+
#define peel_to_type(name, namelen, obj, type) \
1511+
repo_peel_to_type(the_repository, name, namelen, obj, type)
14921512

14931513
enum date_mode_type {
14941514
DATE_NORMAL = 0,

commit-graph.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
397397
item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
398398
}
399399

400+
static inline void set_commit_tree(struct commit *c, struct tree *t)
401+
{
402+
c->maybe_tree = t;
403+
}
404+
400405
static int fill_commit_in_graph(struct repository *r,
401406
struct commit *item,
402407
struct commit_graph *g, uint32_t pos)
@@ -410,7 +415,7 @@ static int fill_commit_in_graph(struct repository *r,
410415
item->object.parsed = 1;
411416
item->graph_pos = pos;
412417

413-
item->maybe_tree = NULL;
418+
set_commit_tree(item, NULL);
414419

415420
date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
416421
date_low = get_be32(commit_data + g->hash_len + 12);
@@ -496,7 +501,7 @@ static struct tree *load_tree_for_commit(struct repository *r,
496501
GRAPH_DATA_WIDTH * (c->graph_pos);
497502

498503
hashcpy(oid.hash, commit_data);
499-
c->maybe_tree = lookup_tree(r, &oid);
504+
set_commit_tree(c, lookup_tree(r, &oid));
500505

501506
return c->maybe_tree;
502507
}

commit.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,21 @@ void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
340340
}
341341
}
342342

343-
struct tree *get_commit_tree(const struct commit *commit)
343+
static inline void set_commit_tree(struct commit *c, struct tree *t)
344+
{
345+
c->maybe_tree = t;
346+
}
347+
348+
struct tree *repo_get_commit_tree(struct repository *r,
349+
const struct commit *commit)
344350
{
345351
if (commit->maybe_tree || !commit->object.parsed)
346352
return commit->maybe_tree;
347353

348354
if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
349355
BUG("commit has NULL tree, but was not loaded from commit-graph");
350356

351-
return get_commit_tree_in_graph(the_repository, commit);
357+
return get_commit_tree_in_graph(r, commit);
352358
}
353359

354360
struct object_id *get_commit_tree_oid(const struct commit *commit)
@@ -358,7 +364,7 @@ struct object_id *get_commit_tree_oid(const struct commit *commit)
358364

359365
void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
360366
{
361-
c->maybe_tree = NULL;
367+
set_commit_tree(c, NULL);
362368
c->index = 0;
363369
free_commit_buffer(pool, c);
364370
free_commit_list(c->parents);
@@ -406,7 +412,7 @@ int parse_commit_buffer(struct repository *r, struct commit *item, const void *b
406412
if (get_oid_hex(bufptr + 5, &parent) < 0)
407413
return error("bad tree pointer in commit %s",
408414
oid_to_hex(&item->object.oid));
409-
item->maybe_tree = lookup_tree(r, &parent);
415+
set_commit_tree(item, lookup_tree(r, &parent));
410416
bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
411417
pptr = &item->parents;
412418

commit.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct commit {
3232

3333
/*
3434
* If the commit is loaded from the commit-graph file, then this
35-
* member may be NULL. Only access it through get_commit_tree()
35+
* member may be NULL. Only access it through repo_get_commit_tree()
3636
* or get_commit_tree_oid().
3737
*/
3838
struct tree *maybe_tree;
@@ -149,7 +149,8 @@ void repo_unuse_commit_buffer(struct repository *r,
149149
*/
150150
void free_commit_buffer(struct parsed_object_pool *pool, struct commit *);
151151

152-
struct tree *get_commit_tree(const struct commit *);
152+
struct tree *repo_get_commit_tree(struct repository *, const struct commit *);
153+
#define get_commit_tree(c) repo_get_commit_tree(the_repository, c)
153154
struct object_id *get_commit_tree_oid(const struct commit *);
154155

155156
/*

contrib/coccinelle/commit.cocci

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@ expression c;
1010
- c->maybe_tree->object.oid.hash
1111
+ get_commit_tree_oid(c)->hash
1212

13-
// These excluded functions must access c->maybe_tree direcly.
1413
@@
15-
identifier f !~ "^(get_commit_tree|get_commit_tree_in_graph_one|load_tree_for_commit)$";
14+
identifier f !~ "^set_commit_tree$";
1615
expression c;
16+
expression s;
1717
@@
1818
f(...) {<...
19-
- c->maybe_tree
20-
+ get_commit_tree(c)
19+
- c->maybe_tree = s
20+
+ set_commit_tree(c, s)
2121
...>}
2222

23+
// These excluded functions must access c->maybe_tree direcly.
24+
// Note that if c->maybe_tree is written somewhere outside of these
25+
// functions, then the recommended transformation will be bogus with
26+
// repo_get_commit_tree() on the LHS.
2327
@@
28+
identifier f !~ "^(repo_get_commit_tree|get_commit_tree_in_graph_one|load_tree_for_commit|set_commit_tree)$";
2429
expression c;
25-
expression s;
2630
@@
27-
- get_commit_tree(c) = s
28-
+ c->maybe_tree = s
31+
f(...) {<...
32+
- c->maybe_tree
33+
+ repo_get_commit_tree(specify_the_right_repo_here, c)
34+
...>}

dir.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,14 @@ int file_exists(const char *f)
23162316
return lstat(f, &sb) == 0;
23172317
}
23182318

2319+
int repo_file_exists(struct repository *repo, const char *path)
2320+
{
2321+
if (repo != the_repository)
2322+
BUG("do not know how to check file existence in arbitrary repo");
2323+
2324+
return file_exists(path);
2325+
}
2326+
23192327
static int cmp_icase(char a, char b)
23202328
{
23212329
if (a == b)

dir.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ extern void add_exclude(const char *string, const char *base,
269269
int baselen, struct exclude_list *el, int srcpos);
270270
extern void clear_exclude_list(struct exclude_list *el);
271271
extern void clear_directory(struct dir_struct *dir);
272-
extern int file_exists(const char *);
272+
273+
int repo_file_exists(struct repository *repo, const char *path);
274+
int file_exists(const char *);
273275

274276
extern int is_inside_dir(const char *dir);
275277
extern int dir_inside_of(const char *subdir, const char *dir);

merge-recursive.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,19 @@ static struct tree *shift_tree_object(struct repository *repo,
163163
return lookup_tree(repo, &shifted);
164164
}
165165

166+
static inline void set_commit_tree(struct commit *c, struct tree *t)
167+
{
168+
c->maybe_tree = t;
169+
}
170+
166171
static struct commit *make_virtual_commit(struct repository *repo,
167172
struct tree *tree,
168173
const char *comment)
169174
{
170175
struct commit *commit = alloc_commit_node(repo);
171176

172177
set_merge_remote_desc(commit, comment, (struct object *)commit);
173-
commit->maybe_tree = tree;
178+
set_commit_tree(commit, tree);
174179
commit->object.parsed = 1;
175180
return commit;
176181
}

0 commit comments

Comments
 (0)