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.

64 lines
1.3 KiB

  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. "context"
  8. "fmt"
  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. )
  20. // String converts an ArchiveType to string
  21. func (a ArchiveType) String() string {
  22. switch a {
  23. case ZIP:
  24. return "zip"
  25. case TARGZ:
  26. return "tar.gz"
  27. }
  28. return "unknown"
  29. }
  30. // CreateArchiveOpts represents options for creating an archive
  31. type CreateArchiveOpts struct {
  32. Format ArchiveType
  33. Prefix bool
  34. }
  35. // CreateArchive create archive content to the target path
  36. func (c *Commit) CreateArchive(ctx context.Context, target string, opts CreateArchiveOpts) error {
  37. if opts.Format.String() == "unknown" {
  38. return fmt.Errorf("unknown format: %v", opts.Format)
  39. }
  40. args := []string{
  41. "archive",
  42. }
  43. if opts.Prefix {
  44. args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/")
  45. }
  46. args = append(args,
  47. "--format="+opts.Format.String(),
  48. "-o",
  49. target,
  50. c.ID.String(),
  51. )
  52. _, err := NewCommandContext(ctx, args...).RunInDir(c.repo.Path)
  53. return err
  54. }