diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-09-07 17:53:49 -0400 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-10-17 09:49:01 -0400 |
commit | f0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch) | |
tree | be5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/rebase.c | |
parent | 5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff) | |
download | libgit2-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/rebase.c')
-rw-r--r-- | src/rebase.c | 163 |
1 files changed, 82 insertions, 81 deletions
diff --git a/src/rebase.c b/src/rebase.c index 4f10c296b..302fc81fc 100644 --- a/src/rebase.c +++ b/src/rebase.c @@ -7,10 +7,11 @@ #include "common.h" -#include "buffer.h" +#include "str.h" #include "repository.h" #include "posix.h" #include "filebuf.h" +#include "commit.h" #include "merge.h" #include "array.h" #include "config.h" @@ -90,22 +91,22 @@ static int rebase_state_type( char **path_out, git_repository *repo) { - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; git_rebase_t type = GIT_REBASE_NONE; - if (git_buf_joinpath(&path, repo->gitdir, REBASE_APPLY_DIR) < 0) + if (git_str_joinpath(&path, repo->gitdir, REBASE_APPLY_DIR) < 0) return -1; - if (git_path_isdir(git_buf_cstr(&path))) { + if (git_path_isdir(git_str_cstr(&path))) { type = GIT_REBASE_APPLY; goto done; } - git_buf_clear(&path); - if (git_buf_joinpath(&path, repo->gitdir, REBASE_MERGE_DIR) < 0) + git_str_clear(&path); + if (git_str_joinpath(&path, repo->gitdir, REBASE_MERGE_DIR) < 0) return -1; - if (git_path_isdir(git_buf_cstr(&path))) { + if (git_path_isdir(git_str_cstr(&path))) { type = GIT_REBASE_MERGE; goto done; } @@ -114,36 +115,36 @@ done: *type_out = type; if (type != GIT_REBASE_NONE && path_out) - *path_out = git_buf_detach(&path); + *path_out = git_str_detach(&path); - git_buf_dispose(&path); + git_str_dispose(&path); return 0; } GIT_INLINE(int) rebase_readfile( - git_buf *out, - git_buf *state_path, + git_str *out, + git_str *state_path, const char *filename) { size_t state_path_len = state_path->size; int error; - git_buf_clear(out); + git_str_clear(out); - if ((error = git_buf_joinpath(state_path, state_path->ptr, filename)) < 0 || + if ((error = git_str_joinpath(state_path, state_path->ptr, filename)) < 0 || (error = git_futils_readbuffer(out, state_path->ptr)) < 0) goto done; - git_buf_rtrim(out); + git_str_rtrim(out); done: - git_buf_truncate(state_path, state_path_len); + git_str_truncate(state_path, state_path_len); return error; } GIT_INLINE(int) rebase_readint( - size_t *out, git_buf *asc_out, git_buf *state_path, const char *filename) + size_t *out, git_str *asc_out, git_str *state_path, const char *filename) { int32_t num; const char *eol; @@ -163,7 +164,7 @@ GIT_INLINE(int) rebase_readint( } GIT_INLINE(int) rebase_readoid( - git_oid *out, git_buf *str_out, git_buf *state_path, const char *filename) + git_oid *out, git_str *str_out, git_str *state_path, const char *filename) { int error; @@ -201,13 +202,13 @@ static git_rebase_operation *rebase_operation_alloc( static int rebase_open_merge(git_rebase *rebase) { - git_buf state_path = GIT_BUF_INIT, buf = GIT_BUF_INIT, cmt = GIT_BUF_INIT; + git_str state_path = GIT_STR_INIT, buf = GIT_STR_INIT, cmt = GIT_STR_INIT; git_oid id; git_rebase_operation *operation; size_t i, msgnum = 0, end; int error; - if ((error = git_buf_puts(&state_path, rebase->state_path)) < 0) + if ((error = git_str_puts(&state_path, rebase->state_path)) < 0) goto done; /* Read 'msgnum' if it exists (otherwise, let msgnum = 0) */ @@ -234,9 +235,9 @@ static int rebase_open_merge(git_rebase *rebase) GIT_ERROR_CHECK_ARRAY(rebase->operations); for (i = 0; i < end; i++) { - git_buf_clear(&cmt); + git_str_clear(&cmt); - if ((error = git_buf_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 || + if ((error = git_str_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 || (error = rebase_readoid(&id, &buf, &state_path, cmt.ptr)) < 0) goto done; @@ -248,12 +249,12 @@ static int rebase_open_merge(git_rebase *rebase) if ((error = rebase_readfile(&buf, &state_path, ONTO_NAME_FILE)) < 0) goto done; - rebase->onto_name = git_buf_detach(&buf); + rebase->onto_name = git_str_detach(&buf); done: - git_buf_dispose(&cmt); - git_buf_dispose(&state_path); - git_buf_dispose(&buf); + git_str_dispose(&cmt); + git_str_dispose(&state_path); + git_str_dispose(&buf); return error; } @@ -296,8 +297,8 @@ int git_rebase_open( const git_rebase_options *given_opts) { git_rebase *rebase; - git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT, - orig_head_id = GIT_BUF_INIT, onto_id = GIT_BUF_INIT; + git_str path = GIT_STR_INIT, orig_head_name = GIT_STR_INIT, + orig_head_id = GIT_STR_INIT, onto_id = GIT_STR_INIT; size_t state_path_len; int error; @@ -320,54 +321,54 @@ int git_rebase_open( goto done; } - if ((error = git_buf_puts(&path, rebase->state_path)) < 0) + if ((error = git_str_puts(&path, rebase->state_path)) < 0) goto done; - state_path_len = git_buf_len(&path); + state_path_len = git_str_len(&path); - if ((error = git_buf_joinpath(&path, path.ptr, HEAD_NAME_FILE)) < 0 || + if ((error = git_str_joinpath(&path, path.ptr, HEAD_NAME_FILE)) < 0 || (error = git_futils_readbuffer(&orig_head_name, path.ptr)) < 0) goto done; - git_buf_rtrim(&orig_head_name); + git_str_rtrim(&orig_head_name); if (strcmp(ORIG_DETACHED_HEAD, orig_head_name.ptr) == 0) rebase->head_detached = 1; - git_buf_truncate(&path, state_path_len); + git_str_truncate(&path, state_path_len); - if ((error = git_buf_joinpath(&path, path.ptr, ORIG_HEAD_FILE)) < 0) + if ((error = git_str_joinpath(&path, path.ptr, ORIG_HEAD_FILE)) < 0) goto done; if (!git_path_isfile(path.ptr)) { /* Previous versions of git.git used 'head' here; support that. */ - git_buf_truncate(&path, state_path_len); + git_str_truncate(&path, state_path_len); - if ((error = git_buf_joinpath(&path, path.ptr, HEAD_FILE)) < 0) + if ((error = git_str_joinpath(&path, path.ptr, HEAD_FILE)) < 0) goto done; } if ((error = git_futils_readbuffer(&orig_head_id, path.ptr)) < 0) goto done; - git_buf_rtrim(&orig_head_id); + git_str_rtrim(&orig_head_id); if ((error = git_oid_fromstr(&rebase->orig_head_id, orig_head_id.ptr)) < 0) goto done; - git_buf_truncate(&path, state_path_len); + git_str_truncate(&path, state_path_len); - if ((error = git_buf_joinpath(&path, path.ptr, ONTO_FILE)) < 0 || + if ((error = git_str_joinpath(&path, path.ptr, ONTO_FILE)) < 0 || (error = git_futils_readbuffer(&onto_id, path.ptr)) < 0) goto done; - git_buf_rtrim(&onto_id); + git_str_rtrim(&onto_id); if ((error = git_oid_fromstr(&rebase->onto_id, onto_id.ptr)) < 0) goto done; if (!rebase->head_detached) - rebase->orig_head_name = git_buf_detach(&orig_head_name); + rebase->orig_head_name = git_str_detach(&orig_head_name); switch (rebase->type) { case GIT_REBASE_INTERACTIVE: @@ -391,10 +392,10 @@ done: else git_rebase_free(rebase); - git_buf_dispose(&path); - git_buf_dispose(&orig_head_name); - git_buf_dispose(&orig_head_id); - git_buf_dispose(&onto_id); + git_str_dispose(&path); + git_str_dispose(&orig_head_name); + git_str_dispose(&orig_head_id); + git_str_dispose(&onto_id); return error; } @@ -410,20 +411,20 @@ static int rebase_cleanup(git_rebase *rebase) static int rebase_setupfile(git_rebase *rebase, const char *filename, int flags, const char *fmt, ...) { - git_buf path = GIT_BUF_INIT, - contents = GIT_BUF_INIT; + git_str path = GIT_STR_INIT, + contents = GIT_STR_INIT; va_list ap; int error; va_start(ap, fmt); - git_buf_vprintf(&contents, fmt, ap); + git_str_vprintf(&contents, fmt, ap); va_end(ap); - if ((error = git_buf_joinpath(&path, rebase->state_path, filename)) == 0) + if ((error = git_str_joinpath(&path, rebase->state_path, filename)) == 0) error = git_futils_writebuffer(&contents, path.ptr, flags, REBASE_FILE_MODE); - git_buf_dispose(&path); - git_buf_dispose(&contents); + git_str_dispose(&path); + git_str_dispose(&contents); return error; } @@ -440,7 +441,7 @@ static const char *rebase_onto_name(const git_annotated_commit *onto) static int rebase_setupfiles_merge(git_rebase *rebase) { - git_buf commit_filename = GIT_BUF_INIT; + git_str commit_filename = GIT_STR_INIT; char id_str[GIT_OID_HEXSZ]; git_rebase_operation *operation; size_t i; @@ -453,8 +454,8 @@ static int rebase_setupfiles_merge(git_rebase *rebase) for (i = 0; i < git_array_size(rebase->operations); i++) { operation = git_array_get(rebase->operations, i); - git_buf_clear(&commit_filename); - git_buf_printf(&commit_filename, CMT_FILE_FMT, i+1); + git_str_clear(&commit_filename); + git_str_printf(&commit_filename, CMT_FILE_FMT, i+1); git_oid_fmt(id_str, &operation->id); @@ -464,7 +465,7 @@ static int rebase_setupfiles_merge(git_rebase *rebase) } done: - git_buf_dispose(&commit_filename); + git_str_dispose(&commit_filename); return error; } @@ -626,16 +627,16 @@ static int rebase_init_merge( { git_reference *head_ref = NULL; git_commit *onto_commit = NULL; - git_buf reflog = GIT_BUF_INIT; - git_buf state_path = GIT_BUF_INIT; + git_str reflog = GIT_STR_INIT; + git_str state_path = GIT_STR_INIT; int error; GIT_UNUSED(upstream); - if ((error = git_buf_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0) + if ((error = git_str_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0) goto done; - rebase->state_path = git_buf_detach(&state_path); + rebase->state_path = git_str_detach(&state_path); GIT_ERROR_CHECK_ALLOC(rebase->state_path); if (branch->ref_name && strcmp(branch->ref_name, "HEAD")) { @@ -654,7 +655,7 @@ static int rebase_init_merge( git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto)); if ((error = rebase_setupfiles(rebase)) < 0 || - (error = git_buf_printf(&reflog, + (error = git_str_printf(&reflog, "rebase: checkout %s", rebase_onto_name(onto))) < 0 || (error = git_commit_lookup( &onto_commit, repo, git_annotated_commit_id(onto))) < 0 || @@ -667,8 +668,8 @@ static int rebase_init_merge( done: git_reference_free(head_ref); git_commit_free(onto_commit); - git_buf_dispose(&reflog); - git_buf_dispose(&state_path); + git_str_dispose(&reflog); + git_str_dispose(&state_path); return error; } @@ -795,7 +796,7 @@ static int rebase_next_merge( git_rebase_operation **out, git_rebase *rebase) { - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; git_commit *current_commit = NULL, *parent_commit = NULL; git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL; git_index *index = NULL; @@ -848,7 +849,7 @@ done: git_tree_free(parent_tree); git_commit_free(parent_commit); git_commit_free(current_commit); - git_buf_dispose(&path); + git_str_dispose(&path); return error; } @@ -955,14 +956,14 @@ static int create_signed( size_t parent_count, const git_commit **parents) { - git_buf commit_content = GIT_BUF_INIT, - commit_signature = GIT_BUF_INIT, - signature_field = GIT_BUF_INIT; + git_str commit_content = GIT_STR_INIT; + git_buf commit_signature = { NULL, 0, 0 }, + signature_field = { NULL, 0, 0 }; int error; git_error_clear(); - if ((error = git_commit_create_buffer(&commit_content, + if ((error = git_commit__create_buffer(&commit_content, rebase->repo, author, committer, message_encoding, message, tree, parent_count, parents)) < 0) goto done; @@ -986,7 +987,7 @@ static int create_signed( done: git_buf_dispose(&commit_signature); git_buf_dispose(&signature_field); - git_buf_dispose(&commit_content); + git_str_dispose(&commit_content); return error; } #endif @@ -1216,13 +1217,13 @@ done: return error; } -static int notes_ref_lookup(git_buf *out, git_rebase *rebase) +static int notes_ref_lookup(git_str *out, git_rebase *rebase) { git_config *config = NULL; int do_rewrite, error; if (rebase->options.rewrite_notes_ref) { - git_buf_attach_notowned(out, + git_str_attach_notowned(out, rebase->options.rewrite_notes_ref, strlen(rebase->options.rewrite_notes_ref)); return 0; @@ -1239,7 +1240,7 @@ static int notes_ref_lookup(git_buf *out, git_rebase *rebase) } error = do_rewrite ? - git_config_get_string_buf(out, config, "notes.rewriteref") : + git_config__get_string_buf(out, config, "notes.rewriteref") : GIT_ENOTFOUND; done: @@ -1294,7 +1295,7 @@ static int rebase_copy_notes( git_rebase *rebase, const git_signature *committer) { - git_buf path = GIT_BUF_INIT, rewritten = GIT_BUF_INIT, notes_ref = GIT_BUF_INIT; + git_str path = GIT_STR_INIT, rewritten = GIT_STR_INIT, notes_ref = GIT_STR_INIT; char *pair_list, *fromstr, *tostr, *end; git_oid from, to; unsigned int linenum = 1; @@ -1309,7 +1310,7 @@ static int rebase_copy_notes( goto done; } - if ((error = git_buf_joinpath(&path, rebase->state_path, REWRITTEN_FILE)) < 0 || + if ((error = git_str_joinpath(&path, rebase->state_path, REWRITTEN_FILE)) < 0 || (error = git_futils_readbuffer(&rewritten, path.ptr)) < 0) goto done; @@ -1349,9 +1350,9 @@ on_error: error = -1; done: - git_buf_dispose(&rewritten); - git_buf_dispose(&path); - git_buf_dispose(¬es_ref); + git_str_dispose(&rewritten); + git_str_dispose(&path); + git_str_dispose(¬es_ref); return error; } @@ -1360,16 +1361,16 @@ static int return_to_orig_head(git_rebase *rebase) { git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL; git_commit *terminal_commit = NULL; - git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT; + git_str branch_msg = GIT_STR_INIT, head_msg = GIT_STR_INIT; char onto[GIT_OID_HEXSZ]; int error = 0; git_oid_fmt(onto, &rebase->onto_id); - if ((error = git_buf_printf(&branch_msg, + if ((error = git_str_printf(&branch_msg, "rebase finished: %s onto %.*s", rebase->orig_head_name, GIT_OID_HEXSZ, onto)) == 0 && - (error = git_buf_printf(&head_msg, + (error = git_str_printf(&head_msg, "rebase finished: returning to %s", rebase->orig_head_name)) == 0 && (error = git_repository_head(&terminal_ref, rebase->repo)) == 0 && @@ -1383,8 +1384,8 @@ static int return_to_orig_head(git_rebase *rebase) rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, head_msg.ptr); - git_buf_dispose(&head_msg); - git_buf_dispose(&branch_msg); + git_str_dispose(&head_msg); + git_str_dispose(&branch_msg); git_commit_free(terminal_commit); git_reference_free(head_ref); git_reference_free(branch_ref); |