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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "path/filepath"
  10. "strings"
  11. )
  12. // ArchiveType archive types
  13. type ArchiveType int
  14. const (
  15. // ZIP zip archive type
  16. ZIP ArchiveType = iota + 1
  17. // TARGZ tar gz archive type
  18. TARGZ
  19. // BUNDLE bundle archive type
  20. BUNDLE
  21. )
  22. // String converts an ArchiveType to string
  23. func (a ArchiveType) String() string {
  24. switch a {
  25. case ZIP:
  26. return "zip"
  27. case TARGZ:
  28. return "tar.gz"
  29. case BUNDLE:
  30. return "bundle"
  31. }
  32. return "unknown"
  33. }
  34. func ToArchiveType(s string) ArchiveType {
  35. switch s {
  36. case "zip":
  37. return ZIP
  38. case "tar.gz":
  39. return TARGZ
  40. case "bundle":
  41. return BUNDLE
  42. }
  43. return 0
  44. }
  45. // CreateArchive create archive content to the target path
  46. func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error {
  47. if format.String() == "unknown" {
  48. return fmt.Errorf("unknown format: %v", format)
  49. }
  50. cmd := NewCommand(ctx, "archive")
  51. if usePrefix {
  52. cmd.AddOptionFormat("--prefix=%s", filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/")
  53. }
  54. cmd.AddOptionFormat("--format=%s", format.String())
  55. cmd.AddDynamicArguments(commitID)
  56. var stderr strings.Builder
  57. err := cmd.Run(&RunOpts{
  58. Dir: repo.Path,
  59. Stdout: target,
  60. Stderr: &stderr,
  61. })
  62. if err != nil {
  63. return ConcatenateError(err, stderr.String())
  64. }
  65. return nil
  66. }