diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-02-16 09:11:57 +0000 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-02-16 09:11:57 +0000 |
commit | 35580d88a834438275adea77785a5f356352ea21 (patch) | |
tree | cdc1e0bb26593334374b9e6ef9910312250bb3aa | |
parent | fb9515abc80c945dfe74713989dfb4f41d03d106 (diff) | |
download | libgit2-35580d88a834438275adea77785a5f356352ea21.tar.gz |
stash: fixes from code review
-rw-r--r-- | include/git2/stash.h | 22 | ||||
-rw-r--r-- | src/libgit2/stash.c | 73 |
2 files changed, 38 insertions, 57 deletions
diff --git a/include/git2/stash.h b/include/git2/stash.h index 3d70b36c3..dcfc013dc 100644 --- a/include/git2/stash.h +++ b/include/git2/stash.h @@ -57,15 +57,10 @@ typedef enum { * * @param out Object id of the commit containing the stashed state. * This commit is also the target of the direct reference refs/stash. - * * @param repo The owning repository. - * * @param stasher The identity of the person performing the stashing. - * * @param message Optional description along with the stashed state. - * * @param flags Flags to control the stashing process. (see GIT_STASH_* above) - * * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, * or error code. */ @@ -86,25 +81,21 @@ GIT_EXTERN(int) git_stash_save( typedef struct git_stash_save_options { unsigned int version; + /** Flags to control the stashing process. (see GIT_STASH_* above) */ + uint32_t flags; + /** The identity of the person performing the stashing. */ const git_signature *stasher; /** Optional description along with the stashed state. */ const char *message; - /** Flags to control the stashing process. (see GIT_STASH_* above) */ - uint32_t flags; - /** Optional paths that control which files are stashed. */ git_strarray paths; } git_stash_save_options; #define GIT_STASH_SAVE_OPTIONS_VERSION 1 -#define GIT_STASH_SAVE_OPTIONS_INIT { \ - GIT_STASH_SAVE_OPTIONS_VERSION, \ - NULL, \ - NULL, \ - GIT_STASH_DEFAULT } +#define GIT_STASH_SAVE_OPTIONS_INIT { GIT_STASH_SAVE_OPTIONS_VERSION } /** * Initialize git_stash_save_options structure @@ -121,14 +112,11 @@ GIT_EXTERN(int) git_stash_save_options_init( /** * Save the local modifications to a new stash, with options. - * + * * @param out Object id of the commit containing the stashed state. * This commit is also the target of the direct reference refs/stash. - * * @param repo The owning repository. - * * @param opts The stash options. - * * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, * or error code. */ diff --git a/src/libgit2/stash.c b/src/libgit2/stash.c index af65aae3c..80eaddecf 100644 --- a/src/libgit2/stash.c +++ b/src/libgit2/stash.c @@ -199,16 +199,16 @@ static int stash_update_index_from_paths( const git_strarray *paths) { unsigned int status_flags; - size_t i, error = 0; + size_t i; + int error = 0; - for(i = 0; i < paths->count; i++) { + for (i = 0; i < paths->count; i++) { git_status_file(&status_flags, repo, paths->strings[i]); if (status_flags & (GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_DELETED)) { if ((error = git_index_remove(index, paths->strings[i], 0)) < 0) return error; - } - else { + } else { if ((error = stash_to_index(repo, index, paths->strings[i])) < 0) return error; } @@ -453,7 +453,7 @@ static int build_stash_commit_from_index( { git_tree *tree; int error; - + if ((error = build_tree_from_index(&tree, repo, index)) < 0) goto cleanup; @@ -465,8 +465,7 @@ static int build_stash_commit_from_index( i_commit, b_commit, u_commit, - tree - ); + tree); cleanup: git_tree_free(tree); @@ -599,7 +598,11 @@ static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flag return error; } -static int has_changes_cb(const char *path, unsigned int status, void *payload) { +static int has_changes_cb( + const char *path, + unsigned int status, + void *payload) +{ GIT_UNUSED(path); GIT_UNUSED(status); GIT_UNUSED(payload); @@ -619,9 +622,9 @@ static int ensure_there_are_changes_to_stash_paths( git_status_options opts = GIT_STATUS_OPTIONS_INIT; opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; - opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES - | GIT_STATUS_OPT_INCLUDE_UNMODIFIED - | GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; + opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES | + GIT_STATUS_OPT_INCLUDE_UNMODIFIED | + GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; if (flags & GIT_STASH_INCLUDE_UNTRACKED) opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | @@ -630,7 +633,7 @@ static int ensure_there_are_changes_to_stash_paths( if (flags & GIT_STASH_INCLUDE_IGNORED) opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; - + git_strarray_copy(&opts.pathspec, paths); error = git_status_foreach_ext(repo, &opts, has_changes_cb, NULL); @@ -664,17 +667,20 @@ int git_stash_save( uint32_t flags) { git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT; - + GIT_ASSERT_ARG(stasher); - + opts.stasher = stasher; opts.message = message; opts.flags = flags; + return git_stash_save_with_opts(out, repo, &opts); } int git_stash_save_with_opts( - git_oid *out, git_repository *repo, const git_stash_save_options *opts) + git_oid *out, + git_repository *repo, + const git_stash_save_options *opts) { git_index *index = NULL, *paths_index = NULL; git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL; @@ -725,22 +731,12 @@ int git_stash_save_with_opts( i_commit, b_commit, u_commit)) < 0) goto cleanup; } else { - if ((error = git_index_new(&paths_index)) < 0) - goto cleanup; - - if ((error = retrieve_head(&head, repo)) < 0) - goto cleanup; - - if ((error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0) - goto cleanup; - - if ((error = git_index_read_tree(paths_index, tree)) < 0) - goto cleanup; - - if ((error = stash_update_index_from_paths(repo, paths_index, &opts->paths)) < 0) - goto cleanup; - - if ((error = build_stash_commit_from_index(out, repo, opts->stasher, git_str_cstr(&msg), + if ((error = git_index_new(&paths_index)) < 0 || + (error = retrieve_head(&head, repo)) < 0 || + (error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0 || + (error = git_index_read_tree(paths_index, tree)) < 0 || + (error = stash_update_index_from_paths(repo, paths_index, &opts->paths)) < 0 || + (error = build_stash_commit_from_index(out, repo, opts->stasher, git_str_cstr(&msg), i_commit, b_commit, u_commit, paths_index)) < 0) goto cleanup; } @@ -750,23 +746,20 @@ int git_stash_save_with_opts( if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0) goto cleanup; - if (!(opts->flags & GIT_STASH_KEEP_ALL) && (error = reset_index_and_workdir(repo, - (opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, opts->flags)) < 0) + if (!(opts->flags & GIT_STASH_KEEP_ALL) && + (error = reset_index_and_workdir(repo, + (opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit,opts->flags)) < 0) goto cleanup; cleanup: - git_str_dispose(&msg); git_commit_free(i_commit); git_commit_free(b_commit); git_commit_free(u_commit); git_tree_free(tree); - - if (has_paths) { - git_reference_free(head); - git_index_free(index); - git_index_free(paths_index); - } + git_reference_free(head); + git_index_free(index); + git_index_free(paths_index); return error; } |