Skip to content

Commit

Permalink
Merge branch 'nd/sha1-name-c-wo-the-repository'
Browse files Browse the repository at this point in the history
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()
  ...
  • Loading branch information
gitster committed May 8, 2019
2 parents ce2a18f + 0daf7ff commit 0b179f3
Show file tree
Hide file tree
Showing 19 changed files with 408 additions and 242 deletions.
5 changes: 2 additions & 3 deletions builtin/rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
branch_name = options.head_name;

} else {
free(options.head_name);
options.head_name = NULL;
FREE_AND_NULL(options.head_name);
branch_name = "HEAD";
}
if (get_oid("HEAD", &options.orig_head))
Expand Down Expand Up @@ -1769,7 +1768,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
* we just fast-forwarded.
*/
strbuf_reset(&msg);
if (!oidcmp(&merge_base, &options.orig_head)) {
if (oideq(&merge_base, &options.orig_head)) {
printf(_("Fast-forwarded %s to %s.\n"),
branch_name, options.onto_name);
strbuf_addf(&msg, "rebase finished: %s onto %s",
Expand Down
6 changes: 4 additions & 2 deletions builtin/show-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
/* Ah, that is a date spec... */
timestamp_t at;
at = approxidate(reflog_base);
read_ref_at(ref, flags, at, -1, &oid, NULL,
read_ref_at(get_main_ref_store(the_repository),
ref, flags, at, -1, &oid, NULL,
NULL, NULL, &base);
}
}
Expand All @@ -765,7 +766,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
timestamp_t timestamp;
int tz;

if (read_ref_at(ref, flags, 0, base + i, &oid, &logmsg,
if (read_ref_at(get_main_ref_store(the_repository),
ref, flags, 0, base + i, &oid, &logmsg,
&timestamp, &tz, NULL)) {
reflog = i;
break;
Expand Down
52 changes: 36 additions & 16 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,10 @@ extern void check_repository_format(void);
* Note that while this version avoids the static buffer, it is not fully
* reentrant, as it calls into other non-reentrant git code.
*/
extern const char *find_unique_abbrev(const struct object_id *oid, int len);
extern int find_unique_abbrev_r(char *hex, const struct object_id *oid, int len);
const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
#define find_unique_abbrev(oid, len) repo_find_unique_abbrev(the_repository, oid, len)
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)

extern const unsigned char null_sha1[GIT_MAX_RAWSZ];
extern const struct object_id null_oid;
Expand Down Expand Up @@ -1380,20 +1382,32 @@ enum get_oid_result {
*/
};

extern int get_oid(const char *str, struct object_id *oid);
extern int get_oidf(struct object_id *oid, const char *fmt, ...);
extern int get_oid_commit(const char *str, struct object_id *oid);
extern int get_oid_committish(const char *str, struct object_id *oid);
extern int get_oid_tree(const char *str, struct object_id *oid);
extern int get_oid_treeish(const char *str, struct object_id *oid);
extern int get_oid_blob(const char *str, struct object_id *oid);
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
int get_oidf(struct object_id *oid, const char *fmt, ...);
int repo_get_oid_commit(struct repository *r, const char *str, struct object_id *oid);
int repo_get_oid_committish(struct repository *r, const char *str, struct object_id *oid);
int repo_get_oid_tree(struct repository *r, const char *str, struct object_id *oid);
int repo_get_oid_treeish(struct repository *r, const char *str, struct object_id *oid);
int repo_get_oid_blob(struct repository *r, const char *str, struct object_id *oid);
int repo_get_oid_mb(struct repository *r, const char *str, struct object_id *oid);
void maybe_die_on_misspelt_object_name(struct repository *repo,
const char *name,
const char *prefix);
extern enum get_oid_result get_oid_with_context(struct repository *repo, const char *str,
unsigned flags, struct object_id *oid,
struct object_context *oc);

#define get_oid(str, oid) repo_get_oid(the_repository, str, oid)
#define get_oid_commit(str, oid) repo_get_oid_commit(the_repository, str, oid)
#define get_oid_committish(str, oid) repo_get_oid_committish(the_repository, str, oid)
#define get_oid_tree(str, oid) repo_get_oid_tree(the_repository, str, oid)
#define get_oid_treeish(str, oid) repo_get_oid_treeish(the_repository, str, oid)
#define get_oid_blob(str, oid) repo_get_oid_blob(the_repository, str, oid)
#define get_oid_mb(str, oid) repo_get_oid_mb(the_repository, str, oid)

typedef int each_abbrev_fn(const struct object_id *oid, void *);
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
int repo_for_each_abbrev(struct repository *r, const char *prefix, each_abbrev_fn, void *);
#define for_each_abbrev(prefix, fn, data) repo_for_each_abbrev(the_repository, prefix, fn, data)

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

Expand Down Expand Up @@ -1471,9 +1485,12 @@ extern int parse_oid_hex(const char *hex, struct object_id *oid, const char **en
#define INTERPRET_BRANCH_LOCAL (1<<0)
#define INTERPRET_BRANCH_REMOTE (1<<1)
#define INTERPRET_BRANCH_HEAD (1<<2)
extern int interpret_branch_name(const char *str, int len, struct strbuf *,
unsigned allowed);
extern int get_oid_mb(const char *str, struct object_id *oid);
int repo_interpret_branch_name(struct repository *r,
const char *str, int len,
struct strbuf *buf,
unsigned allowed);
#define interpret_branch_name(str, len, buf, allowed) \
repo_interpret_branch_name(the_repository, str, len, buf, allowed)

extern int validate_headref(const char *ref);

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

extern struct object *peel_to_type(const char *name, int namelen,
struct object *o, enum object_type);
struct object *repo_peel_to_type(struct repository *r,
const char *name, int namelen,
struct object *o, enum object_type);
#define peel_to_type(name, namelen, obj, type) \
repo_peel_to_type(the_repository, name, namelen, obj, type)

enum date_mode_type {
DATE_NORMAL = 0,
Expand Down
9 changes: 7 additions & 2 deletions commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
}

static inline void set_commit_tree(struct commit *c, struct tree *t)
{
c->maybe_tree = t;
}

static int fill_commit_in_graph(struct repository *r,
struct commit *item,
struct commit_graph *g, uint32_t pos)
Expand All @@ -410,7 +415,7 @@ static int fill_commit_in_graph(struct repository *r,
item->object.parsed = 1;
item->graph_pos = pos;

item->maybe_tree = NULL;
set_commit_tree(item, NULL);

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

hashcpy(oid.hash, commit_data);
c->maybe_tree = lookup_tree(r, &oid);
set_commit_tree(c, lookup_tree(r, &oid));

return c->maybe_tree;
}
Expand Down
14 changes: 10 additions & 4 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,21 @@ void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
}
}

struct tree *get_commit_tree(const struct commit *commit)
static inline void set_commit_tree(struct commit *c, struct tree *t)
{
c->maybe_tree = t;
}

struct tree *repo_get_commit_tree(struct repository *r,
const struct commit *commit)
{
if (commit->maybe_tree || !commit->object.parsed)
return commit->maybe_tree;

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

return get_commit_tree_in_graph(the_repository, commit);
return get_commit_tree_in_graph(r, commit);
}

struct object_id *get_commit_tree_oid(const struct commit *commit)
Expand All @@ -358,7 +364,7 @@ struct object_id *get_commit_tree_oid(const struct commit *commit)

void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
{
c->maybe_tree = NULL;
set_commit_tree(c, NULL);
c->index = 0;
free_commit_buffer(pool, c);
free_commit_list(c->parents);
Expand Down Expand Up @@ -406,7 +412,7 @@ int parse_commit_buffer(struct repository *r, struct commit *item, const void *b
if (get_oid_hex(bufptr + 5, &parent) < 0)
return error("bad tree pointer in commit %s",
oid_to_hex(&item->object.oid));
item->maybe_tree = lookup_tree(r, &parent);
set_commit_tree(item, lookup_tree(r, &parent));
bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
pptr = &item->parents;

Expand Down
5 changes: 3 additions & 2 deletions commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct commit {

/*
* If the commit is loaded from the commit-graph file, then this
* member may be NULL. Only access it through get_commit_tree()
* member may be NULL. Only access it through repo_get_commit_tree()
* or get_commit_tree_oid().
*/
struct tree *maybe_tree;
Expand Down Expand Up @@ -149,7 +149,8 @@ void repo_unuse_commit_buffer(struct repository *r,
*/
void free_commit_buffer(struct parsed_object_pool *pool, struct commit *);

struct tree *get_commit_tree(const struct commit *);
struct tree *repo_get_commit_tree(struct repository *, const struct commit *);
#define get_commit_tree(c) repo_get_commit_tree(the_repository, c)
struct object_id *get_commit_tree_oid(const struct commit *);

/*
Expand Down
20 changes: 13 additions & 7 deletions contrib/coccinelle/commit.cocci
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ expression c;
- c->maybe_tree->object.oid.hash
+ get_commit_tree_oid(c)->hash

// These excluded functions must access c->maybe_tree direcly.
@@
identifier f !~ "^(get_commit_tree|get_commit_tree_in_graph_one|load_tree_for_commit)$";
identifier f !~ "^set_commit_tree$";
expression c;
expression s;
@@
f(...) {<...
- c->maybe_tree
+ get_commit_tree(c)
- c->maybe_tree = s
+ set_commit_tree(c, s)
...>}

// These excluded functions must access c->maybe_tree direcly.
// Note that if c->maybe_tree is written somewhere outside of these
// functions, then the recommended transformation will be bogus with
// repo_get_commit_tree() on the LHS.
@@
identifier f !~ "^(repo_get_commit_tree|get_commit_tree_in_graph_one|load_tree_for_commit|set_commit_tree)$";
expression c;
expression s;
@@
- get_commit_tree(c) = s
+ c->maybe_tree = s
f(...) {<...
- c->maybe_tree
+ repo_get_commit_tree(specify_the_right_repo_here, c)
...>}
8 changes: 8 additions & 0 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,14 @@ int file_exists(const char *f)
return lstat(f, &sb) == 0;
}

int repo_file_exists(struct repository *repo, const char *path)
{
if (repo != the_repository)
BUG("do not know how to check file existence in arbitrary repo");

return file_exists(path);
}

static int cmp_icase(char a, char b)
{
if (a == b)
Expand Down
4 changes: 3 additions & 1 deletion dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ extern void add_exclude(const char *string, const char *base,
int baselen, struct exclude_list *el, int srcpos);
extern void clear_exclude_list(struct exclude_list *el);
extern void clear_directory(struct dir_struct *dir);
extern int file_exists(const char *);

int repo_file_exists(struct repository *repo, const char *path);
int file_exists(const char *);

extern int is_inside_dir(const char *dir);
extern int dir_inside_of(const char *subdir, const char *dir);
Expand Down
7 changes: 6 additions & 1 deletion merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,19 @@ static struct tree *shift_tree_object(struct repository *repo,
return lookup_tree(repo, &shifted);
}

static inline void set_commit_tree(struct commit *c, struct tree *t)
{
c->maybe_tree = t;
}

static struct commit *make_virtual_commit(struct repository *repo,
struct tree *tree,
const char *comment)
{
struct commit *commit = alloc_commit_node(repo);

set_merge_remote_desc(commit, comment, (struct object *)commit);
commit->maybe_tree = tree;
set_commit_tree(commit, tree);
commit->object.parsed = 1;
return commit;
}
Expand Down
14 changes: 7 additions & 7 deletions packfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,25 +903,25 @@ static void prepare_packed_git(struct repository *r);
* all unreachable objects about to be pruned, in which case they're not really
* interesting as a measure of repo size in the first place.
*/
unsigned long approximate_object_count(void)
unsigned long repo_approximate_object_count(struct repository *r)
{
if (!the_repository->objects->approximate_object_count_valid) {
if (!r->objects->approximate_object_count_valid) {
unsigned long count;
struct multi_pack_index *m;
struct packed_git *p;

prepare_packed_git(the_repository);
prepare_packed_git(r);
count = 0;
for (m = get_multi_pack_index(the_repository); m; m = m->next)
for (m = get_multi_pack_index(r); m; m = m->next)
count += m->num_objects;
for (p = the_repository->objects->packed_git; p; p = p->next) {
for (p = r->objects->packed_git; p; p = p->next) {
if (open_pack_index(p))
continue;
count += p->num_objects;
}
the_repository->objects->approximate_object_count = count;
r->objects->approximate_object_count = count;
}
return the_repository->objects->approximate_object_count;
return r->objects->approximate_object_count;
}

static void *get_next_packed_git(const void *p)
Expand Down
3 changes: 2 additions & 1 deletion packfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ struct packed_git *get_all_packs(struct repository *r);
* Give a rough count of objects in the repository. This sacrifices accuracy
* for speed.
*/
unsigned long approximate_object_count(void);
unsigned long repo_approximate_object_count(struct repository *r);
#define approximate_object_count() repo_approximate_object_count(the_repository)

struct packed_git *find_sha1_pack(const unsigned char *sha1,
struct packed_git *packs);
Expand Down
Loading

0 comments on commit 0b179f3

Please sign in to comment.