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.

repo_archive.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "context"
  7. "fmt"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. // ArchiveType archive types
  14. type ArchiveType int
  15. const (
  16. // ZIP zip archive type
  17. ZIP ArchiveType = iota + 1
  18. // TARGZ tar gz archive type
  19. TARGZ
  20. // BUNDLE bundle archive type
  21. BUNDLE
  22. )
  23. // String converts an ArchiveType to string
  24. func (a ArchiveType) String() string {
  25. switch a {
  26. case ZIP:
  27. return "zip"
  28. case TARGZ:
  29. return "tar.gz"
  30. case BUNDLE:
  31. return "bundle"
  32. }
  33. return "unknown"
  34. }
  35. func ToArchiveType(s string) ArchiveType {
  36. switch s {
  37. case "zip":
  38. return ZIP
  39. case "tar.gz":
  40. return TARGZ
  41. case "bundle":
  42. return BUNDLE
  43. }
  44. return 0
  45. }
  46. // CreateArchive create archive content to the target path
  47. func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error {
  48. if format.String() == "unknown" {
  49. return fmt.Errorf("unknown format: %v", format)
  50. }
  51. cmd := NewCommand(ctx, "archive")
  52. if usePrefix {
  53. cmd.AddOptionFormat("--prefix=%s", filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/")
  54. }
  55. cmd.AddOptionFormat("--format=%s", format.String())
  56. cmd.AddDynamicArguments(commitID)
  57. // Avoid LFS hooks getting installed because of /etc/gitconfig, which can break pull requests.
  58. env := append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1")
  59. var stderr strings.Builder
  60. err := cmd.Run(&RunOpts{
  61. Dir: repo.Path,
  62. Stdout: target,
  63. Stderr: &stderr,
  64. Env: env,
  65. })
  66. if err != nil {
  67. return ConcatenateError(err, stderr.String())
  68. }
  69. return nil
  70. }