Commit fa6420f73e8a621cc04e95820b625097b5c2fbf2

nulltoken 2012-04-29T21:46:33

buf: deploy git_buf_len()

diff --git a/src/config_file.c b/src/config_file.c
index 7841ea0..be09774 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1233,7 +1233,7 @@ static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int i
 	 * standard, this character **has** to be last one in the buf, with
 	 * no whitespace after it */
 	assert(is_multiline_var(value->ptr));
-	git_buf_truncate(value, value->size - 1);
+	git_buf_truncate(value, git_buf_len(value) - 1);
 
 	proc_line = fixup_line(line, in_quotes);
 	if (proc_line == NULL) {
diff --git a/src/crlf.c b/src/crlf.c
index 536b50f..8fe588a 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -105,7 +105,7 @@ static int crlf_load_attributes(struct crlf_attrs *ca, git_repository *repo, con
 static int drop_crlf(git_buf *dest, const git_buf *source)
 {
 	const char *scan = source->ptr, *next;
-	const char *scan_end = source->ptr + source->size;
+	const char *scan_end = git_buf_cstr(source) + git_buf_len(source);
 
 	/* Main scan loop.  Find the next carriage return and copy the
 	 * whole chunk up to that point to the destination buffer.
@@ -138,7 +138,7 @@ static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *sou
 	assert(self && dest && source);
 
 	/* Empty file? Nothing to do */
-	if (source->size == 0)
+	if (git_buf_len(source) == 0)
 		return 0;
 
 	/* Heuristics to see if we can skip the conversion.
diff --git a/src/filter.c b/src/filter.c
index d2d1134..3389bed 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -19,13 +19,13 @@ void git_text_gather_stats(git_text_stats *stats, const git_buf *text)
 
 	memset(stats, 0, sizeof(*stats));
 
-	for (i = 0; i < text->size; i++) {
+	for (i = 0; i < git_buf_len(text); i++) {
 		unsigned char c = text->ptr[i];
 
 		if (c == '\r') {
 			stats->cr++;
 
-			if (i + 1 < text->size && text->ptr[i + 1] == '\n')
+			if (i + 1 < git_buf_len(text) && text->ptr[i + 1] == '\n')
 				stats->crlf++;
 		}
 
@@ -59,7 +59,7 @@ void git_text_gather_stats(git_text_stats *stats, const git_buf *text)
 	}
 
 	/* If file ends with EOF then don't count this EOF as non-printable. */
-	if (text->size >= 1 && text->ptr[text->size - 1] == '\032')
+	if (git_buf_len(text) >= 1 && text->ptr[text->size - 1] == '\032')
 		stats->nonprintable--;
 }
 
@@ -127,14 +127,14 @@ int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters)
 
 	src = 0;
 
-	if (source->size == 0) {
+	if (git_buf_len(source) == 0) {
 		git_buf_clear(dest);
 		return GIT_SUCCESS;
 	}
 
 	/* Pre-grow the destination buffer to more or less the size
 	 * we expect it to have */
-	if (git_buf_grow(dest, source->size) < 0)
+	if (git_buf_grow(dest, git_buf_len(source)) < 0)
 		return GIT_ENOMEM;
 
 	for (i = 0; i < filters->length; ++i) {
diff --git a/src/indexer.c b/src/indexer.c
index 22a510a..d2e492c 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -401,7 +401,7 @@ static int index_path_stream(git_buf *path, git_indexer_stream *idx, const char 
 
 	git_buf_truncate(path, slash);
 	git_buf_puts(path, prefix);
-	git_oid_fmt(path->ptr + path->size, &idx->hash);
+	git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash);
 	path->size += GIT_OID_HEXSZ;
 	git_buf_puts(path, suffix);
 
@@ -633,7 +633,7 @@ static int index_path(git_buf *path, git_indexer *idx)
 
 	git_buf_truncate(path, slash);
 	git_buf_puts(path, prefix);
-	git_oid_fmt(path->ptr + path->size, &idx->hash);
+	git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash);
 	path->size += GIT_OID_HEXSZ;
 	git_buf_puts(path, suffix);
 
diff --git a/src/odb_loose.c b/src/odb_loose.c
index b593d18..d028dec 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -61,13 +61,13 @@ static int object_file_name(git_buf *name, const char *dir, const git_oid *id)
 	git_buf_sets(name, dir);
 
 	/* expand length for 40 hex sha1 chars + 2 * '/' + '\0' */
-	if (git_buf_grow(name, name->size + GIT_OID_HEXSZ + 3) < 0)
+	if (git_buf_grow(name, git_buf_len(name) + GIT_OID_HEXSZ + 3) < 0)
 		return -1;
 
 	git_path_to_dir(name);
 
 	/* loose object filename: aa/aaa... (41 bytes) */
-	git_oid_pathfmt(name->ptr + name->size, id);
+	git_oid_pathfmt(name->ptr + git_buf_len(name), id);
 	name->size += GIT_OID_HEXSZ + 1;
 	name->ptr[name->size] = '\0';
 
@@ -81,7 +81,7 @@ static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj)
 	unsigned char *data = (unsigned char *)obj->ptr;
 	size_t shift, size, used = 0;
 
-	if (obj->size == 0)
+	if (git_buf_len(obj) == 0)
 		return 0;
 
 	c = data[used++];
@@ -90,7 +90,7 @@ static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj)
 	size = c & 15;
 	shift = 4;
 	while (c & 0x80) {
-		if (obj->size <= used)
+		if (git_buf_len(obj) <= used)
 			return 0;
 		if (sizeof(size_t) * 8 <= shift)
 			return 0;
@@ -182,7 +182,7 @@ static int start_inflate(z_stream *s, git_buf *obj, void *out, size_t len)
 	int status;
 
 	init_stream(s, out, len);
-	set_stream_input(s, obj->ptr, obj->size);
+	set_stream_input(s, obj->ptr, git_buf_len(obj));
 
 	if ((status = inflateInit(s)) < Z_OK)
 		return status;
@@ -469,7 +469,7 @@ static int locate_object(
 static int fn_locate_object_short_oid(void *state, git_buf *pathbuf) {
 	loose_locate_object_state *sstate = (loose_locate_object_state *)state;
 
-	if (pathbuf->size - sstate->dir_len != GIT_OID_HEXSZ - 2) {
+	if (git_buf_len(pathbuf) - sstate->dir_len != GIT_OID_HEXSZ - 2) {
 		/* Entry cannot be an object. Continue to next entry */
 		return 0;
 	}
@@ -517,7 +517,7 @@ static int locate_object_short_oid(
 	git_path_to_dir(object_location);
 
 	/* save adjusted position at end of dir so it can be restored later */
-	dir_len = object_location->size;
+	dir_len = git_buf_len(object_location);
 
 	/* Convert raw oid to hex formatted oid */
 	git_oid_fmt((char *)state.short_oid, short_oid);
@@ -530,7 +530,7 @@ static int locate_object_short_oid(
 	if (git_path_isdir(object_location->ptr) == false)
 		return git_odb__error_notfound("failed to locate from short oid");
 
-	state.dir_len = object_location->size;
+	state.dir_len = git_buf_len(object_location);
 	state.short_oid_len = len;
 	state.found = 0;
 
diff --git a/src/odb_pack.c b/src/odb_pack.c
index b91e3ca..242200b 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -219,7 +219,7 @@ static int packfile_load__cb(void *_data, git_buf *path)
 
 	for (i = 0; i < backend->packs.length; ++i) {
 		struct git_pack_file *p = git_vector_get(&backend->packs, i);
-		if (memcmp(p->pack_name, path->ptr, path->size - strlen(".idx")) == 0)
+		if (memcmp(p->pack_name, git_buf_cstr(path), git_buf_len(path) - strlen(".idx")) == 0)
 			return 0;
 	}
 
diff --git a/src/path.c b/src/path.c
index a7cf440..f562b0b 100644
--- a/src/path.c
+++ b/src/path.c
@@ -221,8 +221,8 @@ int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base)
 int git_path_to_dir(git_buf *path)
 {
 	if (path->asize > 0 &&
-		path->size > 0 &&
-		path->ptr[path->size - 1] != '/')
+		git_buf_len(path) > 0 &&
+		path->ptr[git_buf_len(path) - 1] != '/')
 		git_buf_putc(path, '/');
 
 	return git_buf_oom(path) ? -1 : 0;
@@ -327,12 +327,12 @@ int git_path_walk_up(
 		if (git__prefixcmp(path->ptr, ceiling) == 0)
 			stop = (ssize_t)strlen(ceiling);
 		else
-			stop = path->size;
+			stop = git_buf_len(path);
 	}
-	scan = path->size;
+	scan = git_buf_len(path);
 
 	iter.ptr = path->ptr;
-	iter.size = path->size;
+	iter.size = git_buf_len(path);
 	iter.asize = path->asize;
 
 	while (scan >= stop) {
@@ -407,7 +407,7 @@ static bool _check_dir_contents(
 	bool (*predicate)(const char *))
 {
 	bool result;
-	size_t dir_size = dir->size;
+	size_t dir_size = git_buf_len(dir);
 	size_t sub_size = strlen(sub);
 
 	/* leave base valid even if we could not make space for subdir */
@@ -503,7 +503,7 @@ int git_path_direach(
 	if (git_path_to_dir(path) < 0)
 		return -1;
 
-	wd_len = path->size;
+	wd_len = git_buf_len(path);
 
 	if ((dir = opendir(path->ptr)) == NULL) {
 		giterr_set(GITERR_OS, "Failed to open directory '%s'", path->ptr);
diff --git a/src/pkt.c b/src/pkt.c
index ae7a408..6cf4dac 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -277,7 +277,7 @@ static int buffer_want_with_caps(git_remote_head *head, git_transport_caps *caps
 	len = (unsigned int)
 		(strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ +
 		 strlen(capstr) + 1 /* LF */);
-	git_buf_grow(buf, buf->size + len);
+	git_buf_grow(buf, git_buf_len(buf) + len);
 
 	git_oid_fmt(oid, &head->oid);
 	return git_buf_printf(buf, "%04xwant %s%c%s\n", len, oid, 0, capstr);
diff --git a/src/protocol.c b/src/protocol.c
index 4c4a7f7..a7df961 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -17,7 +17,7 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
 	const char *line_end, *ptr;
 
 	if (len == 0) { /* EOF */
-		if (buf->size != 0) {
+		if (git_buf_len(buf) != 0) {
 			giterr_set(GITERR_NET, "Unexpected EOF");
 			return p->error = -1;
 		} else {
@@ -30,10 +30,10 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
 	while (1) {
 		git_pkt *pkt;
 
-		if (buf->size == 0)
+		if (git_buf_len(buf) == 0)
 			return 0;
 
-		error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->size);
+		error = git_pkt_parse_line(&pkt, ptr, &line_end, git_buf_len(buf));
 		if (error == GIT_ESHORTBUFFER)
 			return 0; /* Ask for more */
 		if (error < 0)
diff --git a/src/refs.c b/src/refs.c
index 659ac94..28e8f78 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -139,7 +139,7 @@ static int loose_parse_symbolic(git_reference *ref, git_buf *file_content)
 
 	refname_start = (const char *)file_content->ptr;
 
-	if (file_content->size < header_len + 1)
+	if (git_buf_len(file_content) < header_len + 1)
 		goto corrupt;
 
 	/*
@@ -174,7 +174,7 @@ static int loose_parse_oid(git_oid *oid, git_buf *file_content)
 	buffer = (char *)file_content->ptr;
 
 	/* File format: 40 chars (OID) + newline */
-	if (file_content->size < GIT_OID_HEXSZ + 1)
+	if (git_buf_len(file_content) < GIT_OID_HEXSZ + 1)
 		goto corrupt;
 
 	if (git_oid_fromstr(oid, buffer) < 0)
diff --git a/src/refspec.c b/src/refspec.c
index d51fd4c..7a5127c 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -103,10 +103,10 @@ int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *n
 	 * No '*' at the end means that it's mapped to one specific local
 	 * branch, so no actual transformation is needed.
 	 */
-	if (out->size > 0 && out->ptr[out->size - 1] != '*')
+	if (git_buf_len(out) > 0 && out->ptr[git_buf_len(out) - 1] != '*')
 		return GIT_SUCCESS;
 
-	git_buf_truncate(out, out->size - 1); /* remove trailing '*' */
+	git_buf_truncate(out, git_buf_len(out) - 1); /* remove trailing '*' */
 	git_buf_puts(out, name + strlen(spec->src) - 1);
 
 	if (git_buf_oom(out))
diff --git a/src/repository.c b/src/repository.c
index affc0c4..cfabee4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -278,7 +278,7 @@ static int find_repo(
 	if ((error = git_buf_joinpath(&path, path.ptr, DOT_GIT)) < 0)
 		return error;
 
-	while (!error && !repo_path->size) {
+	while (!error && !git_buf_len(repo_path)) {
 		if (p_stat(path.ptr, &st) == 0) {
 			/* check that we have not crossed device boundaries */
 			if (initial_device == 0)
@@ -328,7 +328,7 @@ static int find_repo(
 	}
 
 	if (!error && parent_path != NULL) {
-		if (!repo_path->size)
+		if (!git_buf_len(repo_path))
 			git_buf_clear(parent_path);
 		else {
 			git_path_dirname_r(parent_path, path.ptr);
@@ -340,7 +340,7 @@ static int find_repo(
 
 	git_buf_free(&path);
 
-	if (!repo_path->size && !error) {
+	if (!git_buf_len(repo_path) && !error) {
 		giterr_set(GITERR_REPOSITORY,
 			"Could not find repository from '%s'", start_path);
 		error = GIT_ENOTFOUND;
diff --git a/src/transports/http.c b/src/transports/http.c
index 012e8ff..938076f 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -321,7 +321,7 @@ static int on_body_parse_response(http_parser *parser, const char *str, size_t l
 	const char *line_end, *ptr;
 
 	if (len == 0) { /* EOF */
-		if (buf->size != 0) {
+		if (git_buf_len(buf) != 0) {
 			giterr_set(GITERR_NET, "Unexpected EOF");
 			return t->error = -1;
 		} else {
@@ -334,10 +334,10 @@ static int on_body_parse_response(http_parser *parser, const char *str, size_t l
 	while (1) {
 		git_pkt *pkt;
 
-		if (buf->size == 0)
+		if (git_buf_len(buf) == 0)
 			return 0;
 
-		error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->size);
+		error = git_pkt_parse_line(&pkt, ptr, &line_end, git_buf_len(buf));
 		if (error == GIT_ESHORTBUFFER) {
 			return 0; /* Ask for more */
 		}
@@ -555,9 +555,9 @@ static int http_download_pack(git_transport *transport, git_repository *repo, gi
 	memset(&settings, 0x0, sizeof(settings));
 	settings.on_message_complete = on_message_complete_download_pack;
 	settings.on_body = on_body_download_pack;
-	*bytes = oldbuf->size;
+	*bytes = git_buf_len(oldbuf);
 
-	if (git_indexer_stream_add(idx, oldbuf->ptr, oldbuf->size, stats) < 0)
+	if (git_indexer_stream_add(idx, git_buf_cstr(oldbuf), git_buf_len(oldbuf), stats) < 0)
 		goto on_error;
 
 	do {
diff --git a/src/tree.c b/src/tree.c
index 56a7229..4695573 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -711,7 +711,7 @@ static int tree_walk_post(
 
 		if (entry_is_tree(entry)) {
 			git_tree *subtree;
-			size_t path_len = path->size;
+			size_t path_len = git_buf_len(path);
 
 			if ((error = git_tree_lookup(
 				&subtree, tree->object.repo, &entry->oid)) < 0)