diff options
author | Koni Marti <koni.marti@gmail.com> | 2023-11-24 16:03:06 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-12-30 15:42:09 +0100 |
commit | 6592d6e711a2d909465a758259adc76edc5ae3e6 (patch) | |
tree | 2316f4e69cf1b005af010ea177e86f5c90a09198 /commands/patch/rebase_test.go | |
parent | c711fe1cab738fdc1e9c12c601d80d6bf5892c9e (diff) | |
download | aerc-6592d6e711a2d909465a758259adc76edc5ae3e6.tar.gz |
patch/rebase: add rebase sub-cmd
Implement the :patch rebase command. Rebase the internal patch data in
case the repository was rebased too. The :patch rebase command accepts a
optional argument which is used as the reference commit from which the
rebasing will occur.
Open an editor with the commits that are found on top of the rebase
reference. Any untracked commit will have the tag 'untracked'. If a line
is removed or the 'untracked' tag remains, it will be dropped from the
internal data.
To group commits into a patch set, assign the same tag names.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/patch/rebase_test.go')
-rw-r--r-- | commands/patch/rebase_test.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/commands/patch/rebase_test.go b/commands/patch/rebase_test.go new file mode 100644 index 00000000..fd3d705b --- /dev/null +++ b/commands/patch/rebase_test.go @@ -0,0 +1,114 @@ +package patch + +import ( + "fmt" + "reflect" + "strings" + "testing" + + "git.sr.ht/~rjarry/aerc/lib/pama/models" +) + +func TestRebase_reorder(t *testing.T) { + newCommits := func(order []string) []models.Commit { + var commits []models.Commit + for _, s := range order { + commits = append(commits, models.Commit{ID: s}) + } + return commits + } + tests := []struct { + name string + commits []models.Commit + now []string + by []string + want []models.Commit + }{ + { + name: "nothing to reorder", + commits: newCommits([]string{"1", "2", "3"}), + now: []string{"1", "2", "3"}, + by: []string{"1", "2", "3"}, + want: newCommits([]string{"1", "2", "3"}), + }, + { + name: "reorder", + commits: newCommits([]string{"1", "3", "2"}), + now: []string{"1", "3", "2"}, + by: []string{"1", "2", "3"}, + want: newCommits([]string{"1", "2", "3"}), + }, + { + name: "reorder inverted", + commits: newCommits([]string{"3", "2", "1"}), + now: []string{"3", "2", "1"}, + by: []string{"1", "2", "3"}, + want: newCommits([]string{"1", "2", "3"}), + }, + { + name: "changed hash: do not sort", + commits: newCommits([]string{"1", "6", "3"}), + now: []string{"1", "6", "3"}, + by: []string{"1", "2", "3"}, + want: newCommits([]string{"1", "6", "3"}), + }, + } + + for _, test := range tests { + reorder(test.commits, test.now, test.by) + if !reflect.DeepEqual(test.commits, test.want) { + t.Errorf("test '%s' failed to reorder: got %v but "+ + "want %v", test.name, test.commits, test.want) + } + } +} + +func newCommit(id, subj, tag string) models.Commit { + return models.Commit{ + ID: id, + Subject: subj, + Tag: tag, + } +} + +func TestRebase_parse(t *testing.T) { + input := ` + # some header info + hello_v1 123 same info + hello_v1 456 same info + untracked 789 same info + hello_v2 012 diff info + untracked 345 diff info # not very useful comment + # some footer info + ` + commits := []models.Commit{ + newCommit("123123", "same info", "hello_v1"), + newCommit("456456", "same info", "hello_v1"), + newCommit("789789", "same info", models.Untracked), + newCommit("012012", "diff info", "hello_v2"), + newCommit("345345", "diff info", models.Untracked), + } + + var order []string + for _, c := range commits { + order = append(order, fmt.Sprintf("%3.3s", c.ID)) + } + + table := make(map[string]models.Commit) + for i, shortId := range order { + table[shortId] = commits[i] + } + + rebase := &rebase{ + commits: commits, + table: table, + order: order, + } + + results := rebase.parse(strings.NewReader(input)) + + if len(results) != 3 { + t.Errorf("failed to return correct number of commits: "+ + "got %d but wanted 3", len(results)) + } +} |