Skip to content

Commit 88b291f

Browse files
pyokagangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 13b97ea commit 88b291f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

builtin/am.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "merge-recursive.h"
2424
#include "revision.h"
2525
#include "log-tree.h"
26+
#include "notes-utils.h"
2627

2728
/**
2829
* 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)
478479
return ret;
479480
}
480481

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+
481540
/**
482541
* Determines if the file looks like a piece of RFC2822 mail by grabbing all
483542
* non-indented lines and checking if they look like they begin with valid
@@ -1405,6 +1464,7 @@ static void am_run(struct am_state *state, int resume)
14051464

14061465
if (!is_empty_file(am_path(state, "rewritten"))) {
14071466
assert(state->rebasing);
1467+
copy_notes_for_rebase(state);
14081468
run_post_rewrite_hook(state);
14091469
}
14101470

0 commit comments

Comments
 (0)