Monorepo for Tangled tangled.org
817
fork

Configure Feed

Select the types of activity you want to include in your feed.

at lt/knot-merge-base-comined-patch 237 lines 5.6 kB view raw
1package git 2 3import ( 4 "bytes" 5 "fmt" 6 "log" 7 "os" 8 "os/exec" 9 "slices" 10 "strings" 11 12 "github.com/bluekeyes/go-gitdiff/gitdiff" 13 "github.com/go-git/go-git/v5/plumbing" 14 "github.com/go-git/go-git/v5/plumbing/object" 15 "tangled.org/core/patchutil" 16 "tangled.org/core/types" 17) 18 19func (g *GitRepo) Diff() (*types.NiceDiff, error) { 20 c, err := g.r.CommitObject(g.h) 21 if err != nil { 22 return nil, fmt.Errorf("commit object: %w", err) 23 } 24 25 patch := &object.Patch{} 26 commitTree, err := c.Tree() 27 parent := &object.Commit{} 28 if err == nil { 29 parentTree := &object.Tree{} 30 if c.NumParents() != 0 { 31 parent, err = c.Parents().Next() 32 if err == nil { 33 parentTree, err = parent.Tree() 34 if err == nil { 35 patch, err = parentTree.Patch(commitTree) 36 if err != nil { 37 return nil, fmt.Errorf("patch: %w", err) 38 } 39 } 40 } 41 } else { 42 patch, err = parentTree.Patch(commitTree) 43 if err != nil { 44 return nil, fmt.Errorf("patch: %w", err) 45 } 46 } 47 } 48 49 diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String())) 50 if err != nil { 51 log.Println(err) 52 } 53 54 nd := types.NiceDiff{} 55 for _, d := range diffs { 56 ndiff := types.Diff{} 57 ndiff.Name.New = d.NewName 58 ndiff.Name.Old = d.OldName 59 ndiff.IsBinary = d.IsBinary 60 ndiff.IsNew = d.IsNew 61 ndiff.IsDelete = d.IsDelete 62 ndiff.IsCopy = d.IsCopy 63 ndiff.IsRename = d.IsRename 64 65 for _, tf := range d.TextFragments { 66 ndiff.TextFragments = append(ndiff.TextFragments, *tf) 67 nd.Stat.Insertions += tf.LinesAdded 68 nd.Stat.Deletions += tf.LinesDeleted 69 } 70 71 nd.Diff = append(nd.Diff, ndiff) 72 } 73 74 nd.Stat.FilesChanged += len(diffs) 75 nd.Commit.FromGoGitCommit(c) 76 77 return &nd, nil 78} 79 80func (g *GitRepo) MergeBase(a, b *object.Commit) (*object.Commit, error) { 81 out, err := g.mergeBase(a.Hash.String(), b.Hash.String()) 82 if err != nil { 83 return nil, fmt.Errorf("merge-base %s %s: %w", a.Hash, b.Hash, err) 84 } 85 86 hash := plumbing.NewHash(strings.TrimSpace(string(out))) 87 return g.r.CommitObject(hash) 88} 89 90func (g *GitRepo) DiffTree(commit1, commit2 *object.Commit) (*types.DiffTree, error) { 91 tree1, err := commit1.Tree() 92 if err != nil { 93 return nil, err 94 } 95 96 tree2, err := commit2.Tree() 97 if err != nil { 98 return nil, err 99 } 100 101 diff, err := object.DiffTree(tree1, tree2) 102 if err != nil { 103 return nil, err 104 } 105 106 patch, err := diff.Patch() 107 if err != nil { 108 return nil, err 109 } 110 111 diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String())) 112 if err != nil { 113 return nil, err 114 } 115 116 return &types.DiffTree{ 117 Rev1: commit1.Hash.String(), 118 Rev2: commit2.Hash.String(), 119 Patch: patch.String(), 120 Diff: diffs, 121 }, nil 122} 123 124// FormatPatch generates a git-format-patch output between two commits, 125// and returns the raw format-patch series, a parsed FormatPatch and an error. 126func (g *GitRepo) formatSinglePatch(commit plumbing.Hash, extraArgs ...string) (string, *types.FormatPatch, error) { 127 var stdout bytes.Buffer 128 129 args := []string{ 130 "-C", 131 g.path, 132 "format-patch", 133 "-1", 134 commit.String(), 135 "--stdout", 136 } 137 args = append(args, extraArgs...) 138 139 cmd := exec.Command("git", args...) 140 cmd.Stdout = &stdout 141 cmd.Stderr = os.Stderr 142 err := cmd.Run() 143 if err != nil { 144 return "", nil, err 145 } 146 147 formatPatch, err := patchutil.ExtractPatches(stdout.String()) 148 if err != nil { 149 return "", nil, err 150 } 151 152 if len(formatPatch) > 1 { 153 return "", nil, fmt.Errorf("running format-patch on single commit produced more than on patch") 154 } 155 156 return stdout.String(), &formatPatch[0], nil 157} 158 159func (g *GitRepo) ResolveRevision(revStr string) (*object.Commit, error) { 160 rev, err := g.r.ResolveRevision(plumbing.Revision(revStr)) 161 if err != nil { 162 return nil, fmt.Errorf("resolving revision %s: %w", revStr, err) 163 } 164 165 commit, err := g.r.CommitObject(*rev) 166 if err != nil { 167 168 return nil, fmt.Errorf("getting commit for %s: %w", revStr, err) 169 } 170 171 return commit, nil 172} 173 174func (g *GitRepo) commitsBetween(newCommit, oldCommit *object.Commit) ([]*object.Commit, error) { 175 var commits []*object.Commit 176 177 output, err := g.revList( 178 "--no-merges", // format-patch explicitly prepares only non-merges 179 fmt.Sprintf("%s..%s", oldCommit.Hash.String(), newCommit.Hash.String()), 180 ) 181 if err != nil { 182 return nil, fmt.Errorf("revlist: %w", err) 183 } 184 185 lines := strings.Split(strings.TrimSpace(string(output)), "\n") 186 if len(lines) == 1 && lines[0] == "" { 187 return commits, nil 188 } 189 190 for _, item := range lines { 191 obj, err := g.r.CommitObject(plumbing.NewHash(item)) 192 if err != nil { 193 continue 194 } 195 commits = append(commits, obj) 196 } 197 198 return commits, nil 199} 200 201func (g *GitRepo) FormatPatch(base, commit2 *object.Commit) (string, []types.FormatPatch, error) { 202 // get list of commits between commit2 and base 203 commits, err := g.commitsBetween(commit2, base) 204 if err != nil { 205 return "", nil, fmt.Errorf("failed to get commits: %w", err) 206 } 207 208 // reverse the list so we start from the oldest one and go up to the most recent one 209 slices.Reverse(commits) 210 211 var allPatchesContent strings.Builder 212 var allPatches []types.FormatPatch 213 214 for _, commit := range commits { 215 changeId := "" 216 if val, ok := commit.ExtraHeaders["change-id"]; ok { 217 changeId = string(val) 218 } 219 220 var additionalArgs []string 221 if changeId != "" { 222 additionalArgs = append(additionalArgs, "--add-header", fmt.Sprintf("Change-Id: %s", changeId)) 223 } 224 225 stdout, patch, err := g.formatSinglePatch(commit.Hash, additionalArgs...) 226 if err != nil { 227 return "", nil, fmt.Errorf("failed to format patch for commit %s: %w", commit.Hash.String(), err) 228 } 229 230 allPatchesContent.WriteString(stdout) 231 allPatchesContent.WriteString("\n") 232 233 allPatches = append(allPatches, *patch) 234 } 235 236 return allPatchesContent.String(), allPatches, nil 237}