|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package gitdiff |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/git" |
| 13 | + "code.gitea.io/gitea/modules/log" |
| 14 | +) |
| 15 | + |
| 16 | +type DiffTree struct { |
| 17 | + Files []*DiffTreeRecord |
| 18 | +} |
| 19 | + |
| 20 | +type DiffTreeRecord struct { |
| 21 | + // Status is one of 'added', 'deleted', 'modified', 'renamed', 'copied' |
| 22 | + Status string |
| 23 | + |
| 24 | + HeadPath string |
| 25 | + BasePath string |
| 26 | + HeadMode git.EntryMode |
| 27 | + BaseMode git.EntryMode |
| 28 | + HeadBlobID string |
| 29 | + BaseBlobID string |
| 30 | +} |
| 31 | + |
| 32 | +// GetDiffTree returns the list of path of the files that have changed between the two commits |
| 33 | +func GetDiffTree(ctx context.Context, gitRepo *git.Repository, baseSha, headSha string) (*DiffTree, error) { |
| 34 | + gitDiffTreeRecords, err := runGitDiffTree(ctx, gitRepo, baseSha, headSha) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + return &DiffTree{ |
| 40 | + Files: gitDiffTreeRecords, |
| 41 | + }, nil |
| 42 | +} |
| 43 | + |
| 44 | +func runGitDiffTree(ctx context.Context, gitRepo *git.Repository, baseSha, headSha string) ([]*DiffTreeRecord, error) { |
| 45 | + baseCommitID, headCommitID, err := validateGitDiffTreeArguments(gitRepo, baseSha, headSha) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + cmd := git.NewCommand(ctx, "diff-tree", "--raw", "-r", "--find-renames").AddDynamicArguments(baseCommitID, headCommitID) |
| 51 | + stdout, _, runErr := cmd.RunStdString(&git.RunOpts{Dir: gitRepo.Path}) |
| 52 | + if runErr != nil { |
| 53 | + log.Warn("git diff-tree: %v", runErr) |
| 54 | + return nil, runErr |
| 55 | + } |
| 56 | + |
| 57 | + return parseGitDiffTree(stdout) |
| 58 | +} |
| 59 | + |
| 60 | +func validateGitDiffTreeArguments(gitRepo *git.Repository, baseSha, headSha string) (string, string, error) { |
| 61 | + // if the head is empty its an error |
| 62 | + if headSha == "" { |
| 63 | + return "", "", fmt.Errorf("headSha is empty") |
| 64 | + } |
| 65 | + |
| 66 | + // if the head commit doesn't exist its and error |
| 67 | + headCommit, err := gitRepo.GetCommit(headSha) |
| 68 | + if err != nil { |
| 69 | + return "", "", fmt.Errorf("failed to get commit headSha: %v", err) |
| 70 | + } |
| 71 | + headCommitID := headCommit.ID.String() |
| 72 | + |
| 73 | + // if the base is empty we should use the parent of the head commit |
| 74 | + if baseSha == "" { |
| 75 | + // if the headCommit has no parent we should use an empty commit |
| 76 | + // this can happen when we are generating a diff against an orphaned commit |
| 77 | + if headCommit.ParentCount() == 0 { |
| 78 | + objectFormat, err := gitRepo.GetObjectFormat() |
| 79 | + if err != nil { |
| 80 | + return "", "", err |
| 81 | + } |
| 82 | + |
| 83 | + return objectFormat.EmptyTree().String(), headCommitID, nil |
| 84 | + } |
| 85 | + |
| 86 | + baseCommit, err := headCommit.Parent(0) |
| 87 | + if err != nil { |
| 88 | + return "", "", fmt.Errorf("baseSha is '', attempted to use parent of commit %s, got error: %v", headCommit.ID.String(), err) |
| 89 | + } |
| 90 | + return baseCommit.ID.String(), headCommitID, nil |
| 91 | + } |
| 92 | + |
| 93 | + // try and get the base commit |
| 94 | + baseCommit, err := gitRepo.GetCommit(baseSha) |
| 95 | + // propagate the error if we couldn't get the base commit |
| 96 | + if err != nil { |
| 97 | + return "", "", fmt.Errorf("failed to get base commit %s: %v", baseSha, err) |
| 98 | + } |
| 99 | + |
| 100 | + return baseCommit.ID.String(), headCommit.ID.String(), nil |
| 101 | +} |
| 102 | + |
| 103 | +func parseGitDiffTree(output string) ([]*DiffTreeRecord, error) { |
| 104 | + /* |
| 105 | + The output of `git diff-tree --raw -r --find-renames` is of the form: |
| 106 | +
|
| 107 | + :<old_mode> <new_mode> <old_sha> <new_sha> <status>\t<path> |
| 108 | +
|
| 109 | + or for renames: |
| 110 | +
|
| 111 | + :<old_mode> <new_mode> <old_sha> <new_sha> <status>\t<old_path>\t<new_path> |
| 112 | +
|
| 113 | + See: <https://git-scm.com/docs/git-diff-tree#_raw_output_format> for more details |
| 114 | + */ |
| 115 | + if output == "" { |
| 116 | + return []*DiffTreeRecord{}, nil |
| 117 | + } |
| 118 | + |
| 119 | + results := make([]*DiffTreeRecord, 0) |
| 120 | + |
| 121 | + lines := bufio.NewScanner(strings.NewReader(output)) |
| 122 | + for lines.Scan() { |
| 123 | + line := lines.Text() |
| 124 | + |
| 125 | + if len(line) == 0 { |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + record, err := parseGitDiffTreeLine(line) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + results = append(results, record) |
| 135 | + } |
| 136 | + |
| 137 | + if err := lines.Err(); err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + return results, nil |
| 142 | +} |
| 143 | + |
| 144 | +func parseGitDiffTreeLine(line string) (*DiffTreeRecord, error) { |
| 145 | + line = strings.TrimPrefix(line, ":") |
| 146 | + splitSections := strings.SplitN(line, "\t", 2) |
| 147 | + if len(splitSections) < 2 { |
| 148 | + return nil, fmt.Errorf("unparsable output for diff --raw: `%s`)", line) |
| 149 | + } |
| 150 | + |
| 151 | + fields := strings.Fields(splitSections[0]) |
| 152 | + if len(fields) < 5 { |
| 153 | + return nil, fmt.Errorf("unparsable output for diff --raw: `%s`, expected 5 space delimited values got %d)", line, len(fields)) |
| 154 | + } |
| 155 | + |
| 156 | + baseMode, err := git.ParseEntryMode(fields[0]) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + headMode, err := git.ParseEntryMode(fields[1]) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + |
| 166 | + baseBlobID := fields[2] |
| 167 | + headBlobID := fields[3] |
| 168 | + |
| 169 | + status, err := statusFromLetter(fields[4]) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + filePaths := strings.Split(splitSections[1], "\t") |
| 175 | + |
| 176 | + var headPath, basePath string |
| 177 | + if status == "renamed" { |
| 178 | + if len(filePaths) != 2 { |
| 179 | + return nil, fmt.Errorf("unparsable output for diff --raw: `%s`, expected 2 paths found %d", line, len(filePaths)) |
| 180 | + } |
| 181 | + basePath = filePaths[0] |
| 182 | + headPath = filePaths[1] |
| 183 | + } else { |
| 184 | + basePath = filePaths[0] |
| 185 | + headPath = filePaths[0] |
| 186 | + } |
| 187 | + |
| 188 | + return &DiffTreeRecord{ |
| 189 | + Status: status, |
| 190 | + BaseMode: baseMode, |
| 191 | + HeadMode: headMode, |
| 192 | + BaseBlobID: baseBlobID, |
| 193 | + HeadBlobID: headBlobID, |
| 194 | + BasePath: basePath, |
| 195 | + HeadPath: headPath, |
| 196 | + }, nil |
| 197 | +} |
| 198 | + |
| 199 | +func statusFromLetter(letter string) (string, error) { |
| 200 | + if len(letter) < 1 { |
| 201 | + return "", fmt.Errorf("empty status letter") |
| 202 | + } |
| 203 | + switch letter[0] { |
| 204 | + case 'A': |
| 205 | + return "added", nil |
| 206 | + case 'D': |
| 207 | + return "deleted", nil |
| 208 | + case 'M': |
| 209 | + return "modified", nil |
| 210 | + case 'R': |
| 211 | + // This is of the form "R<score>" but we are choosing to ignore the score |
| 212 | + return "renamed", nil |
| 213 | + case 'C': |
| 214 | + // This is of the form "C<score>" but we are choosing to ignore the score |
| 215 | + return "copied", nil |
| 216 | + default: |
| 217 | + return "", fmt.Errorf("unknown status letter: '%s'", letter) |
| 218 | + } |
| 219 | +} |
0 commit comments