This repository has been archived on 2025-10-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
gitwarrior/internal/diff/diff.go
Yves Biener 0cb4190540 add: push changes to gitea server after merging local changes
This implementation has no configuration yet to authorize accordingly.
WIP for #3 and #4.
2023-10-24 22:17:37 +02:00

73 lines
1.9 KiB
Go

package diff
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)
func Prompt(value, theirs, mine string) string {
// TODO: create tmp file with the corresponding contents
// open tmp file using the $EDITOR environment variable
// parse and return output of tmp file after $EDITOR execution has been completed
value = strings.ReplaceAll(value, "\r\n", "\n")
theirs = strings.ReplaceAll(theirs, "\r\n", "\n")
mine = strings.ReplaceAll(mine, "\r\n", "\n")
file, err := os.CreateTemp("", "gitw-*")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return value
}
defer os.Remove(file.Name())
// prepare file contents
if _, err := file.WriteString(value); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
if _, err := file.WriteString("\n\n--- theirs ---\n\n"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
if _, err := file.WriteString(theirs); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
if _, err := file.WriteString("\n\n--- mine ---\n\n"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
if _, err := file.WriteString(mine); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
file.Close()
cmd := exec.Command(os.Getenv("EDITOR"), file.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
return value
}
contents, err := os.ReadFile(file.Name())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
// TODO: what should I do with the '\r' characters? Who and when should they be handled?
re := regexp.MustCompile("(?s)^(.*)--- theirs ---")
matches := re.FindAllStringSubmatch(string(contents), -1)
if len(matches) > 0 && len(matches[0]) == 2 {
return matches[0][1]
} else {
fmt.Fprintln(os.Stderr, errors.New("No matches found, could not read merged value. Aborting."))
os.Exit(-1)
return "" // just to make compiler happy
}
}