summaryrefslogtreecommitdiff
path: root/src/indexer.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-07 17:53:49 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-10-17 09:49:01 -0400
commitf0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch)
treebe5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/indexer.c
parent5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff)
downloadlibgit2-ethomson/gitstr.tar.gz
str: introduce `git_str` for internal, `git_buf` is externalethomson/gitstr
libgit2 has two distinct requirements that were previously solved by `git_buf`. We require: 1. A general purpose string class that provides a number of utility APIs for manipulating data (eg, concatenating, truncating, etc). 2. A structure that we can use to return strings to callers that they can take ownership of. By using a single class (`git_buf`) for both of these purposes, we have confused the API to the point that refactorings are difficult and reasoning about correctness is also difficult. Move the utility class `git_buf` to be called `git_str`: this represents its general purpose, as an internal string buffer class. The name also is an homage to Junio Hamano ("gitstr"). The public API remains `git_buf`, and has a much smaller footprint. It is generally only used as an "out" param with strict requirements that follow the documentation. (Exceptions exist for some legacy APIs to avoid breaking callers unnecessarily.) Utility functions exist to convert a user-specified `git_buf` to a `git_str` so that we can call internal functions, then converting it back again.
Diffstat (limited to 'src/indexer.c')
-rw-r--r--src/indexer.c66
1 files changed, 33 insertions, 33 deletions
diff --git a/src/indexer.c b/src/indexer.c
index 16ed7bfae..213ad7581 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -48,7 +48,7 @@ struct git_indexer {
off64_t off;
off64_t entry_start;
git_object_t entry_type;
- git_buf entry_data;
+ git_str entry_data;
git_packfile_stream stream;
size_t nr_objects;
git_vector objects;
@@ -137,7 +137,7 @@ int git_indexer_new(
{
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
git_indexer *idx;
- git_buf path = GIT_BUF_INIT, tmp_path = GIT_BUF_INIT;
+ git_str path = GIT_STR_INIT, tmp_path = GIT_STR_INIT;
static const char suff[] = "/pack";
int error, fd = -1;
@@ -150,7 +150,7 @@ int git_indexer_new(
idx->progress_cb = opts.progress_cb;
idx->progress_payload = opts.progress_cb_payload;
idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
- git_buf_init(&idx->entry_data, 0);
+ git_str_init(&idx->entry_data, 0);
if ((error = git_hash_ctx_init(&idx->hash_ctx, GIT_HASH_ALGORITHM_SHA1)) < 0 ||
(error = git_hash_ctx_init(&idx->trailer, GIT_HASH_ALGORITHM_SHA1)) < 0 ||
@@ -162,17 +162,17 @@ int git_indexer_new(
if (git_repository__fsync_gitdir)
idx->do_fsync = 1;
- error = git_buf_joinpath(&path, prefix, suff);
+ error = git_str_joinpath(&path, prefix, suff);
if (error < 0)
goto cleanup;
- fd = git_futils_mktmp(&tmp_path, git_buf_cstr(&path), idx->mode);
- git_buf_dispose(&path);
+ fd = git_futils_mktmp(&tmp_path, git_str_cstr(&path), idx->mode);
+ git_str_dispose(&path);
if (fd < 0)
goto cleanup;
- error = git_packfile_alloc(&idx->pack, git_buf_cstr(&tmp_path));
- git_buf_dispose(&tmp_path);
+ error = git_packfile_alloc(&idx->pack, git_str_cstr(&tmp_path));
+ git_str_dispose(&tmp_path);
if (error < 0)
goto cleanup;
@@ -188,14 +188,14 @@ cleanup:
if (fd != -1)
p_close(fd);
- if (git_buf_len(&tmp_path) > 0)
- p_unlink(git_buf_cstr(&tmp_path));
+ if (git_str_len(&tmp_path) > 0)
+ p_unlink(git_str_cstr(&tmp_path));
if (idx->pack != NULL)
p_unlink(idx->pack->pack_name);
- git_buf_dispose(&path);
- git_buf_dispose(&tmp_path);
+ git_str_dispose(&path);
+ git_str_dispose(&tmp_path);
git__free(idx);
return -1;
}
@@ -245,7 +245,7 @@ static int hash_object_stream(git_indexer*idx, git_packfile_stream *stream)
break;
if (idx->do_verify)
- git_buf_put(&idx->entry_data, idx->objbuf, read);
+ git_str_put(&idx->entry_data, idx->objbuf, read);
git_hash_update(&idx->hash_ctx, idx->objbuf, read);
} while (read > 0);
@@ -730,7 +730,7 @@ static int read_stream_object(git_indexer *idx, git_indexer_progress *stats)
git_mwindow_close(&w);
idx->entry_start = entry_start;
git_hash_init(&idx->hash_ctx);
- git_buf_clear(&idx->entry_data);
+ git_str_clear(&idx->entry_data);
if (type == GIT_OBJECT_REF_DELTA || type == GIT_OBJECT_OFS_DELTA) {
error = advance_delta_offset(idx, type);
@@ -876,7 +876,7 @@ on_error:
return error;
}
-static int index_path(git_buf *path, git_indexer *idx, const char *suffix)
+static int index_path(git_str *path, git_indexer *idx, const char *suffix)
{
const char prefix[] = "pack-";
size_t slash = (size_t)path->size;
@@ -885,17 +885,17 @@ static int index_path(git_buf *path, git_indexer *idx, const char *suffix)
while (slash > 0 && path->ptr[slash - 1] != '/')
slash--;
- if (git_buf_grow(path, slash + 1 + strlen(prefix) +
+ if (git_str_grow(path, slash + 1 + strlen(prefix) +
GIT_OID_HEXSZ + strlen(suffix) + 1) < 0)
return -1;
- git_buf_truncate(path, slash);
- git_buf_puts(path, prefix);
- git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash);
+ git_str_truncate(path, slash);
+ git_str_puts(path, prefix);
+ git_oid_fmt(path->ptr + git_str_len(path), &idx->hash);
path->size += GIT_OID_HEXSZ;
- git_buf_puts(path, suffix);
+ git_str_puts(path, suffix);
- return git_buf_oom(path) ? -1 : 0;
+ return git_str_oom(path) ? -1 : 0;
}
/**
@@ -915,7 +915,7 @@ static int inject_object(git_indexer *idx, git_oid *id)
struct git_pack_entry *pentry = NULL;
git_oid foo = {{0}};
unsigned char hdr[64];
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
off64_t entry_start;
const void *data;
size_t len, hdr_len;
@@ -956,7 +956,7 @@ static int inject_object(git_indexer *idx, git_oid *id)
idx->pack->mwf.size += buf.size;
entry->crc = htonl(crc32(entry->crc, (unsigned char *)buf.ptr, (uInt)buf.size));
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
/* Write a fake trailer so the pack functions play ball */
@@ -1152,7 +1152,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
unsigned int i, long_offsets = 0, left;
int error;
struct git_pack_idx_header hdr;
- git_buf filename = GIT_BUF_INIT;
+ git_str filename = GIT_STR_INIT;
struct entry *entry;
git_oid trailer_hash, file_hash;
git_filebuf index_file = {0};
@@ -1226,10 +1226,10 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
* files with different contents have different names */
git_oid_cpy(&idx->hash, &trailer_hash);
- git_buf_sets(&filename, idx->pack->pack_name);
- git_buf_shorten(&filename, strlen("pack"));
- git_buf_puts(&filename, "idx");
- if (git_buf_oom(&filename))
+ git_str_sets(&filename, idx->pack->pack_name);
+ git_str_shorten(&filename, strlen("pack"));
+ git_str_puts(&filename, "idx");
+ if (git_str_oom(&filename))
return -1;
if (git_filebuf_open(&index_file, filename.ptr,
@@ -1336,23 +1336,23 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
goto on_error;
/* And don't forget to rename the packfile to its new place. */
- if (p_rename(idx->pack->pack_name, git_buf_cstr(&filename)) < 0)
+ if (p_rename(idx->pack->pack_name, git_str_cstr(&filename)) < 0)
goto on_error;
/* And fsync the parent directory if we're asked to. */
if (idx->do_fsync &&
- git_futils_fsync_parent(git_buf_cstr(&filename)) < 0)
+ git_futils_fsync_parent(git_str_cstr(&filename)) < 0)
goto on_error;
idx->pack_committed = 1;
- git_buf_dispose(&filename);
+ git_str_dispose(&filename);
return 0;
on_error:
git_mwindow_free_all(&idx->pack->mwf);
git_filebuf_cleanup(&index_file);
- git_buf_dispose(&filename);
+ git_str_dispose(&filename);
return -1;
}
@@ -1389,7 +1389,7 @@ void git_indexer_free(git_indexer *idx)
git_hash_ctx_cleanup(&idx->trailer);
git_hash_ctx_cleanup(&idx->hash_ctx);
- git_buf_dispose(&idx->entry_data);
+ git_str_dispose(&idx->entry_data);
git_oidmap_free(idx->expected_oids);
git__free(idx);
}