Commit 3c1b3ded12afa443158287c22b81fb9419680072

Vicent Marti 2014-01-14T12:41:01

Merge pull request #2047 from arthurschreiber/arthur/fix-dup-functions Align `*_dup` functions

diff --git a/include/git2/signature.h b/include/git2/signature.h
index 2fa46d0..a1dd1ec 100644
--- a/include/git2/signature.h
+++ b/include/git2/signature.h
@@ -68,10 +68,11 @@ GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo)
  *
  * Call `git_signature_free()` to free the data.
  *
- * @param sig signature to duplicated
- * @return a copy of sig, NULL on out of memory
+ * @param dest pointer where to store the copy
+ * @param entry signature to duplicate
+ * @return 0 or an error code
  */
-GIT_EXTERN(git_signature *) git_signature_dup(const git_signature *sig);
+GIT_EXTERN(int) git_signature_dup(git_signature **dest, const git_signature *sig);
 
 /**
  * Free an existing signature.
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 4223656..6350ada 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -150,10 +150,11 @@ GIT_EXTERN(int) git_tree_entry_bypath(
  * Create a copy of a tree entry. The returned copy is owned by the user,
  * and must be freed explicitly with `git_tree_entry_free()`.
  *
- * @param entry A tree entry to duplicate
- * @return a copy of the original entry or NULL on error (alloc failure)
+ * @param dest pointer where to store the copy
+ * @param entry tree entry to duplicate
+ * @return 0 or an error code
  */
-GIT_EXTERN(git_tree_entry *) git_tree_entry_dup(const git_tree_entry *entry);
+GIT_EXTERN(int) git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source);
 
 /**
  * Free a user-owned tree entry
diff --git a/src/blame.c b/src/blame.c
index b885de2..0f2d906 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -76,8 +76,8 @@ static git_blame_hunk* dup_hunk(git_blame_hunk *hunk)
 	git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id);
 	git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id);
 	newhunk->boundary = hunk->boundary;
-	newhunk->final_signature = git_signature_dup(hunk->final_signature);
-	newhunk->orig_signature = git_signature_dup(hunk->orig_signature);
+	git_signature_dup(&newhunk->final_signature, hunk->final_signature);
+	git_signature_dup(&newhunk->orig_signature, hunk->orig_signature);
 	return newhunk;
 }
 
@@ -269,8 +269,8 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e)
 			e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path);
 	git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit));
 	git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit));
-	h->final_signature = git_signature_dup(git_commit_author(e->suspect->commit));
-	h->orig_signature = git_signature_dup(git_commit_author(e->suspect->commit));
+	git_signature_dup(&h->final_signature, git_commit_author(e->suspect->commit));
+	git_signature_dup(&h->orig_signature, git_commit_author(e->suspect->commit));
 	h->boundary = e->is_boundary ? 1 : 0;
 	return h;
 }
diff --git a/src/reflog.c b/src/reflog.c
index 9b2b201..8e41621 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -82,7 +82,7 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_sign
 	entry = git__calloc(1, sizeof(git_reflog_entry));
 	GITERR_CHECK_ALLOC(entry);
 
-	if ((entry->committer = git_signature_dup(committer)) == NULL)
+	if ((git_signature_dup(&entry->committer, committer)) < 0)
 		goto cleanup;
 
 	if (msg != NULL) {
diff --git a/src/signature.c b/src/signature.c
index ec51a42..f658d60 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -82,23 +82,28 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema
 	return 0;
 }
 
-git_signature *git_signature_dup(const git_signature *sig)
+int git_signature_dup(git_signature **dest, const git_signature *source)
 {
-	git_signature *new;
+	git_signature *signature;
 
-	if (sig == NULL)
-		return NULL;
+	if (source == NULL)
+		return 0;
+
+	signature = git__calloc(1, sizeof(git_signature));
+	GITERR_CHECK_ALLOC(signature);
+
+	signature->name = git__strdup(source->name);
+	GITERR_CHECK_ALLOC(signature->name);
 
-	new = git__calloc(1, sizeof(git_signature));
-	if (new == NULL)
-		return NULL;
+	signature->email = git__strdup(source->email);
+	GITERR_CHECK_ALLOC(signature->email);
 
-	new->name = git__strdup(sig->name);
-	new->email = git__strdup(sig->email);
-	new->when.time = sig->when.time;
-	new->when.offset = sig->when.offset;
+	signature->when.time = source->when.time;
+	signature->when.offset = source->when.offset;
 
-	return new;
+	*dest = signature;
+
+	return 0;
 }
 
 int git_signature_now(git_signature **sig_out, const char *name, const char *email)
diff --git a/src/tree.c b/src/tree.c
index 4d77ff7..fc105ed 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -204,22 +204,22 @@ void git_tree_entry_free(git_tree_entry *entry)
 	git__free(entry);
 }
 
-git_tree_entry *git_tree_entry_dup(const git_tree_entry *entry)
+int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source)
 {
 	size_t total_size;
 	git_tree_entry *copy;
 
-	assert(entry);
+	assert(source);
 
-	total_size = sizeof(git_tree_entry) + entry->filename_len + 1;
+	total_size = sizeof(git_tree_entry) + source->filename_len + 1;
 
 	copy = git__malloc(total_size);
-	if (!copy)
-		return NULL;
+	GITERR_CHECK_ALLOC(copy);
 
-	memcpy(copy, entry, total_size);
+	memcpy(copy, source, total_size);
 
-	return copy;
+	*dest = copy;
+	return 0;
 }
 
 void git_tree__free(void *_tree)
@@ -853,8 +853,7 @@ int git_tree_entry_bypath(
 	case '\0':
 		/* If there are no more components in the path, return
 		 * this entry */
-		*entry_out = git_tree_entry_dup(entry);
-		return 0;
+		return git_tree_entry_dup(entry_out, entry);
 	}
 
 	if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0)