|
18 | 18 | #include "parse-options.h" |
19 | 19 | #include "string-list.h" |
20 | 20 | #include "notes-merge.h" |
21 | | - |
22 | | -static void commit_notes(struct notes_tree *t, const char *msg); |
23 | | -static combine_notes_fn parse_combine_notes_fn(const char *v); |
| 21 | +#include "notes-utils.h" |
24 | 22 |
|
25 | 23 | static const char * const git_notes_usage[] = { |
26 | 24 | N_("git notes [--ref <notes_ref>] [list [<object>]]"), |
@@ -287,139 +285,13 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset |
287 | 285 | return parse_reuse_arg(opt, arg, unset); |
288 | 286 | } |
289 | 287 |
|
290 | | -static void commit_notes(struct notes_tree *t, const char *msg) |
291 | | -{ |
292 | | - struct strbuf buf = STRBUF_INIT; |
293 | | - unsigned char commit_sha1[20]; |
294 | | - |
295 | | - if (!t) |
296 | | - t = &default_notes_tree; |
297 | | - if (!t->initialized || !t->ref || !*t->ref) |
298 | | - die(_("Cannot commit uninitialized/unreferenced notes tree")); |
299 | | - if (!t->dirty) |
300 | | - return; /* don't have to commit an unchanged tree */ |
301 | | - |
302 | | - /* Prepare commit message and reflog message */ |
303 | | - strbuf_addstr(&buf, msg); |
304 | | - if (buf.buf[buf.len - 1] != '\n') |
305 | | - strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */ |
306 | | - |
307 | | - create_notes_commit(t, NULL, &buf, commit_sha1); |
308 | | - strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */ |
309 | | - update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR); |
310 | | - |
311 | | - strbuf_release(&buf); |
312 | | -} |
313 | | - |
314 | | -static combine_notes_fn parse_combine_notes_fn(const char *v) |
315 | | -{ |
316 | | - if (!strcasecmp(v, "overwrite")) |
317 | | - return combine_notes_overwrite; |
318 | | - else if (!strcasecmp(v, "ignore")) |
319 | | - return combine_notes_ignore; |
320 | | - else if (!strcasecmp(v, "concatenate")) |
321 | | - return combine_notes_concatenate; |
322 | | - else if (!strcasecmp(v, "cat_sort_uniq")) |
323 | | - return combine_notes_cat_sort_uniq; |
324 | | - else |
325 | | - return NULL; |
326 | | -} |
327 | | - |
328 | | -static int notes_rewrite_config(const char *k, const char *v, void *cb) |
329 | | -{ |
330 | | - struct notes_rewrite_cfg *c = cb; |
331 | | - if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) { |
332 | | - c->enabled = git_config_bool(k, v); |
333 | | - return 0; |
334 | | - } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) { |
335 | | - if (!v) |
336 | | - config_error_nonbool(k); |
337 | | - c->combine = parse_combine_notes_fn(v); |
338 | | - if (!c->combine) { |
339 | | - error(_("Bad notes.rewriteMode value: '%s'"), v); |
340 | | - return 1; |
341 | | - } |
342 | | - return 0; |
343 | | - } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) { |
344 | | - /* note that a refs/ prefix is implied in the |
345 | | - * underlying for_each_glob_ref */ |
346 | | - if (!prefixcmp(v, "refs/notes/")) |
347 | | - string_list_add_refs_by_glob(c->refs, v); |
348 | | - else |
349 | | - warning(_("Refusing to rewrite notes in %s" |
350 | | - " (outside of refs/notes/)"), v); |
351 | | - return 0; |
352 | | - } |
353 | | - |
354 | | - return 0; |
355 | | -} |
356 | | - |
357 | | - |
358 | | -struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) |
359 | | -{ |
360 | | - struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg)); |
361 | | - const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT); |
362 | | - const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT); |
363 | | - c->cmd = cmd; |
364 | | - c->enabled = 1; |
365 | | - c->combine = combine_notes_concatenate; |
366 | | - c->refs = xcalloc(1, sizeof(struct string_list)); |
367 | | - c->refs->strdup_strings = 1; |
368 | | - c->refs_from_env = 0; |
369 | | - c->mode_from_env = 0; |
370 | | - if (rewrite_mode_env) { |
371 | | - c->mode_from_env = 1; |
372 | | - c->combine = parse_combine_notes_fn(rewrite_mode_env); |
373 | | - if (!c->combine) |
374 | | - /* TRANSLATORS: The first %s is the name of the |
375 | | - environment variable, the second %s is its value */ |
376 | | - error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT, |
377 | | - rewrite_mode_env); |
378 | | - } |
379 | | - if (rewrite_refs_env) { |
380 | | - c->refs_from_env = 1; |
381 | | - string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env); |
382 | | - } |
383 | | - git_config(notes_rewrite_config, c); |
384 | | - if (!c->enabled || !c->refs->nr) { |
385 | | - string_list_clear(c->refs, 0); |
386 | | - free(c->refs); |
387 | | - free(c); |
388 | | - return NULL; |
389 | | - } |
390 | | - c->trees = load_notes_trees(c->refs); |
391 | | - string_list_clear(c->refs, 0); |
392 | | - free(c->refs); |
393 | | - return c; |
394 | | -} |
395 | | - |
396 | | -int copy_note_for_rewrite(struct notes_rewrite_cfg *c, |
397 | | - const unsigned char *from_obj, const unsigned char *to_obj) |
398 | | -{ |
399 | | - int ret = 0; |
400 | | - int i; |
401 | | - for (i = 0; c->trees[i]; i++) |
402 | | - ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; |
403 | | - return ret; |
404 | | -} |
405 | | - |
406 | | -void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c) |
407 | | -{ |
408 | | - int i; |
409 | | - for (i = 0; c->trees[i]; i++) { |
410 | | - commit_notes(c->trees[i], "Notes added by 'git notes copy'"); |
411 | | - free_notes(c->trees[i]); |
412 | | - } |
413 | | - free(c->trees); |
414 | | - free(c); |
415 | | -} |
416 | | - |
417 | 288 | static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
418 | 289 | { |
419 | 290 | struct strbuf buf = STRBUF_INIT; |
420 | 291 | struct notes_rewrite_cfg *c = NULL; |
421 | 292 | struct notes_tree *t = NULL; |
422 | 293 | int ret = 0; |
| 294 | + const char *msg = "Notes added by 'git notes copy'"; |
423 | 295 |
|
424 | 296 | if (rewrite_cmd) { |
425 | 297 | c = init_copy_notes_for_rewrite(rewrite_cmd); |
@@ -461,10 +333,10 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
461 | 333 | } |
462 | 334 |
|
463 | 335 | if (!rewrite_cmd) { |
464 | | - commit_notes(t, "Notes added by 'git notes copy'"); |
| 336 | + commit_notes(t, msg); |
465 | 337 | free_notes(t); |
466 | 338 | } else { |
467 | | - finish_copy_notes_for_rewrite(c); |
| 339 | + finish_copy_notes_for_rewrite(c, msg); |
468 | 340 | } |
469 | 341 | return ret; |
470 | 342 | } |
|
0 commit comments