commit_archive.go 717 B

12345678910111213141516171819202122232425262728293031323334353637
  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. }