MCPcopy Create free account
hub / github.com/cwarden/git-add--interactive / editHunk

Method editHunk

internal/ui/patch.go:550–633  ·  view source on GitHub ↗
(hunk *git.Hunk, mode git.PatchMode, header git.Hunk)

Source from the content-addressed store, hash-verified

548}
549
550func (a *App) editHunk(hunk *git.Hunk, mode git.PatchMode, header git.Hunk) (*git.Hunk, error) {
551 hunkFile := filepath.Join(a.repo.GitDir(), "addp-hunk-edit.diff")
552
553 content := "# Manual hunk edit mode -- see bottom for a quick guide.\n"
554 for _, line := range hunk.Text {
555 content += line + "\n"
556 }
557
558 content += "# ---\n"
559 content += "# To remove '-' lines, make them ' ' lines (context).\n"
560 content += "# To remove '+' lines, delete them.\n"
561 content += "# Lines starting with # will be removed.\n"
562
563 if err := ioutil.WriteFile(hunkFile, []byte(content), 0644); err != nil {
564 return nil, err
565 }
566
567 defer os.Remove(hunkFile)
568
569 editor := os.Getenv("EDITOR")
570 if editor == "" {
571 editorOutput, err := a.repo.RunCommand("var", "GIT_EDITOR")
572 if err != nil {
573 editor = "vi"
574 } else {
575 editor = strings.TrimSpace(string(editorOutput))
576 }
577 }
578
579 cmd := exec.Command("sh", "-c", editor+" "+hunkFile)
580 cmd.Stdin = os.Stdin
581 cmd.Stdout = os.Stdout
582 cmd.Stderr = os.Stderr
583
584 if err := cmd.Run(); err != nil {
585 return nil, err
586 }
587
588 editedContent, err := ioutil.ReadFile(hunkFile)
589 if err != nil {
590 return nil, err
591 }
592
593 lines := strings.Split(string(editedContent), "\n")
594 var newText []string
595 var newDisplay []string
596
597 for _, line := range lines {
598 if !strings.HasPrefix(line, "#") && strings.TrimSpace(line) != "" {
599 newText = append(newText, line)
600 newDisplay = append(newDisplay, line)
601 }
602 }
603
604 if len(newText) == 0 {
605 return nil, nil
606 }
607

Callers 1

patchUpdateFileMethod · 0.95

Calls 5

reassemblePatchMethod · 0.95
promptYesNoMethod · 0.95
GitDirMethod · 0.80
RunCommandMethod · 0.80
CheckPatchMethod · 0.80

Tested by

no test coverage detected