]> git.proxmox.com Git - libgit2.git/blobdiff - src/submodule.c
Add range checking around cache opts
[libgit2.git] / src / submodule.c
index 94b4f724e5e1c2eb0d30a05c5e434aed23ef0dd5..0a22e3b13e55b93ce74c5e51fd0745a985a49f37 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 the libgit2 contributors
+ * Copyright (C) the libgit2 contributors. All rights reserved.
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
@@ -7,6 +7,7 @@
 
 #include "common.h"
 #include "git2/config.h"
+#include "git2/sys/config.h"
 #include "git2/types.h"
 #include "git2/repository.h"
 #include "git2/index.h"
@@ -66,7 +67,7 @@ __KHASH_IMPL(
        str, static kh_inline, const char *, void *, 1,
        str_hash_no_trailing_slash, str_equal_no_trailing_slash);
 
-static int load_submodule_config(git_repository *repo, bool force);
+static int load_submodule_config(git_repository *repo);
 static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *);
 static int lookup_head_remote(git_buf *url, git_repository *repo);
 static int submodule_get(git_submodule **, git_repository *, const char *, const char *);
@@ -106,7 +107,7 @@ int git_submodule_lookup(
 
        assert(repo && name);
 
-       if ((error = load_submodule_config(repo, false)) < 0)
+       if ((error = load_submodule_config(repo)) < 0)
                return error;
 
        pos = git_strmap_lookup_index(repo->submodules, name);
@@ -148,7 +149,7 @@ int git_submodule_foreach(
 
        assert(repo && callback);
 
-       if ((error = load_submodule_config(repo, false)) < 0)
+       if ((error = load_submodule_config(repo)) < 0)
                return error;
 
        git_strmap_foreach_value(repo->submodules, sm, {
@@ -157,7 +158,7 @@ int git_submodule_foreach(
                 * and path are not the same.
                 */
                if (sm->refcount > 1) {
-                       if (git_vector_bsearch(&seen, sm) != GIT_ENOTFOUND)
+                       if (git_vector_bsearch(NULL, &seen, sm) != GIT_ENOTFOUND)
                                continue;
                        if ((error = git_vector_insert(&seen, sm)) < 0)
                                break;
@@ -332,7 +333,7 @@ int git_submodule_add_finalize(git_submodule *sm)
        assert(sm);
 
        if ((error = git_repository_index__weakptr(&index, sm->owner)) < 0 ||
-               (error = git_index_add_from_workdir(index, GIT_MODULES_FILE)) < 0)
+               (error = git_index_add_bypath(index, GIT_MODULES_FILE)) < 0)
                return error;
 
        return git_submodule_add_to_index(sm, true);
@@ -694,7 +695,7 @@ int git_submodule_open(
        git_buf_free(&path);
 
        /* if we have opened the submodule successfully, let's grab the HEAD OID */
-       if (!error && !(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) {
+       if (!error) {
                if (!git_reference_name_to_id(
                                &submodule->wd_oid, *subrepo, GIT_HEAD_FILE))
                        submodule->flags |= GIT_SUBMODULE_STATUS__WD_OID_VALID;
@@ -708,14 +709,16 @@ int git_submodule_open(
 int git_submodule_reload_all(git_repository *repo)
 {
        assert(repo);
-       return load_submodule_config(repo, true);
+       git_submodule_config_free(repo);
+       return load_submodule_config(repo);
 }
 
 int git_submodule_reload(git_submodule *submodule)
 {
        git_repository *repo;
        git_index *index;
-       int pos, error;
+       int error;
+       size_t pos;
        git_tree *head;
        git_config_backend *mods;
 
@@ -731,8 +734,7 @@ int git_submodule_reload(git_submodule *submodule)
                ~(GIT_SUBMODULE_STATUS_IN_INDEX |
                  GIT_SUBMODULE_STATUS__INDEX_OID_VALID);
 
-       pos = git_index_find(index, submodule->path);
-       if (pos >= 0) {
+       if (!git_index_find(&pos, index, submodule->path)) {
                const git_index_entry *entry = git_index_get_byindex(index, pos);
 
                if (S_ISGITLINK(entry->mode)) {
@@ -829,6 +831,20 @@ int git_submodule_status(
        return error;
 }
 
+int git_submodule_location(
+       unsigned int *location_status,
+       git_submodule *submodule)
+{
+       assert(location_status && submodule);
+
+       *location_status = submodule->flags &
+               (GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX |
+                GIT_SUBMODULE_STATUS_IN_CONFIG | GIT_SUBMODULE_STATUS_IN_WD);
+
+       return 0;
+}
+
+
 /*
  * INTERNAL FUNCTIONS
  */
@@ -1115,13 +1131,15 @@ static int load_submodule_config_from_index(
        git_repository *repo, git_oid *gitmodules_oid)
 {
        int error;
+       git_index *index;
        git_iterator *i;
        const git_index_entry *entry;
 
-       if ((error = git_iterator_for_repo_index(&i, repo)) < 0)
+       if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
+               (error = git_iterator_for_index(&i, index, 0, NULL, NULL)) < 0)
                return error;
 
-       error = git_iterator_current(i, &entry);
+       error = git_iterator_current(&entry, i);
 
        while (!error && entry != NULL) {
 
@@ -1137,7 +1155,7 @@ static int load_submodule_config_from_index(
                                git_oid_cpy(gitmodules_oid, &entry->oid);
                }
 
-               error = git_iterator_advance(i, &entry);
+               error = git_iterator_advance(&entry, i);
        }
 
        git_iterator_free(i);
@@ -1156,12 +1174,12 @@ static int load_submodule_config_from_head(
        if ((error = git_repository_head_tree(&head, repo)) < 0)
                return error;
 
-       if ((error = git_iterator_for_tree(&i, head)) < 0) {
+       if ((error = git_iterator_for_tree(&i, head, 0, NULL, NULL)) < 0) {
                git_tree_free(head);
                return error;
        }
 
-       error = git_iterator_current(i, &entry);
+       error = git_iterator_current(&entry, i);
 
        while (!error && entry != NULL) {
 
@@ -1178,7 +1196,7 @@ static int load_submodule_config_from_head(
                                git_oid_cpy(gitmodules_oid, &entry->oid);
                }
 
-               error = git_iterator_advance(i, &entry);
+               error = git_iterator_advance(&entry, i);
        }
 
        git_iterator_free(i);
@@ -1225,14 +1243,14 @@ static git_config_backend *open_gitmodules(
        return mods;
 }
 
-static int load_submodule_config(git_repository *repo, bool force)
+static int load_submodule_config(git_repository *repo)
 {
        int error;
        git_oid gitmodules_oid;
        git_buf path = GIT_BUF_INIT;
        git_config_backend *mods = NULL;
 
-       if (repo->submodules && !force)
+       if (repo->submodules)
                return 0;
 
        memset(&gitmodules_oid, 0, sizeof(gitmodules_oid));
@@ -1310,7 +1328,7 @@ static int lookup_head_remote(git_buf *url, git_repository *repo)
                goto cleanup;
        }
 
-       if ((error = git_branch_tracking(&remote, head)) < 0)
+       if ((error = git_branch_upstream(&remote, head)) < 0)
                goto cleanup;
 
        /* remote should refer to something like refs/remotes/ORIGIN/BRANCH */
@@ -1480,7 +1498,7 @@ static int submodule_wd_status(unsigned int *status, git_submodule *sm)
                        if (untracked > 0)
                                *status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED;
 
-                       if ((git_diff_num_deltas(diff) - untracked) > 0)
+                       if (git_diff_num_deltas(diff) != untracked)
                                *status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED;
 
                        git_diff_list_free(diff);