@@ -116,6 +116,10 @@ struct stash_info {
116
116
int has_u ;
117
117
};
118
118
119
+ #define STASH_INFO_INIT { \
120
+ .revision = STRBUF_INIT, \
121
+ }
122
+
119
123
static void free_stash_info (struct stash_info * info )
120
124
{
121
125
strbuf_release (& info -> revision );
@@ -157,10 +161,8 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
157
161
if (argc == 1 )
158
162
commit = argv [0 ];
159
163
160
- strbuf_init (& info -> revision , 0 );
161
164
if (!commit ) {
162
165
if (!ref_exists (ref_stash )) {
163
- free_stash_info (info );
164
166
fprintf_ln (stderr , _ ("No stash entries found." ));
165
167
return -1 ;
166
168
}
@@ -174,11 +176,8 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
174
176
175
177
revision = info -> revision .buf ;
176
178
177
- if (get_oid (revision , & info -> w_commit )) {
178
- error (_ ("%s is not a valid reference" ), revision );
179
- free_stash_info (info );
180
- return -1 ;
181
- }
179
+ if (get_oid (revision , & info -> w_commit ))
180
+ return error (_ ("%s is not a valid reference" ), revision );
182
181
183
182
assert_stash_like (info , revision );
184
183
@@ -197,7 +196,7 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
197
196
info -> is_stash_ref = !strcmp (expanded_ref , ref_stash );
198
197
break ;
199
198
default : /* Invalid or ambiguous */
200
- free_stash_info ( info ) ;
199
+ break ;
201
200
}
202
201
203
202
free (expanded_ref );
@@ -598,10 +597,10 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
598
597
599
598
static int apply_stash (int argc , const char * * argv , const char * prefix )
600
599
{
601
- int ret ;
600
+ int ret = -1 ;
602
601
int quiet = 0 ;
603
602
int index = 0 ;
604
- struct stash_info info ;
603
+ struct stash_info info = STASH_INFO_INIT ;
605
604
struct option options [] = {
606
605
OPT__QUIET (& quiet , N_ ("be quiet, only report errors" )),
607
606
OPT_BOOL (0 , "index" , & index ,
@@ -613,9 +612,10 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
613
612
git_stash_apply_usage , 0 );
614
613
615
614
if (get_stash_info (& info , argc , argv ))
616
- return -1 ;
615
+ goto cleanup ;
617
616
618
617
ret = do_apply_stash (prefix , & info , index , quiet );
618
+ cleanup :
619
619
free_stash_info (& info );
620
620
return ret ;
621
621
}
@@ -651,20 +651,25 @@ static int do_drop_stash(struct stash_info *info, int quiet)
651
651
return 0 ;
652
652
}
653
653
654
- static void assert_stash_ref (struct stash_info * info )
654
+ static int get_stash_info_assert (struct stash_info * info , int argc ,
655
+ const char * * argv )
655
656
{
656
- if (!info -> is_stash_ref ) {
657
- error (_ ("'%s' is not a stash reference" ), info -> revision .buf );
658
- free_stash_info (info );
659
- exit (1 );
660
- }
657
+ int ret = get_stash_info (info , argc , argv );
658
+
659
+ if (ret < 0 )
660
+ return ret ;
661
+
662
+ if (!info -> is_stash_ref )
663
+ return error (_ ("'%s' is not a stash reference" ), info -> revision .buf );
664
+
665
+ return 0 ;
661
666
}
662
667
663
668
static int drop_stash (int argc , const char * * argv , const char * prefix )
664
669
{
665
- int ret ;
670
+ int ret = -1 ;
666
671
int quiet = 0 ;
667
- struct stash_info info ;
672
+ struct stash_info info = STASH_INFO_INIT ;
668
673
struct option options [] = {
669
674
OPT__QUIET (& quiet , N_ ("be quiet, only report errors" )),
670
675
OPT_END ()
@@ -673,22 +678,21 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
673
678
argc = parse_options (argc , argv , prefix , options ,
674
679
git_stash_drop_usage , 0 );
675
680
676
- if (get_stash_info (& info , argc , argv ))
677
- return -1 ;
678
-
679
- assert_stash_ref (& info );
681
+ if (get_stash_info_assert (& info , argc , argv ))
682
+ goto cleanup ;
680
683
681
684
ret = do_drop_stash (& info , quiet );
685
+ cleanup :
682
686
free_stash_info (& info );
683
687
return ret ;
684
688
}
685
689
686
690
static int pop_stash (int argc , const char * * argv , const char * prefix )
687
691
{
688
- int ret ;
692
+ int ret = -1 ;
689
693
int index = 0 ;
690
694
int quiet = 0 ;
691
- struct stash_info info ;
695
+ struct stash_info info = STASH_INFO_INIT ;
692
696
struct option options [] = {
693
697
OPT__QUIET (& quiet , N_ ("be quiet, only report errors" )),
694
698
OPT_BOOL (0 , "index" , & index ,
@@ -699,25 +703,25 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
699
703
argc = parse_options (argc , argv , prefix , options ,
700
704
git_stash_pop_usage , 0 );
701
705
702
- if (get_stash_info (& info , argc , argv ))
703
- return -1 ;
706
+ if (get_stash_info_assert (& info , argc , argv ))
707
+ goto cleanup ;
704
708
705
- assert_stash_ref (& info );
706
709
if ((ret = do_apply_stash (prefix , & info , index , quiet )))
707
710
printf_ln (_ ("The stash entry is kept in case "
708
711
"you need it again." ));
709
712
else
710
713
ret = do_drop_stash (& info , quiet );
711
714
715
+ cleanup :
712
716
free_stash_info (& info );
713
717
return ret ;
714
718
}
715
719
716
720
static int branch_stash (int argc , const char * * argv , const char * prefix )
717
721
{
718
- int ret ;
722
+ int ret = -1 ;
719
723
const char * branch = NULL ;
720
- struct stash_info info ;
724
+ struct stash_info info = STASH_INFO_INIT ;
721
725
struct child_process cp = CHILD_PROCESS_INIT ;
722
726
struct option options [] = {
723
727
OPT_END ()
@@ -734,7 +738,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
734
738
branch = argv [0 ];
735
739
736
740
if (get_stash_info (& info , argc - 1 , argv + 1 ))
737
- return -1 ;
741
+ goto cleanup ;
738
742
739
743
cp .git_cmd = 1 ;
740
744
strvec_pushl (& cp .args , "checkout" , "-b" , NULL );
@@ -746,8 +750,8 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
746
750
if (!ret && info .is_stash_ref )
747
751
ret = do_drop_stash (& info , 0 );
748
752
753
+ cleanup :
749
754
free_stash_info (& info );
750
-
751
755
return ret ;
752
756
}
753
757
@@ -825,8 +829,8 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
825
829
static int show_stash (int argc , const char * * argv , const char * prefix )
826
830
{
827
831
int i ;
828
- int ret = 0 ;
829
- struct stash_info info ;
832
+ int ret = -1 ;
833
+ struct stash_info info = STASH_INFO_INIT ;
830
834
struct rev_info rev ;
831
835
struct strvec stash_args = STRVEC_INIT ;
832
836
struct strvec revision_args = STRVEC_INIT ;
@@ -844,6 +848,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
844
848
UNTRACKED_ONLY , PARSE_OPT_NONEG ),
845
849
OPT_END ()
846
850
};
851
+ int do_usage = 0 ;
847
852
848
853
init_diff_ui_defaults ();
849
854
git_config (git_diff_ui_config , NULL );
@@ -861,10 +866,8 @@ static int show_stash(int argc, const char **argv, const char *prefix)
861
866
strvec_push (& revision_args , argv [i ]);
862
867
}
863
868
864
- ret = get_stash_info (& info , stash_args .nr , stash_args .v );
865
- strvec_clear (& stash_args );
866
- if (ret )
867
- return -1 ;
869
+ if (get_stash_info (& info , stash_args .nr , stash_args .v ))
870
+ goto cleanup ;
868
871
869
872
/*
870
873
* The config settings are applied only if there are not passed
@@ -878,16 +881,14 @@ static int show_stash(int argc, const char **argv, const char *prefix)
878
881
rev .diffopt .output_format |= DIFF_FORMAT_PATCH ;
879
882
880
883
if (!show_stat && !show_patch ) {
881
- free_stash_info ( & info ) ;
882
- return 0 ;
884
+ ret = 0 ;
885
+ goto cleanup ;
883
886
}
884
887
}
885
888
886
889
argc = setup_revisions (revision_args .nr , revision_args .v , & rev , NULL );
887
- if (argc > 1 ) {
888
- free_stash_info (& info );
889
- usage_with_options (git_stash_show_usage , options );
890
- }
890
+ if (argc > 1 )
891
+ goto usage ;
891
892
if (!rev .diffopt .output_format ) {
892
893
rev .diffopt .output_format = DIFF_FORMAT_PATCH ;
893
894
diff_setup_done (& rev .diffopt );
@@ -912,8 +913,16 @@ static int show_stash(int argc, const char **argv, const char *prefix)
912
913
}
913
914
log_tree_diff_flush (& rev );
914
915
916
+ ret = diff_result_code (& rev .diffopt , 0 );
917
+ cleanup :
918
+ strvec_clear (& stash_args );
915
919
free_stash_info (& info );
916
- return diff_result_code (& rev .diffopt , 0 );
920
+ if (do_usage )
921
+ usage_with_options (git_stash_show_usage , options );
922
+ return ret ;
923
+ usage :
924
+ do_usage = 1 ;
925
+ goto cleanup ;
917
926
}
918
927
919
928
static int do_store_stash (const struct object_id * w_commit , const char * stash_msg ,
@@ -1409,9 +1418,9 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
1409
1418
1410
1419
static int create_stash (int argc , const char * * argv , const char * prefix )
1411
1420
{
1412
- int ret = 0 ;
1421
+ int ret ;
1413
1422
struct strbuf stash_msg_buf = STRBUF_INIT ;
1414
- struct stash_info info ;
1423
+ struct stash_info info = STASH_INFO_INIT ;
1415
1424
struct pathspec ps ;
1416
1425
1417
1426
/* Starting with argv[1], since argv[0] is "create" */
@@ -1426,6 +1435,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1426
1435
if (!ret )
1427
1436
printf_ln ("%s" , oid_to_hex (& info .w_commit ));
1428
1437
1438
+ free_stash_info (& info );
1429
1439
strbuf_release (& stash_msg_buf );
1430
1440
return ret ;
1431
1441
}
@@ -1434,7 +1444,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1434
1444
int keep_index , int patch_mode , int include_untracked , int only_staged )
1435
1445
{
1436
1446
int ret = 0 ;
1437
- struct stash_info info ;
1447
+ struct stash_info info = STASH_INFO_INIT ;
1438
1448
struct strbuf patch = STRBUF_INIT ;
1439
1449
struct strbuf stash_msg_buf = STRBUF_INIT ;
1440
1450
struct strbuf untracked_files = STRBUF_INIT ;
@@ -1632,6 +1642,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1632
1642
}
1633
1643
1634
1644
done :
1645
+ free_stash_info (& info );
1635
1646
strbuf_release (& stash_msg_buf );
1636
1647
return ret ;
1637
1648
}
0 commit comments