t: use LF variable defined in the test harness
[gitweb.git] / sha1-name.c
index d9050776dde740d3d81748953d739ca4e076522e..728e6f1f61ea4641272e59e6287b6b47bfe0c74e 100644 (file)
@@ -12,6 +12,7 @@
 #include "packfile.h"
 #include "object-store.h"
 #include "repository.h"
+#include "submodule.h"
 #include "midx.h"
 #include "commit-reach.h"
 
@@ -156,6 +157,9 @@ static void unique_in_pack(struct packed_git *p,
        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;
 
@@ -468,6 +472,18 @@ static enum get_oid_result get_short_oid(struct repository *r,
        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(the_repository);
+               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;
 
@@ -612,6 +628,9 @@ static void find_abbrev_len_for_pack(struct packed_git *p,
        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;
 
@@ -1337,7 +1356,9 @@ static int interpret_nth_prior_checkout(struct repository *r,
        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;
@@ -1347,27 +1368,29 @@ int get_oid_mb(const char *name, struct object_id *oid)
 
        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;
+       if (r != the_repository)
+               BUG("sorry get_merge_bases() can't take struct repository yet");
        mbs = get_merge_bases(one, two);
        if (!mbs || mbs->next)
                st = -1;
@@ -1572,12 +1595,31 @@ 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);
 }
 
+/*
+ * 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
@@ -1589,43 +1631,48 @@ int get_oid(const char *name, struct object_id *oid)
  * 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);
 }
 
@@ -1637,7 +1684,7 @@ static void diagnose_invalid_oid_path(const char *prefix,
                                      int object_name_len)
 {
        struct object_id oid;
-       unsigned mode;
+       unsigned short mode;
 
        if (!prefix)
                prefix = "";
@@ -1664,11 +1711,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);
@@ -1708,7 +1756,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).",
@@ -1718,12 +1766,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 */
@@ -1784,7 +1832,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 {
@@ -1795,8 +1843,8 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                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;
@@ -1814,7 +1862,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;
        }
@@ -1838,9 +1886,17 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
                        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,
@@ -1875,11 +1931,13 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
  * 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);
 }