Esempio n. 1
0
func updateCookbook(c config.CodeResourceOperator) error {
	path := c.CacheRepoPath()
	if _, err := os.Stat(path + "/.git"); os.IsNotExist(err) {
		gitCloneOption := new(git.CloneOptions)
		fmt.Println(c.RemotoRepoUrl())
		if _, err := git.Clone(c.RemotoRepoUrl(), path, gitCloneOption); err != nil {
			return err
		}
		fmt.Printf("clone to locat from %s\n", c.RemotoRepoUrl())
	} else {
		repo, err := git.OpenRepository(path)
		if err != nil {
			return err
		}

		remote, err := repo.Remotes.Lookup("origin")
		fmt.Printf("fetching to locat from %s\n", remote.Url())
		if err != nil {
			return err
		}
		if err := remote.Fetch([]string{}, nil, ""); err != nil {
			return err
		}

		remoteLs, err := remote.Ls("HEAD")
		if err != nil {
			return err
		}
		remoteOid := remoteLs[0].Id
		headCommit, err := repo.LookupCommit(remoteOid)
		if err != nil {
			return err
		}
		if err := repo.ResetToCommit(headCommit, git.ResetHard, &git.CheckoutOpts{}); err != nil {
			return err
		}
		// TODO use short ref id
		fmt.Printf(" -- now %s\n", remoteOid)
	}
	return nil
}