commit_archive.go 717 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "fmt"
  7. "github.com/Unknwon/com"
  8. )
  9. type ArchiveType int
  10. const (
  11. ZIP ArchiveType = iota + 1
  12. TARGZ
  13. )
  14. func (c *Commit) CreateArchive(path string, archiveType ArchiveType) error {
  15. var format string
  16. switch archiveType {
  17. case ZIP:
  18. format = "zip"
  19. case TARGZ:
  20. format = "tar.gz"
  21. default:
  22. return fmt.Errorf("unknown format: %v", archiveType)
  23. }
  24. _, stderr, err := com.ExecCmdDir(c.repo.Path, "git", "archive", "--format="+format, "-o", path, c.Id.String())
  25. if err != nil {
  26. return fmt.Errorf("%s", stderr)
  27. }
  28. return nil
  29. }