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() error { 31 branch := g.h.String() 32 33 fetchOpts := &git.FetchOptions{ 34 RefSpecs: []config.RefSpec{ 35 config.RefSpec("+" + branch + ":" + branch), // +refs/heads/master:refs/heads/master 36 }, 37 } 38 39 err := g.r.Fetch(fetchOpts) 40 if errors.Is(git.NoErrAlreadyUpToDate, err) { 41 return nil 42 } else if err != nil { 43 return fmt.Errorf("failed to fetch origin branch: %s: %w", branch, err) 44 } 45 return nil 46} 47 48// TrackHiddenRemoteRef tracks a hidden remote in the repository. For example, 49// if the feature branch on the fork (forkRef) is feature-1, and the remoteRef, 50// i.e. the branch we want to merge into, is main, this will result in a refspec: 51// 52// +refs/heads/main:refs/hidden/feature-1/main 53func (g *GitRepo) TrackHiddenRemoteRef(forkRef, remoteRef string) error { 54 fetchOpts := &git.FetchOptions{ 55 RefSpecs: []config.RefSpec{ 56 config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/hidden/%s/%s", remoteRef, forkRef, remoteRef)), 57 }, 58 RemoteName: "origin", 59 } 60 61 err := g.r.Fetch(fetchOpts) 62 if errors.Is(git.NoErrAlreadyUpToDate, err) { 63 return nil 64 } else if err != nil { 65 return fmt.Errorf("failed to fetch hidden remote: %s: %w", forkRef, err) 66 } 67 return nil 68}