Skip to content

Commit ec67f95

Browse files
stash: better option validation for stash save
1 parent 65210e9 commit ec67f95

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/stash.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,9 @@ int git_stash_save(
666666
uint32_t flags)
667667
{
668668
git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT;
669+
670+
GIT_ASSERT_ARG(stasher);
671+
669672
opts.stasher = stasher;
670673
opts.message = message;
671674
opts.flags = flags;
@@ -680,21 +683,26 @@ int git_stash_save_with_opts(
680683
git_buf msg = GIT_BUF_INIT;
681684
git_tree *tree = NULL;
682685
git_reference *head = NULL;
686+
bool has_paths = false;
687+
683688
int error;
684689

685690
GIT_ASSERT_ARG(out);
686691
GIT_ASSERT_ARG(repo);
692+
GIT_ASSERT_ARG(opts && opts->stasher);
693+
694+
has_paths = opts->paths.count > 0;
687695

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

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

694-
if (opts->paths.count == 0 &&
702+
if (!has_paths &&
695703
(error = ensure_there_are_changes_to_stash(repo, opts->flags)) < 0)
696704
goto cleanup;
697-
else if (opts->paths.count > 0 &&
705+
else if (has_paths &&
698706
(error = ensure_there_are_changes_to_stash_paths(
699707
repo, opts->flags, &opts->paths)) < 0)
700708
goto cleanup;
@@ -714,7 +722,7 @@ int git_stash_save_with_opts(
714722
if ((error = prepare_worktree_commit_message(&msg, opts->message)) < 0)
715723
goto cleanup;
716724

717-
if (opts->paths.count == 0) {
725+
if (!has_paths) {
718726
if ((error = commit_worktree(out, repo, opts->stasher, git_buf_cstr(&msg),
719727
i_commit, b_commit, u_commit)) < 0)
720728
goto cleanup;
@@ -755,9 +763,12 @@ int git_stash_save_with_opts(
755763
git_commit_free(b_commit);
756764
git_commit_free(u_commit);
757765
git_tree_free(tree);
758-
git_reference_free(head);
759-
git_index_free(index);
760-
git_index_free(paths_index);
766+
767+
if (has_paths) {
768+
git_reference_free(head);
769+
git_index_free(index);
770+
git_index_free(paths_index);
771+
}
761772

762773
return error;
763774
}

0 commit comments

Comments
 (0)