|
9 | 9 | #include "commit.h" |
10 | 10 | #include "trailer.h" |
11 | 11 | #include "list.h" |
| 12 | +#include "wrapper.h" |
| 13 | + |
12 | 14 | /* |
13 | 15 | * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org> |
14 | 16 | */ |
@@ -1224,18 +1226,66 @@ void trailer_iterator_release(struct trailer_iterator *iter) |
1224 | 1226 | strbuf_release(&iter->key); |
1225 | 1227 | } |
1226 | 1228 |
|
1227 | | -int amend_file_with_trailers(const char *path, const struct strvec *trailer_args) |
| 1229 | +static int amend_strbuf_with_trailers(struct strbuf *buf, |
| 1230 | + const struct strvec *trailer_args) |
1228 | 1231 | { |
1229 | | - struct child_process run_trailer = CHILD_PROCESS_INIT; |
1230 | | - |
1231 | | - run_trailer.git_cmd = 1; |
1232 | | - strvec_pushl(&run_trailer.args, "interpret-trailers", |
1233 | | - "--in-place", "--no-divider", |
1234 | | - path, NULL); |
1235 | | - strvec_pushv(&run_trailer.args, trailer_args->v); |
1236 | | - return run_command(&run_trailer); |
| 1232 | + struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
| 1233 | + LIST_HEAD(new_trailer_head); |
| 1234 | + struct strbuf out = STRBUF_INIT; |
| 1235 | + size_t i; |
| 1236 | + |
| 1237 | + opts.no_divider = 1; |
| 1238 | + |
| 1239 | + for (i = 0; i < trailer_args->nr; i++) { |
| 1240 | + const char *text = trailer_args->v[i]; |
| 1241 | + struct new_trailer_item *item; |
| 1242 | + |
| 1243 | + if (!*text) |
| 1244 | + continue; |
| 1245 | + item = xcalloc(1, sizeof(*item)); |
| 1246 | + INIT_LIST_HEAD(&item->list); |
| 1247 | + item->text = text; |
| 1248 | + list_add_tail(&item->list, &new_trailer_head); |
| 1249 | + } |
| 1250 | + |
| 1251 | + process_trailers(&opts, &new_trailer_head, buf, &out); |
| 1252 | + |
| 1253 | + strbuf_swap(buf, &out); |
| 1254 | + strbuf_release(&out); |
| 1255 | + while (!list_empty(&new_trailer_head)) { |
| 1256 | + struct new_trailer_item *item = |
| 1257 | + list_first_entry(&new_trailer_head, struct new_trailer_item, list); |
| 1258 | + list_del(&item->list); |
| 1259 | + free(item); |
| 1260 | + } |
| 1261 | + return 0; |
1237 | 1262 | } |
1238 | 1263 |
|
| 1264 | +int amend_file_with_trailers(const char *path, |
| 1265 | + const struct strvec *trailer_args) |
| 1266 | +{ |
| 1267 | + struct strbuf buf = STRBUF_INIT; |
| 1268 | + |
| 1269 | + if (!trailer_args || !trailer_args->nr) |
| 1270 | + return 0; |
| 1271 | + |
| 1272 | + if (strbuf_read_file(&buf, path, 0) < 0) |
| 1273 | + return error_errno("could not read '%s'", path); |
| 1274 | + |
| 1275 | + if (amend_strbuf_with_trailers(&buf, trailer_args)) { |
| 1276 | + strbuf_release(&buf); |
| 1277 | + return error("failed to append trailers"); |
| 1278 | + } |
| 1279 | + |
| 1280 | + if (write_file_buf_gently(path, buf.buf, buf.len)) { |
| 1281 | + strbuf_release(&buf); |
| 1282 | + return -1; |
| 1283 | + } |
| 1284 | + |
| 1285 | + strbuf_release(&buf); |
| 1286 | + return 0; |
| 1287 | + } |
| 1288 | + |
1239 | 1289 | void process_trailers(const struct process_trailer_options *opts, |
1240 | 1290 | struct list_head *new_trailer_head, |
1241 | 1291 | struct strbuf *sb, struct strbuf *out) |
|
0 commit comments