You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temp_repo.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package files
  5. import (
  6. "bytes"
  7. "context"
  8. "fmt"
  9. "io"
  10. "os"
  11. "regexp"
  12. "strings"
  13. "time"
  14. "code.gitea.io/gitea/models"
  15. repo_model "code.gitea.io/gitea/models/repo"
  16. user_model "code.gitea.io/gitea/models/user"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/log"
  19. "code.gitea.io/gitea/modules/setting"
  20. asymkey_service "code.gitea.io/gitea/services/asymkey"
  21. "code.gitea.io/gitea/services/gitdiff"
  22. )
  23. // TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone
  24. type TemporaryUploadRepository struct {
  25. repo *repo_model.Repository
  26. gitRepo *git.Repository
  27. basePath string
  28. }
  29. // NewTemporaryUploadRepository creates a new temporary upload repository
  30. func NewTemporaryUploadRepository(repo *repo_model.Repository) (*TemporaryUploadRepository, error) {
  31. basePath, err := models.CreateTemporaryPath("upload")
  32. if err != nil {
  33. return nil, err
  34. }
  35. t := &TemporaryUploadRepository{repo: repo, basePath: basePath}
  36. return t, nil
  37. }
  38. // Close the repository cleaning up all files
  39. func (t *TemporaryUploadRepository) Close() {
  40. defer t.gitRepo.Close()
  41. if err := models.RemoveTemporaryPath(t.basePath); err != nil {
  42. log.Error("Failed to remove temporary path %s: %v", t.basePath, err)
  43. }
  44. }
  45. // Clone the base repository to our path and set branch as the HEAD
  46. func (t *TemporaryUploadRepository) Clone(branch string) error {
  47. if _, err := git.NewCommand("clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath).Run(); err != nil {
  48. stderr := err.Error()
  49. if matched, _ := regexp.MatchString(".*Remote branch .* not found in upstream origin.*", stderr); matched {
  50. return git.ErrBranchNotExist{
  51. Name: branch,
  52. }
  53. } else if matched, _ := regexp.MatchString(".* repository .* does not exist.*", stderr); matched {
  54. return repo_model.ErrRepoNotExist{
  55. ID: t.repo.ID,
  56. UID: t.repo.OwnerID,
  57. OwnerName: t.repo.OwnerName,
  58. Name: t.repo.Name,
  59. }
  60. } else {
  61. return fmt.Errorf("Clone: %v %s", err, stderr)
  62. }
  63. }
  64. gitRepo, err := git.OpenRepository(t.basePath)
  65. if err != nil {
  66. return err
  67. }
  68. t.gitRepo = gitRepo
  69. return nil
  70. }
  71. // SetDefaultIndex sets the git index to our HEAD
  72. func (t *TemporaryUploadRepository) SetDefaultIndex() error {
  73. if _, err := git.NewCommand("read-tree", "HEAD").RunInDir(t.basePath); err != nil {
  74. return fmt.Errorf("SetDefaultIndex: %v", err)
  75. }
  76. return nil
  77. }
  78. // LsFiles checks if the given filename arguments are in the index
  79. func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, error) {
  80. stdOut := new(bytes.Buffer)
  81. stdErr := new(bytes.Buffer)
  82. cmdArgs := []string{"ls-files", "-z", "--"}
  83. for _, arg := range filenames {
  84. if arg != "" {
  85. cmdArgs = append(cmdArgs, arg)
  86. }
  87. }
  88. if err := git.NewCommand(cmdArgs...).RunInDirPipeline(t.basePath, stdOut, stdErr); err != nil {
  89. 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())
  90. 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())
  91. return nil, err
  92. }
  93. filelist := make([]string, len(filenames))
  94. for _, line := range bytes.Split(stdOut.Bytes(), []byte{'\000'}) {
  95. filelist = append(filelist, string(line))
  96. }
  97. return filelist, nil
  98. }
  99. // RemoveFilesFromIndex removes the given files from the index
  100. func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) error {
  101. stdOut := new(bytes.Buffer)
  102. stdErr := new(bytes.Buffer)
  103. stdIn := new(bytes.Buffer)
  104. for _, file := range filenames {
  105. if file != "" {
  106. stdIn.WriteString("0 0000000000000000000000000000000000000000\t")
  107. stdIn.WriteString(file)
  108. stdIn.WriteByte('\000')
  109. }
  110. }
  111. if err := git.NewCommand("update-index", "--remove", "-z", "--index-info").RunInDirFullPipeline(t.basePath, stdOut, stdErr, stdIn); err != nil {
  112. 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())
  113. 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())
  114. }
  115. return nil
  116. }
  117. // HashObject writes the provided content to the object db and returns its hash
  118. func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error) {
  119. stdOut := new(bytes.Buffer)
  120. stdErr := new(bytes.Buffer)
  121. if err := git.NewCommand("hash-object", "-w", "--stdin").RunInDirFullPipeline(t.basePath, stdOut, stdErr, content); err != nil {
  122. 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())
  123. 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())
  124. }
  125. return strings.TrimSpace(stdOut.String()), nil
  126. }
  127. // AddObjectToIndex adds the provided object hash to the index with the provided mode and path
  128. func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPath string) error {
  129. if _, err := git.NewCommand("update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath).RunInDir(t.basePath); err != nil {
  130. stderr := err.Error()
  131. if matched, _ := regexp.MatchString(".*Invalid path '.*", stderr); matched {
  132. return models.ErrFilePathInvalid{
  133. Message: objectPath,
  134. Path: objectPath,
  135. }
  136. }
  137. 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)
  138. return fmt.Errorf("Unable to add object to index at %s in temporary repo %s Error: %v", objectPath, t.repo.FullName(), err)
  139. }
  140. return nil
  141. }
  142. // WriteTree writes the current index as a tree to the object db and returns its hash
  143. func (t *TemporaryUploadRepository) WriteTree() (string, error) {
  144. stdout, err := git.NewCommand("write-tree").RunInDir(t.basePath)
  145. if err != nil {
  146. log.Error("Unable to write tree in temporary repo: %s(%s): Error: %v", t.repo.FullName(), t.basePath, err)
  147. return "", fmt.Errorf("Unable to write-tree in temporary repo for: %s Error: %v", t.repo.FullName(), err)
  148. }
  149. return strings.TrimSpace(stdout), nil
  150. }
  151. // GetLastCommit gets the last commit ID SHA of the repo
  152. func (t *TemporaryUploadRepository) GetLastCommit() (string, error) {
  153. return t.GetLastCommitByRef("HEAD")
  154. }
  155. // GetLastCommitByRef gets the last commit ID SHA of the repo by ref
  156. func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, error) {
  157. if ref == "" {
  158. ref = "HEAD"
  159. }
  160. stdout, err := git.NewCommand("rev-parse", ref).RunInDir(t.basePath)
  161. if err != nil {
  162. log.Error("Unable to get last ref for %s in temporary repo: %s(%s): Error: %v", ref, t.repo.FullName(), t.basePath, err)
  163. return "", fmt.Errorf("Unable to rev-parse %s in temporary repo for: %s Error: %v", ref, t.repo.FullName(), err)
  164. }
  165. return strings.TrimSpace(stdout), nil
  166. }
  167. // CommitTree creates a commit from a given tree for the user with provided message
  168. func (t *TemporaryUploadRepository) CommitTree(author, committer *user_model.User, treeHash string, message string, signoff bool) (string, error) {
  169. return t.CommitTreeWithDate(author, committer, treeHash, message, signoff, time.Now(), time.Now())
  170. }
  171. // CommitTreeWithDate creates a commit from a given tree for the user with provided message
  172. func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_model.User, treeHash string, message string, signoff bool, authorDate, committerDate time.Time) (string, error) {
  173. authorSig := author.NewGitSig()
  174. committerSig := committer.NewGitSig()
  175. err := git.LoadGitVersion()
  176. if err != nil {
  177. return "", fmt.Errorf("Unable to get git version: %v", err)
  178. }
  179. // Because this may call hooks we should pass in the environment
  180. env := append(os.Environ(),
  181. "GIT_AUTHOR_NAME="+authorSig.Name,
  182. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  183. "GIT_AUTHOR_DATE="+authorDate.Format(time.RFC3339),
  184. "GIT_COMMITTER_DATE="+committerDate.Format(time.RFC3339),
  185. )
  186. messageBytes := new(bytes.Buffer)
  187. _, _ = messageBytes.WriteString(message)
  188. _, _ = messageBytes.WriteString("\n")
  189. args := []string{"commit-tree", treeHash, "-p", "HEAD"}
  190. // Determine if we should sign
  191. if git.CheckGitVersionAtLeast("1.7.9") == nil {
  192. sign, keyID, signer, _ := asymkey_service.SignCRUDAction(t.repo.RepoPath(), author, t.basePath, "HEAD")
  193. if sign {
  194. args = append(args, "-S"+keyID)
  195. if t.repo.GetTrustModel() == repo_model.CommitterTrustModel || t.repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
  196. if committerSig.Name != authorSig.Name || committerSig.Email != authorSig.Email {
  197. // Add trailers
  198. _, _ = messageBytes.WriteString("\n")
  199. _, _ = messageBytes.WriteString("Co-authored-by: ")
  200. _, _ = messageBytes.WriteString(committerSig.String())
  201. _, _ = messageBytes.WriteString("\n")
  202. _, _ = messageBytes.WriteString("Co-committed-by: ")
  203. _, _ = messageBytes.WriteString(committerSig.String())
  204. _, _ = messageBytes.WriteString("\n")
  205. }
  206. committerSig = signer
  207. }
  208. } else if git.CheckGitVersionAtLeast("2.0.0") == nil {
  209. args = append(args, "--no-gpg-sign")
  210. }
  211. }
  212. if signoff {
  213. // Signed-off-by
  214. _, _ = messageBytes.WriteString("\n")
  215. _, _ = messageBytes.WriteString("Signed-off-by: ")
  216. _, _ = messageBytes.WriteString(committerSig.String())
  217. }
  218. env = append(env,
  219. "GIT_COMMITTER_NAME="+committerSig.Name,
  220. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  221. )
  222. stdout := new(bytes.Buffer)
  223. stderr := new(bytes.Buffer)
  224. if err := git.NewCommand(args...).RunInDirTimeoutEnvFullPipeline(env, -1, t.basePath, stdout, stderr, messageBytes); err != nil {
  225. log.Error("Unable to commit-tree in temporary repo: %s (%s) Error: %v\nStdout: %s\nStderr: %s",
  226. t.repo.FullName(), t.basePath, err, stdout, stderr)
  227. return "", fmt.Errorf("Unable to commit-tree in temporary repo: %s Error: %v\nStdout: %s\nStderr: %s",
  228. t.repo.FullName(), err, stdout, stderr)
  229. }
  230. return strings.TrimSpace(stdout.String()), nil
  231. }
  232. // Push the provided commitHash to the repository branch by the provided user
  233. func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash string, branch string) error {
  234. // Because calls hooks we need to pass in the environment
  235. env := models.PushingEnvironment(doer, t.repo)
  236. if err := git.Push(t.gitRepo.Ctx, t.basePath, git.PushOptions{
  237. Remote: t.repo.RepoPath(),
  238. Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch),
  239. Env: env,
  240. }); err != nil {
  241. if git.IsErrPushOutOfDate(err) {
  242. return err
  243. } else if git.IsErrPushRejected(err) {
  244. rejectErr := err.(*git.ErrPushRejected)
  245. log.Info("Unable to push back to repo from temporary repo due to rejection: %s (%s)\nStdout: %s\nStderr: %s\nError: %v",
  246. t.repo.FullName(), t.basePath, rejectErr.StdOut, rejectErr.StdErr, rejectErr.Err)
  247. return err
  248. }
  249. log.Error("Unable to push back to repo from temporary repo: %s (%s)\nError: %v",
  250. t.repo.FullName(), t.basePath, err)
  251. return fmt.Errorf("Unable to push back to repo from temporary repo: %s (%s) Error: %v",
  252. t.repo.FullName(), t.basePath, err)
  253. }
  254. return nil
  255. }
  256. // DiffIndex returns a Diff of the current index to the head
  257. func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {
  258. stdoutReader, stdoutWriter, err := os.Pipe()
  259. if err != nil {
  260. log.Error("Unable to open stdout pipe: %v", err)
  261. return nil, fmt.Errorf("Unable to open stdout pipe: %v", err)
  262. }
  263. defer func() {
  264. _ = stdoutReader.Close()
  265. _ = stdoutWriter.Close()
  266. }()
  267. stderr := new(bytes.Buffer)
  268. var diff *gitdiff.Diff
  269. var finalErr error
  270. if err := git.NewCommand("diff-index", "--src-prefix=\\a/", "--dst-prefix=\\b/", "--cached", "-p", "HEAD").
  271. RunInDirTimeoutEnvFullPipelineFunc(nil, 30*time.Second, t.basePath, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error {
  272. _ = stdoutWriter.Close()
  273. diff, finalErr = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdoutReader, "")
  274. if finalErr != nil {
  275. log.Error("ParsePatch: %v", finalErr)
  276. cancel()
  277. }
  278. _ = stdoutReader.Close()
  279. return finalErr
  280. }); err != nil {
  281. if finalErr != nil {
  282. log.Error("Unable to ParsePatch in temporary repo %s (%s). Error: %v", t.repo.FullName(), t.basePath, finalErr)
  283. return nil, finalErr
  284. }
  285. log.Error("Unable to run diff-index pipeline in temporary repo %s (%s). Error: %v\nStderr: %s",
  286. t.repo.FullName(), t.basePath, err, stderr)
  287. return nil, fmt.Errorf("Unable to run diff-index pipeline in temporary repo %s. Error: %v\nStderr: %s",
  288. t.repo.FullName(), err, stderr)
  289. }
  290. diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.basePath, "--cached", "HEAD")
  291. if err != nil {
  292. return nil, err
  293. }
  294. return diff, nil
  295. }
  296. // GetBranchCommit Gets the commit object of the given branch
  297. func (t *TemporaryUploadRepository) GetBranchCommit(branch string) (*git.Commit, error) {
  298. if t.gitRepo == nil {
  299. return nil, fmt.Errorf("repository has not been cloned")
  300. }
  301. return t.gitRepo.GetBranchCommit(branch)
  302. }
  303. // GetCommit Gets the commit object of the given commit ID
  304. func (t *TemporaryUploadRepository) GetCommit(commitID string) (*git.Commit, error) {
  305. if t.gitRepo == nil {
  306. return nil, fmt.Errorf("repository has not been cloned")
  307. }
  308. return t.gitRepo.GetCommit(commitID)
  309. }