Skip to content

Commit 35580d8

Browse files
committed
stash: fixes from code review
1 parent fb9515a commit 35580d8

2 files changed

Lines changed: 38 additions & 57 deletions

File tree

include/git2/stash.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,10 @@ typedef enum {
5757
*
5858
* @param out Object id of the commit containing the stashed state.
5959
* This commit is also the target of the direct reference refs/stash.
60-
*
6160
* @param repo The owning repository.
62-
*
6361
* @param stasher The identity of the person performing the stashing.
64-
*
6562
* @param message Optional description along with the stashed state.
66-
*
6763
* @param flags Flags to control the stashing process. (see GIT_STASH_* above)
68-
*
6964
* @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
7065
* or error code.
7166
*/
@@ -86,25 +81,21 @@ GIT_EXTERN(int) git_stash_save(
8681
typedef struct git_stash_save_options {
8782
unsigned int version;
8883

84+
/** Flags to control the stashing process. (see GIT_STASH_* above) */
85+
uint32_t flags;
86+
8987
/** The identity of the person performing the stashing. */
9088
const git_signature *stasher;
9189

9290
/** Optional description along with the stashed state. */
9391
const char *message;
9492

95-
/** Flags to control the stashing process. (see GIT_STASH_* above) */
96-
uint32_t flags;
97-
9893
/** Optional paths that control which files are stashed. */
9994
git_strarray paths;
10095
} git_stash_save_options;
10196

10297
#define GIT_STASH_SAVE_OPTIONS_VERSION 1
103-
#define GIT_STASH_SAVE_OPTIONS_INIT { \
104-
GIT_STASH_SAVE_OPTIONS_VERSION, \
105-
NULL, \
106-
NULL, \
107-
GIT_STASH_DEFAULT }
98+
#define GIT_STASH_SAVE_OPTIONS_INIT { GIT_STASH_SAVE_OPTIONS_VERSION }
10899

109100
/**
110101
* Initialize git_stash_save_options structure
@@ -121,14 +112,11 @@ GIT_EXTERN(int) git_stash_save_options_init(
121112

122113
/**
123114
* Save the local modifications to a new stash, with options.
124-
*
115+
*
125116
* @param out Object id of the commit containing the stashed state.
126117
* This commit is also the target of the direct reference refs/stash.
127-
*
128118
* @param repo The owning repository.
129-
*
130119
* @param opts The stash options.
131-
*
132120
* @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
133121
* or error code.
134122
*/

src/libgit2/stash.c

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ static int stash_update_index_from_paths(
199199
const git_strarray *paths)
200200
{
201201
unsigned int status_flags;
202-
size_t i, error = 0;
202+
size_t i;
203+
int error = 0;
203204

204-
for(i = 0; i < paths->count; i++) {
205+
for (i = 0; i < paths->count; i++) {
205206
git_status_file(&status_flags, repo, paths->strings[i]);
206207

207208
if (status_flags & (GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_DELETED)) {
208209
if ((error = git_index_remove(index, paths->strings[i], 0)) < 0)
209210
return error;
210-
}
211-
else {
211+
} else {
212212
if ((error = stash_to_index(repo, index, paths->strings[i])) < 0)
213213
return error;
214214
}
@@ -453,7 +453,7 @@ static int build_stash_commit_from_index(
453453
{
454454
git_tree *tree;
455455
int error;
456-
456+
457457
if ((error = build_tree_from_index(&tree, repo, index)) < 0)
458458
goto cleanup;
459459

@@ -465,8 +465,7 @@ static int build_stash_commit_from_index(
465465
i_commit,
466466
b_commit,
467467
u_commit,
468-
tree
469-
);
468+
tree);
470469

471470
cleanup:
472471
git_tree_free(tree);
@@ -599,7 +598,11 @@ static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flag
599598
return error;
600599
}
601600

602-
static int has_changes_cb(const char *path, unsigned int status, void *payload) {
601+
static int has_changes_cb(
602+
const char *path,
603+
unsigned int status,
604+
void *payload)
605+
{
603606
GIT_UNUSED(path);
604607
GIT_UNUSED(status);
605608
GIT_UNUSED(payload);
@@ -619,9 +622,9 @@ static int ensure_there_are_changes_to_stash_paths(
619622
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
620623

621624
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
622-
opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES
623-
| GIT_STATUS_OPT_INCLUDE_UNMODIFIED
624-
| GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
625+
opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES |
626+
GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
627+
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
625628

626629
if (flags & GIT_STASH_INCLUDE_UNTRACKED)
627630
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
@@ -630,7 +633,7 @@ static int ensure_there_are_changes_to_stash_paths(
630633
if (flags & GIT_STASH_INCLUDE_IGNORED)
631634
opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED |
632635
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
633-
636+
634637
git_strarray_copy(&opts.pathspec, paths);
635638

636639
error = git_status_foreach_ext(repo, &opts, has_changes_cb, NULL);
@@ -664,17 +667,20 @@ int git_stash_save(
664667
uint32_t flags)
665668
{
666669
git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT;
667-
670+
668671
GIT_ASSERT_ARG(stasher);
669-
672+
670673
opts.stasher = stasher;
671674
opts.message = message;
672675
opts.flags = flags;
676+
673677
return git_stash_save_with_opts(out, repo, &opts);
674678
}
675679

676680
int git_stash_save_with_opts(
677-
git_oid *out, git_repository *repo, const git_stash_save_options *opts)
681+
git_oid *out,
682+
git_repository *repo,
683+
const git_stash_save_options *opts)
678684
{
679685
git_index *index = NULL, *paths_index = NULL;
680686
git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
@@ -725,22 +731,12 @@ int git_stash_save_with_opts(
725731
i_commit, b_commit, u_commit)) < 0)
726732
goto cleanup;
727733
} else {
728-
if ((error = git_index_new(&paths_index)) < 0)
729-
goto cleanup;
730-
731-
if ((error = retrieve_head(&head, repo)) < 0)
732-
goto cleanup;
733-
734-
if ((error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0)
735-
goto cleanup;
736-
737-
if ((error = git_index_read_tree(paths_index, tree)) < 0)
738-
goto cleanup;
739-
740-
if ((error = stash_update_index_from_paths(repo, paths_index, &opts->paths)) < 0)
741-
goto cleanup;
742-
743-
if ((error = build_stash_commit_from_index(out, repo, opts->stasher, git_str_cstr(&msg),
734+
if ((error = git_index_new(&paths_index)) < 0 ||
735+
(error = retrieve_head(&head, repo)) < 0 ||
736+
(error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0 ||
737+
(error = git_index_read_tree(paths_index, tree)) < 0 ||
738+
(error = stash_update_index_from_paths(repo, paths_index, &opts->paths)) < 0 ||
739+
(error = build_stash_commit_from_index(out, repo, opts->stasher, git_str_cstr(&msg),
744740
i_commit, b_commit, u_commit, paths_index)) < 0)
745741
goto cleanup;
746742
}
@@ -750,23 +746,20 @@ int git_stash_save_with_opts(
750746
if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0)
751747
goto cleanup;
752748

753-
if (!(opts->flags & GIT_STASH_KEEP_ALL) && (error = reset_index_and_workdir(repo,
754-
(opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, opts->flags)) < 0)
749+
if (!(opts->flags & GIT_STASH_KEEP_ALL) &&
750+
(error = reset_index_and_workdir(repo,
751+
(opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit,opts->flags)) < 0)
755752
goto cleanup;
756753

757754
cleanup:
758-
759755
git_str_dispose(&msg);
760756
git_commit_free(i_commit);
761757
git_commit_free(b_commit);
762758
git_commit_free(u_commit);
763759
git_tree_free(tree);
764-
765-
if (has_paths) {
766-
git_reference_free(head);
767-
git_index_free(index);
768-
git_index_free(paths_index);
769-
}
760+
git_reference_free(head);
761+
git_index_free(index);
762+
git_index_free(paths_index);
770763

771764
return error;
772765
}

0 commit comments

Comments
 (0)