summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorschu <schu-github@schulog.org>2011-08-11 19:38:13 +0200
committerVicent Marti <tanoku@gmail.com>2011-11-06 03:15:26 +0100
commit75abd2b92452782a9e6cee6ed5041339bd00c5bf (patch)
treed2ef55289b8d10e1caa813a86fdebb1e00436489 /src
parent4fd89fa0392967fabb905c7f4001cd4834f11dbd (diff)
downloadlibgit2-75abd2b92452782a9e6cee6ed5041339bd00c5bf.tar.gz
Free all used references in the source tree
Since references are not owned by the repository anymore we have to free them manually now. Signed-off-by: schu <schu-github@schulog.org>
Diffstat (limited to 'src')
-rw-r--r--src/commit.c15
-rw-r--r--src/reflog.c6
-rw-r--r--src/repository.c42
-rw-r--r--src/tag.c24
-rw-r--r--src/transports/local.c7
5 files changed, 77 insertions, 17 deletions
diff --git a/src/commit.c b/src/commit.c
index 1010fdc56..83bc9fc4c 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -145,8 +145,10 @@ int git_commit_create(
error = git_reference_resolve(&target, head);
if (error < GIT_SUCCESS) {
- if (error != GIT_ENOTFOUND)
+ if (error != GIT_ENOTFOUND) {
+ git_reference_free(head);
return git__rethrow(error, "Failed to create commit");
+ }
/*
* The target of the reference was not found. This can happen
* just after a repository has been initialized (the master
@@ -154,10 +156,19 @@ int git_commit_create(
* point to) or after an orphan checkout, so if the target
* branch doesn't exist yet, create it and return.
*/
- return git_reference_create_oid(&head, repo, git_reference_target(head), oid, 1);
+ error = git_reference_create_oid(&target, repo, git_reference_target(head), oid, 1);
+
+ git_reference_free(head);
+ if (error == GIT_SUCCESS)
+ git_reference_free(target);
+
+ return error;
}
error = git_reference_set_oid(target, oid);
+
+ git_reference_free(head);
+ git_reference_free(target);
}
if (error < GIT_SUCCESS)
diff --git a/src/reflog.c b/src/reflog.c
index 5fc357a0f..81e171acf 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -218,8 +218,12 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
return git__rethrow(error, "Failed to write reflog. Cannot resolve reference `%s`", ref->name);
oid = git_reference_oid(r);
- if (oid == NULL)
+ if (oid == NULL) {
+ git_reference_free(r);
return git__throw(GIT_ERROR, "Failed to write reflog. Cannot resolve reference `%s`", r->name);
+ }
+
+ git_reference_free(r);
git_oid_to_string(new, GIT_OID_HEXSZ+1, oid);
diff --git a/src/repository.c b/src/repository.c
index 849e1a9cf..6c75aa190 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -603,8 +603,14 @@ static int repo_init_reinit(const char *repository_path, int is_bare)
static int repo_init_createhead(git_repository *repo)
{
+ int error;
git_reference *head_reference;
- return git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE, 0);
+
+ error = git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE, 0);
+
+ git_reference_free(head_reference);
+
+ return error;
}
static int repo_init_structure(const char *git_dir, int is_bare)
@@ -715,10 +721,15 @@ int git_repository_head_detached(git_repository *repo)
if (error < GIT_SUCCESS)
return error;
- if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
+ if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
+ git_reference_free(ref);
return 0;
+ }
error = git_odb_read_header(&_size, &type, repo->db, git_reference_oid(ref));
+
+ git_reference_free(ref);
+
if (error < GIT_SUCCESS)
return error;
@@ -730,7 +741,7 @@ int git_repository_head_detached(git_repository *repo)
int git_repository_head(git_reference **head_out, git_repository *repo)
{
- git_reference *ref;
+ git_reference *ref, *resolved_ref;
int error;
*head_out = NULL;
@@ -739,11 +750,15 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
if (error < GIT_SUCCESS)
return git__rethrow(GIT_ENOTAREPO, "Failed to locate the HEAD");
- error = git_reference_resolve(&ref, ref);
- if (error < GIT_SUCCESS)
+ error = git_reference_resolve(&resolved_ref, ref);
+ if (error < GIT_SUCCESS) {
+ git_reference_free(ref);
return git__rethrow(error, "Failed to resolve the HEAD");
+ }
- *head_out = ref;
+ git_reference_free(ref);
+
+ *head_out = resolved_ref;
return GIT_SUCCESS;
}
@@ -754,6 +769,9 @@ int git_repository_head_orphan(git_repository *repo)
error = git_repository_head(&ref, repo);
+ if (error == GIT_SUCCESS)
+ git_reference_free(ref);
+
return error == GIT_ENOTFOUND ? 1 : error;
}
@@ -766,13 +784,21 @@ int git_repository_is_empty(git_repository *repo)
if (error < GIT_SUCCESS)
return git__throw(error, "Corrupted repository. HEAD does not exist");
- if (git_reference_type(head) != GIT_REF_SYMBOLIC)
+ if (git_reference_type(head) != GIT_REF_SYMBOLIC) {
+ git_reference_free(head);
return 0;
+ }
- if (strcmp(git_reference_target(head), "refs/heads/master") != 0)
+ if (strcmp(git_reference_target(head), "refs/heads/master") != 0) {
+ git_reference_free(head);
return 0;
+ }
error = git_reference_resolve(&branch, head);
+
+ git_reference_free(head);
+ git_reference_free(branch);
+
return error == GIT_ENOTFOUND ? 1 : error;
}
diff --git a/src/tag.c b/src/tag.c
index 0bdca93bb..7372e68c7 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -153,6 +153,8 @@ static int retrieve_tag_reference(git_reference **tag_reference_out, char *ref_n
git_reference *tag_ref;
int error;
+ *tag_reference_out = NULL;
+
git_path_join(ref_name_out, GIT_REFS_TAGS_DIR, tag_name);
error = git_reference_lookup(&tag_ref, repo, ref_name_out);
if (error < GIT_SUCCESS)
@@ -224,6 +226,7 @@ static int git_tag_create__internal(
break;
default:
+ git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
}
@@ -232,6 +235,7 @@ static int git_tag_create__internal(
if (new_ref != NULL) {
if (!allow_ref_overwrite) {
git_oid_cpy(oid, git_reference_oid(new_ref));
+ git_reference_free(new_ref);
return git__throw(GIT_EEXISTS, "Tag already exists");
} else {
should_update_ref = 1;
@@ -239,8 +243,10 @@ static int git_tag_create__internal(
}
if (create_tag_annotation) {
- if ((error = write_tag_annotation(oid, repo, tag_name, target, tagger, message)) < GIT_SUCCESS)
+ if ((error = write_tag_annotation(oid, repo, tag_name, target, tagger, message)) < GIT_SUCCESS) {
+ git_reference_free(new_ref);
return error;
+ }
} else
git_oid_cpy(oid, git_object_id(target));
@@ -249,6 +255,8 @@ static int git_tag_create__internal(
else
error = git_reference_set_oid(new_ref, oid);
+ git_reference_free(new_ref);
+
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create tag");
}
@@ -281,7 +289,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
git_odb_stream *stream;
git_odb_object *target_obj;
- git_reference *new_ref;
+ git_reference *new_ref = NULL;
char ref_name[GIT_REFNAME_MAX];
assert(oid && buffer);
@@ -309,6 +317,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
break;
default:
+ git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
}
@@ -317,6 +326,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
if (new_ref != NULL) {
if (!allow_ref_overwrite) {
git_oid_cpy(oid, git_reference_oid(new_ref));
+ git_reference_free(new_ref);
return git__throw(GIT_EEXISTS, "Tag already exists");
} else {
should_update_ref = 1;
@@ -324,22 +334,28 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
}
/* write the buffer */
- if ((error = git_odb_open_wstream(&stream, repo->db, strlen(buffer), GIT_OBJ_TAG)) < GIT_SUCCESS)
+ if ((error = git_odb_open_wstream(&stream, repo->db, strlen(buffer), GIT_OBJ_TAG)) < GIT_SUCCESS) {
+ git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
+ }
stream->write(stream, buffer, strlen(buffer));
error = stream->finalize_write(oid, stream);
stream->free(stream);
- if (error < GIT_SUCCESS)
+ if (error < GIT_SUCCESS) {
+ git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
+ }
if (!should_update_ref)
error = git_reference_create_oid(&new_ref, repo, ref_name, oid, 0);
else
error = git_reference_set_oid(new_ref, oid);
+ git_reference_free(new_ref);
+
git_signature_free(tag.tagger);
git__free(tag.tag_name);
git__free(tag.message);
diff --git a/src/transports/local.c b/src/transports/local.c
index e09680478..058ed7e79 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -54,7 +54,7 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
{
const char peeled[] = "^{}";
git_remote_head *head;
- git_reference *ref;
+ git_reference *ref, *resolved_ref;
git_object *obj = NULL;
int error = GIT_SUCCESS, peel_len, ret;
@@ -72,7 +72,7 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
if (error < GIT_SUCCESS)
goto out;
- error = git_reference_resolve(&ref, ref);
+ error = git_reference_resolve(&resolved_ref, ref);
if (error < GIT_SUCCESS)
goto out;
@@ -111,6 +111,9 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
goto out;
out:
+ git_reference_free(ref);
+ git_reference_free(resolved_ref);
+
git_object_close(obj);
if (error < GIT_SUCCESS) {
git__free(head->name);