| 1 | package command |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | gogit "github.com/go-git/go-git/v5" |
| 10 | "github.com/go-git/go-git/v5/plumbing" |
| 11 | "github.com/isacikgoz/gitbatch/internal/git" |
| 12 | ) |
| 13 | |
| 14 | // Diff is a wrapper function for "git diff" command |
| 15 | // Diff function returns the diff to previous commit detail of the given has |
| 16 | // of a specific commit |
| 17 | func Diff(r *git.Repository, hash string) (diff string, err error) { |
| 18 | // in case we want to configure diff options |
| 19 | mode := ModeNative |
| 20 | |
| 21 | switch mode { |
| 22 | case ModeLegacy: |
| 23 | return diffWithGit(r, hash) |
| 24 | case ModeNative: |
| 25 | return diffWithGoGit(r, hash) |
| 26 | } |
| 27 | return diff, fmt.Errorf("unhandled diff operation") |
| 28 | } |
| 29 | |
| 30 | // DiffFile is a wrapper of "git diff" command for a file to compare with HEAD rev |
| 31 | func DiffFile(f *git.File) (output string, err error) { |
| 32 | args := make([]string, 0) |
| 33 | args = append(args, "diff") |
| 34 | args = append(args, "HEAD") |
| 35 | args = append(args, f.Name) |
| 36 | output, err = Run(strings.TrimSuffix(f.AbsPath, f.Name), "git", args) |
| 37 | if err != nil { |
| 38 | return "", err |
| 39 | } |
| 40 | return output, err |
| 41 | } |
| 42 | |
| 43 | // DiffStat shows current working status "git diff --stat" |
| 44 | func DiffStat(r *git.Repository) (string, error) { |
| 45 | args := make([]string, 0) |
| 46 | args = append(args, "diff") |
| 47 | args = append(args, "--stat") |
| 48 | output, err := Run(r.AbsPath, "git", args) |
| 49 | if err != nil { |
| 50 | return "", err |
| 51 | } |
| 52 | re := regexp.MustCompile(`\n?\r`) |
| 53 | output = re.ReplaceAllString(output, "\n") |
| 54 | return output, err |
| 55 | } |
| 56 | |
| 57 | // DiffStatRefs shows diff stat of two refs "git diff a1b2c3..e4f5g6 --stat" |
| 58 | func DiffStatRefs(r *git.Repository, ref1, ref2 string) (string, error) { |
| 59 | args := make([]string, 0) |
| 60 | args = append(args, "diff") |
| 61 | args = append(args, ref1+".."+ref2) |
| 62 | args = append(args, "--shortstat") |
| 63 | output, err := Run(r.AbsPath, "git", args) |
| 64 | if err != nil { |
| 65 | return "", err |
| 66 | } |
| 67 | re := regexp.MustCompile(`\n?\r`) |
| 68 | output = re.ReplaceAllString(output, "\n") |
| 69 | return output, err |
| 70 | } |
| 71 | |
| 72 | // PlainDiff shows current working status "git diff" |
| 73 | func PlainDiff(r *git.Repository) (string, error) { |
| 74 | args := make([]string, 0) |
| 75 | args = append(args, "diff") |
| 76 | output, err := Run(r.AbsPath, "git", args) |
| 77 | if err != nil { |
| 78 | return "", err |
| 79 | } |
| 80 | re := regexp.MustCompile(`\n?\r`) |
| 81 | output = re.ReplaceAllString(output, "\n") |
| 82 | return output, err |
| 83 | } |
| 84 | |
| 85 | // StashDiff shows diff of stash item "git show stash@{0}" |
| 86 | func StashDiff(r *git.Repository, id int) (string, error) { |
| 87 | args := make([]string, 0) |
| 88 | args = append(args, "show") |
| 89 | args = append(args, "stash@{"+strconv.Itoa(id)+"}") |
| 90 | output, err := Run(r.AbsPath, "git", args) |
| 91 | if err != nil { |
| 92 | return "", err |
| 93 | } |
| 94 | re := regexp.MustCompile(`\n?\r`) |
| 95 | output = re.ReplaceAllString(output, "\n") |
| 96 | return output, err |
| 97 | } |
| 98 | |
| 99 | func diffWithGit(r *git.Repository, hash string) (diff string, err error) { |
| 100 | return diff, nil |
| 101 | } |
| 102 | |
| 103 | func diffWithGoGit(r *git.Repository, hash string) (diff string, err error) { |
| 104 | currentCommitIndex := 0 |
| 105 | for i, cs := range r.State.Branch.Commits { |
| 106 | if cs.Hash == hash { |
| 107 | currentCommitIndex = i |
| 108 | } |
| 109 | } |
| 110 | if len(r.State.Branch.Commits)-currentCommitIndex <= 1 { |
| 111 | return "there is no diff", nil |
| 112 | } |
| 113 | |
| 114 | // maybe we don't need to log the repo again? |
| 115 | commits, err := r.Repo.Log(&gogit.LogOptions{ |
| 116 | From: plumbing.NewHash(r.State.Branch.State.Commit.Hash), |
| 117 | Order: gogit.LogOrderCommitterTime, |
| 118 | }) |
| 119 | if err != nil { |
| 120 | return "", err |
| 121 | } |
| 122 | |
| 123 | currentCommit, err := commits.Next() |
| 124 | if err != nil { |
| 125 | return "", err |
| 126 | } |
| 127 | currentTree, err := currentCommit.Tree() |
| 128 | if err != nil { |
| 129 | return diff, err |
| 130 | } |
| 131 | |
| 132 | prevCommit, err := commits.Next() |
| 133 | if err != nil { |
| 134 | return "", err |
| 135 | } |
| 136 | prevTree, err := prevCommit.Tree() |
| 137 | if err != nil { |
| 138 | return diff, err |
| 139 | } |
| 140 | |
| 141 | changes, err := prevTree.Diff(currentTree) |
| 142 | if err != nil { |
| 143 | return "", err |
| 144 | } |
| 145 | |
| 146 | // here we collect the actual diff |
| 147 | for _, c := range changes { |
| 148 | patch, err := c.Patch() |
| 149 | if err != nil { |
| 150 | break |
| 151 | } |
| 152 | diff = diff + patch.String() + "\n" |
| 153 | } |
| 154 | return diff, nil |
| 155 | } |