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 } }