#include "packfile.h"
#include "object-store.h"
#include "repository.h"
+#include "submodule.h"
#include "midx.h"
#include "commit-reach.h"
-static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
+static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *);
typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
}
-static enum get_oid_result get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags);
+static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
-static int get_oid_basic(const char *str, int len, struct object_id *oid,
- unsigned int flags)
+static int get_oid_basic(struct repository *r, const char *str, int len,
+ struct object_id *oid, unsigned int flags)
{
static const char *warn_msg = "refname '%.*s' is ambiguous.";
static const char *object_name_msg = N_(
int refs_found = 0;
int at, reflog_len, nth_prior = 0;
- if (len == the_hash_algo->hexsz && !get_oid_hex(str, oid)) {
+ if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
- refs_found = dwim_ref(str, len, &tmp_oid, &real_ref);
+ refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref);
if (refs_found > 0) {
warning(warn_msg, len, str);
if (advice_object_name_warning)
struct strbuf buf = STRBUF_INIT;
int detached;
- if (interpret_nth_prior_checkout(the_repository, str, len, &buf) > 0) {
- detached = (buf.len == the_hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
+ if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
+ detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
strbuf_release(&buf);
if (detached)
return 0;
if (!len && reflog_len)
/* allow "@{...}" to mean the current branch reflog */
- refs_found = dwim_ref("HEAD", 4, oid, &real_ref);
+ refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref);
else if (reflog_len)
- refs_found = dwim_log(str, len, oid, &real_ref);
+ refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
else
- refs_found = dwim_ref(str, len, oid, &real_ref);
+ refs_found = repo_dwim_ref(r, str, len, oid, &real_ref);
if (!refs_found)
return -1;
if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
(refs_found > 1 ||
- !get_short_oid(the_repository,
- str, len, &tmp_oid, GET_OID_QUIETLY)))
+ !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
warning(warn_msg, len, str);
if (reflog_len) {
return -1;
}
}
- if (read_ref_at(get_main_ref_store(the_repository),
+ if (read_ref_at(get_main_ref_store(r),
real_ref, flags, at_time, nth, oid, NULL,
&co_time, &co_tz, &co_cnt)) {
if (!len) {
return 0;
}
-static enum get_oid_result get_parent(const char *name, int len,
+static enum get_oid_result get_parent(struct repository *r,
+ const char *name, int len,
struct object_id *result, int idx)
{
struct object_id oid;
- enum get_oid_result ret = get_oid_1(name, len, &oid,
+ enum get_oid_result ret = get_oid_1(r, name, len, &oid,
GET_OID_COMMITTISH);
struct commit *commit;
struct commit_list *p;
if (ret)
return ret;
- commit = lookup_commit_reference(the_repository, &oid);
+ commit = lookup_commit_reference(r, &oid);
if (parse_commit(commit))
return MISSING_OBJECT;
if (!idx) {
return MISSING_OBJECT;
}
-static enum get_oid_result get_nth_ancestor(const char *name, int len,
+static enum get_oid_result get_nth_ancestor(struct repository *r,
+ const char *name, int len,
struct object_id *result,
int generation)
{
struct commit *commit;
int ret;
- ret = get_oid_1(name, len, &oid, GET_OID_COMMITTISH);
+ ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
if (ret)
return ret;
- commit = lookup_commit_reference(the_repository, &oid);
+ commit = lookup_commit_reference(r, &oid);
if (!commit)
return MISSING_OBJECT;
return FOUND;
}
-struct object *peel_to_type(const char *name, int namelen,
- struct object *o, enum object_type expected_type)
+struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
+ struct object *o, enum object_type expected_type)
{
if (name && !namelen)
namelen = strlen(name);
while (1) {
- if (!o || (!o->parsed && !parse_object(the_repository, &o->oid)))
+ if (!o || (!o->parsed && !parse_object(r, &o->oid)))
return NULL;
if (expected_type == OBJ_ANY || o->type == expected_type)
return o;
if (o->type == OBJ_TAG)
o = ((struct tag*) o)->tagged;
else if (o->type == OBJ_COMMIT)
- o = &(get_commit_tree(((struct commit *)o))->object);
+ o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
else {
if (name)
error("%.*s: expected %s type, but the object "
}
}
-static int peel_onion(const char *name, int len, struct object_id *oid,
- unsigned lookup_flags)
+static int peel_onion(struct repository *r, const char *name, int len,
+ struct object_id *oid, unsigned lookup_flags)
{
struct object_id outer;
const char *sp;
else if (expected_type == OBJ_TREE)
lookup_flags |= GET_OID_TREEISH;
- if (get_oid_1(name, sp - name - 2, &outer, lookup_flags))
+ if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
return -1;
- o = parse_object(the_repository, &outer);
+ o = parse_object(r, &outer);
if (!o)
return -1;
if (!expected_type) {
- o = deref_tag(the_repository, o, name, sp - name - 2);
- if (!o || (!o->parsed && !parse_object(the_repository, &o->oid)))
+ o = deref_tag(r, o, name, sp - name - 2);
+ if (!o || (!o->parsed && !parse_object(r, &o->oid)))
return -1;
oidcpy(oid, &o->oid);
return 0;
* if we do not get the needed object, we should
* barf.
*/
- o = peel_to_type(name, len, o, expected_type);
+ o = repo_peel_to_type(r, name, len, o, expected_type);
if (!o)
return -1;
prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
commit_list_insert((struct commit *)o, &list);
- ret = get_oid_oneline(prefix, oid, list);
+ ret = get_oid_oneline(r, prefix, oid, list);
free(prefix);
return ret;
}
return 0;
}
-static int get_describe_name(const char *name, int len, struct object_id *oid)
+static int get_describe_name(struct repository *r,
+ const char *name, int len,
+ struct object_id *oid)
{
const char *cp;
unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
if (ch == 'g' && cp[-1] == '-') {
cp++;
len -= cp - name;
- return get_short_oid(the_repository,
+ return get_short_oid(r,
cp, len, oid, flags);
}
}
return -1;
}
-static enum get_oid_result get_oid_1(const char *name, int len,
+static enum get_oid_result get_oid_1(struct repository *r,
+ const char *name, int len,
struct object_id *oid,
unsigned lookup_flags)
{
if (!num && len1 == len - 1)
num = 1;
if (has_suffix == '^')
- return get_parent(name, len1, oid, num);
+ return get_parent(r, name, len1, oid, num);
/* else if (has_suffix == '~') -- goes without saying */
- return get_nth_ancestor(name, len1, oid, num);
+ return get_nth_ancestor(r, name, len1, oid, num);
}
- ret = peel_onion(name, len, oid, lookup_flags);
+ ret = peel_onion(r, name, len, oid, lookup_flags);
if (!ret)
return FOUND;
- ret = get_oid_basic(name, len, oid, lookup_flags);
+ ret = get_oid_basic(r, name, len, oid, lookup_flags);
if (!ret)
return FOUND;
/* It could be describe output that is "SOMETHING-gXXXX" */
- ret = get_describe_name(name, len, oid);
+ ret = get_describe_name(r, name, len, oid);
if (!ret)
return FOUND;
- return get_short_oid(the_repository, name, len, oid, lookup_flags);
+ return get_short_oid(r, name, len, oid, lookup_flags);
}
/*
/* Remember to update object flag allocation in object.h */
#define ONELINE_SEEN (1u<<20)
+struct handle_one_ref_cb {
+ struct repository *repo;
+ struct commit_list **list;
+};
+
static int handle_one_ref(const char *path, const struct object_id *oid,
int flag, void *cb_data)
{
- struct commit_list **list = cb_data;
- struct object *object = parse_object(the_repository, oid);
+ struct handle_one_ref_cb *cb = cb_data;
+ struct commit_list **list = cb->list;
+ struct object *object = parse_object(cb->repo, oid);
if (!object)
return 0;
if (object->type == OBJ_TAG) {
- object = deref_tag(the_repository, object, path,
+ object = deref_tag(cb->repo, object, path,
strlen(path));
if (!object)
return 0;
return 0;
}
-static int get_oid_oneline(const char *prefix, struct object_id *oid,
- struct commit_list *list)
+static int get_oid_oneline(struct repository *r,
+ const char *prefix, struct object_id *oid,
+ struct commit_list *list)
{
struct commit_list *backup = NULL, *l;
int found = 0;
int matches;
commit = pop_most_recent_commit(&list, ONELINE_SEEN);
- if (!parse_object(the_repository, &commit->object.oid))
+ if (!parse_object(r, &commit->object.oid))
continue;
buf = get_commit_buffer(commit, NULL);
p = strstr(buf, "\n\n");
return 1;
}
-static int reinterpret(const char *name, int namelen, int len,
+static int reinterpret(struct repository *r,
+ const char *name, int namelen, int len,
struct strbuf *buf, unsigned allowed)
{
/* we have extra data, which might need further processing */
int ret;
strbuf_add(buf, name + len, namelen - len);
- ret = interpret_branch_name(buf->buf, buf->len, &tmp, allowed);
+ ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, allowed);
/* that data was not interpreted, remove our cruft */
if (ret < 0) {
strbuf_setlen(buf, used);
const char *start;
int len;
- if (r != the_repository)
- BUG("interpret_branch_name() does not really use 'r' yet");
if (!namelen)
namelen = strlen(name);
if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {
- len = interpret_nth_prior_checkout(the_repository, name, namelen, buf);
+ len = interpret_nth_prior_checkout(r, name, namelen, buf);
if (!len) {
return len; /* syntax Ok, not enough switches */
} else if (len > 0) {
if (len == namelen)
return len; /* consumed all */
else
- return reinterpret(name, namelen, len, buf, allowed);
+ return reinterpret(r, name, namelen, len, buf, allowed);
}
}
if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {
len = interpret_empty_at(name, namelen, at - name, buf);
if (len > 0)
- return reinterpret(name, namelen, len, buf,
+ return reinterpret(r, name, namelen, len, buf,
allowed);
}
- len = interpret_branch_mark(the_repository, name, namelen, at - name, buf,
+ len = interpret_branch_mark(r, name, namelen, at - name, buf,
upstream_mark, branch_get_upstream,
allowed);
if (len > 0)
return len;
- len = interpret_branch_mark(the_repository, name, namelen, at - name, buf,
+ len = interpret_branch_mark(r, name, namelen, at - name, buf,
push_mark, branch_get_push,
allowed);
if (len > 0)
* This is like "get_oid_basic()", except it allows "object ID expressions",
* notably "xyz^" for "parent of xyz"
*/
-int get_oid(const char *name, struct object_id *oid)
+int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
{
struct object_context unused;
- return get_oid_with_context(the_repository, name, 0, oid, &unused);
+ return get_oid_with_context(r, name, 0, oid, &unused);
}
}
/* Must be called only when :stage:filename doesn't exist. */
-static void diagnose_invalid_index_path(struct index_state *istate,
+static void diagnose_invalid_index_path(struct repository *r,
int stage,
const char *prefix,
const char *filename)
{
+ struct index_state *istate = r->index;
const struct cache_entry *ce;
int pos;
unsigned namelen = strlen(filename);
ce_stage(ce), filename);
}
- if (file_exists(filename))
+ if (repo_file_exists(r, filename))
die("Path '%s' exists on disk, but not in the index.", filename);
if (is_missing_file_error(errno))
die("Path '%s' does not exist (neither on disk nor in the index).",
}
-static char *resolve_relative_path(const char *rel)
+static char *resolve_relative_path(struct repository *r, const char *rel)
{
if (!starts_with(rel, "./") && !starts_with(rel, "../"))
return NULL;
- if (!is_inside_work_tree())
+ if (r != the_repository || !is_inside_work_tree())
die("relative path syntax can't be used outside working tree.");
/* die() inside prefix_path() if resolved path is outside worktree */
memset(oc, 0, sizeof(*oc));
oc->mode = S_IFINVALID;
strbuf_init(&oc->symlink_path, 0);
- ret = get_oid_1(name, namelen, oid, flags);
+ ret = get_oid_1(repo, name, namelen, oid, flags);
if (!ret)
return ret;
/*
char *new_path = NULL;
int pos;
if (!only_to_die && namelen > 2 && name[1] == '/') {
+ struct handle_one_ref_cb cb;
struct commit_list *list = NULL;
- for_each_ref(handle_one_ref, &list);
- head_ref(handle_one_ref, &list);
+ cb.repo = repo;
+ cb.list = &list;
+ refs_for_each_ref(repo->refs, handle_one_ref, &cb);
+ refs_head_ref(repo->refs, handle_one_ref, &cb);
commit_list_sort_by_date(&list);
- return get_oid_oneline(name + 2, oid, list);
+ return get_oid_oneline(repo, name + 2, oid, list);
}
if (namelen < 3 ||
name[2] != ':' ||
stage = name[1] - '0';
cp = name + 3;
}
- new_path = resolve_relative_path(cp);
+ new_path = resolve_relative_path(repo, cp);
if (!new_path) {
namelen = namelen - (cp - name);
} else {
oc->path = xstrdup(cp);
if (!repo->index->cache)
- repo_read_index(the_repository);
+ repo_read_index(repo);
pos = index_name_pos(repo->index, cp, namelen);
if (pos < 0)
pos = -pos - 1;
pos++;
}
if (only_to_die && name[1] && name[1] != '/')
- diagnose_invalid_index_path(repo->index, stage, prefix, cp);
+ diagnose_invalid_index_path(repo, stage, prefix, cp);
free(new_path);
return -1;
}
sub_flags &= ~GET_OID_DISAMBIGUATORS;
sub_flags |= GET_OID_TREEISH;
- if (!get_oid_1(name, len, &tree_oid, sub_flags)) {
+ if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
const char *filename = cp+1;
char *new_filename = NULL;
- new_filename = resolve_relative_path(filename);
+ new_filename = resolve_relative_path(repo, filename);
if (new_filename)
filename = new_filename;
+ /*
+ * NEEDSWORK: Eventually get_tree_entry*() should
+ * learn to take struct repository directly and we
+ * would not need to inject submodule odb to the
+ * in-core odb.
+ */
+ if (repo != the_repository)
+ add_to_alternates_memory(repo->objects->odb->path);
if (flags & GET_OID_FOLLOW_SYMLINKS) {
ret = get_tree_entry_follow_symlinks(&tree_oid,
filename, oid, &oc->symlink_path,