#include "packfile.h"
#include "object-store.h"
#include "repository.h"
+#include "submodule.h"
#include "midx.h"
#include "commit-reach.h"
uint32_t num, i, first = 0;
const struct object_id *current = NULL;
+ if (p->multi_pack_index)
+ return;
+
if (open_pack_index(p) || !p->num_objects)
return;
find_short_packed_object(&ds);
status = finish_object_disambiguation(&ds, oid);
+ /*
+ * If we didn't find it, do the usual reprepare() slow-path,
+ * since the object may have recently been added to the repository
+ * or migrated from loose to packed.
+ */
+ if (status == MISSING_OBJECT) {
+ reprepare_packed_git(r);
+ find_short_object_filename(&ds);
+ find_short_packed_object(&ds);
+ status = finish_object_disambiguation(&ds, oid);
+ }
+
if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
struct oid_array collect = OID_ARRAY_INIT;
struct object_id oid;
const struct object_id *mad_oid;
+ if (p->multi_pack_index)
+ return;
+
if (open_pack_index(p) || !p->num_objects)
return;
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(struct repository *r, const char *str, int len,
"because it will be ignored when you just specify 40-hex. These refs\n"
"may be created by mistake. For example,\n"
"\n"
- " git checkout -b $br $(git rev-parse ...)\n"
+ " git switch -c $br $(git rev-parse ...)\n"
"\n"
"where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
"examine these refs and maybe delete them. Turn this message off by\n"
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(the_repository, prefix, oid, list);
+ ret = get_oid_oneline(r, prefix, oid, list);
free(prefix);
return ret;
}
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(the_repository, 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(the_repository, 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 retval;
}
-int get_oid_mb(const char *name, struct object_id *oid)
+int repo_get_oid_mb(struct repository *r,
+ const char *name,
+ struct object_id *oid)
{
struct commit *one, *two;
struct commit_list *mbs;
dots = strstr(name, "...");
if (!dots)
- return get_oid(name, oid);
+ return repo_get_oid(r, name, oid);
if (dots == name)
- st = get_oid("HEAD", &oid_tmp);
+ st = repo_get_oid(r, "HEAD", &oid_tmp);
else {
struct strbuf sb;
strbuf_init(&sb, dots - name);
strbuf_add(&sb, name, dots - name);
- st = get_oid_committish(sb.buf, &oid_tmp);
+ st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
strbuf_release(&sb);
}
if (st)
return st;
- one = lookup_commit_reference_gently(the_repository, &oid_tmp, 0);
+ one = lookup_commit_reference_gently(r, &oid_tmp, 0);
if (!one)
return -1;
- if (get_oid_committish(dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
+ if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
return -1;
- two = lookup_commit_reference_gently(the_repository, &oid_tmp, 0);
+ two = lookup_commit_reference_gently(r, &oid_tmp, 0);
if (!two)
return -1;
- mbs = get_merge_bases(one, two);
+ mbs = repo_get_merge_bases(r, one, two);
if (!mbs || mbs->next)
st = -1;
else {
* 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);
}
+/*
+ * This returns a non-zero value if the string (built using printf
+ * format and the given arguments) is not a valid object.
+ */
+int get_oidf(struct object_id *oid, const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+ struct strbuf sb = STRBUF_INIT;
+
+ va_start(ap, fmt);
+ strbuf_vaddf(&sb, fmt, ap);
+ va_end(ap);
+
+ ret = get_oid(sb.buf, oid);
+ strbuf_release(&sb);
+
+ return ret;
+}
/*
* Many callers know that the user meant to name a commit-ish by
* commit-ish. It is merely to give a hint to the disambiguation
* machinery.
*/
-int get_oid_committish(const char *name, struct object_id *oid)
+int repo_get_oid_committish(struct repository *r,
+ const char *name,
+ struct object_id *oid)
{
struct object_context unused;
- return get_oid_with_context(the_repository,
- name, GET_OID_COMMITTISH,
+ return get_oid_with_context(r, name, GET_OID_COMMITTISH,
oid, &unused);
}
-int get_oid_treeish(const char *name, struct object_id *oid)
+int repo_get_oid_treeish(struct repository *r,
+ const char *name,
+ struct object_id *oid)
{
struct object_context unused;
- return get_oid_with_context(the_repository,
- name, GET_OID_TREEISH,
+ return get_oid_with_context(r, name, GET_OID_TREEISH,
oid, &unused);
}
-int get_oid_commit(const char *name, struct object_id *oid)
+int repo_get_oid_commit(struct repository *r,
+ const char *name,
+ struct object_id *oid)
{
struct object_context unused;
- return get_oid_with_context(the_repository,
- name, GET_OID_COMMIT,
+ return get_oid_with_context(r, name, GET_OID_COMMIT,
oid, &unused);
}
-int get_oid_tree(const char *name, struct object_id *oid)
+int repo_get_oid_tree(struct repository *r,
+ const char *name,
+ struct object_id *oid)
{
struct object_context unused;
- return get_oid_with_context(the_repository,
- name, GET_OID_TREE,
+ return get_oid_with_context(r, name, GET_OID_TREE,
oid, &unused);
}
-int get_oid_blob(const char *name, struct object_id *oid)
+int repo_get_oid_blob(struct repository *r,
+ const char *name,
+ struct object_id *oid)
{
struct object_context unused;
- return get_oid_with_context(the_repository,
- name, GET_OID_BLOB,
+ return get_oid_with_context(r, name, GET_OID_BLOB,
oid, &unused);
}
/* Must be called only when object_name:filename doesn't exist. */
-static void diagnose_invalid_oid_path(const char *prefix,
+static void diagnose_invalid_oid_path(struct repository *r,
+ const char *prefix,
const char *filename,
const struct object_id *tree_oid,
const char *object_name,
int object_name_len)
{
struct object_id oid;
- unsigned mode;
+ unsigned short mode;
if (!prefix)
prefix = "";
if (is_missing_file_error(errno)) {
char *fullname = xstrfmt("%s%s", prefix, filename);
- if (!get_tree_entry(tree_oid, fullname, &oid, &mode)) {
+ if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
die("Path '%s' exists, but not '%s'.\n"
"Did you mean '%.*s:%s' aka '%.*s:./%s'?",
fullname,
}
/* 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(repo, name + 2, oid, list);
}
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 {
if (flags & GET_OID_RECORD_PATH)
oc->path = xstrdup(cp);
- if (!repo->index->cache)
- repo_read_index(the_repository);
+ if (!repo->index || !repo->index->cache)
+ 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;
if (flags & GET_OID_FOLLOW_SYMLINKS) {
- ret = get_tree_entry_follow_symlinks(&tree_oid,
+ ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
filename, oid, &oc->symlink_path,
&oc->mode);
} else {
- ret = get_tree_entry(&tree_oid, filename, oid,
+ ret = get_tree_entry(repo, &tree_oid, filename, oid,
&oc->mode);
if (ret && only_to_die) {
- diagnose_invalid_oid_path(prefix,
+ diagnose_invalid_oid_path(repo, prefix,
filename,
&tree_oid,
name, len);
* exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
* you have a chance to diagnose the error further.
*/
-void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
+void maybe_die_on_misspelt_object_name(struct repository *r,
+ const char *name,
+ const char *prefix)
{
struct object_context oc;
struct object_id oid;
- get_oid_with_context_1(the_repository, name, GET_OID_ONLY_TO_DIE,
+ get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE,
prefix, &oid, &oc);
}