From: sxenos@google.com
To: git@vger.kernel.org
Cc: Stefan Xenos <sxenos@google.com>
Subject: [PATCH v5 7/8] evolve: implement the git change command
Date: Thu, 14 Feb 2019 20:31:04 -0800 [thread overview]
Message-ID: <20190215043105.163688-7-sxenos@google.com> (raw)
In-Reply-To: <20190215043105.163688-1-sxenos@google.com>
From: Stefan Xenos <sxenos@google.com>
Implement the git change update command, which
are sufficient for constructing change graphs.
For example, to create a new change (a stable name) that refers to HEAD:
git change update -c HEAD
To record a rebase or amend in the change graph:
git change update -c <new_commit> -r <old_commit>
To record a cherry-pick in the change graph:
git change update -c <new_commit> -o <original_commit>
Signed-off-by: Stefan Xenos <sxenos@google.com>
---
.gitignore | 1 +
Makefile | 1 +
builtin.h | 1 +
builtin/change.c | 195 +++++++++++++++++++++++++++++++++++++++++++++++
git.c | 1 +
5 files changed, 199 insertions(+)
create mode 100644 builtin/change.c
diff --git a/.gitignore b/.gitignore
index 7374587f9d..6c1384e4cb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,6 +27,7 @@
/git-branch
/git-bundle
/git-cat-file
+/git-change
/git-check-attr
/git-check-ignore
/git-check-mailmap
diff --git a/Makefile b/Makefile
index 38f2592ec6..ad65b4f8fd 100644
--- a/Makefile
+++ b/Makefile
@@ -1055,6 +1055,7 @@ BUILTIN_OBJS += builtin/blame.o
BUILTIN_OBJS += builtin/branch.o
BUILTIN_OBJS += builtin/bundle.o
BUILTIN_OBJS += builtin/cat-file.o
+BUILTIN_OBJS += builtin/change.o
BUILTIN_OBJS += builtin/check-attr.o
BUILTIN_OBJS += builtin/check-ignore.o
BUILTIN_OBJS += builtin/check-mailmap.o
diff --git a/builtin.h b/builtin.h
index 6538932e99..d2d39d9da8 100644
--- a/builtin.h
+++ b/builtin.h
@@ -137,6 +137,7 @@ extern int cmd_blame(int argc, const char **argv, const char *prefix);
extern int cmd_branch(int argc, const char **argv, const char *prefix);
extern int cmd_bundle(int argc, const char **argv, const char *prefix);
extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
+extern int cmd_change(int argc, const char **argv, const char *prefix);
extern int cmd_checkout(int argc, const char **argv, const char *prefix);
extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
diff --git a/builtin/change.c b/builtin/change.c
new file mode 100644
index 0000000000..807d148805
--- /dev/null
+++ b/builtin/change.c
@@ -0,0 +1,195 @@
+#include "builtin.h"
+#include "ref-filter.h"
+#include "parse-options.h"
+#include "metacommit.h"
+#include "change-table.h"
+#include "config.h"
+
+static const char * const builtin_change_usage[] = {
+ N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
+ NULL
+};
+
+static const char * const builtin_update_usage[] = {
+ N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
+ NULL
+};
+
+struct update_state {
+ int options;
+ const char* change;
+ const char* content;
+ struct string_list replace;
+ struct string_list origin;
+};
+
+static void init_update_state(struct update_state *state)
+{
+ memset(state, 0, sizeof(*state));
+ state->content = "HEAD";
+ string_list_init(&state->replace, 0);
+ string_list_init(&state->origin, 0);
+}
+
+static void clear_update_state(struct update_state *state)
+{
+ string_list_clear(&state->replace, 0);
+ string_list_clear(&state->origin, 0);
+}
+
+static int update_option_parse_replace(const struct option *opt,
+ const char *arg, int unset)
+{
+ struct update_state *state = opt->value;
+ string_list_append(&state->replace, arg);
+ return 0;
+}
+
+static int update_option_parse_origin(const struct option *opt,
+ const char *arg, int unset)
+{
+ struct update_state *state = opt->value;
+ string_list_append(&state->origin, arg);
+ return 0;
+}
+
+static int resolve_commit(const char *committish, struct object_id *result)
+{
+ struct commit *commit;
+ if (get_oid_committish(committish, result))
+ die(_("Failed to resolve '%s' as a valid revision."), committish);
+ commit = lookup_commit_reference(the_repository, result);
+ if (!commit)
+ die(_("Could not parse object '%s'."), committish);
+ oidcpy(result, &commit->object.oid);
+ return 0;
+}
+
+static void resolve_commit_list(const struct string_list *commitsish_list,
+ struct oid_array* result)
+{
+ int i;
+ for (i = 0; i < commitsish_list->nr; i++) {
+ struct string_list_item *item = &commitsish_list->items[i];
+ struct object_id next;
+ resolve_commit(item->string, &next);
+ oid_array_append(result, &next);
+ }
+}
+
+/*
+ * Given the command-line options for the update command, fills in a
+ * metacommit_data with the corresponding changes.
+ */
+static void get_metacommit_from_command_line(
+ const struct update_state* commands, struct metacommit_data *result)
+{
+ resolve_commit(commands->content, &(result->content));
+ resolve_commit_list(&(commands->replace), &(result->replace));
+ resolve_commit_list(&(commands->origin), &(result->origin));
+}
+
+static int perform_update(
+ struct repository *repo,
+ const struct update_state *state,
+ struct strbuf *err)
+{
+ struct metacommit_data metacommit;
+ struct change_table chtable;
+ struct string_list changes;
+ int ret;
+ int i;
+
+ change_table_init(&chtable);
+ change_table_add_all_visible(&chtable, repo);
+ string_list_init(&changes, 1);
+
+ init_metacommit_data(&metacommit);
+
+ get_metacommit_from_command_line(state, &metacommit);
+
+ ret = record_metacommit_withresult(repo, &chtable, &metacommit, state->change,
+ state->options, err, &changes);
+
+ for (i = 0; i < changes.nr; i++) {
+ struct string_list_item *it = &changes.items[i];
+
+ if (it->util)
+ fprintf(stdout, N_("Updated change %s\n"), it->string);
+ else
+ fprintf(stdout, N_("Created change %s\n"), it->string);
+ }
+
+ string_list_clear(&changes, 0);
+ change_table_clear(&chtable);
+ clear_metacommit_data(&metacommit);
+
+ return ret;
+}
+
+static int change_update(int argc, const char **argv, const char* prefix)
+{
+ int result;
+ int force = 0;
+ int newchange = 0;
+ struct strbuf err = STRBUF_INIT;
+ struct update_state state;
+ struct option options[] = {
+ { OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
+ N_("marks the given commit as being obsolete"),
+ 0, update_option_parse_replace },
+ { OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
+ N_("marks the given commit as being the origin of this commit"),
+ 0, update_option_parse_origin },
+ OPT_BOOL('F', "force", &force,
+ N_("overwrite an existing change of the same name")),
+ OPT_STRING('c', "content", &state.content, N_("commit"),
+ N_("identifies the new content commit for the change")),
+ OPT_STRING('g', "change", &state.change, N_("commit"),
+ N_("name of the change to update")),
+ OPT_BOOL('n', "new", &newchange,
+ N_("create a new change - do not append to any existing change")),
+ OPT_END()
+ };
+
+ init_update_state(&state);
+
+ argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
+
+ if (force) state.options |= UPDATE_OPTION_FORCE;
+ if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;
+
+ result = perform_update(the_repository, &state, &err);
+
+ if (result < 0) {
+ error("%s", err.buf);
+ strbuf_release(&err);
+ }
+
+ clear_update_state(&state);
+
+ return result;
+}
+
+int cmd_change(int argc, const char **argv, const char *prefix)
+{
+ /* No options permitted before subcommand currently */
+ struct option options[] = {
+ OPT_END()
+ };
+ int result = 1;
+
+ argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+
+ if (argc < 1)
+ usage_with_options(builtin_change_usage, options);
+ else if (!strcmp(argv[0], "update"))
+ result = change_update(argc, argv, prefix);
+ else {
+ error(_("Unknown subcommand: %s"), argv[0]);
+ usage_with_options(builtin_change_usage, options);
+ }
+
+ return result ? 1 : 0;
+}
diff --git a/git.c b/git.c
index 2dd588674f..33e3a04395 100644
--- a/git.c
+++ b/git.c
@@ -453,6 +453,7 @@ static struct cmd_struct commands[] = {
{ "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG },
{ "bundle", cmd_bundle, RUN_SETUP_GENTLY | NO_PARSEOPT },
{ "cat-file", cmd_cat_file, RUN_SETUP },
+ { "change", cmd_change, RUN_SETUP },
{ "check-attr", cmd_check_attr, RUN_SETUP },
{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
--
2.21.0.rc0.258.g878e2cd30e-goog
next prev parent reply other threads:[~2019-02-15 4:31 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-15 4:30 [PATCH v5 1/8] technical doc: add a design doc for the evolve command sxenos
2019-02-15 4:30 ` [PATCH v5 2/8] sha1-array: implement oid_array_readonly_contains sxenos
2019-02-15 4:31 ` [PATCH v5 3/8] ref-filter: add the metas namespace to ref-filter sxenos
2019-02-15 4:31 ` [PATCH v5 4/8] evolve: add support for parsing metacommits sxenos
2019-02-15 4:31 ` [PATCH v5 5/8] evolve: add the change-table structure sxenos
2019-02-15 4:31 ` [PATCH v5 6/8] evolve: add support for writing metacommits sxenos
2019-02-15 4:31 ` sxenos [this message]
2019-02-15 4:31 ` [PATCH v5 8/8] evolve: add the git change list command sxenos
2019-02-15 18:23 ` [PATCH v5 1/8] technical doc: add a design doc for the evolve command Junio C Hamano
2019-02-15 22:18 ` Stefan Xenos
2019-02-15 23:36 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190215043105.163688-7-sxenos@google.com \
--to=sxenos@google.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).