Skip to content

Commit 2613ef9

Browse files
committed
Move GetAffectedFiles to modules/git
1 parent 80d0a95 commit 2613ef9

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

modules/git/diff.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"fmt"
1212
"io"
13+
"os"
1314
"os/exec"
1415
"regexp"
1516
"strconv"
@@ -273,3 +274,46 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
273274
oldBegin, oldNumOfLines, newBegin, newNumOfLines)
274275
return strings.Join(newHunk, "\n"), nil
275276
}
277+
278+
// Get the affected files between two commits
279+
func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repository) ([]string, error) {
280+
stdoutReader, stdoutWriter, err := os.Pipe()
281+
if err != nil {
282+
log.Error("Unable to create os.Pipe for %s", repo.Path)
283+
return nil, err
284+
}
285+
defer func() {
286+
_ = stdoutReader.Close()
287+
_ = stdoutWriter.Close()
288+
}()
289+
290+
affectedFiles := make([]string, 0, 32)
291+
292+
// Run `git diff --name-only` to get the names of the changed files
293+
err = NewCommand("diff", "--name-only", oldCommitID, newCommitID).
294+
RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
295+
stdoutWriter, nil, nil,
296+
func(ctx context.Context, cancel context.CancelFunc) error {
297+
// Close the writer end of the pipe to begin processing
298+
_ = stdoutWriter.Close()
299+
defer func() {
300+
// Close the reader on return to terminate the git command if necessary
301+
_ = stdoutReader.Close()
302+
}()
303+
// Now scan the output from the command
304+
scanner := bufio.NewScanner(stdoutReader)
305+
for scanner.Scan() {
306+
path := strings.TrimSpace(scanner.Text())
307+
if len(path) == 0 {
308+
continue
309+
}
310+
affectedFiles = append(affectedFiles, path)
311+
}
312+
return scanner.Err()
313+
})
314+
if err != nil {
315+
log.Error("Unable to get affected files for commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
316+
}
317+
318+
return affectedFiles, err
319+
}

services/pull/patch.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func CheckFileProtection(oldCommitID, newCommitID string, patterns []glob.Glob,
248248
if len(patterns) == 0 {
249249
return nil, nil
250250
}
251-
affectedFiles, err := getAffectedFiles(oldCommitID, newCommitID, env, repo)
251+
affectedFiles, err := git.GetAffectedFiles(oldCommitID, newCommitID, env, repo)
252252
if err != nil {
253253
return nil, err
254254
}
@@ -278,7 +278,7 @@ func CheckUnprotectedFiles(oldCommitID, newCommitID string, patterns []glob.Glob
278278
if len(patterns) == 0 {
279279
return false, nil
280280
}
281-
affectedFiles, err := getAffectedFiles(oldCommitID, newCommitID, env, repo)
281+
affectedFiles, err := git.GetAffectedFiles(oldCommitID, newCommitID, env, repo)
282282
if err != nil {
283283
return false, err
284284
}
@@ -298,48 +298,6 @@ func CheckUnprotectedFiles(oldCommitID, newCommitID string, patterns []glob.Glob
298298
return true, nil
299299
}
300300

301-
func getAffectedFiles(oldCommitID, newCommitID string, env []string, repo *git.Repository) ([]string, error) {
302-
stdoutReader, stdoutWriter, err := os.Pipe()
303-
if err != nil {
304-
log.Error("Unable to create os.Pipe for %s", repo.Path)
305-
return nil, err
306-
}
307-
defer func() {
308-
_ = stdoutReader.Close()
309-
_ = stdoutWriter.Close()
310-
}()
311-
312-
affectedFiles := make([]string, 0, 32)
313-
314-
// Run `git diff --name-only` to get the names of the changed files
315-
err = git.NewCommand("diff", "--name-only", oldCommitID, newCommitID).
316-
RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
317-
stdoutWriter, nil, nil,
318-
func(ctx context.Context, cancel context.CancelFunc) error {
319-
// Close the writer end of the pipe to begin processing
320-
_ = stdoutWriter.Close()
321-
defer func() {
322-
// Close the reader on return to terminate the git command if necessary
323-
_ = stdoutReader.Close()
324-
}()
325-
// Now scan the output from the command
326-
scanner := bufio.NewScanner(stdoutReader)
327-
for scanner.Scan() {
328-
path := strings.TrimSpace(scanner.Text())
329-
if len(path) == 0 {
330-
continue
331-
}
332-
affectedFiles = append(affectedFiles, path)
333-
}
334-
return scanner.Err()
335-
})
336-
if err != nil {
337-
log.Error("Unable to get affected files for commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
338-
}
339-
340-
return affectedFiles, err
341-
}
342-
343301
// checkPullFilesProtection check if pr changed protected files and save results
344302
func checkPullFilesProtection(pr *models.PullRequest, gitRepo *git.Repository) error {
345303
if err := pr.LoadProtectedBranch(); err != nil {

0 commit comments

Comments
 (0)