Skip to content

Commit d37c42e

Browse files
pks-tgitster
authored andcommitted
builtin/history: implement "split" subcommand
It is quite a common use case that one wants to split up one commit into multiple commits by moving parts of the changes of the original commit out into a separate commit. This is quite an involved operation though: 1. Identify the commit in question that is to be dropped. 2. Perform an interactive rebase on top of that commit's parent. 3. Modify the instruction sheet to "edit" the commit that is to be split up. 4. Drop the commit via "git reset HEAD~". 5. Stage changes that should go into the first commit and commit it. 6. Stage changes that should go into the second commit and commit it. 7. Finalize the rebase. This is quite complex, and overall I would claim that most people who are not experts in Git would struggle with this flow. Introduce a new "split" subcommand for git-history(1) to make this way easier. All the user needs to do is to say `git history split $COMMIT`. From hereon, Git asks the user which parts of the commit shall be moved out into a separate commit and, once done, asks the user for the commit message. Git then creates that split-out commit and applies the original commit on top of it. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fba7552 commit d37c42e

File tree

4 files changed

+689
-0
lines changed

4 files changed

+689
-0
lines changed

Documentation/git-history.adoc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SYNOPSIS
99
--------
1010
[synopsis]
1111
git history reword <commit>
12+
git history split <commit> [--] [<pathspec>...]
1213

1314
DESCRIPTION
1415
-----------
@@ -37,11 +38,72 @@ Several commands are available to rewrite history in different ways:
3738
details of this commit remain unchanged. This command will spawn an
3839
editor with the current message of that commit.
3940

41+
`split <commit> [--] [<pathspec>...]`::
42+
Interactively split up <commit> into two commits by choosing
43+
hunks introduced by it that will be moved into the new split-out
44+
commit. These hunks will then be written into a new commit that
45+
becomes the parent of the previous commit. The original commit
46+
stays intact, except that its parent will be the newly split-out
47+
commit.
48+
+
49+
The commit messages of the split-up commits will be asked for by launching
50+
the configured editor. Authorship of the commit will be the same as for the
51+
original commit.
52+
+
53+
If passed, _<pathspec>_ can be used to limit which changes shall be split out
54+
of the original commit. Files not matching any of the pathspecs will remain
55+
part of the original commit. For more details, see the 'pathspec' entry in
56+
linkgit:gitglossary[7].
57+
+
58+
It is invalid to select either all or no hunks, as that would lead to
59+
one of the commits becoming empty.
60+
4061
CONFIGURATION
4162
-------------
4263

4364
include::includes/cmd-config-section-all.adoc[]
4465

66+
EXAMPLES
67+
--------
68+
69+
Split a commit
70+
~~~~~~~~~~~~~~
71+
72+
----------
73+
$ git log --stat --oneline
74+
3f81232 (HEAD -> main) original
75+
bar | 1 +
76+
foo | 1 +
77+
2 files changed, 2 insertions(+)
78+
79+
$ git history split HEAD
80+
diff --git a/bar b/bar
81+
new file mode 100644
82+
index 0000000..5716ca5
83+
--- /dev/null
84+
+++ b/bar
85+
@@ -0,0 +1 @@
86+
+bar
87+
(1/1) Stage addition [y,n,q,a,d,e,p,?]? y
88+
89+
diff --git a/foo b/foo
90+
new file mode 100644
91+
index 0000000..257cc56
92+
--- /dev/null
93+
+++ b/foo
94+
@@ -0,0 +1 @@
95+
+foo
96+
(1/1) Stage addition [y,n,q,a,d,e,p,?]? n
97+
98+
$ git log --stat --oneline
99+
7cebe64 (HEAD -> main) original
100+
foo | 1 +
101+
1 file changed, 1 insertion(+)
102+
d1582f3 split-out commit
103+
bar | 1 +
104+
1 file changed, 1 insertion(+)
105+
----------
106+
45107
GIT
46108
---
47109
Part of the linkgit:git[1] suite

builtin/history.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
#define USE_THE_REPOSITORY_VARIABLE
22

