@@ -46,6 +46,8 @@ static LIST_HEAD(conf_head);
46
46
47
47
static char * separators = ":" ;
48
48
49
+ static int configured ;
50
+
49
51
#define TRAILER_ARG_STRING "$ARG"
50
52
51
53
static const char * git_generated_prefixes [] = {
@@ -546,6 +548,17 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
546
548
return 0 ;
547
549
}
548
550
551
+ static void ensure_configured (void )
552
+ {
553
+ if (configured )
554
+ return ;
555
+
556
+ /* Default config must be setup first */
557
+ git_config (git_trailer_default_config , NULL );
558
+ git_config (git_trailer_config , NULL );
559
+ configured = 1 ;
560
+ }
561
+
549
562
static const char * token_from_item (struct arg_item * item , char * tok )
550
563
{
551
564
if (item -> conf .key )
@@ -875,59 +888,43 @@ static int process_input_file(FILE *outfile,
875
888
const char * str ,
876
889
struct list_head * head )
877
890
{
878
- int patch_start , trailer_start , trailer_end ;
891
+ struct trailer_info info ;
879
892
struct strbuf tok = STRBUF_INIT ;
880
893
struct strbuf val = STRBUF_INIT ;
881
- struct trailer_item * last = NULL ;
882
- struct strbuf * trailer , * * trailer_lines , * * ptr ;
894
+ int i ;
883
895
884
- patch_start = find_patch_start (str );
885
- trailer_end = find_trailer_end (str , patch_start );
886
- trailer_start = find_trailer_start (str , trailer_end );
896
+ trailer_info_get (& info , str );
887
897
888
898
/* Print lines before the trailers as is */
889
- fwrite (str , 1 , trailer_start , outfile );
899
+ fwrite (str , 1 , info . trailer_start - str , outfile );
890
900
891
- if (!ends_with_blank_line ( str , trailer_start ) )
901
+ if (!info . blank_line_before_trailer )
892
902
fprintf (outfile , "\n" );
893
903
894
- /* Parse trailer lines */
895
- trailer_lines = strbuf_split_buf (str + trailer_start ,
896
- trailer_end - trailer_start ,
897
- '\n' ,
898
- 0 );
899
- for (ptr = trailer_lines ; * ptr ; ptr ++ ) {
904
+ for (i = 0 ; i < info .trailer_nr ; i ++ ) {
900
905
int separator_pos ;
901
- trailer = * ptr ;
902
- if (trailer -> buf [0 ] == comment_line_char )
903
- continue ;
904
- if (last && isspace (trailer -> buf [0 ])) {
905
- struct strbuf sb = STRBUF_INIT ;
906
- strbuf_addf (& sb , "%s\n%s" , last -> value , trailer -> buf );
907
- strbuf_strip_suffix (& sb , "\n" );
908
- free (last -> value );
909
- last -> value = strbuf_detach (& sb , NULL );
906
+ char * trailer = info .trailers [i ];
907
+ if (trailer [0 ] == comment_line_char )
910
908
continue ;
911
- }
912
- separator_pos = find_separator (trailer -> buf , separators );
909
+ separator_pos = find_separator (trailer , separators );
913
910
if (separator_pos >= 1 ) {
914
- parse_trailer (& tok , & val , NULL , trailer -> buf ,
911
+ parse_trailer (& tok , & val , NULL , trailer ,
915
912
separator_pos );
916
- last = add_trailer_item (head ,
917
- strbuf_detach (& tok , NULL ),
918
- strbuf_detach (& val , NULL ));
913
+ add_trailer_item (head ,
914
+ strbuf_detach (& tok , NULL ),
915
+ strbuf_detach (& val , NULL ));
919
916
} else {
920
- strbuf_addbuf (& val , trailer );
917
+ strbuf_addstr (& val , trailer );
921
918
strbuf_strip_suffix (& val , "\n" );
922
919
add_trailer_item (head ,
923
920
NULL ,
924
921
strbuf_detach (& val , NULL ));
925
- last = NULL ;
926
922
}
927
923
}
928
- strbuf_list_free (trailer_lines );
929
924
930
- return trailer_end ;
925
+ trailer_info_release (& info );
926
+
927
+ return info .trailer_end - str ;
931
928
}
932
929
933
930
static void free_all (struct list_head * head )
@@ -978,9 +975,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
978
975
int trailer_end ;
979
976
FILE * outfile = stdout ;
980
977
981
- /* Default config must be setup first */
982
- git_config (git_trailer_default_config , NULL );
983
- git_config (git_trailer_config , NULL );
978
+ ensure_configured ();
984
979
985
980
read_input_file (& sb , file );
986
981
@@ -1007,3 +1002,54 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
1007
1002
1008
1003
strbuf_release (& sb );
1009
1004
}
1005
+
1006
+ void trailer_info_get (struct trailer_info * info , const char * str )
1007
+ {
1008
+ int patch_start , trailer_end , trailer_start ;
1009
+ struct strbuf * * trailer_lines , * * ptr ;
1010
+ char * * trailer_strings = NULL ;
1011
+ size_t nr = 0 , alloc = 0 ;
1012
+ char * * last = NULL ;
1013
+
1014
+ ensure_configured ();
1015
+
1016
+ patch_start = find_patch_start (str );
1017
+ trailer_end = find_trailer_end (str , patch_start );
1018
+ trailer_start = find_trailer_start (str , trailer_end );
1019
+
1020
+ trailer_lines = strbuf_split_buf (str + trailer_start ,
1021
+ trailer_end - trailer_start ,
1022
+ '\n' ,
1023
+ 0 );
1024
+ for (ptr = trailer_lines ; * ptr ; ptr ++ ) {
1025
+ if (last && isspace ((* ptr )-> buf [0 ])) {
1026
+ struct strbuf sb = STRBUF_INIT ;
1027
+ strbuf_attach (& sb , * last , strlen (* last ), strlen (* last ));
1028
+ strbuf_addbuf (& sb , * ptr );
1029
+ * last = strbuf_detach (& sb , NULL );
1030
+ continue ;
1031
+ }
1032
+ ALLOC_GROW (trailer_strings , nr + 1 , alloc );
1033
+ trailer_strings [nr ] = strbuf_detach (* ptr , NULL );
1034
+ last = find_separator (trailer_strings [nr ], separators ) >= 1
1035
+ ? & trailer_strings [nr ]
1036
+ : NULL ;
1037
+ nr ++ ;
1038
+ }
1039
+ strbuf_list_free (trailer_lines );
1040
+
1041
+ info -> blank_line_before_trailer = ends_with_blank_line (str ,
1042
+ trailer_start );
1043
+ info -> trailer_start = str + trailer_start ;
1044
+ info -> trailer_end = str + trailer_end ;
1045
+ info -> trailers = trailer_strings ;
1046
+ info -> trailer_nr = nr ;
1047
+ }
1048
+
1049
+ void trailer_info_release (struct trailer_info * info )
1050
+ {
1051
+ int i ;
1052
+ for (i = 0 ; i < info -> trailer_nr ; i ++ )
1053
+ free (info -> trailers [i ]);
1054
+ free (info -> trailers );
1055
+ }
0 commit comments