diff mbox series

[RFC,16/35] update: add --rebase mode

Message ID 20210705123209.1808663-17-felipe.contreras@gmail.com (mailing list archive)
State New, archived
Headers show
Series git update: fix broken git pull | expand

Commit Message

Felipe Contreras July 5, 2021, 12:31 p.m. UTC
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/git-update.txt |  4 ++++
 builtin/update.c             | 20 +++++++++++++++++++-
 t/t5563-update.sh            | 17 +++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/Documentation/git-update.txt b/Documentation/git-update.txt
index 075653c6fd..0fefda1379 100644
--- a/Documentation/git-update.txt
+++ b/Documentation/git-update.txt
@@ -33,6 +33,10 @@  OPTIONS
 --merge::
 	Forces a merge.
 
+-r::
+--rebase::
+	Forces a rebase.
+
 SEE ALSO
 --------
 linkgit:git-fetch[1], linkgit:git-fast-forward[1],
diff --git a/builtin/update.c b/builtin/update.c
index 94e83532a8..989104421e 100644
--- a/builtin/update.c
+++ b/builtin/update.c
@@ -9,7 +9,8 @@ 
 
 enum update_mode_type {
 	UPDATE_MODE_FAST_FORWARD = 0,
-	UPDATE_MODE_MERGE
+	UPDATE_MODE_MERGE,
+	UPDATE_MODE_REBASE
 };
 
 static enum update_mode_type mode = UPDATE_MODE_FAST_FORWARD;
@@ -26,6 +27,9 @@  static struct option update_options[] = {
 	OPT_SET_INT_F('m', "merge", &mode,
 		N_("incorporate changes by merging"),
 		UPDATE_MODE_MERGE, PARSE_OPT_NONEG),
+	OPT_SET_INT_F('r', "rebase", &mode,
+		N_("incorporate changes by rebasing"),
+		UPDATE_MODE_REBASE, PARSE_OPT_NONEG),
 
 	OPT_END()
 };
@@ -66,6 +70,18 @@  static int run_merge(void)
 	return ret;
 }
 
+static int run_rebase(void)
+{
+	int ret;
+	struct strvec args = STRVEC_INIT;
+
+	strvec_pushl(&args, "rebase", "FETCH_HEAD", NULL);
+
+	ret = run_command_v_opt(args.v, RUN_GIT_CMD);
+	strvec_clear(&args);
+	return ret;
+}
+
 int cmd_update(int argc, const char **argv, const char *prefix)
 {
 	if (!getenv("GIT_REFLOG_ACTION"))
@@ -86,6 +102,8 @@  int cmd_update(int argc, const char **argv, const char *prefix)
 		return run_fast_forward();
 	if (mode == UPDATE_MODE_MERGE)
 		return run_merge();
+	if (mode == UPDATE_MODE_REBASE)
+		return run_rebase();
 
 	return 1;
 }
diff --git a/t/t5563-update.sh b/t/t5563-update.sh
index aabbf5a965..73e67f9783 100755
--- a/t/t5563-update.sh
+++ b/t/t5563-update.sh
@@ -67,4 +67,21 @@  test_expect_success 'git update non-fast-forward with merge' '
 	)
 '
 
+test_expect_success 'git update non-fast-forward with rebase' '
+	test_when_finished "rm -rf test" &&
+	(
+	git clone . test &&
+	cd test &&
+	git checkout -b other master^ &&
+	>new &&
+	git add new &&
+	git commit -m new &&
+	git checkout -b test -t other &&
+	git reset --hard master &&
+	git update --rebase &&
+	test_cmp_rev @^ other &&
+	test_must_fail git rev-parse --verify -q @^2
+	)
+'
+
 test_done