summaryrefslogtreecommitdiff
path: root/src/index.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-11-25 08:16:26 +0100
committerVicent Marti <tanoku@gmail.com>2011-11-26 08:37:08 +0100
commit9462c471435b4de74848408bebe41d770dc49a50 (patch)
treeaac5e696d1b3e7b4cba64082b28255e5c4593b66 /src/index.c
parent880b6f0c22153db164ecb3a18c362ba8337365d3 (diff)
downloadlibgit2-9462c471435b4de74848408bebe41d770dc49a50.tar.gz
repository: Change ownership semantics
The ownership semantics have been changed all over the library to be consistent. There are no more "borrowed" or duplicated references. Main changes: - `git_repository_open2` and `3` have been dropped. - Added setters and getters to hotswap all the repository owned objects: `git_repository_index` `git_repository_set_index` `git_repository_odb` `git_repository_set_odb` `git_repository_config` `git_repository_set_config` `git_repository_workdir` `git_repository_set_workdir` Now working directories/index files/ODBs and so on can be hot-swapped after creating a repository and between operations. - All these objects now have proper ownership semantics with refcounting: they all require freeing after they are no longer needed (the repository always keeps its internal reference). - Repository open and initialization has been updated to keep in mind the configuration files. Bare repositories are now always detected, and a default config file is created on init. - All the tests affected by these changes have been dropped from the old test suite and ported to the new one.
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c53
1 files changed, 26 insertions, 27 deletions
diff --git a/src/index.c b/src/index.c
index aad117164..d01262b39 100644
--- a/src/index.c
+++ b/src/index.c
@@ -31,6 +31,8 @@ static const unsigned int INDEX_HEADER_SIG = 0x44495243;
static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'};
static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'};
+#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))
+
struct index_header {
uint32_t signature;
uint32_t version;
@@ -124,7 +126,7 @@ static unsigned int index_create_mode(unsigned int mode)
return S_IFREG | ((mode & 0100) ? 0755 : 0644);
}
-static int index_initialize(git_index **index_out, git_repository *owner, const char *index_path)
+int git_index_open(git_index **index_out, const char *index_path)
{
git_index *index;
@@ -142,8 +144,6 @@ static int index_initialize(git_index **index_out, git_repository *owner, const
return GIT_ENOMEM;
}
- index->repository = owner;
-
git_vector_init(&index->entries, 32, index_cmp);
/* Check if index file is stored on disk already */
@@ -151,23 +151,18 @@ static int index_initialize(git_index **index_out, git_repository *owner, const
index->on_disk = 1;
*index_out = index;
+ GIT_REFCOUNT_INC(index);
return git_index_read(index);
}
-int git_index_open(git_index **index_out, const char *index_path)
+static void index_free(git_index *index)
{
- return index_initialize(index_out, NULL, index_path);
-}
-
-/*
- * Moved from `repository.c`
- */
-int git_repository_index(git_index **index_out, git_repository *repo)
-{
- if (repo->is_bare)
- return git__throw(GIT_EBAREINDEX, "Failed to open index. Repository is bare");
+ git_index_clear(index);
+ git_vector_free(&index->entries);
+ git_vector_free(&index->unmerged);
- return index_initialize(index_out, repo, repo->path_index);
+ git__free(index->index_file_path);
+ git__free(index);
}
void git_index_free(git_index *index)
@@ -175,12 +170,7 @@ void git_index_free(git_index *index)
if (index == NULL)
return;
- git_index_clear(index);
- git_vector_free(&index->entries);
- git_vector_free(&index->unmerged);
-
- git__free(index->index_file_path);
- git__free(index);
+ GIT_REFCOUNT_DEC(index, index_free);
}
void git_index_clear(git_index *index)
@@ -298,20 +288,29 @@ static int index_entry_init(git_index_entry **entry_out, git_index *index, const
struct stat st;
git_oid oid;
int error;
+ const char *workdir;
+
+ if (INDEX_OWNER(index) == NULL)
+ return git__throw(GIT_EBAREINDEX,
+ "Failed to initialize entry. Repository is bare");
- if (index->repository == NULL)
- return git__throw(GIT_EBAREINDEX, "Failed to initialize entry. Repository is bare");
+ workdir = git_repository_workdir(INDEX_OWNER(index));
+ if (workdir == NULL)
+ return git__throw(GIT_EBAREINDEX,
+ "Failed to initialize entry. Cannot resolved workdir");
- git_path_join(full_path, index->repository->path_workdir, rel_path);
+ git_path_join(full_path, workdir, rel_path);
if (p_lstat(full_path, &st) < 0)
- return git__throw(GIT_ENOTFOUND, "Failed to initialize entry. '%s' cannot be opened", full_path);
+ return git__throw(GIT_ENOTFOUND,
+ "Failed to initialize entry. '%s' cannot be opened", full_path);
if (stage < 0 || stage > 3)
- return git__throw(GIT_ERROR, "Failed to initialize entry. Invalid stage %i", stage);
+ return git__throw(GIT_ERROR,
+ "Failed to initialize entry. Invalid stage %i", stage);
/* write the blob to disk and get the oid */
- if ((error = git_blob_create_fromfile(&oid, index->repository, rel_path)) < GIT_SUCCESS)
+ if ((error = git_blob_create_fromfile(&oid, INDEX_OWNER(index), rel_path)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to initialize index entry");
entry = git__malloc(sizeof(git_index_entry));