mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Support unprotected file patterns (#16395)
Fixes #16381 Note that changes to unprotected files via the web editor still cannot be pushed directly to the protected branch. I could easily add such support for edits and deletes if needed. But for adding, uploading or renaming unprotected files, it is not trivial. * Extract & Move GetAffectedFiles to modules/git
This commit is contained in:
		@@ -10,6 +10,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strconv"
 | 
			
		||||
@@ -273,3 +274,46 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
 | 
			
		||||
		oldBegin, oldNumOfLines, newBegin, newNumOfLines)
 | 
			
		||||
	return strings.Join(newHunk, "\n"), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetAffectedFiles returns the affected files between two commits
 | 
			
		||||
func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repository) ([]string, error) {
 | 
			
		||||
	stdoutReader, stdoutWriter, err := os.Pipe()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Unable to create os.Pipe for %s", repo.Path)
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		_ = stdoutReader.Close()
 | 
			
		||||
		_ = stdoutWriter.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	affectedFiles := make([]string, 0, 32)
 | 
			
		||||
 | 
			
		||||
	// Run `git diff --name-only` to get the names of the changed files
 | 
			
		||||
	err = NewCommand("diff", "--name-only", oldCommitID, newCommitID).
 | 
			
		||||
		RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
 | 
			
		||||
			stdoutWriter, nil, nil,
 | 
			
		||||
			func(ctx context.Context, cancel context.CancelFunc) error {
 | 
			
		||||
				// Close the writer end of the pipe to begin processing
 | 
			
		||||
				_ = stdoutWriter.Close()
 | 
			
		||||
				defer func() {
 | 
			
		||||
					// Close the reader on return to terminate the git command if necessary
 | 
			
		||||
					_ = stdoutReader.Close()
 | 
			
		||||
				}()
 | 
			
		||||
				// Now scan the output from the command
 | 
			
		||||
				scanner := bufio.NewScanner(stdoutReader)
 | 
			
		||||
				for scanner.Scan() {
 | 
			
		||||
					path := strings.TrimSpace(scanner.Text())
 | 
			
		||||
					if len(path) == 0 {
 | 
			
		||||
						continue
 | 
			
		||||
					}
 | 
			
		||||
					affectedFiles = append(affectedFiles, path)
 | 
			
		||||
				}
 | 
			
		||||
				return scanner.Err()
 | 
			
		||||
			})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Unable to get affected files for commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return affectedFiles, err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user