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.

commit_archive.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "fmt"
  8. "path/filepath"
  9. "strings"
  10. )
  11. // ArchiveType archive types
  12. type ArchiveType int
  13. const (
  14. // ZIP zip archive type
  15. ZIP ArchiveType = iota + 1
  16. // TARGZ tar gz archive type
  17. TARGZ
  18. )
  19. // String converts an ArchiveType to string
  20. func (a ArchiveType) String() string {
  21. switch a {
  22. case ZIP:
  23. return "zip"
  24. case TARGZ:
  25. return "tar.gz"
  26. }
  27. return "unknown"
  28. }
  29. // CreateArchiveOpts represents options for creating an archive
  30. type CreateArchiveOpts struct {
  31. Format ArchiveType
  32. Prefix bool
  33. }
  34. // CreateArchive create archive content to the target path
  35. func (c *Commit) CreateArchive(target string, opts CreateArchiveOpts) error {
  36. if opts.Format.String() == "unknown" {
  37. return fmt.Errorf("unknown format: %v", opts.Format)
  38. }
  39. args := []string{
  40. "archive",
  41. }
  42. if opts.Prefix {
  43. args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/")
  44. }
  45. args = append(args,
  46. "--format="+opts.Format.String(),
  47. "-o",
  48. target,
  49. c.ID.String(),
  50. )
  51. _, err := NewCommand(args...).RunInDir(c.repo.Path)
  52. return err
  53. }