git.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package git
  2. import (
  3. "buildme/utils"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "buildme"
  8. "dev.hexasoftware.com/hxs/prettylog"
  9. git "gopkg.in/src-d/go-git.v4"
  10. )
  11. var (
  12. log = prettylog.New("GitFetcher")
  13. )
  14. func init() {
  15. // Register fetcher
  16. buildme.RegisterFetcher("git", &Git{})
  17. }
  18. type Git struct{}
  19. // Fetcher return a project
  20. func (g *Git) Fetch(fpath string) (*buildme.Project, error) {
  21. log.Println("Building git repo:", fpath)
  22. // File possibilities, does not cover the folder/subfolder
  23. if fpath == "." || fpath[0:2] == "./" || fpath[0] == '/' {
  24. absPath, err := filepath.Abs(fpath)
  25. if err != nil {
  26. return nil, err
  27. }
  28. fpath = "file://" + absPath
  29. }
  30. // Create tmpdir to clone project
  31. tmpdirname, err := ioutil.TempDir(buildme.TempDir(), "buildme.")
  32. if err != nil {
  33. return nil, err
  34. }
  35. log.Println("Cloning to:", tmpdirname)
  36. _, err = git.PlainClone(tmpdirname, false, &git.CloneOptions{URL: fpath, Progress: os.Stdout})
  37. if err != nil {
  38. log.Println("Err:", err)
  39. return nil, err
  40. }
  41. projName := filepath.Base(fpath)
  42. //git.
  43. log.Println("Taring content")
  44. /*tarfile, err := ioutil.TempFile("/tmp", "tarbuildme.")
  45. reader, err := utils.TarDir(tmpdir, tarfile)*/
  46. // Create a temporary tar file from tmpdirname
  47. tarFile, err := utils.TempTar(tmpdirname, buildme.TempDir(), "buildme.tar")
  48. log.Println("Tar file:", tarFile.Name())
  49. log.Println("Removing dir", tmpdirname)
  50. os.RemoveAll(tmpdirname)
  51. proj := buildme.NewProject(projName)
  52. proj.SetTarFile(tarFile.Name(), true) // Set as temporary will delete as soon as project closes
  53. return proj, nil
  54. }