Commit 35580d88 by Edward Thomson

stash: fixes from code review

parent fb9515ab
...@@ -57,15 +57,10 @@ typedef enum { ...@@ -57,15 +57,10 @@ typedef enum {
* *
* @param out Object id of the commit containing the stashed state. * @param out Object id of the commit containing the stashed state.
* This commit is also the target of the direct reference refs/stash. * This commit is also the target of the direct reference refs/stash.
*
* @param repo The owning repository. * @param repo The owning repository.
*
* @param stasher The identity of the person performing the stashing. * @param stasher The identity of the person performing the stashing.
*
* @param message Optional description along with the stashed state. * @param message Optional description along with the stashed state.
*
* @param flags Flags to control the stashing process. (see GIT_STASH_* above) * @param flags Flags to control the stashing process. (see GIT_STASH_* above)
*
* @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
* or error code. * or error code.
*/ */
...@@ -86,25 +81,21 @@ GIT_EXTERN(int) git_stash_save( ...@@ -86,25 +81,21 @@ GIT_EXTERN(int) git_stash_save(
typedef struct git_stash_save_options { typedef struct git_stash_save_options {
unsigned int version; unsigned int version;
/** Flags to control the stashing process. (see GIT_STASH_* above) */
uint32_t flags;
/** The identity of the person performing the stashing. */ /** The identity of the person performing the stashing. */
const git_signature *stasher; const git_signature *stasher;
/** Optional description along with the stashed state. */ /** Optional description along with the stashed state. */
const char *message; const char *message;
/** Flags to control the stashing process. (see GIT_STASH_* above) */
uint32_t flags;
/** Optional paths that control which files are stashed. */ /** Optional paths that control which files are stashed. */
git_strarray paths; git_strarray paths;
} git_stash_save_options; } git_stash_save_options;
#define GIT_STASH_SAVE_OPTIONS_VERSION 1 #define GIT_STASH_SAVE_OPTIONS_VERSION 1
#define GIT_STASH_SAVE_OPTIONS_INIT { \ #define GIT_STASH_SAVE_OPTIONS_INIT { GIT_STASH_SAVE_OPTIONS_VERSION }
GIT_STASH_SAVE_OPTIONS_VERSION, \
NULL, \
NULL, \
GIT_STASH_DEFAULT }
/** /**
* Initialize git_stash_save_options structure * Initialize git_stash_save_options structure
...@@ -121,14 +112,11 @@ GIT_EXTERN(int) git_stash_save_options_init( ...@@ -121,14 +112,11 @@ GIT_EXTERN(int) git_stash_save_options_init(
/** /**
* Save the local modifications to a new stash, with options. * Save the local modifications to a new stash, with options.
* *
* @param out Object id of the commit containing the stashed state. * @param out Object id of the commit containing the stashed state.
* This commit is also the target of the direct reference refs/stash. * This commit is also the target of the direct reference refs/stash.
*
* @param repo The owning repository. * @param repo The owning repository.
*
* @param opts The stash options. * @param opts The stash options.
*
* @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
* or error code. * or error code.
*/ */
......
...@@ -199,16 +199,16 @@ static int stash_update_index_from_paths( ...@@ -199,16 +199,16 @@ static int stash_update_index_from_paths(
const git_strarray *paths) const git_strarray *paths)
{ {
unsigned int status_flags; 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]); git_status_file(&status_flags, repo, paths->strings[i]);
if (status_flags & (GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_DELETED)) { if (status_flags & (GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_DELETED)) {
if ((error = git_index_remove(index, paths->strings[i], 0)) < 0) if ((error = git_index_remove(index, paths->strings[i], 0)) < 0)
return error; return error;
} } else {
else {
if ((error = stash_to_index(repo, index, paths->strings[i])) < 0) if ((error = stash_to_index(repo, index, paths->strings[i])) < 0)
return error; return error;
} }
...@@ -453,7 +453,7 @@ static int build_stash_commit_from_index( ...@@ -453,7 +453,7 @@ static int build_stash_commit_from_index(
{ {
git_tree *tree; git_tree *tree;
int error; int error;
if ((error = build_tree_from_index(&tree, repo, index)) < 0) if ((error = build_tree_from_index(&tree, repo, index)) < 0)
goto cleanup; goto cleanup;
...@@ -465,8 +465,7 @@ static int build_stash_commit_from_index( ...@@ -465,8 +465,7 @@ static int build_stash_commit_from_index(
i_commit, i_commit,
b_commit, b_commit,
u_commit, u_commit,
tree tree);
);
cleanup: cleanup:
git_tree_free(tree); git_tree_free(tree);
...@@ -599,7 +598,11 @@ static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flag ...@@ -599,7 +598,11 @@ static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flag
return error; 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(path);
GIT_UNUSED(status); GIT_UNUSED(status);
GIT_UNUSED(payload); GIT_UNUSED(payload);
...@@ -619,9 +622,9 @@ static int ensure_there_are_changes_to_stash_paths( ...@@ -619,9 +622,9 @@ static int ensure_there_are_changes_to_stash_paths(
git_status_options opts = GIT_STATUS_OPTIONS_INIT; git_status_options opts = GIT_STATUS_OPTIONS_INIT;
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES |
| GIT_STATUS_OPT_INCLUDE_UNMODIFIED GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
| GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
if (flags & GIT_STASH_INCLUDE_UNTRACKED) if (flags & GIT_STASH_INCLUDE_UNTRACKED)
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
...@@ -630,7 +633,7 @@ static int ensure_there_are_changes_to_stash_paths( ...@@ -630,7 +633,7 @@ static int ensure_there_are_changes_to_stash_paths(
if (flags & GIT_STASH_INCLUDE_IGNORED) if (flags & GIT_STASH_INCLUDE_IGNORED)
opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED | opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
git_strarray_copy(&opts.pathspec, paths); git_strarray_copy(&opts.pathspec, paths);
error = git_status_foreach_ext(repo, &opts, has_changes_cb, NULL); error = git_status_foreach_ext(repo, &opts, has_changes_cb, NULL);
...@@ -664,17 +667,20 @@ int git_stash_save( ...@@ -664,17 +667,20 @@ int git_stash_save(
uint32_t flags) uint32_t flags)
{ {
git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT; git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT;
GIT_ASSERT_ARG(stasher); GIT_ASSERT_ARG(stasher);
opts.stasher = stasher; opts.stasher = stasher;
opts.message = message; opts.message = message;
opts.flags = flags; opts.flags = flags;
return git_stash_save_with_opts(out, repo, &opts); return git_stash_save_with_opts(out, repo, &opts);
} }
int git_stash_save_with_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_index *index = NULL, *paths_index = NULL;
git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL; git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
...@@ -725,22 +731,12 @@ int git_stash_save_with_opts( ...@@ -725,22 +731,12 @@ int git_stash_save_with_opts(
i_commit, b_commit, u_commit)) < 0) i_commit, b_commit, u_commit)) < 0)
goto cleanup; goto cleanup;
} else { } else {
if ((error = git_index_new(&paths_index)) < 0) if ((error = git_index_new(&paths_index)) < 0 ||
goto cleanup; (error = retrieve_head(&head, repo)) < 0 ||
(error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0 ||
if ((error = retrieve_head(&head, repo)) < 0) (error = git_index_read_tree(paths_index, tree)) < 0 ||
goto cleanup; (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),
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),
i_commit, b_commit, u_commit, paths_index)) < 0) i_commit, b_commit, u_commit, paths_index)) < 0)
goto cleanup; goto cleanup;
} }
...@@ -750,23 +746,20 @@ int git_stash_save_with_opts( ...@@ -750,23 +746,20 @@ int git_stash_save_with_opts(
if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0) if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0)
goto cleanup; goto cleanup;
if (!(opts->flags & GIT_STASH_KEEP_ALL) && (error = reset_index_and_workdir(repo, if (!(opts->flags & GIT_STASH_KEEP_ALL) &&
(opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, opts->flags)) < 0) (error = reset_index_and_workdir(repo,
(opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit,opts->flags)) < 0)
goto cleanup; goto cleanup;
cleanup: cleanup:
git_str_dispose(&msg); git_str_dispose(&msg);
git_commit_free(i_commit); git_commit_free(i_commit);
git_commit_free(b_commit); git_commit_free(b_commit);
git_commit_free(u_commit); git_commit_free(u_commit);
git_tree_free(tree); git_tree_free(tree);
git_reference_free(head);
if (has_paths) { git_index_free(index);
git_reference_free(head); git_index_free(paths_index);
git_index_free(index);
git_index_free(paths_index);
}
return error; return error;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment