2019-02-12 18:37:31 +05:30
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
2021-11-24 13:26:24 +05:30
package files
2019-02-12 18:37:31 +05:30
import (
"bytes"
"context"
"fmt"
"io"
"os"
2019-04-17 21:36:35 +05:30
"regexp"
2019-02-12 18:37:31 +05:30
"strings"
"time"
"code.gitea.io/gitea/models"
2021-12-10 06:57:50 +05:30
repo_model "code.gitea.io/gitea/models/repo"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2019-04-17 21:36:35 +05:30
"code.gitea.io/gitea/modules/git"
2019-05-11 20:59:17 +05:30
"code.gitea.io/gitea/modules/log"
2019-02-12 18:37:31 +05:30
"code.gitea.io/gitea/modules/setting"
2021-12-10 13:44:24 +05:30
asymkey_service "code.gitea.io/gitea/services/asymkey"
2019-09-06 07:50:09 +05:30
"code.gitea.io/gitea/services/gitdiff"
2019-02-12 18:37:31 +05:30
)
2019-04-17 21:36:35 +05:30
// TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone
2019-02-12 18:37:31 +05:30
type TemporaryUploadRepository struct {
2022-01-20 04:56:57 +05:30
ctx context . Context
2021-12-10 06:57:50 +05:30
repo * repo_model . Repository
2019-04-17 21:36:35 +05:30
gitRepo * git . Repository
2019-02-12 18:37:31 +05:30
basePath string
}
// NewTemporaryUploadRepository creates a new temporary upload repository
2022-01-20 04:56:57 +05:30
func NewTemporaryUploadRepository ( ctx context . Context , repo * repo_model . Repository ) ( * TemporaryUploadRepository , error ) {
2019-05-11 20:59:17 +05:30
basePath , err := models . CreateTemporaryPath ( "upload" )
if err != nil {
return nil , err
2019-02-12 18:37:31 +05:30
}
2022-01-20 04:56:57 +05:30
t := & TemporaryUploadRepository { ctx : ctx , repo : repo , basePath : basePath }
2019-02-12 18:37:31 +05:30
return t , nil
}
// Close the repository cleaning up all files
func ( t * TemporaryUploadRepository ) Close ( ) {
2019-11-13 12:31:19 +05:30
defer t . gitRepo . Close ( )
2019-05-11 20:59:17 +05:30
if err := models . RemoveTemporaryPath ( t . basePath ) ; err != nil {
log . Error ( "Failed to remove temporary path %s: %v" , t . basePath , err )
2019-02-12 18:37:31 +05:30
}
}
// Clone the base repository to our path and set branch as the HEAD
func ( t * TemporaryUploadRepository ) Clone ( branch string ) error {
2022-02-07 00:31:47 +05:30
if _ , err := git . NewCommand ( t . ctx , "clone" , "-s" , "--bare" , "-b" , branch , t . repo . RepoPath ( ) , t . basePath ) . Run ( ) ; err != nil {
2019-11-11 17:16:28 +05:30
stderr := err . Error ( )
2019-04-17 21:36:35 +05:30
if matched , _ := regexp . MatchString ( ".*Remote branch .* not found in upstream origin.*" , stderr ) ; matched {
2019-04-19 17:47:27 +05:30
return git . ErrBranchNotExist {
2019-04-17 21:36:35 +05:30
Name : branch ,
}
} else if matched , _ := regexp . MatchString ( ".* repository .* does not exist.*" , stderr ) ; matched {
2021-12-10 06:57:50 +05:30
return repo_model . ErrRepoNotExist {
2019-04-17 21:36:35 +05:30
ID : t . repo . ID ,
UID : t . repo . OwnerID ,
OwnerName : t . repo . OwnerName ,
Name : t . repo . Name ,
}
} else {
return fmt . Errorf ( "Clone: %v %s" , err , stderr )
}
}
2022-01-20 04:56:57 +05:30
gitRepo , err := git . OpenRepositoryCtx ( t . ctx , t . basePath )
2019-04-17 21:36:35 +05:30
if err != nil {
return err
2019-02-12 18:37:31 +05:30
}
2019-04-17 21:36:35 +05:30
t . gitRepo = gitRepo
2019-02-12 18:37:31 +05:30
return nil
}
2022-03-29 01:18:41 +05:30
// Init the repository
func ( t * TemporaryUploadRepository ) Init ( ) error {
if err := git . InitRepository ( t . ctx , t . basePath , false ) ; err != nil {
return err
}
gitRepo , err := git . OpenRepositoryCtx ( t . ctx , t . basePath )
if err != nil {
return err
}
t . gitRepo = gitRepo
return nil
}
2019-02-12 18:37:31 +05:30
// SetDefaultIndex sets the git index to our HEAD
func ( t * TemporaryUploadRepository ) SetDefaultIndex ( ) error {
2022-02-07 00:31:47 +05:30
if _ , err := git . NewCommand ( t . ctx , "read-tree" , "HEAD" ) . RunInDir ( t . basePath ) ; err != nil {
2019-11-11 17:16:28 +05:30
return fmt . Errorf ( "SetDefaultIndex: %v" , err )
2019-02-12 18:37:31 +05:30
}
return nil
}
// LsFiles checks if the given filename arguments are in the index
func ( t * TemporaryUploadRepository ) LsFiles ( filenames ... string ) ( [ ] string , error ) {
stdOut := new ( bytes . Buffer )
stdErr := new ( bytes . Buffer )
cmdArgs := [ ] string { "ls-files" , "-z" , "--" }
for _ , arg := range filenames {
if arg != "" {
cmdArgs = append ( cmdArgs , arg )
}
}
2022-02-11 18:17:22 +05:30
if err := git . NewCommand ( t . ctx , cmdArgs ... ) .
RunWithContext ( & git . RunContext {
Timeout : - 1 ,
Dir : t . basePath ,
Stdout : stdOut ,
Stderr : stdErr ,
} ) ; err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to run git ls-files for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s" , t . repo . FullName ( ) , t . basePath , err , stdOut . String ( ) , stdErr . String ( ) )
err = fmt . Errorf ( "Unable to run git ls-files for temporary repo of: %s Error: %v\nstdout: %s\nstderr: %s" , t . repo . FullName ( ) , err , stdOut . String ( ) , stdErr . String ( ) )
2019-02-12 18:37:31 +05:30
return nil , err
}
filelist := make ( [ ] string , len ( filenames ) )
for _ , line := range bytes . Split ( stdOut . Bytes ( ) , [ ] byte { '\000' } ) {
filelist = append ( filelist , string ( line ) )
}
2019-11-11 17:16:28 +05:30
return filelist , nil
2019-02-12 18:37:31 +05:30
}
// RemoveFilesFromIndex removes the given files from the index
func ( t * TemporaryUploadRepository ) RemoveFilesFromIndex ( filenames ... string ) error {
stdOut := new ( bytes . Buffer )
stdErr := new ( bytes . Buffer )
stdIn := new ( bytes . Buffer )
for _ , file := range filenames {
if file != "" {
stdIn . WriteString ( "0 0000000000000000000000000000000000000000\t" )
stdIn . WriteString ( file )
stdIn . WriteByte ( '\000' )
}
}
2022-02-11 18:17:22 +05:30
if err := git . NewCommand ( t . ctx , "update-index" , "--remove" , "-z" , "--index-info" ) .
RunWithContext ( & git . RunContext {
Timeout : - 1 ,
Dir : t . basePath ,
Stdin : stdIn ,
Stdout : stdOut ,
Stderr : stdErr ,
} ) ; err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to update-index for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s" , t . repo . FullName ( ) , t . basePath , err , stdOut . String ( ) , stdErr . String ( ) )
return fmt . Errorf ( "Unable to update-index for temporary repo: %s Error: %v\nstdout: %s\nstderr: %s" , t . repo . FullName ( ) , err , stdOut . String ( ) , stdErr . String ( ) )
2019-02-12 18:37:31 +05:30
}
2019-11-11 17:16:28 +05:30
return nil
2019-02-12 18:37:31 +05:30
}
// HashObject writes the provided content to the object db and returns its hash
func ( t * TemporaryUploadRepository ) HashObject ( content io . Reader ) ( string , error ) {
2019-11-11 17:16:28 +05:30
stdOut := new ( bytes . Buffer )
stdErr := new ( bytes . Buffer )
2019-02-12 18:37:31 +05:30
2022-02-11 18:17:22 +05:30
if err := git . NewCommand ( t . ctx , "hash-object" , "-w" , "--stdin" ) .
RunWithContext ( & git . RunContext {
Timeout : - 1 ,
Dir : t . basePath ,
Stdin : content ,
Stdout : stdOut ,
Stderr : stdErr ,
} ) ; err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to hash-object to temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s" , t . repo . FullName ( ) , t . basePath , err , stdOut . String ( ) , stdErr . String ( ) )
return "" , fmt . Errorf ( "Unable to hash-object to temporary repo: %s Error: %v\nstdout: %s\nstderr: %s" , t . repo . FullName ( ) , err , stdOut . String ( ) , stdErr . String ( ) )
2019-02-12 18:37:31 +05:30
}
2019-11-11 17:16:28 +05:30
return strings . TrimSpace ( stdOut . String ( ) ) , nil
2019-02-12 18:37:31 +05:30
}
// AddObjectToIndex adds the provided object hash to the index with the provided mode and path
func ( t * TemporaryUploadRepository ) AddObjectToIndex ( mode , objectHash , objectPath string ) error {
2022-02-07 00:31:47 +05:30
if _ , err := git . NewCommand ( t . ctx , "update-index" , "--add" , "--replace" , "--cacheinfo" , mode , objectHash , objectPath ) . RunInDir ( t . basePath ) ; err != nil {
2019-11-11 17:16:28 +05:30
stderr := err . Error ( )
2019-04-17 21:36:35 +05:30
if matched , _ := regexp . MatchString ( ".*Invalid path '.*" , stderr ) ; matched {
return models . ErrFilePathInvalid {
Message : objectPath ,
Path : objectPath ,
}
}
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to add object to index: %s %s %s in temporary repo %s(%s) Error: %v" , mode , objectHash , objectPath , t . repo . FullName ( ) , t . basePath , err )
return fmt . Errorf ( "Unable to add object to index at %s in temporary repo %s Error: %v" , objectPath , t . repo . FullName ( ) , err )
2019-02-12 18:37:31 +05:30
}
return nil
}
// WriteTree writes the current index as a tree to the object db and returns its hash
func ( t * TemporaryUploadRepository ) WriteTree ( ) ( string , error ) {
2022-02-07 00:31:47 +05:30
stdout , err := git . NewCommand ( t . ctx , "write-tree" ) . RunInDir ( t . basePath )
2019-02-12 18:37:31 +05:30
if err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to write tree in temporary repo: %s(%s): Error: %v" , t . repo . FullName ( ) , t . basePath , err )
return "" , fmt . Errorf ( "Unable to write-tree in temporary repo for: %s Error: %v" , t . repo . FullName ( ) , err )
2019-02-12 18:37:31 +05:30
}
2019-11-11 17:16:28 +05:30
return strings . TrimSpace ( stdout ) , nil
2019-04-17 21:36:35 +05:30
}
2019-02-12 18:37:31 +05:30
2019-04-17 21:36:35 +05:30
// GetLastCommit gets the last commit ID SHA of the repo
func ( t * TemporaryUploadRepository ) GetLastCommit ( ) ( string , error ) {
return t . GetLastCommitByRef ( "HEAD" )
}
// GetLastCommitByRef gets the last commit ID SHA of the repo by ref
func ( t * TemporaryUploadRepository ) GetLastCommitByRef ( ref string ) ( string , error ) {
if ref == "" {
ref = "HEAD"
}
2022-02-07 00:31:47 +05:30
stdout , err := git . NewCommand ( t . ctx , "rev-parse" , ref ) . RunInDir ( t . basePath )
2019-04-17 21:36:35 +05:30
if err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to get last ref for %s in temporary repo: %s(%s): Error: %v" , ref , t . repo . FullName ( ) , t . basePath , err )
return "" , fmt . Errorf ( "Unable to rev-parse %s in temporary repo for: %s Error: %v" , ref , t . repo . FullName ( ) , err )
2019-04-17 21:36:35 +05:30
}
2019-11-11 17:16:28 +05:30
return strings . TrimSpace ( stdout ) , nil
2019-02-12 18:37:31 +05:30
}
// CommitTree creates a commit from a given tree for the user with provided message
2022-03-29 01:18:41 +05:30
func ( t * TemporaryUploadRepository ) CommitTree ( parent string , author , committer * user_model . User , treeHash , message string , signoff bool ) ( string , error ) {
return t . CommitTreeWithDate ( parent , author , committer , treeHash , message , signoff , time . Now ( ) , time . Now ( ) )
2019-12-24 08:03:52 +05:30
}
// CommitTreeWithDate creates a commit from a given tree for the user with provided message
2022-03-29 01:18:41 +05:30
func ( t * TemporaryUploadRepository ) CommitTreeWithDate ( parent string , author , committer * user_model . User , treeHash , message string , signoff bool , authorDate , committerDate time . Time ) ( string , error ) {
2019-04-17 21:36:35 +05:30
authorSig := author . NewGitSig ( )
committerSig := committer . NewGitSig ( )
2019-02-12 18:37:31 +05:30
2020-09-05 22:12:58 +05:30
err := git . LoadGitVersion ( )
2019-10-12 05:43:27 +05:30
if err != nil {
return "" , fmt . Errorf ( "Unable to get git version: %v" , err )
}
2019-02-12 18:37:31 +05:30
// Because this may call hooks we should pass in the environment
env := append ( os . Environ ( ) ,
2019-04-17 21:36:35 +05:30
"GIT_AUTHOR_NAME=" + authorSig . Name ,
"GIT_AUTHOR_EMAIL=" + authorSig . Email ,
2019-12-24 08:03:52 +05:30
"GIT_AUTHOR_DATE=" + authorDate . Format ( time . RFC3339 ) ,
"GIT_COMMITTER_DATE=" + committerDate . Format ( time . RFC3339 ) ,
2019-02-12 18:37:31 +05:30
)
2019-10-16 19:12:42 +05:30
2019-10-12 05:43:27 +05:30
messageBytes := new ( bytes . Buffer )
_ , _ = messageBytes . WriteString ( message )
_ , _ = messageBytes . WriteString ( "\n" )
2022-03-29 01:18:41 +05:30
var args [ ] string
if parent != "" {
args = [ ] string { "commit-tree" , treeHash , "-p" , parent }
} else {
args = [ ] string { "commit-tree" , treeHash }
}
2019-10-16 19:12:42 +05:30
// Determine if we should sign
2020-10-21 21:12:08 +05:30
if git . CheckGitVersionAtLeast ( "1.7.9" ) == nil {
2022-03-29 01:18:41 +05:30
var sign bool
var keyID string
var signer * git . Signature
if parent != "" {
sign , keyID , signer , _ = asymkey_service . SignCRUDAction ( t . ctx , t . repo . RepoPath ( ) , author , t . basePath , parent )
} else {
sign , keyID , signer , _ = asymkey_service . SignInitialCommit ( t . ctx , t . repo . RepoPath ( ) , author )
}
2019-10-16 19:12:42 +05:30
if sign {
args = append ( args , "-S" + keyID )
2021-12-10 06:57:50 +05:30
if t . repo . GetTrustModel ( ) == repo_model . CommitterTrustModel || t . repo . GetTrustModel ( ) == repo_model . CollaboratorCommitterTrustModel {
2020-09-19 22:14:55 +05:30
if committerSig . Name != authorSig . Name || committerSig . Email != authorSig . Email {
// Add trailers
_ , _ = messageBytes . WriteString ( "\n" )
2020-12-22 07:49:33 +05:30
_ , _ = messageBytes . WriteString ( "Co-authored-by: " )
2020-09-19 22:14:55 +05:30
_ , _ = messageBytes . WriteString ( committerSig . String ( ) )
_ , _ = messageBytes . WriteString ( "\n" )
2020-12-22 07:49:33 +05:30
_ , _ = messageBytes . WriteString ( "Co-committed-by: " )
2020-09-19 22:14:55 +05:30
_ , _ = messageBytes . WriteString ( committerSig . String ( ) )
_ , _ = messageBytes . WriteString ( "\n" )
}
committerSig = signer
}
2020-10-21 21:12:08 +05:30
} else if git . CheckGitVersionAtLeast ( "2.0.0" ) == nil {
2019-10-16 19:12:42 +05:30
args = append ( args , "--no-gpg-sign" )
}
2019-10-12 05:43:27 +05:30
}
2021-01-29 14:27:45 +05:30
if signoff {
// Signed-off-by
_ , _ = messageBytes . WriteString ( "\n" )
_ , _ = messageBytes . WriteString ( "Signed-off-by: " )
_ , _ = messageBytes . WriteString ( committerSig . String ( ) )
}
2020-09-19 22:14:55 +05:30
env = append ( env ,
"GIT_COMMITTER_NAME=" + committerSig . Name ,
"GIT_COMMITTER_EMAIL=" + committerSig . Email ,
)
2019-11-11 17:16:28 +05:30
stdout := new ( bytes . Buffer )
stderr := new ( bytes . Buffer )
2022-02-11 18:17:22 +05:30
if err := git . NewCommand ( t . ctx , args ... ) .
RunWithContext ( & git . RunContext {
Env : env ,
Timeout : - 1 ,
Dir : t . basePath ,
Stdin : messageBytes ,
Stdout : stdout ,
Stderr : stderr ,
} ) ; err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to commit-tree in temporary repo: %s (%s) Error: %v\nStdout: %s\nStderr: %s" ,
t . repo . FullName ( ) , t . basePath , err , stdout , stderr )
return "" , fmt . Errorf ( "Unable to commit-tree in temporary repo: %s Error: %v\nStdout: %s\nStderr: %s" ,
t . repo . FullName ( ) , err , stdout , stderr )
2019-02-12 18:37:31 +05:30
}
2019-11-11 17:16:28 +05:30
return strings . TrimSpace ( stdout . String ( ) ) , nil
2019-02-12 18:37:31 +05:30
}
// Push the provided commitHash to the repository branch by the provided user
2021-12-20 10:11:31 +05:30
func ( t * TemporaryUploadRepository ) Push ( doer * user_model . User , commitHash , branch string ) error {
2019-02-12 18:37:31 +05:30
// Because calls hooks we need to pass in the environment
2019-05-11 20:59:17 +05:30
env := models . PushingEnvironment ( doer , t . repo )
2022-01-20 04:56:57 +05:30
if err := git . Push ( t . ctx , t . basePath , git . PushOptions {
2020-03-28 09:43:18 +05:30
Remote : t . repo . RepoPath ( ) ,
2021-12-02 12:58:08 +05:30
Branch : strings . TrimSpace ( commitHash ) + ":" + git . BranchPrefix + strings . TrimSpace ( branch ) ,
2020-03-28 09:43:18 +05:30
Env : env ,
} ) ; err != nil {
if git . IsErrPushOutOfDate ( err ) {
return err
} else if git . IsErrPushRejected ( err ) {
rejectErr := err . ( * git . ErrPushRejected )
log . Info ( "Unable to push back to repo from temporary repo due to rejection: %s (%s)\nStdout: %s\nStderr: %s\nError: %v" ,
t . repo . FullName ( ) , t . basePath , rejectErr . StdOut , rejectErr . StdErr , rejectErr . Err )
2020-02-22 18:38:48 +05:30
return err
}
2020-03-28 09:43:18 +05:30
log . Error ( "Unable to push back to repo from temporary repo: %s (%s)\nError: %v" ,
t . repo . FullName ( ) , t . basePath , err )
2019-11-11 17:16:28 +05:30
return fmt . Errorf ( "Unable to push back to repo from temporary repo: %s (%s) Error: %v" ,
t . repo . FullName ( ) , t . basePath , err )
2019-02-12 18:37:31 +05:30
}
return nil
}
// DiffIndex returns a Diff of the current index to the head
2019-11-11 17:16:28 +05:30
func ( t * TemporaryUploadRepository ) DiffIndex ( ) ( * gitdiff . Diff , error ) {
stdoutReader , stdoutWriter , err := os . Pipe ( )
2019-02-12 18:37:31 +05:30
if err != nil {
2019-11-11 17:16:28 +05:30
log . Error ( "Unable to open stdout pipe: %v" , err )
return nil , fmt . Errorf ( "Unable to open stdout pipe: %v" , err )
2019-02-12 18:37:31 +05:30
}
2019-11-11 17:16:28 +05:30
defer func ( ) {
_ = stdoutReader . Close ( )
_ = stdoutWriter . Close ( )
} ( )
stderr := new ( bytes . Buffer )
var diff * gitdiff . Diff
var finalErr error
2022-02-07 00:31:47 +05:30
if err := git . NewCommand ( t . ctx , "diff-index" , "--src-prefix=\\a/" , "--dst-prefix=\\b/" , "--cached" , "-p" , "HEAD" ) .
2022-02-11 18:17:22 +05:30
RunWithContext ( & git . RunContext {
Timeout : 30 * time . Second ,
Dir : t . basePath ,
Stdout : stdoutWriter ,
Stderr : stderr ,
PipelineFunc : func ( ctx context . Context , cancel context . CancelFunc ) error {
_ = stdoutWriter . Close ( )
diff , finalErr = gitdiff . ParsePatch ( setting . Git . MaxGitDiffLines , setting . Git . MaxGitDiffLineCharacters , setting . Git . MaxGitDiffFiles , stdoutReader , "" )
if finalErr != nil {
log . Error ( "ParsePatch: %v" , finalErr )
cancel ( )
}
_ = stdoutReader . Close ( )
return finalErr
} ,
2019-11-11 17:16:28 +05:30
} ) ; err != nil {
if finalErr != nil {
log . Error ( "Unable to ParsePatch in temporary repo %s (%s). Error: %v" , t . repo . FullName ( ) , t . basePath , finalErr )
return nil , finalErr
}
log . Error ( "Unable to run diff-index pipeline in temporary repo %s (%s). Error: %v\nStderr: %s" ,
t . repo . FullName ( ) , t . basePath , err , stderr )
return nil , fmt . Errorf ( "Unable to run diff-index pipeline in temporary repo %s. Error: %v\nStderr: %s" ,
t . repo . FullName ( ) , err , stderr )
2019-02-12 18:37:31 +05:30
}
2022-01-20 04:56:57 +05:30
diff . NumFiles , diff . TotalAddition , diff . TotalDeletion , err = git . GetDiffShortStat ( t . ctx , t . basePath , "--cached" , "HEAD" )
2020-05-26 11:28:07 +05:30
if err != nil {
return nil , err
}
2019-02-12 18:37:31 +05:30
return diff , nil
}
2019-04-17 21:36:35 +05:30
// GetBranchCommit Gets the commit object of the given branch
func ( t * TemporaryUploadRepository ) GetBranchCommit ( branch string ) ( * git . Commit , error ) {
if t . gitRepo == nil {
return nil , fmt . Errorf ( "repository has not been cloned" )
}
return t . gitRepo . GetBranchCommit ( branch )
}
// GetCommit Gets the commit object of the given commit ID
func ( t * TemporaryUploadRepository ) GetCommit ( commitID string ) ( * git . Commit , error ) {
if t . gitRepo == nil {
return nil , fmt . Errorf ( "repository has not been cloned" )
}
return t . gitRepo . GetCommit ( commitID )
}