Commit 720d5472f8aa629161749450a27d3a4e3ecefea3

Vicent Marti 2011-04-02T12:42:04

Change `parse` methods to const buffer Signed-off-by: Vicent Marti <tanoku@gmail.com>

diff --git a/src/commit.c b/src/commit.c
index 03b111d..9fc3f07 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -235,9 +235,9 @@ int git_commit_create(
 	return error;
 }
 
-int commit_parse_buffer(git_commit *commit, void *data, size_t len)
+int commit_parse_buffer(git_commit *commit, const void *data, size_t len)
 {
-	char *buffer = (char *)data;
+	const char *buffer = (char *)data;
 	const char *buffer_end = (char *)data + len;
 
 	git_oid parent_oid;
diff --git a/src/oid.c b/src/oid.c
index eb167a6..86c1e00 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -118,13 +118,13 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
 	return out;
 }
 
-int git__parse_oid(git_oid *oid, char **buffer_out,
+int git__parse_oid(git_oid *oid, const char **buffer_out,
 		const char *buffer_end, const char *header)
 {
 	const size_t sha_len = GIT_OID_HEXSZ;
 	const size_t header_len = strlen(header);
 
-	char *buffer = *buffer_out;
+	const char *buffer = *buffer_out;
 
 	if (buffer + (header_len + sha_len + 1) > buffer_end)
 		return GIT_EOBJCORRUPTED;
diff --git a/src/repository.h b/src/repository.h
index fef1c7d..813cac9 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -43,7 +43,7 @@ struct git_repository {
  * export */
 void git_object__free(void *object);
 
-int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
+int git__parse_oid(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
 int git__write_oid(git_odb_stream *src, const char *header, const git_oid *oid);
 
 #endif
diff --git a/src/signature.c b/src/signature.c
index 4126376..0c99755 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -109,14 +109,14 @@ static int parse_timezone_offset(const char *buffer, int *offset_out)
 }
 
 
-int git_signature__parse(git_signature *sig, char **buffer_out,
+int git_signature__parse(git_signature *sig, const char **buffer_out,
 		const char *buffer_end, const char *header)
 {
 	const size_t header_len = strlen(header);
 
 	int name_length, email_length;
-	char *buffer = *buffer_out;
-	char *line_end, *name_end, *email_end;
+	const char *buffer = *buffer_out;
+	const char *line_end, *name_end, *email_end;
 	int offset = 0;
 
 	memset(sig, 0x0, sizeof(git_signature));
@@ -159,7 +159,7 @@ int git_signature__parse(git_signature *sig, char **buffer_out,
 	if (buffer >= line_end)
 		return GIT_EOBJCORRUPTED;
 
-	sig->when.time = strtol(buffer, &buffer, 10);
+	sig->when.time = strtol(buffer, (char **)&buffer, 10);
 
 	if (sig->when.time == 0)
 		return GIT_EOBJCORRUPTED;
diff --git a/src/signature.h b/src/signature.h
index 3534cb2..feba657 100644
--- a/src/signature.h
+++ b/src/signature.h
@@ -6,7 +6,7 @@
 #include "repository.h"
 #include <time.h>
 
-int git_signature__parse(git_signature *sig, char **buffer_out, const char *buffer_end, const char *header);
+int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header);
 int git_signature__write(char **signature, const char *header, const git_signature *sig);
 
 #endif
diff --git a/src/tag.c b/src/tag.c
index a688577..5982a05 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -79,7 +79,7 @@ const char *git_tag_message(git_tag *t)
 	return t->message;
 }
 
-static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
+static int parse_tag_buffer(git_tag *tag, const char *buffer, const char *buffer_end)
 {
 	static const char *tag_types[] = {
 		NULL, "commit\n", "tree\n", "blob\n", "tag\n"
@@ -130,9 +130,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 
 	text_len = search - buffer;
 
-	if (tag->tag_name != NULL)
-		free(tag->tag_name);
-
 	tag->tag_name = git__malloc(text_len + 1);
 	memcpy(tag->tag_name, buffer, text_len);
 	tag->tag_name[text_len] = '\0';
@@ -141,8 +138,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 
 	tag->tagger = git__malloc(sizeof(git_signature));
 
-	if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0)
-		goto cleanup;
+	if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0) {
+		free(tag->tag_name);
+		git_signature_free(tag->tagger);
+		return error;
+	}
 
 	text_len = buffer_end - ++buffer;
 
@@ -151,14 +151,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 	tag->message[text_len] = '\0';
 
 	return GIT_SUCCESS;
-
- cleanup:
-	if(tag->tag_name)
-		free(tag->tag_name);
-	if(tag->tagger)
-		git_signature_free(tag->tagger);
-
-	return error;
 }
 
 int git_tag_create_o(
@@ -194,7 +186,6 @@ int git_tag_create(
 	int type_str_len, tag_name_len, tagger_str_len, message_len;
 	int error;
 
-
 	type_str = git_object_type2string(target_type);
 
 	tagger_str_len = git_signature__write(&tagger_str, "tagger", tagger);
@@ -245,40 +236,31 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
 {
 	git_tag tag;
 	int error;
-	char *buf;
 	git_object *obj;
 
 	assert(oid && buffer);
 
 	memset(&tag, 0, sizeof(tag));
 
-	buf = strdup(buffer);
-	if(buf == NULL)
-		return GIT_ENOMEM;
-
-	if((error = parse_tag_buffer(&tag, buf, buf + strlen(buf))) < 0)
-	   goto exit_freebuf;
+	if ((error = parse_tag_buffer(&tag, buffer, buffer + strlen(buffer))) < GIT_SUCCESS)
+		return error;
 
 	error = git_object_lookup(&obj, repo, &tag.target, tag.type);
-	if(error < 0)
-		goto exit_freetag;
+	if (error < GIT_SUCCESS)
+		goto cleanup;
 
-	error = git_tag_create_o(oid, repo, tag.tag_name, obj,
-							 tag.tagger, tag.message);
+	error = git_tag_create_o(oid, repo, tag.tag_name, obj, tag.tagger, tag.message);
 
 	git_object_close(obj);
 
- exit_freetag:
+cleanup:
 	git_signature_free(tag.tagger);
 	free(tag.tag_name);
 	free(tag.message);
- exit_freebuf:
-	free(buf);
 
 	return error;
 }
 
-
 int git_tag__parse(git_tag *tag, git_odb_object *obj)
 {
 	assert(tag);
diff --git a/src/tree.c b/src/tree.c
index 31b286e..dea5046 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -127,7 +127,7 @@ size_t git_tree_entrycount(git_tree *tree)
 	return tree->entries.length;
 }
 
-static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
+static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end)
 {
 	int error = GIT_SUCCESS;
 
@@ -146,7 +146,7 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
 		if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS)
 			return GIT_ENOMEM;
 
-		entry->attr = strtol(buffer, &buffer, 8);
+		entry->attr = strtol(buffer, (char **)&buffer, 8);
 
 		if (*buffer++ != ' ') {
 			error = GIT_EOBJCORRUPTED;
diff --git a/tests/t04-commit.c b/tests/t04-commit.c
index e928424..bcc0417 100644
--- a/tests/t04-commit.c
+++ b/tests/t04-commit.c
@@ -114,15 +114,15 @@ BEGIN_TEST(parse0, "parse the OID line in a commit")
 	git_oid oid;
 
 #define TEST_OID_PASS(string, header) { \
-	char *ptr = string;\
-	char *ptr_original = ptr;\
+	const char *ptr = string;\
+	const char *ptr_original = ptr;\
 	size_t len = strlen(ptr);\
 	must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
 	must_be_true(ptr == ptr_original + len);\
 }
 
 #define TEST_OID_FAIL(string, header) { \
-	char *ptr = string;\
+	const char *ptr = string;\
 	size_t len = strlen(ptr);\
 	must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
 }
@@ -154,7 +154,7 @@ END_TEST
 BEGIN_TEST(parse1, "parse the signature line in a commit")
 
 #define TEST_SIGNATURE_PASS(_string, _header, _name, _email, _time, _offset) { \
-	char *ptr = _string; \
+	const char *ptr = _string; \
 	size_t len = strlen(_string);\
 	git_signature person = {NULL, NULL, {0, 0}}; \
 	must_pass(git_signature__parse(&person, &ptr, ptr + len, _header));\
@@ -166,7 +166,7 @@ BEGIN_TEST(parse1, "parse the signature line in a commit")
 }
 
 #define TEST_SIGNATURE_FAIL(_string, _header) { \
-	char *ptr = _string; \
+	const char *ptr = _string; \
 	size_t len = strlen(_string);\
 	git_signature person = {NULL, NULL, {0, 0}}; \
 	must_fail(git_signature__parse(&person, &ptr, ptr + len, _header));\