builtin-am: support automatic notes copying
Since eb2151b (rebase: support automatic notes copying, 2010-03-12), git-am.sh supported automatic notes copying in --rebasing mode by invoking "git notes copy" once it has finished applying all the patches. Re-implement this feature in builtin/am.c. Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paul Tan committed
Aug 4, 2015 at 21:51 UTC
88b291fe9db645366853fc759d497342c130fd35
1 file changed
+60
builtin/am.c
+60
@@ -23,6 +23,7 @@
23
#include "merge-recursive.h"
24
#include "revision.h"
25
#include "log-tree.h"
26
+#include "notes-utils.h"
27
28
/**
29
* Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -478,6 +479,64 @@ static int run_post_rewrite_hook(const struct am_state *state)
479
return ret;
480
}
481
482
+/**
483
+ * Reads the state directory's "rewritten" file, and copies notes from the old
484
+ * commits listed in the file to their rewritten commits.
485
+ *
486
+ * Returns 0 on success, -1 on failure.
487
+ */
488
+static int copy_notes_for_rebase(const struct am_state *state)
489
+{
490
+ struct notes_rewrite_cfg *c;
491
+ struct strbuf sb = STRBUF_INIT;
492
+ const char *invalid_line = _("Malformed input line: '%s'.");
493
+ const char *msg = "Notes added by 'git rebase'";
494
+ FILE *fp;
495
+ int ret = 0;
496
+
497
+ assert(state->rebasing);
498
+
499
+ c = init_copy_notes_for_rewrite("rebase");
500
+ if (!c)
501
+ return 0;
502
+
503
+ fp = xfopen(am_path(state, "rewritten"), "r");
504
+
505
+ while (!strbuf_getline(&sb, fp, '\n')) {
506
+ unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];
507
+
508
+ if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
509
+ ret = error(invalid_line, sb.buf);
510
+ goto finish;
511
+ }
512
+
513
+ if (get_sha1_hex(sb.buf, from_obj)) {
514
+ ret = error(invalid_line, sb.buf);
515
+ goto finish;
516
+ }
517
+
518
+ if (sb.buf[GIT_SHA1_HEXSZ] != ' ') {
519
+ ret = error(invalid_line, sb.buf);
520
+ goto finish;
521
+ }
522
+
523
+ if (get_sha1_hex(sb.buf + GIT_SHA1_HEXSZ + 1, to_obj)) {
524
+ ret = error(invalid_line, sb.buf);
525
+ goto finish;
526
+ }
527
+
528
+ if (copy_note_for_rewrite(c, from_obj, to_obj))
529
+ ret = error(_("Failed to copy notes from '%s' to '%s'"),
530
+ sha1_to_hex(from_obj), sha1_to_hex(to_obj));
531
+ }
532
+
533
+finish:
534
+ finish_copy_notes_for_rewrite(c, msg);
535
+ fclose(fp);
536
+ strbuf_release(&sb);
537
+ return ret;
538
+}
539
+
540
/**
541
* Determines if the file looks like a piece of RFC2822 mail by grabbing all
542
* non-indented lines and checking if they look like they begin with valid
@@ -1405,6 +1464,7 @@ next:
1464
1465
if (!is_empty_file(am_path(state, "rewritten"))) {
1466
assert(state->rebasing);
1467
+ copy_notes_for_rebase(state);
1468
run_post_rewrite_hook(state);
1469
}
1470