Skip to content

Commit 67915cf

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 74ece4c commit 67915cf

File tree

4 files changed

+713
-0
lines changed

4 files changed

+713
-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,13 +38,74 @@ 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 message of the new commit will be asked for by launching the
50+
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

4566
include::config/sequencer.adoc[]
4667

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

builtin/history.c

Lines changed: 218 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,
@@ -323,18 +329,230 @@ static int cmd_history_reword(int argc,
323329
return ret;
324330
}
325331

332+
static int split_commit(struct repository *repo,
333+
struct commit *original_commit,
334+
struct pathspec *pathspec,
335+
struct object_id *out)
336+
{
337+
struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;
338+
struct strbuf index_file = STRBUF_INIT, split_message = STRBUF_INIT;
339+
struct child_process read_tree_cmd = CHILD_PROCESS_INIT;
340+
struct index_state index = INDEX_STATE_INIT(repo);
341+
struct object_id original_commit_tree_oid, parent_tree_oid;
342+
const char *original_message, *original_body, *ptr;
343+
char original_commit_oid[GIT_MAX_HEXSZ + 1];
344+
char *original_author = NULL;
345+
struct commit_list *parents = NULL;
346+
struct commit *first_commit;
347+
struct tree *split_tree;
348+
size_t len;
349+
int ret;
350+
351+
if (original_commit->parents)
352+
parent_tree_oid = *get_commit_tree_oid(original_commit->parents->item);
353+
else
354+
oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
355+
original_commit_tree_oid = *get_commit_tree_oid(original_commit);
356+
357+
/*
358+
* Construct the first commit. This is done by taking the original
359+
* commit parent's tree and selectively patching changes from the diff
360+
* between that parent and its child.
361+
*/
362+
repo_git_path_replace(repo, &index_file, "%s", "history-split.index");
363+
364+
read_tree_cmd.git_cmd = 1;
365+
strvec_pushf(&read_tree_cmd.env, "GIT_INDEX_FILE=%s", index_file.buf);
366+
strvec_push(&read_tree_cmd.args, "read-tree");
367+
strvec_push(&read_tree_cmd.args, oid_to_hex(&parent_tree_oid));
368+
ret = run_command(&read_tree_cmd);
369+
if (ret < 0)
370+
goto out;
371+
372+
ret = read_index_from(&index, index_file.buf, repo->gitdir);
373+
if (ret < 0) {
374+
ret = error(_("failed reading temporary index"));
375+
goto out;
376+
}
377+
378+
oid_to_hex_r(original_commit_oid, &original_commit->object.oid);
379+
ret = run_add_p_index(repo, &index, index_file.buf, &interactive_opts,
380+
original_commit_oid, pathspec);
381+
if (ret < 0)
382+
goto out;
383+
384+
split_tree = write_in_core_index_as_tree(repo, &index);
385+
if (!split_tree) {
386+
ret = error(_("failed split tree"));
387+
goto out;
388+
}
389+
390+
unlink(index_file.buf);
391+
392+
/*
393+
* We disallow the cases where either the split-out commit or the
394+
* original commit would become empty. Consequently, if we see that the
395+
* new tree ID matches either of those trees we abort.
396+
*/
397+
if (oideq(&split_tree->object.oid, &parent_tree_oid)) {
398+
ret = error(_("split commit is empty"));
399+
goto out;
400+
} else if (oideq(&split_tree->object.oid, &original_commit_tree_oid)) {
401+
ret = error(_("split commit tree matches original commit"));
402+
goto out;
403+
}
404+
405+
/* We retain authorship of the original commit. */
406+
original_message = repo_logmsg_reencode(repo, original_commit, NULL, NULL);
407+
ptr = find_commit_header(original_message, "author", &len);
408+
if (ptr)
409+
original_author = xmemdupz(ptr, len);
410+
411+
ret = fill_commit_message(repo, &parent_tree_oid, &split_tree->object.oid,
412+
"", "split-out", &split_message);
413+
if (ret < 0)
414+
goto out;
415+
416+
ret = commit_tree(split_message.buf, split_message.len, &split_tree->object.oid,
417+
original_commit->parents, &out[0], original_author, NULL);
418+
if (ret < 0) {
419+
ret = error(_("failed writing split-out commit"));
420+
goto out;
421+
}
422+
423+
/*
424+
* The second commit is much simpler to construct, as we can simply use
425+
* the original commit details, except that we adjust its parent to be
426+
* the newly split-out commit.
427+
*/
428+
find_commit_subject(original_message, &original_body);
429+
first_commit = lookup_commit_reference(repo, &out[0]);
430+
commit_list_append(first_commit, &parents);
431+
432+
ret = commit_tree(original_body, strlen(original_body), &original_commit_tree_oid,
433+
parents, &out[1], original_author, NULL);
434+
if (ret < 0) {
435+
ret = error(_("failed writing second commit"));
436+
goto out;
437+
}
438+
439+
ret = 0;
440+
441+
out:
442+
if (index_file.len)
443+
unlink(index_file.buf);
444+
strbuf_release(&split_message);
445+
strbuf_release(&index_file);
446+
free_commit_list(parents);
447+
free(original_author);
448+
release_index(&index);
449+
return ret;
450+
}
451+
452+
static int cmd_history_split(int argc,
453+
const char **argv,
454+
const char *prefix,
455+
struct repository *repo)
456+
{
457+
const char * const usage[] = {
458+
GIT_HISTORY_SPLIT_USAGE,
459+
NULL,
460+
};
461+
struct option options[] = {
462+
OPT_END(),
463+
};
464+
struct oidmap rewritten_commits = OIDMAP_INIT;
465+
struct commit *original_commit, *parent, *head;
466+
struct strvec commits = STRVEC_INIT;
467+
struct commit_list *from_list = NULL;
468+
struct object_id split_commits[2];
469+
struct pathspec pathspec = { 0 };
470+
int ret;
471+
472+
argc = parse_options(argc, argv, prefix, options, usage, 0);
473+
if (argc < 1) {
474+
ret = error(_("command expects a revision"));
475+
goto out;
476+
}
477+
repo_config(repo, git_default_config, NULL);
478+
479+
original_commit = lookup_commit_reference_by_name(argv[0]);
480+
if (!original_commit) {
481+
ret = error(_("commit to be split cannot be found: %s"), argv[0]);
482+
goto out;
483+
}
484+
485+
parent = original_commit->parents ? original_commit->parents->item : NULL;
486+
if (parent && repo_parse_commit(repo, parent)) {
487+
ret = error(_("unable to parse commit %s"),
488+
oid_to_hex(&parent->object.oid));
489+
goto out;
490+
}
491+
492+
head = lookup_commit_reference_by_name("HEAD");
493+
if (!head) {
494+
ret = error(_("could not resolve HEAD to a commit"));
495+
goto out;
496+
}
497+
498+
commit_list_append(original_commit, &from_list);
499+
if (!repo_is_descendant_of(repo, head, from_list)) {
500+
ret = error(_("split commit must be reachable from current HEAD commit"));
501+
goto out;
502+
}
503+
504+
parse_pathspec(&pathspec, 0,
505+
PATHSPEC_PREFER_FULL | PATHSPEC_SYMLINK_LEADING_PATH | PATHSPEC_PREFIX_ORIGIN,
506+
prefix, argv + 1);
507+
508+
/*
509+
* Collect the list of commits that we'll have to reapply now already.
510+
* This ensures that we'll abort early on in case the range of commits
511+
* contains merges, which we do not yet handle.
512+
*/
513+
ret = collect_commits(repo, parent, head, &commits);
514+
if (ret < 0)
515+
goto out;
516+
517+
/*
518+
* Then we split up the commit and replace the original commit with the
519+
* new ones.
520+
*/
521+
ret = split_commit(repo, original_commit, &pathspec, split_commits);
522+
if (ret < 0)
523+
goto out;
524+
525+
replace_commits(&commits, &original_commit->object.oid,
526+
split_commits, ARRAY_SIZE(split_commits));
527+
528+
ret = apply_commits(repo, &commits, parent, head, "split");
529+
if (ret < 0)
530+
goto out;
531+
532+
ret = 0;
533+
534+
out:
535+
oidmap_clear(&rewritten_commits, 0);
536+
free_commit_list(from_list);
537+
clear_pathspec(&pathspec);
538+
strvec_clear(&commits);
539+
return ret;
540+
}
541+
326542
int cmd_history(int argc,
327543
const char **argv,
328544
const char *prefix,
329545
struct repository *repo)
330546
{
331547
const char * const usage[] = {
332548
GIT_HISTORY_REWORD_USAGE,
549+
GIT_HISTORY_SPLIT_USAGE,
333550
NULL,
334551
};
335552
parse_opt_subcommand_fn *fn = NULL;
336553
struct option options[] = {
337554
OPT_SUBCOMMAND("reword", &fn, cmd_history_reword),
555+
OPT_SUBCOMMAND("split", &fn, cmd_history_split),
338556
OPT_END(),
339557
};
340558

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ integration_tests = [
386386
't3438-rebase-broken-files.sh',
387387
't3450-history.sh',
388388
't3451-history-reword.sh',
389+
't3452-history-split.sh',
389390
't3500-cherry.sh',
390391
't3501-revert-cherry-pick.sh',
391392
't3502-cherry-pick-merge.sh',

0 commit comments

Comments
 (0)