2020-10-29 20:32:13 +00:00
|
|
|
/*
|
2023-11-24 11:10:31 +00:00
|
|
|
* "git replay" builtin command
|
2020-10-29 20:32:13 +00:00
|
|
|
*/
|
|
|
|
|
2022-11-19 13:07:37 +00:00
|
|
|
#define USE_THE_INDEX_VARIABLE
|
2023-11-24 11:10:31 +00:00
|
|
|
#include "git-compat-util.h"
|
|
|
|
|
|
|
|
#include "builtin.h"
|
2020-10-29 20:32:13 +00:00
|
|
|
#include "cache-tree.h"
|
|
|
|
#include "commit.h"
|
2023-03-21 06:26:03 +00:00
|
|
|
#include "environment.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2023-05-16 06:34:00 +00:00
|
|
|
#include "hash.h"
|
2023-02-24 00:09:27 +00:00
|
|
|
#include "hex.h"
|
2020-10-29 20:32:13 +00:00
|
|
|
#include "lockfile.h"
|
|
|
|
#include "merge-ort.h"
|
2023-04-11 07:41:49 +00:00
|
|
|
#include "object-name.h"
|
2023-11-24 11:10:32 +00:00
|
|
|
#include "parse-options.h"
|
2020-10-29 20:32:13 +00:00
|
|
|
#include "refs.h"
|
|
|
|
#include "revision.h"
|
|
|
|
#include "sequencer.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2020-10-29 20:32:13 +00:00
|
|
|
#include "strvec.h"
|
2023-11-24 11:10:31 +00:00
|
|
|
#include <oidset.h>
|
|
|
|
#include <tree.h>
|
2020-10-29 20:32:13 +00:00
|
|
|
|
|
|
|
static const char *short_commit_name(struct commit *commit)
|
|
|
|
{
|
2023-03-28 13:58:46 +00:00
|
|
|
return repo_find_unique_abbrev(the_repository, &commit->object.oid,
|
|
|
|
DEFAULT_ABBREV);
|
2020-10-29 20:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct commit *peel_committish(const char *name)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
struct object_id oid;
|
|
|
|
|
2023-03-28 13:58:46 +00:00
|
|
|
if (repo_get_oid(the_repository, name, &oid))
|
2020-10-29 20:32:13 +00:00
|
|
|
return NULL;
|
|
|
|
obj = parse_object(the_repository, &oid);
|
2023-03-28 13:58:46 +00:00
|
|
|
return (struct commit *)repo_peel_to_type(the_repository, name, 0, obj,
|
|
|
|
OBJ_COMMIT);
|
2020-10-29 20:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_author(const char *message)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *a;
|
|
|
|
|
|
|
|
a = find_commit_header(message, "author", &len);
|
|
|
|
if (a)
|
|
|
|
return xmemdupz(a, len);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct commit *create_commit(struct tree *tree,
|
|
|
|
struct commit *based_on,
|
|
|
|
struct commit *parent)
|
|
|
|
{
|
|
|
|
struct object_id ret;
|
|
|
|
struct object *obj;
|
|
|
|
struct commit_list *parents = NULL;
|
|
|
|
char *author;
|
|
|
|
char *sign_commit = NULL;
|
|
|
|
struct commit_extra_header *extra;
|
|
|
|
struct strbuf msg = STRBUF_INIT;
|
|
|
|
const char *out_enc = get_commit_output_encoding();
|
2023-03-28 13:58:48 +00:00
|
|
|
const char *message = repo_logmsg_reencode(the_repository, based_on,
|
|
|
|
NULL, out_enc);
|
2020-10-29 20:32:13 +00:00
|
|
|
const char *orig_message = NULL;
|
|
|
|
const char *exclude_gpgsig[] = { "gpgsig", NULL };
|
|
|
|
|
|
|
|
commit_list_insert(parent, &parents);
|
|
|
|
extra = read_commit_extra_headers(based_on, exclude_gpgsig);
|
|
|
|
find_commit_subject(message, &orig_message);
|
|
|
|
strbuf_addstr(&msg, orig_message);
|
|
|
|
author = get_author(message);
|
|
|
|
reset_ident_date();
|
|
|
|
if (commit_tree_extended(msg.buf, msg.len, &tree->object.oid, parents,
|
|
|
|
&ret, author, NULL, sign_commit, extra)) {
|
|
|
|
error(_("failed to write commit object"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free(author);
|
|
|
|
strbuf_release(&msg);
|
|
|
|
|
|
|
|
obj = parse_object(the_repository, &ret);
|
|
|
|
return (struct commit *)obj;
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:10:31 +00:00
|
|
|
int cmd_replay(int argc, const char **argv, const char *prefix)
|
2020-10-29 20:32:13 +00:00
|
|
|
{
|
|
|
|
struct commit *onto;
|
2023-11-24 11:10:32 +00:00
|
|
|
const char *onto_name = NULL;
|
2020-10-29 20:32:13 +00:00
|
|
|
struct commit *last_commit = NULL, *last_picked_commit = NULL;
|
|
|
|
struct object_id head;
|
|
|
|
struct lock_file lock = LOCK_INIT;
|
|
|
|
struct strvec rev_walk_args = STRVEC_INIT;
|
|
|
|
struct rev_info revs;
|
|
|
|
struct commit *commit;
|
|
|
|
struct merge_options merge_opt;
|
|
|
|
struct tree *next_tree, *base_tree, *head_tree;
|
|
|
|
struct merge_result result;
|
|
|
|
struct strbuf reflog_msg = STRBUF_INIT;
|
|
|
|
struct strbuf branch_name = STRBUF_INIT;
|
2022-04-13 20:01:40 +00:00
|
|
|
int ret = 0;
|
2020-10-29 20:32:13 +00:00
|
|
|
|
2023-11-24 11:10:32 +00:00
|
|
|
const char * const replay_usage[] = {
|
|
|
|
N_("(EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>"),
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
struct option replay_options[] = {
|
|
|
|
OPT_STRING(0, "onto", &onto_name,
|
|
|
|
N_("revision"),
|
|
|
|
N_("replay onto given commit")),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, prefix, replay_options, replay_usage,
|
|
|
|
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
|
|
|
|
|
|
|
|
if (!onto_name) {
|
|
|
|
error(_("option --onto is mandatory"));
|
|
|
|
usage_with_options(replay_usage, replay_options);
|
2020-10-29 20:32:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:10:32 +00:00
|
|
|
if (argc != 3) {
|
|
|
|
error(_("bad number of arguments"));
|
|
|
|
usage_with_options(replay_usage, replay_options);
|
|
|
|
}
|
2020-10-29 20:32:13 +00:00
|
|
|
|
2023-11-24 11:10:32 +00:00
|
|
|
onto = peel_committish(onto_name);
|
|
|
|
strbuf_addf(&branch_name, "refs/heads/%s", argv[2]);
|
2020-10-29 20:32:13 +00:00
|
|
|
|
|
|
|
/* Sanity check */
|
2023-03-28 13:58:46 +00:00
|
|
|
if (repo_get_oid(the_repository, "HEAD", &head))
|
2020-10-29 20:32:13 +00:00
|
|
|
die(_("Cannot read HEAD"));
|
|
|
|
assert(oideq(&onto->object.oid, &head));
|
|
|
|
|
2022-11-19 13:07:35 +00:00
|
|
|
repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
|
2021-05-20 06:09:31 +00:00
|
|
|
if (repo_read_index(the_repository) < 0)
|
|
|
|
BUG("Could not read index");
|
2020-10-29 20:32:13 +00:00
|
|
|
|
2023-11-24 11:10:31 +00:00
|
|
|
repo_init_revisions(the_repository, &revs, prefix);
|
2023-11-24 11:10:32 +00:00
|
|
|
|
2020-10-29 20:32:13 +00:00
|
|
|
revs.verbose_header = 1;
|
|
|
|
revs.max_parents = 1;
|
|
|
|
revs.cherry_mark = 1;
|
|
|
|
revs.limited = 1;
|
|
|
|
revs.reverse = 1;
|
|
|
|
revs.right_only = 1;
|
|
|
|
revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
|
|
|
|
revs.topo_order = 1;
|
2023-11-24 11:10:32 +00:00
|
|
|
|
|
|
|
strvec_pushl(&rev_walk_args, "", argv[2], "--not", argv[1], NULL);
|
2020-10-29 20:32:13 +00:00
|
|
|
|
2022-04-13 20:01:40 +00:00
|
|
|
if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1) {
|
|
|
|
ret = error(_("unhandled options"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-10-29 20:32:13 +00:00
|
|
|
|
|
|
|
strvec_clear(&rev_walk_args);
|
|
|
|
|
2022-04-13 20:01:40 +00:00
|
|
|
if (prepare_revision_walk(&revs) < 0) {
|
|
|
|
ret = error(_("error preparing revisions"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-10-29 20:32:13 +00:00
|
|
|
|
|
|
|
init_merge_options(&merge_opt, the_repository);
|
|
|
|
memset(&result, 0, sizeof(result));
|
|
|
|
merge_opt.show_rename_progress = 1;
|
|
|
|
merge_opt.branch1 = "HEAD";
|
2023-03-28 13:58:48 +00:00
|
|
|
head_tree = repo_get_commit_tree(the_repository, onto);
|
2020-10-29 20:32:13 +00:00
|
|
|
result.tree = head_tree;
|
|
|
|
last_commit = onto;
|
|
|
|
while ((commit = get_revision(&revs))) {
|
|
|
|
struct commit *base;
|
|
|
|
|
|
|
|
fprintf(stderr, "Rebasing %s...\r",
|
|
|
|
oid_to_hex(&commit->object.oid));
|
2023-11-24 11:10:33 +00:00
|
|
|
|
|
|
|
if (!commit->parents)
|
|
|
|
die(_("replaying down to root commit is not supported yet!"));
|
|
|
|
if (commit->parents->next)
|
|
|
|
die(_("replaying merge commits is not supported yet!"));
|
|
|
|
|
2020-10-29 20:32:13 +00:00
|
|
|
base = commit->parents->item;
|
|
|
|
|
2023-03-28 13:58:48 +00:00
|
|
|
next_tree = repo_get_commit_tree(the_repository, commit);
|
|
|
|
base_tree = repo_get_commit_tree(the_repository, base);
|
2020-10-29 20:32:13 +00:00
|
|
|
|
|
|
|
merge_opt.branch2 = short_commit_name(commit);
|
|
|
|
merge_opt.ancestor = xstrfmt("parent of %s", merge_opt.branch2);
|
|
|
|
|
|
|
|
merge_incore_nonrecursive(&merge_opt,
|
|
|
|
base_tree,
|
|
|
|
result.tree,
|
|
|
|
next_tree,
|
|
|
|
&result);
|
|
|
|
|
|
|
|
free((char*)merge_opt.ancestor);
|
|
|
|
merge_opt.ancestor = NULL;
|
|
|
|
if (!result.clean)
|
fast-rebase: write conflict state to working tree, index, and HEAD
Previously, when fast-rebase hit a conflict, it simply aborted and left
HEAD, the index, and the working tree where they were before the
operation started. While fast-rebase does not support restarting from a
conflicted state, write the conflicted state out anyway as it gives us a
way to see what the conflicts are and write tests that check for them.
This will be important in the upcoming commits, because sequencer.c is
only superficially integrated with merge-ort.c; in particular, it calls
merge_switch_to_result() after EACH merge instead of only calling it at
the end of all the sequence of merges (or when a conflict is hit). This
not only causes needless updates to the working copy and index, but also
causes all intermediate data to be freed and tossed, preventing caching
information from one merge to the next. However, integrating
sequencer.c more deeply with merge-ort.c is a big task, and making this
small extension to fast-rebase.c provides us with a simple way to test
the edge and corner cases that we want to make sure continue working.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-05-20 06:09:32 +00:00
|
|
|
break;
|
2020-10-29 20:32:13 +00:00
|
|
|
last_picked_commit = commit;
|
|
|
|
last_commit = create_commit(result.tree, commit, last_commit);
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:10:30 +00:00
|
|
|
merge_finalize(&merge_opt, &result);
|
2020-10-29 20:32:13 +00:00
|
|
|
|
|
|
|
if (result.clean < 0)
|
|
|
|
exit(128);
|
|
|
|
|
fast-rebase: write conflict state to working tree, index, and HEAD
Previously, when fast-rebase hit a conflict, it simply aborted and left
HEAD, the index, and the working tree where they were before the
operation started. While fast-rebase does not support restarting from a
conflicted state, write the conflicted state out anyway as it gives us a
way to see what the conflicts are and write tests that check for them.
This will be important in the upcoming commits, because sequencer.c is
only superficially integrated with merge-ort.c; in particular, it calls
merge_switch_to_result() after EACH merge instead of only calling it at
the end of all the sequence of merges (or when a conflict is hit). This
not only causes needless updates to the working copy and index, but also
causes all intermediate data to be freed and tossed, preventing caching
information from one merge to the next. However, integrating
sequencer.c more deeply with merge-ort.c is a big task, and making this
small extension to fast-rebase.c provides us with a simple way to test
the edge and corner cases that we want to make sure continue working.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-05-20 06:09:32 +00:00
|
|
|
if (result.clean) {
|
|
|
|
fprintf(stderr, "\nDone.\n");
|
|
|
|
strbuf_addf(&reflog_msg, "finish rebase %s onto %s",
|
|
|
|
oid_to_hex(&last_picked_commit->object.oid),
|
|
|
|
oid_to_hex(&last_commit->object.oid));
|
|
|
|
if (update_ref(reflog_msg.buf, branch_name.buf,
|
|
|
|
&last_commit->object.oid,
|
|
|
|
&last_picked_commit->object.oid,
|
|
|
|
REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
|
2023-11-24 11:10:32 +00:00
|
|
|
error(_("could not update %s"), argv[2]);
|
|
|
|
die("Failed to update %s", argv[2]);
|
fast-rebase: write conflict state to working tree, index, and HEAD
Previously, when fast-rebase hit a conflict, it simply aborted and left
HEAD, the index, and the working tree where they were before the
operation started. While fast-rebase does not support restarting from a
conflicted state, write the conflicted state out anyway as it gives us a
way to see what the conflicts are and write tests that check for them.
This will be important in the upcoming commits, because sequencer.c is
only superficially integrated with merge-ort.c; in particular, it calls
merge_switch_to_result() after EACH merge instead of only calling it at
the end of all the sequence of merges (or when a conflict is hit). This
not only causes needless updates to the working copy and index, but also
causes all intermediate data to be freed and tossed, preventing caching
information from one merge to the next. However, integrating
sequencer.c more deeply with merge-ort.c is a big task, and making this
small extension to fast-rebase.c provides us with a simple way to test
the edge and corner cases that we want to make sure continue working.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-05-20 06:09:32 +00:00
|
|
|
}
|
|
|
|
if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0)
|
|
|
|
die(_("unable to update HEAD"));
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "\nAborting: Hit a conflict.\n");
|
|
|
|
strbuf_addf(&reflog_msg, "rebase progress up to %s",
|
|
|
|
oid_to_hex(&last_picked_commit->object.oid));
|
|
|
|
if (update_ref(reflog_msg.buf, "HEAD",
|
|
|
|
&last_commit->object.oid,
|
|
|
|
&head,
|
|
|
|
REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
|
2023-11-24 11:10:32 +00:00
|
|
|
error(_("could not update %s"), argv[2]);
|
|
|
|
die("Failed to update %s", argv[2]);
|
fast-rebase: write conflict state to working tree, index, and HEAD
Previously, when fast-rebase hit a conflict, it simply aborted and left
HEAD, the index, and the working tree where they were before the
operation started. While fast-rebase does not support restarting from a
conflicted state, write the conflicted state out anyway as it gives us a
way to see what the conflicts are and write tests that check for them.
This will be important in the upcoming commits, because sequencer.c is
only superficially integrated with merge-ort.c; in particular, it calls
merge_switch_to_result() after EACH merge instead of only calling it at
the end of all the sequence of merges (or when a conflict is hit). This
not only causes needless updates to the working copy and index, but also
causes all intermediate data to be freed and tossed, preventing caching
information from one merge to the next. However, integrating
sequencer.c more deeply with merge-ort.c is a big task, and making this
small extension to fast-rebase.c provides us with a simple way to test
the edge and corner cases that we want to make sure continue working.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-05-20 06:09:32 +00:00
|
|
|
}
|
2020-10-29 20:32:13 +00:00
|
|
|
}
|
2022-04-13 20:01:40 +00:00
|
|
|
ret = (result.clean == 0);
|
|
|
|
cleanup:
|
2022-04-13 20:01:30 +00:00
|
|
|
strbuf_release(&reflog_msg);
|
|
|
|
strbuf_release(&branch_name);
|
2022-04-13 20:01:40 +00:00
|
|
|
release_revisions(&revs);
|
|
|
|
return ret;
|
2020-10-29 20:32:13 +00:00
|
|
|
}
|