commit_archive.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2015 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. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/G-Node/libgin/libgin"
  11. )
  12. type ArchiveType int
  13. const (
  14. ArchiveZip ArchiveType = iota + 1
  15. ArchiveTarGz
  16. ArchiveGIN
  17. )
  18. func (c *Commit) CreateArchive(target string, archiveType ArchiveType, cloneL string) error {
  19. var format string
  20. switch archiveType {
  21. case ArchiveZip:
  22. format = "zip"
  23. case ArchiveTarGz:
  24. format = "tar.gz"
  25. case ArchiveGIN:
  26. // tmppath := setting.Repository.Upload.TempPath // Circular module dependency
  27. tmppath := "/data/tmp/uploads" // live config location
  28. to := filepath.Join(tmppath, "archives", filepath.Base(strings.TrimSuffix(c.repo.Path, ".git")))
  29. defer os.RemoveAll(to)
  30. _, err := NewCommand("clone", c.repo.Path, to).RunTimeout(-1)
  31. if err != nil {
  32. return err
  33. }
  34. _, err = NewCommand("remote", "set-url", "origin", cloneL).RunInDirTimeout(-1, to)
  35. if err != nil {
  36. return err
  37. }
  38. fp, err := os.Create(target)
  39. defer fp.Close()
  40. if err != nil {
  41. return err
  42. }
  43. err = libgin.MakeZip(fp, to)
  44. return err
  45. default:
  46. return fmt.Errorf("unknown format: %v", archiveType)
  47. }
  48. _, err := NewCommand("archive", "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/", "--format="+format, "-o", target, c.ID.String()).RunInDir(c.repo.Path)
  49. return err
  50. }