| 1 |
package patch |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"sort" |
| 11 |
"strings" |
| 12 |
"time" |
| 13 |
|
| 14 |
"git.sr.ht/~rjarry/aerc/app" |
| 15 |
"git.sr.ht/~rjarry/aerc/commands" |
| 16 |
"git.sr.ht/~rjarry/aerc/config" |
| 17 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 18 |
"git.sr.ht/~rjarry/aerc/lib/pama" |
| 19 |
"git.sr.ht/~rjarry/aerc/lib/pama/models" |
| 20 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 21 |
) |
| 22 |
|
| 23 |
type Rebase struct { |
| 24 |
Commit string `opt:"commit" required:"false"` |
| 25 |
} |
| 26 |
|
| 27 |
func init() { |
| 28 |
register(Rebase{}) |
| 29 |
} |
| 30 |
|
| 31 |
func (Rebase) Description() string { |
| 32 |
return "Rebase the patch data." |
| 33 |
} |
| 34 |
|
| 35 |
func (Rebase) Context() commands.CommandContext { |
| 36 |
return commands.GLOBAL |
| 37 |
} |
| 38 |
|
| 39 |
func (Rebase) Aliases() []string { |
| 40 |
return []string{"rebase"} |
| 41 |
} |
| 42 |
|
| 43 |
func (r Rebase) Execute(args []string) error { |
| 44 |
m := pama.New() |
| 45 |
current, err := m.CurrentProject() |
| 46 |
if err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
|
| 50 |
baseID := r.Commit |
| 51 |
if baseID == "" { |
| 52 |
baseID = current.Base.ID |
| 53 |
} |
| 54 |
|
| 55 |
commits, err := m.RebaseCommits(current, baseID) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
if len(commits) == 0 { |
| 61 |
err := m.SaveRebased(current, baseID, nil) |
| 62 |
if err != nil { |
| 63 |
return fmt.Errorf("No commits to rebase, but saving of new reference failed: %w", err) |
| 64 |
} |
| 65 |
app.PushStatus("No commits to rebase.", 10*time.Second) |
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 69 |
rebase := newRebase(commits) |
| 70 |
f, err := os.CreateTemp(config.General().TempDir, "aerc-patch-rebase-*") |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
name := f.Name() |
| 75 |
_, err = io.Copy(f, rebase.content()) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
f.Close() |
| 80 |
|
| 81 |
createWidget := func() (ui.DrawableInteractive, error) { |
| 82 |
editorCmd, err := app.CmdFallbackSearch(config.EditorCmds(), true) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
editor := exec.Command("/bin/sh", "-c", editorCmd+" "+name) |
| 87 |
term, err := app.NewTerminal(editor) |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
term.OnClose = func(_ error) { |
| 92 |
app.CloseDialog() |
| 93 |
defer os.Remove(name) |
| 94 |
defer term.Focus(false) |
| 95 |
|
| 96 |
f, err := os.Open(name) |
| 97 |
if err != nil { |
| 98 |
app.PushError(fmt.Sprintf("failed to open file: %v", err)) |
| 99 |
return |
| 100 |
} |
| 101 |
defer f.Close() |
| 102 |
|
| 103 |
if editor.ProcessState.ExitCode() > 0 { |
| 104 |
app.PushError("Quitting rebase without saving.") |
| 105 |
return |
| 106 |
} |
| 107 |
err = m.SaveRebased(current, baseID, rebase.parse(f)) |
| 108 |
if err != nil { |
| 109 |
app.PushError(fmt.Sprintf("Failed to save rebased commits: %v", err)) |
| 110 |
return |
| 111 |
} |
| 112 |
app.PushStatus("Successfully rebased.", 10*time.Second) |
| 113 |
} |
| 114 |
term.Show(true) |
| 115 |
term.Focus(true) |
| 116 |
return term, nil |
| 117 |
} |
| 118 |
|
| 119 |
viewer, err := createWidget() |
| 120 |
if err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
|
| 124 |
app.AddDialog(app.DefaultDialog( |
| 125 |
ui.NewBox(viewer, fmt.Sprintf("Patch Rebase on %-6.6s", baseID), "", |
| 126 |
app.SelectedAccountUiConfig(), |
| 127 |
), |
| 128 |
)) |
| 129 |
|
| 130 |
return nil |
| 131 |
} |
| 132 |
|
| 133 |
type rebase struct { |
| 134 |
commits []models.Commit |
| 135 |
table map[string]models.Commit |
| 136 |
order []string |
| 137 |
} |
| 138 |
|
| 139 |
func newRebase(commits []models.Commit) *rebase { |
| 140 |
return &rebase{ |
| 141 |
commits: commits, |
| 142 |
table: make(map[string]models.Commit), |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
const ( |
| 147 |
header string = "" |
| 148 |
footer string = ` |
| 149 |
# Rebase aerc's patch data. This will not affect the underlying repository in |
| 150 |
# any way. |
| 151 |
# |
| 152 |
# Change the name in the first column to assign a new tag to a commit. To group |
| 153 |
# multiple commits, use the same tag name. |
| 154 |
# |
| 155 |
# An 'untracked' tag indicates that aerc lost track of that commit, either due |
| 156 |
# to a commit-hash change or because that commit was applied outside of aerc. |
| 157 |
# |
| 158 |
# Do not change anything else besides the tag names (first column). |
| 159 |
# |
| 160 |
# Do not reorder the lines. The ordering should remain as in the repository. |
| 161 |
# |
| 162 |
# If you remove a line or keep an 'untracked' tag, those commits will be removed |
| 163 |
# from aerc's patch tracking. |
| 164 |
# |
| 165 |
` |
| 166 |
) |
| 167 |
|
| 168 |
func (r *rebase) content() io.Reader { |
| 169 |
var buf bytes.Buffer |
| 170 |
buf.WriteString(header) |
| 171 |
for _, c := range r.commits { |
| 172 |
tag := c.Tag |
| 173 |
if tag == "" { |
| 174 |
tag = models.Untracked |
| 175 |
} |
| 176 |
shortHash := fmt.Sprintf("%6.6s", c.ID) |
| 177 |
buf.WriteString( |
| 178 |
fmt.Sprintf("%-12s %6.6s %s\n", tag, shortHash, c.Info())) |
| 179 |
r.table[shortHash] = c |
| 180 |
r.order = append(r.order, shortHash) |
| 181 |
} |
| 182 |
buf.WriteString(footer) |
| 183 |
return &buf |
| 184 |
} |
| 185 |
|
| 186 |
func (r *rebase) parse(reader io.Reader) []models.Commit { |
| 187 |
var commits []models.Commit |
| 188 |
var hashes []string |
| 189 |
scanner := bufio.NewScanner(reader) |
| 190 |
duplicated := make(map[string]struct{}) |
| 191 |
for scanner.Scan() { |
| 192 |
s := scanner.Text() |
| 193 |
i := strings.Index(s, "#") |
| 194 |
if i >= 0 { |
| 195 |
s = s[:i] |
| 196 |
} |
| 197 |
if strings.TrimSpace(s) == "" { |
| 198 |
continue |
| 199 |
} |
| 200 |
|
| 201 |
fds := strings.Fields(s) |
| 202 |
if len(fds) < 2 { |
| 203 |
continue |
| 204 |
} |
| 205 |
|
| 206 |
tag, shortHash := fds[0], fds[1] |
| 207 |
if tag == models.Untracked { |
| 208 |
continue |
| 209 |
} |
| 210 |
_, dedup := duplicated[shortHash] |
| 211 |
if dedup { |
| 212 |
log.Warnf("rebase: skipping duplicated hash: %s", shortHash) |
| 213 |
continue |
| 214 |
} |
| 215 |
|
| 216 |
hashes = append(hashes, shortHash) |
| 217 |
c, ok := r.table[shortHash] |
| 218 |
if !ok { |
| 219 |
log.Errorf("Looks like the commit hashes were changed "+ |
| 220 |
"during the rebase. Dropping: %v", shortHash) |
| 221 |
continue |
| 222 |
} |
| 223 |
log.Tracef("save commit %s with tag %s", shortHash, tag) |
| 224 |
c.Tag = tag |
| 225 |
commits = append(commits, c) |
| 226 |
duplicated[shortHash] = struct{}{} |
| 227 |
} |
| 228 |
reorder(commits, hashes, r.order) |
| 229 |
return commits |
| 230 |
} |
| 231 |
|
| 232 |
func reorder(toSort []models.Commit, now []string, by []string) { |
| 233 |
byMap := make(map[string]int) |
| 234 |
for i, s := range by { |
| 235 |
byMap[s] = i |
| 236 |
} |
| 237 |
|
| 238 |
complete := true |
| 239 |
for _, s := range now { |
| 240 |
_, ok := byMap[s] |
| 241 |
complete = complete && ok |
| 242 |
} |
| 243 |
if !complete { |
| 244 |
return |
| 245 |
} |
| 246 |
|
| 247 |
sort.SliceStable(toSort, func(i, j int) bool { |
| 248 |
return byMap[now[i]] < byMap[now[j]] |
| 249 |
}) |
| 250 |
} |