Skip to content

Commit d0155f1

Browse files
stash: better option validation for stash save
1 parent d91de9b commit d0155f1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/libgit2/stash.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ int git_stash_save(
664664
uint32_t flags)
665665
{
666666
git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT;
667+
668+
GIT_ASSERT_ARG(stasher);
669+
667670
opts.stasher = stasher;
668671
opts.message = message;
669672
opts.flags = flags;
@@ -678,21 +681,26 @@ int git_stash_save_with_opts(
678681
git_str msg = GIT_STR_INIT;
679682
git_tree *tree = NULL;
680683
git_reference *head = NULL;
684+
bool has_paths = false;
685+
681686
int error;
682687

683688
GIT_ASSERT_ARG(out);
684689
GIT_ASSERT_ARG(repo);
690+
GIT_ASSERT_ARG(opts && opts->stasher);
691+
692+
has_paths = opts->paths.count > 0;
685693

686694
if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
687695
return error;
688696

689697
if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
690698
goto cleanup;
691699

692-
if (opts->paths.count == 0 &&
700+
if (!has_paths &&
693701
(error = ensure_there_are_changes_to_stash(repo, opts->flags)) < 0)
694702
goto cleanup;
695-
else if (opts->paths.count > 0 &&
703+
else if (has_paths &&
696704
(error = ensure_there_are_changes_to_stash_paths(
697705
repo, opts->flags, &opts->paths)) < 0)
698706
goto cleanup;
@@ -712,7 +720,7 @@ int git_stash_save_with_opts(
712720
if ((error = prepare_worktree_commit_message(&msg, opts->message)) < 0)
713721
goto cleanup;
714722

715-
if (opts->paths.count == 0) {
723+
if (!has_paths) {
716724
if ((error = commit_worktree(out, repo, opts->stasher, git_str_cstr(&msg),
717725
i_commit, b_commit, u_commit)) < 0)
718726
goto cleanup;
@@ -753,9 +761,12 @@ int git_stash_save_with_opts(
753761
git_commit_free(b_commit);
754762
git_commit_free(u_commit);
755763
git_tree_free(tree);
756-
git_reference_free(head);
757-
git_index_free(index);
758-
git_index_free(paths_index);
764+
765+
if (has_paths) {
766+
git_reference_free(head);
767+
git_index_free(index);
768+
git_index_free(paths_index);
769+
}
759770

760771
return error;
761772
}

0 commit comments

Comments
 (0)