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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "io"
  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. // CreateArchive create archive content to the target path
  36. func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error {
  37. if format.String() == "unknown" {
  38. return fmt.Errorf("unknown format: %v", format)
  39. }
  40. args := []string{
  41. "archive",
  42. }
  43. if usePrefix {
  44. args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/")
  45. }
  46. args = append(args,
  47. "--format="+format.String(),
  48. commitID,
  49. )
  50. var stderr strings.Builder
  51. err := NewCommandContext(ctx, args...).RunInDirPipeline(repo.Path, target, &stderr)
  52. if err != nil {
  53. return ConcatenateError(err, stderr.String())
  54. }
  55. return nil
  56. }