33
#include "builtin.h"
4+
#include "cache-tree.h"
45
#include "commit-reach.h"
56
#include "commit.h"
67
#include "config.h"
78
#include "editor.h"
89
#include "environment.h"
910
#include "gettext.h"
1011
#include "hex.h"
12+
#include "oidmap.h"
1113
#include "parse-options.h"
14+
#include "path.h"
15+
#include "read-cache.h"
1216
#include "refs.h"
1317
#include "replay.h"
1418
#include "reset.h"
1519
#include "revision.h"
20+
#include "run-command.h"
1621
#include "sequencer.h"
1722
#include "strvec.h"
1823
#include "tree.h"
1924
#include "wt-status.h"
2025

2126
#define GIT_HISTORY_REWORD_USAGE N_("git history reword <commit>")
27+
#define GIT_HISTORY_SPLIT_USAGE N_("git history split <commit> [--] [<pathspec>...]")
2228

2329
static int collect_commits(struct repository *repo,
2430
struct commit *old_commit,
@@ -364,18 +370,186 @@ static int cmd_history_reword(int argc,
364370
return ret;
365371
}
366372

373+
static int split_commit(struct repository *repo,
374+
struct commit *original_commit,
375+
struct pathspec *pathspec,
376+
struct object_id *out)
377+
{
378+
struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;
379+
struct strbuf index_file = STRBUF_INIT;
380+
struct child_process read_tree_cmd = CHILD_PROCESS_INIT;
381+
struct index_state index = INDEX_STATE_INIT(repo);
382+
struct object_id original_commit_tree_oid, parent_tree_oid;
383+
char original_commit_oid[GIT_MAX_HEXSZ + 1];
384+
struct commit_list *parents = NULL;
385+
struct commit *first_commit;
386+
struct tree *split_tree;
387+
int ret;
388+
389+
if (original_commit->parents)
390+
parent_tree_oid = *get_commit_tree_oid(original_commit->parents->item);
391+
else
392+
oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
393+
original_commit_tree_oid = *get_commit_tree_oid(original_commit);
394+
395+
/*
396+
* Construct the first commit. This is done by taking the original
397+
* commit parent's tree and selectively patching changes from the diff
398+
* between that parent and its child.
399+
*/
400+
repo_git_path_replace(repo, &index_file, "%s", "history-split.index");
401+
402+
read_tree_cmd.git_cmd = 1;
403+
strvec_pushf(&read_tree_cmd.env, "GIT_INDEX_FILE=%s", index_file.buf);
404+
strvec_push(&read_tree_cmd.args, "read-tree");
405+
strvec_push(&read_tree_cmd.args, oid_to_hex(&parent_tree_oid));
406+
ret = run_command(&read_tree_cmd);
407+
if (ret < 0)
408+
goto out;
409+
410+
ret = read_index_from(&index, index_file.buf, repo->gitdir);
411+
if (ret < 0) {
412+
ret = error(_("failed reading temporary index"));
413+
goto out;
414+
}
415+
416+
oid_to_hex_r(original_commit_oid, &original_commit->object.oid);
417+
ret = run_add_p_index(repo, &index, index_file.buf, &interactive_opts,
418+
original_commit_oid, pathspec, ADD_P_DISALLOW_EDIT);
419+
if (ret < 0)
420+
goto out;
421+
422+
split_tree = write_in_core_index_as_tree(repo, &index);
423+
if (!split_tree) {
424+
ret = error(_("failed split tree"));
425+
goto out;
426+
}
427+
428+
unlink(index_file.buf);
429+
430+
/*
431+
* We disallow the cases where either the split-out commit or the
432+
* original commit would become empty. Consequently, if we see that the
433+
* new tree ID matches either of those trees we abort.
434+
*/
435+
if (oideq(&split_tree->object.oid, &parent_tree_oid)) {
436+
ret = error(_("split commit is empty"));
437+
goto out;
438+
} else if (oideq(&split_tree->object.oid, &original_commit_tree_oid)) {
439+
ret = error(_("split commit tree matches original commit"));
440+
goto out;
441+
}
442+
443+
/*
444+
* The first commit is constructed from the split-out tree. The base
445+
* that shall be diffed against is the parent of the original commit.
446+
*/
447+
ret = commit_tree_with_edited_message(repo, "split-out", original_commit,
448+
&split_tree->object.oid,
449+
original_commit->parents, &parent_tree_oid, &out[0]);
450+
if (ret < 0) {
451+
ret = error(_("failed writing split-out commit"));
452+
goto out;
453+
}
454+
455+
/*
456+
* The second commit is constructed from the original tree. The base to
457+
* diff against and the parent in this case is the first split-out
458+
* commit.
459+
*/
460+
first_commit = lookup_commit_reference(repo, &out[0]);
461+
commit_list_append(first_commit, &parents);
462+
463+
ret = commit_tree_with_edited_message(repo, "split-out", original_commit,
464+
&original_commit_tree_oid,
465+
parents, get_commit_tree_oid(first_commit), &out[1]);
466+
if (ret < 0) {
467+
ret = error(_("failed writing split-out commit"));
468+
goto out;
469+
}
470+
471+
ret = 0;
472+
473+
out:
474+
if (index_file.len)
475+
unlink(index_file.buf);
476+
strbuf_release(&index_file);
477+
free_commit_list(parents);
478+
release_index(&index);
479+
return ret;
480+
}
481+
482+
static int cmd_history_split(int argc,
483+
const char **argv,
484+
const char *prefix,
485+
struct repository *repo)
486+
{
487+
const char * const usage[] = {
488+
GIT_HISTORY_SPLIT_USAGE,
489+
NULL,
490+
};
491+
struct option options[] = {
492+
OPT_END(),
493+
};
494+
struct commit *original_commit, *parent, *head;
495+
struct strvec commits = STRVEC_INIT;
496+
struct object_id split_commits[2];
497+
struct pathspec pathspec = { 0 };
498+
int ret;
499+
500+
argc = parse_options(argc, argv, prefix, options, usage, 0);
501+
if (argc < 1) {
502+
ret = error(_("command expects a revision"));
503+
goto out;
504+
}
505+
repo_config(repo, git_default_config, NULL);
506+
507+
parse_pathspec(&pathspec, 0,
508+
PATHSPEC_PREFER_FULL | PATHSPEC_SYMLINK_LEADING_PATH | PATHSPEC_PREFIX_ORIGIN,
509+
prefix, argv + 1);
510+
511+
ret = gather_commits_between_head_and_revision(repo, argv[0], &original_commit,
512+
&parent, &head, &commits);
513+
if (ret < 0)
514+
goto out;
515+
516+
/*
517+
* Then we split up the commit and replace the original commit with the
518+
* new ones.
519+
*/
520+
ret = split_commit(repo, original_commit, &pathspec, split_commits);
521+
if (ret < 0)
522+
goto out;
523+
524+
replace_commits(&commits, &original_commit->object.oid,
525+
split_commits, ARRAY_SIZE(split_commits));
526+
527+
ret = apply_commits(repo, &commits, parent, head, "split");
528+
if (ret < 0)
529+
goto out;
530+
531+
ret = 0;
532+
533+
out:
534+
clear_pathspec(&pathspec);
535+
strvec_clear(&commits);
536+
return ret;
537+
}
538+
367539
int cmd_history(int argc,
368540
const char **argv,
369541
const char *prefix,
370542
struct repository *repo)
371543
{
372544
const char * const usage[] = {
373545
GIT_HISTORY_REWORD_USAGE,
546+
GIT_HISTORY_SPLIT_USAGE,
374547
NULL,
375548
};
376549
parse_opt_subcommand_fn *fn = NULL;
377550
struct option options[] = {
378551
OPT_SUBCOMMAND("reword", &fn, cmd_history_reword),
552+
OPT_SUBCOMMAND("split", &fn, cmd_history_split),
379553
OPT_END(),
380554
};
381555

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ integration_tests = [
388388
't3438-rebase-broken-files.sh',
389389
't3450-history.sh',
390390
't3451-history-reword.sh',
391+
't3452-history-split.sh',
391392
't3500-cherry.sh',
392393
't3501-revert-cherry-pick.sh',
393394
't3502-cherry-pick-merge.sh',

0 commit comments

Comments
 (0)