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,7 +146,7 @@ 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 ;
188
151
struct strbuf sb = STRBUF_INIT ;
189
152
@@ -213,12 +176,10 @@ static void shortlog(const char *name, unsigned char *sha1,
213
176
strbuf_ltrim (& sb );
214
177
215
178
if (!sb .len )
216
- append_to_list (& subjects , xstrdup (sha1_to_hex (
217
- commit -> object .sha1 )),
218
- NULL );
179
+ string_list_append (sha1_to_hex (commit -> object .sha1 ),
180
+ & subjects );
219
181
else
220
- append_to_list (& subjects , strbuf_detach (& sb , NULL ),
221
- NULL );
182
+ string_list_append (strbuf_detach (& sb , NULL ), & subjects );
222
183
}
223
184
224
185
if (count > limit )
@@ -230,15 +191,15 @@ static void shortlog(const char *name, unsigned char *sha1,
230
191
if (i >= limit )
231
192
strbuf_addf (out , " ...\n" );
232
193
else
233
- strbuf_addf (out , " %s\n" , subjects .list [i ]);
194
+ strbuf_addf (out , " %s\n" , subjects .items [i ]. string );
234
195
235
196
clear_commit_marks ((struct commit * )branch , flags );
236
197
clear_commit_marks (head , flags );
237
198
free_commit_list (rev -> commits );
238
199
rev -> commits = NULL ;
239
200
rev -> pending .nr = 0 ;
240
201
241
- free_list (& subjects );
202
+ string_list_clear (& subjects , 0 );
242
203
}
243
204
244
205
int fmt_merge_msg (int merge_summary , struct strbuf * in , struct strbuf * out ) {
@@ -273,14 +234,14 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
273
234
274
235
strbuf_addstr (out , "Merge " );
275
236
for (i = 0 ; i < srcs .nr ; i ++ ) {
276
- struct src_data * src_data = srcs .payload [i ];
237
+ struct src_data * src_data = srcs .items [i ]. util ;
277
238
const char * subsep = "" ;
278
239
279
240
strbuf_addstr (out , sep );
280
241
sep = "; " ;
281
242
282
243
if (src_data -> head_status == 1 ) {
283
- strbuf_addstr (out , srcs .list [i ]);
244
+ strbuf_addstr (out , srcs .items [i ]. string );
284
245
continue ;
285
246
}
286
247
if (src_data -> head_status == 3 ) {
@@ -309,8 +270,8 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
309
270
print_joined ("commit " , "commits " , & src_data -> generic ,
310
271
out );
311
272
}
312
- if (strcmp ("." , srcs .list [i ]))
313
- 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 );
314
275
}
315
276
316
277
if (!strcmp ("master" , current_branch ))
@@ -329,7 +290,7 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
329
290
rev .limited = 1 ;
330
291
331
292
for (i = 0 ; i < origins .nr ; i ++ )
332
- shortlog (origins .list [i ], origins .payload [i ],
293
+ shortlog (origins .items [i ]. string , origins .items [i ]. util ,
333
294
head , & rev , limit , out );
334
295
}
335
296
return 0 ;
0 commit comments