forked from tangled.org/core
this repo has no description
1package git 2 3import ( 4 "errors" 5 "fmt" 6 "os/exec" 7 8 "github.com/go-git/go-git/v5" 9 "github.com/go-git/go-git/v5/config" 10) 11 12func Fork(repoPath, source string) error { 13 _, err := git.PlainClone(repoPath, true, &git.CloneOptions{ 14 URL: source, 15 SingleBranch: false, 16 }) 17 18 if err != nil { 19 return fmt.Errorf("failed to bare clone repository: %w", err) 20 } 21 22 err = exec.Command("git", "-C", repoPath, "config", "receive.hideRefs", "refs/hidden").Run() 23 if err != nil { 24 return fmt.Errorf("failed to configure hidden refs: %w", err) 25 } 26 27 return nil 28} 29 30func (g *GitRepo) Sync(branch string) error { 31 fetchOpts := &git.FetchOptions{ 32 RefSpecs: []config.RefSpec{ 33 config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/heads/%s", branch, branch)), 34 }, 35 } 36 37 err := g.r.Fetch(fetchOpts) 38 if errors.Is(git.NoErrAlreadyUpToDate, err) { 39 return nil 40 } else if err != nil { 41 return fmt.Errorf("failed to fetch origin branch: %s: %w", branch, err) 42 } 43 return nil 44} 45 46// TrackHiddenRemoteRef tracks a hidden remote in the repository. For example, 47// if the feature branch on the fork (forkRef) is feature-1, and the remoteRef, 48// i.e. the branch we want to merge into, is main, this will result in a refspec: 49// 50// +refs/heads/main:refs/hidden/feature-1/main 51func (g *GitRepo) TrackHiddenRemoteRef(forkRef, remoteRef string) error { 52 fetchOpts := &git.FetchOptions{ 53 RefSpecs: []config.RefSpec{ 54 config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/hidden/%s/%s", remoteRef, forkRef, remoteRef)), 55 }, 56 RemoteName: "origin", 57 } 58 59 err := g.r.Fetch(fetchOpts) 60 if errors.Is(git.NoErrAlreadyUpToDate, err) { 61 return nil 62 } else if err != nil { 63 return fmt.Errorf("failed to fetch hidden remote: %s: %w", forkRef, err) 64 } 65 return nil 66}