sha1-name.c: add repo_get_oid()
[gitweb.git] / sha1-name.c
index 7d9512ac44662133d2540ef16496afef3e528ee4..953df0d692d62f8d2ce3bc7d97ecd9b59ce18a19 100644 (file)
 #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 *);
 
@@ -770,11 +771,11 @@ static inline int push_mark(const char *string, int len)
        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_(
@@ -792,9 +793,9 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
        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)
@@ -835,8 +836,8 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
                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;
@@ -845,19 +846,18 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
 
        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) {
@@ -889,7 +889,7 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
                                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) {
@@ -922,18 +922,19 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
        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) {
@@ -951,7 +952,8 @@ static enum get_oid_result get_parent(const char *name, int len,
        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)
 {
@@ -959,10 +961,10 @@ static enum get_oid_result get_nth_ancestor(const char *name, int len,
        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;
 
@@ -975,20 +977,20 @@ static enum get_oid_result get_nth_ancestor(const char *name, int len,
        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 "
@@ -1000,8 +1002,8 @@ struct object *peel_to_type(const char *name, int namelen,
        }
 }
 
-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;
@@ -1051,15 +1053,15 @@ static int peel_onion(const char *name, int len, struct object_id *oid,
        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;
@@ -1070,7 +1072,7 @@ static int peel_onion(const char *name, int len, struct object_id *oid,
         * 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;
 
@@ -1090,14 +1092,16 @@ static int peel_onion(const char *name, int len, struct object_id *oid,
 
                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;
@@ -1111,7 +1115,7 @@ static int get_describe_name(const char *name, int len, struct object_id *oid)
                        if (ch == 'g' && cp[-1] == '-') {
                                cp++;
                                len -= cp - name;
-                               return get_short_oid(the_repository,
+                               return get_short_oid(r,
                                                     cp, len, oid, flags);
                        }
                }
@@ -1119,7 +1123,8 @@ static int get_describe_name(const char *name, int len, struct object_id *oid)
        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)
 {
@@ -1148,25 +1153,25 @@ static enum get_oid_result get_oid_1(const char *name, int len,
                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);
 }
 
 /*
@@ -1185,15 +1190,21 @@ static enum get_oid_result get_oid_1(const char *name, int len,
 /* 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;
@@ -1204,8 +1215,9 @@ static int handle_one_ref(const char *path, const struct object_id *oid,
        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;
@@ -1236,7 +1248,7 @@ static int get_oid_oneline(const char *prefix, struct object_id *oid,
                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");
@@ -1390,7 +1402,8 @@ static int interpret_empty_at(const char *name, int namelen, int len, struct str
        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 */
@@ -1399,7 +1412,7 @@ static int reinterpret(const char *name, int namelen, int len,
        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);
@@ -1482,20 +1495,18 @@ int repo_interpret_branch_name(struct repository *r,
        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);
                }
        }
 
@@ -1506,17 +1517,17 @@ int repo_interpret_branch_name(struct repository *r,
                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)
@@ -1562,10 +1573,10 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
  * 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);
 }
 
 
@@ -1654,11 +1665,12 @@ static void diagnose_invalid_oid_path(const char *prefix,
 }
 
 /* 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);
@@ -1698,7 +1710,7 @@ static void diagnose_invalid_index_path(struct index_state *istate,
                            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).",
@@ -1708,12 +1720,12 @@ static void diagnose_invalid_index_path(struct index_state *istate,
 }
 
 
-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 */
@@ -1740,7 +1752,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
        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;
        /*
@@ -1756,12 +1768,15 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                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] != ':' ||
@@ -1771,7 +1786,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                        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 {
@@ -1783,7 +1798,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                        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;
@@ -1801,7 +1816,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                        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;
        }
@@ -1821,13 +1836,21 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                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,