func syncGit(b *ui.Task, dir, source, finalDest string) (err error) {
	task := b.SubProgress("sync", 1)
	defer func() {
		task.Done()
		now := time.Now()
		if err == nil {
			err = errors.WithStack(os.Chtimes(finalDest, now, now))
		}
	}()
	info, _ := os.Stat(filepath.Join(finalDest, ".git"))
	if info != nil {
		err = util.RunInDir(b, finalDest, "git", "pull")
		if err == nil {
			return nil
		}
	}
	_ = os.RemoveAll(finalDest)
	dest, err := ioutil.TempDir(dir, filepath.Base(finalDest)+"-*")
	if err != nil {
		return errors.WithStack(err)
	}
	defer os.RemoveAll(dest)
	if err = util.RunInDir(b, dest, "git", "clone", "--depth=1", source, dest); err != nil {
		return errors.WithStack(err)
	}
	if err = os.Rename(dest, finalDest); err != nil && !os.IsExist(err) {  
		return errors.WithStack(err)
	}
	return nil
}
Atomically clone git repo.
package sources
import (
	"github.com/cashapp/hermit/ui"
	"github.com/cashapp/hermit/util"
	"github.com/pkg/errors"
	"io/fs"
	"io/ioutil"
	"os"
	"path/filepath"
	"time"
)
type GitSource struct {
	fs        *uriFS
	sourceDir string
	path      string
}
func NewGitSource(uri string, sourceDir string) *GitSource {
	key := util.Hash(uri)
	path := filepath.Join(sourceDir, key)
	return &GitSource{&uriFS{
		uri: uri,
		FS:  os.DirFS(path),
	}, sourceDir, path}
}
func (s *GitSource) Sync(p *ui.UI, force bool) error {  
	info, _ := os.Stat(s.path)
	task := p.Task(s.fs.uri)
	if info == nil || force || time.Since(info.ModTime()) >= SyncFrequency {
		err := s.ensureSourcesDirExists()
		if err != nil {
			return errors.WithStack(err)
		}
		err = syncGit(task, s.sourceDir, s.fs.uri, s.path)
		if err != nil {
			if info != nil {
				task.Warnf("git sync failed: %s", err)
			} else {
				return errors.Wrap(err, "git sync failed")
			}
		}
	} else {
		task.Debugf("Sync skipped, updated within the last %s", SyncFrequency)
	}
	return nil
}
func (s *GitSource) URI() string {  
	return s.fs.uri
}
func (s *GitSource) Bundle() fs.FS {  
	return s.fs
}
func (s *GitSource) ensureSourcesDirExists() error {
	if err := os.MkdirAll(s.sourceDir, 0700); err != nil {
		return errors.WithStack(err)
	}
	return nil
}