4
4
#include "diff.h"
5
5
#include "revision.h"
6
6
#include "tag.h"
7
+ #include "string-list.h"
7
8
8
9
static const char * const fmt_merge_msg_usage [] = {
9
10
"git fmt-merge-msg [--log|--no-log] [--file <file>]" ,
@@ -24,65 +25,29 @@ static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
24
25
return 0 ;
25
26
}
26
27
27
- struct list {
28
- char * * list ;
29
- void * * payload ;
30
- unsigned nr , alloc ;
28
+ struct src_data {
29
+ struct string_list branch , tag , r_branch , generic ;
30
+ int head_status ;
31
31
};
32
32
33
- static void append_to_list (struct list * list , char * value , void * payload )
34
- {
35
- if (list -> nr == list -> alloc ) {
36
- list -> alloc += 32 ;
37
- list -> list = xrealloc (list -> list , sizeof (char * ) * list -> alloc );
38
- list -> payload = xrealloc (list -> payload ,
39
- sizeof (char * ) * list -> alloc );
40
- }
41
- list -> payload [list -> nr ] = payload ;
42
- list -> list [list -> nr ++ ] = value ;
43
- }
44
-
45
- static int find_in_list (struct list * list , char * value )
46
- {
47
- int i ;
48
-
49
- for (i = 0 ; i < list -> nr ; i ++ )
50
- if (!strcmp (list -> list [i ], value ))
51
- return i ;
52
-
53
- return -1 ;
54
- }
55
-
56
- static void free_list (struct list * list )
33
+ void init_src_data (struct src_data * data )
57
34
{
58
- int i ;
59
-
60
- if (list -> alloc == 0 )
61
- return ;
62
-
63
- for (i = 0 ; i < list -> nr ; i ++ ) {
64
- free (list -> list [i ]);
65
- free (list -> payload [i ]);
66
- }
67
- free (list -> list );
68
- free (list -> payload );
69
- list -> nr = list -> alloc = 0 ;
35
+ data -> branch .strdup_strings = 1 ;
36
+ data -> tag .strdup_strings = 1 ;
37
+ data -> r_branch .strdup_strings = 1 ;
38
+ data -> generic .strdup_strings = 1 ;
70
39
}
71
40
72
- struct src_data {
73
- struct list branch , tag , r_branch , generic ;
74
- int head_status ;
75
- };
76
-
77
- static struct list srcs = { NULL , NULL , 0 , 0 };
78
- static struct list origins = { NULL , NULL , 0 , 0 };
41
+ static struct string_list srcs = { NULL , 0 , 0 , 1 };
42
+ static struct string_list origins = { NULL , 0 , 0 , 1 };
79
43
80
44
static int handle_line (char * line )
81
45
{
82
46
int i , len = strlen (line );
83
47
unsigned char * sha1 ;
84
48
char * src , * origin ;
85
49
struct src_data * src_data ;
50
+ struct string_list_item * item ;
86
51
int pulling_head = 0 ;
87
52
88
53
if (len < 43 || line [40 ] != '\t' )
@@ -115,64 +80,62 @@ static int handle_line(char *line)
115
80
pulling_head = 1 ;
116
81
}
117
82
118
- i = find_in_list (& srcs , src );
119
- if (i < 0 ) {
120
- i = srcs . nr ;
121
- append_to_list ( & srcs , xstrdup ( src ),
122
- xcalloc ( 1 , sizeof ( struct src_data )) );
83
+ item = unsorted_string_list_lookup (& srcs , src );
84
+ if (! item ) {
85
+ item = string_list_append ( src , & srcs ) ;
86
+ item -> util = xcalloc ( 1 , sizeof ( struct src_data ));
87
+ init_src_data ( item -> util );
123
88
}
124
- src_data = srcs . payload [ i ] ;
89
+ src_data = item -> util ;
125
90
126
91
if (pulling_head ) {
127
- origin = xstrdup ( src ) ;
92
+ origin = src ;
128
93
src_data -> head_status |= 1 ;
129
94
} else if (!prefixcmp (line , "branch " )) {
130
- origin = xstrdup ( line + 7 ) ;
131
- append_to_list ( & src_data -> branch , origin , NULL );
95
+ origin = line + 7 ;
96
+ string_list_append ( origin , & src_data -> branch );
132
97
src_data -> head_status |= 2 ;
133
98
} else if (!prefixcmp (line , "tag " )) {
134
99
origin = line ;
135
- append_to_list ( & src_data -> tag , xstrdup ( origin + 4 ), NULL );
100
+ string_list_append ( origin + 4 , & src_data -> tag );
136
101
src_data -> head_status |= 2 ;
137
102
} else if (!prefixcmp (line , "remote branch " )) {
138
- origin = xstrdup ( line + 14 ) ;
139
- append_to_list ( & src_data -> r_branch , origin , NULL );
103
+ origin = line + 14 ;
104
+ string_list_append ( origin , & src_data -> r_branch );
140
105
src_data -> head_status |= 2 ;
141
106
} else {
142
- origin = xstrdup ( src ) ;
143
- append_to_list ( & src_data -> generic , xstrdup ( line ), NULL );
107
+ origin = src ;
108
+ string_list_append ( line , & src_data -> generic );
144
109
src_data -> head_status |= 2 ;
145
110
}
146
111
147
112
if (!strcmp ("." , src ) || !strcmp (src , origin )) {
148
113
int len = strlen (origin );
149
- if (origin [0 ] == '\'' && origin [len - 1 ] == '\'' ) {
114
+ if (origin [0 ] == '\'' && origin [len - 1 ] == '\'' )
150
115
origin = xmemdupz (origin + 1 , len - 2 );
151
- } else {
152
- origin = xstrdup (origin );
153
- }
154
116
} else {
155
117
char * new_origin = xmalloc (strlen (origin ) + strlen (src ) + 5 );
156
118
sprintf (new_origin , "%s of %s" , origin , src );
157
119
origin = new_origin ;
158
120
}
159
- append_to_list ( & origins , origin , sha1 ) ;
121
+ string_list_append ( origin , & origins ) -> util = sha1 ;
160
122
return 0 ;
161
123
}
162
124
163
125
static void print_joined (const char * singular , const char * plural ,
164
- struct list * list , struct strbuf * out )
126
+ struct string_list * list , struct strbuf * out )
165
127
{
166
128
if (list -> nr == 0 )
167
129
return ;
168
130
if (list -> nr == 1 ) {
169
- strbuf_addf (out , "%s%s" , singular , list -> list [0 ]);
131
+ strbuf_addf (out , "%s%s" , singular , list -> items [0 ]. string );
170
132
} else {
171
133
int i ;
172
134
strbuf_addstr (out , plural );
173
135
for (i = 0 ; i < list -> nr - 1 ; i ++ )
174
- strbuf_addf (out , "%s%s" , i > 0 ? ", " : "" , list -> list [i ]);
175
- strbuf_addf (out , " and %s" , list -> list [list -> nr - 1 ]);
136
+ strbuf_addf (out , "%s%s" , i > 0 ? ", " : "" ,
137
+ list -> items [i ].string );
138
+ strbuf_addf (out , " and %s" , list -> items [list -> nr - 1 ].string );
176
139
}
177
140
}
178
141
@@ -183,8 +146,9 @@ static void shortlog(const char *name, unsigned char *sha1,
183
146
int i , count = 0 ;
184
147
struct commit * commit ;
185
148
struct object * branch ;
186
- struct list subjects = { NULL , NULL , 0 , 0 };
149
+ struct string_list subjects = { NULL , 0 , 0 , 1 };
187
150
int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED ;
151
+ struct strbuf sb = STRBUF_INIT ;
188
152
189
153
branch = deref_tag (parse_object (sha1 ), sha1_to_hex (sha1 ), 40 );
190
154
if (!branch || branch -> type != OBJ_COMMIT )
@@ -198,7 +162,7 @@ static void shortlog(const char *name, unsigned char *sha1,
198
162
if (prepare_revision_walk (rev ))
199
163
die ("revision walk setup failed" );
200
164
while ((commit = get_revision (rev )) != NULL ) {
201
- char * oneline , * bol , * eol ;
165
+ struct pretty_print_context ctx = { 0 } ;
202
166
203
167
/* ignore merges */
204
168
if (commit -> parents && commit -> parents -> next )
@@ -208,30 +172,14 @@ static void shortlog(const char *name, unsigned char *sha1,
208
172
if (subjects .nr > limit )
209
173
continue ;
210
174
211
- bol = strstr (commit -> buffer , "\n\n" );
212
- if (bol ) {
213
- unsigned char c ;
214
- do {
215
- c = * ++ bol ;
216
- } while (isspace (c ));
217
- if (!c )
218
- bol = NULL ;
219
- }
220
-
221
- if (!bol ) {
222
- append_to_list (& subjects , xstrdup (sha1_to_hex (
223
- commit -> object .sha1 )),
224
- NULL );
225
- continue ;
226
- }
175
+ format_commit_message (commit , "%s" , & sb , & ctx );
176
+ strbuf_ltrim (& sb );
227
177
228
- eol = strchr (bol , '\n' );
229
- if (eol ) {
230
- oneline = xmemdupz (bol , eol - bol );
231
- } else {
232
- oneline = xstrdup (bol );
233
- }
234
- append_to_list (& subjects , oneline , NULL );
178
+ if (!sb .len )
179
+ string_list_append (sha1_to_hex (commit -> object .sha1 ),
180
+ & subjects );
181
+ else
182
+ string_list_append (strbuf_detach (& sb , NULL ), & subjects );
235
183
}
236
184
237
185
if (count > limit )
@@ -243,15 +191,15 @@ static void shortlog(const char *name, unsigned char *sha1,
243
191
if (i >= limit )
244
192
strbuf_addf (out , " ...\n" );
245
193
else
246
- strbuf_addf (out , " %s\n" , subjects .list [i ]);
194
+ strbuf_addf (out , " %s\n" , subjects .items [i ]. string );
247
195
248
196
clear_commit_marks ((struct commit * )branch , flags );
249
197
clear_commit_marks (head , flags );
250
198
free_commit_list (rev -> commits );
251
199
rev -> commits = NULL ;
252
200
rev -> pending .nr = 0 ;
253
201
254
- free_list (& subjects );
202
+ string_list_clear (& subjects , 0 );
255
203
}
256
204
257
205
int fmt_merge_msg (int merge_summary , struct strbuf * in , struct strbuf * out ) {
@@ -281,16 +229,19 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
281
229
die ("Error in line %d: %.*s" , i , len , p );
282
230
}
283
231
232
+ if (!srcs .nr )
233
+ return 0 ;
234
+
284
235
strbuf_addstr (out , "Merge " );
285
236
for (i = 0 ; i < srcs .nr ; i ++ ) {
286
- struct src_data * src_data = srcs .payload [i ];
237
+ struct src_data * src_data = srcs .items [i ]. util ;
287
238
const char * subsep = "" ;
288
239
289
240
strbuf_addstr (out , sep );
290
241
sep = "; " ;
291
242
292
243
if (src_data -> head_status == 1 ) {
293
- strbuf_addstr (out , srcs .list [i ]);
244
+ strbuf_addstr (out , srcs .items [i ]. string );
294
245
continue ;
295
246
}
296
247
if (src_data -> head_status == 3 ) {
@@ -319,8 +270,8 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
319
270
print_joined ("commit " , "commits " , & src_data -> generic ,
320
271
out );
321
272
}
322
- if (strcmp ("." , srcs .list [i ]))
323
- strbuf_addf (out , " of %s" , srcs .list [i ]);
273
+ if (strcmp ("." , srcs .items [i ]. string ))
274
+ strbuf_addf (out , " of %s" , srcs .items [i ]. string );
324
275
}
325
276
326
277
if (!strcmp ("master" , current_branch ))
@@ -339,7 +290,7 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
339
290
rev .limited = 1 ;
340
291
341
292
for (i = 0 ; i < origins .nr ; i ++ )
342
- shortlog (origins .list [i ], origins .payload [i ],
293
+ shortlog (origins .items [i ]. string , origins .items [i ]. util ,
343
294
head , & rev , limit , out );
344
295
}
345
296
return 0 ;
@@ -350,7 +301,9 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
350
301
const char * inpath = NULL ;
351
302
struct option options [] = {
352
303
OPT_BOOLEAN (0 , "log" , & merge_summary , "populate log with the shortlog" ),
353
- OPT_BOOLEAN (0 , "summary" , & merge_summary , "alias for --log" ),
304
+ { OPTION_BOOLEAN , 0 , "summary" , & merge_summary , NULL ,
305
+ "alias for --log (deprecated)" ,
306
+ PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
354
307
OPT_FILENAME ('F' , "file" , & inpath , "file to read from" ),
355
308
OPT_END ()
356
309
};
0 commit comments