@@ -666,6 +666,9 @@ int git_stash_save(
666
666
uint32_t flags )
667
667
{
668
668
git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT ;
669
+
670
+ GIT_ASSERT_ARG (stasher );
671
+
669
672
opts .stasher = stasher ;
670
673
opts .message = message ;
671
674
opts .flags = flags ;
@@ -680,21 +683,26 @@ int git_stash_save_with_opts(
680
683
git_buf msg = GIT_BUF_INIT ;
681
684
git_tree * tree = NULL ;
682
685
git_reference * head = NULL ;
686
+ bool has_paths = false;
687
+
683
688
int error ;
684
689
685
690
GIT_ASSERT_ARG (out );
686
691
GIT_ASSERT_ARG (repo );
692
+ GIT_ASSERT_ARG (opts && opts -> stasher );
693
+
694
+ has_paths = opts -> paths .count > 0 ;
687
695
688
696
if ((error = git_repository__ensure_not_bare (repo , "stash save" )) < 0 )
689
697
return error ;
690
698
691
699
if ((error = retrieve_base_commit_and_message (& b_commit , & msg , repo )) < 0 )
692
700
goto cleanup ;
693
701
694
- if (opts -> paths . count == 0 &&
702
+ if (! has_paths &&
695
703
(error = ensure_there_are_changes_to_stash (repo , opts -> flags )) < 0 )
696
704
goto cleanup ;
697
- else if (opts -> paths . count > 0 &&
705
+ else if (has_paths &&
698
706
(error = ensure_there_are_changes_to_stash_paths (
699
707
repo , opts -> flags , & opts -> paths )) < 0 )
700
708
goto cleanup ;
@@ -714,7 +722,7 @@ int git_stash_save_with_opts(
714
722
if ((error = prepare_worktree_commit_message (& msg , opts -> message )) < 0 )
715
723
goto cleanup ;
716
724
717
- if (opts -> paths . count == 0 ) {
725
+ if (! has_paths ) {
718
726
if ((error = commit_worktree (out , repo , opts -> stasher , git_buf_cstr (& msg ),
719
727
i_commit , b_commit , u_commit )) < 0 )
720
728
goto cleanup ;
@@ -755,9 +763,12 @@ int git_stash_save_with_opts(
755
763
git_commit_free (b_commit );
756
764
git_commit_free (u_commit );
757
765
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
+ }
761
772
762
773
return error ;
763
774
}
0 commit comments