| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "html/template" |
| 7 | "net/http" |
| 8 | "strconv" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | ) |
| 12 | |
| 13 | // errUnchosen is a conflict form sent without a choice for every hunk. |
| 14 | var errUnchosen = errors.New("mapa: a conflict has no choice") |
| 15 | |
| 16 | // hunkView is one hunk of the conflict view, drawn. |
| 17 | type hunkView struct { |
| 18 | Conflict bool |
| 19 | Number int |
| 20 | HTML template.HTML |
| 21 | Mine, Theirs template.HTML |
| 22 | } |
| 23 | |
| 24 | // drawHunks draws a merge's hunks for scope: settled blocks as they read, |
| 25 | // and each conflict's two sides, numbered for the form. |
| 26 | func (index *Index) drawHunks(ctx context.Context, scope Scope, page int64, |
| 27 | hunks []markup.Hunk) []hunkView { |
| 28 | if index.Sync(ctx) != nil { |
| 29 | return nil |
| 30 | } |
| 31 | index.mutex.RLock() |
| 32 | defer index.mutex.RUnlock() |
| 33 | draw := resolver{index: index, scope: scope, page: page, |
| 34 | images: index.imageLookup(ctx)} |
| 35 | views := make([]hunkView, len(hunks)) |
| 36 | number := 0 |
| 37 | for at, hunk := range hunks { |
| 38 | if !hunk.Conflict { |
| 39 | views[at] = hunkView{HTML: markup.Render(hunk.Blocks, draw)} |
| 40 | continue |
| 41 | } |
| 42 | views[at] = hunkView{Conflict: true, Number: number, |
| 43 | Mine: markup.Render(hunk.Mine, draw), |
| 44 | Theirs: markup.Render(hunk.Theirs, draw)} |
| 45 | number++ |
| 46 | } |
| 47 | return views |
| 48 | } |
| 49 | |
| 50 | // chosen is the body the person chose in the conflict form, and whether |
| 51 | // there is anything of theirs to keep: choosing only the current text |
| 52 | // just discards the draft. |
| 53 | func (app *app) chosen(request *http.Request, viewer Viewer, |
| 54 | pageID int64) (string, bool, error) { |
| 55 | conflict, err := app.store.ConflictOf(request.Context(), viewer, pageID) |
| 56 | if err != nil { |
| 57 | return "", false, err |
| 58 | } |
| 59 | if conflict.NoBase { |
| 60 | switch request.PostFormValue("whole") { |
| 61 | case "mine": |
| 62 | return conflict.Mine, true, nil |
| 63 | case "theirs": |
| 64 | return "", false, nil |
| 65 | } |
| 66 | return "", false, errUnchosen |
| 67 | } |
| 68 | choices := make([]markup.Choice, conflict.Conflicts()) |
| 69 | for number := range choices { |
| 70 | choices[number] = choiceOf(request.PostFormValue( |
| 71 | "choice-" + strconv.Itoa(number))) |
| 72 | } |
| 73 | body, ok := markup.Settle(conflict.Hunks, choices) |
| 74 | if !ok { |
| 75 | return "", false, errUnchosen |
| 76 | } |
| 77 | return body, cleanBody(body) != cleanBody(conflict.Theirs), nil |
| 78 | } |
| 79 | |
| 80 | func choiceOf(value string) markup.Choice { |
| 81 | switch value { |
| 82 | case "mine": |
| 83 | return markup.KeepMine |
| 84 | case "theirs": |
| 85 | return markup.KeepTheirs |
| 86 | case "both": |
| 87 | return markup.KeepBoth |
| 88 | } |
| 89 | return 0 |
| 90 | } |
| 91 | |