github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/test/setup/reposetup/setup_structure.go (about) 1 package reposetup 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 "github.com/Benchkram/bob/bob" 8 9 git "github.com/go-git/go-git/v5" 10 11 "github.com/Benchkram/errz" 12 ) 13 14 const ( 15 // TopRepoDirName is the name of the top-repo 16 // ususally the first repo containing a .bob directory 17 TopRepoDirName = "top-repo" 18 19 // ChildReposDirName syntetically created repos for testing purpose 20 ChildReposDirName = "repos" 21 ) 22 23 // Names for the used child repos. 24 var ( 25 ChildOne = "child1" 26 ChildTwo = "child2" 27 ChildThree = "child3" 28 29 Childs = []string{ 30 ChildOne, 31 ChildTwo, 32 ChildThree, 33 } 34 35 ChildRecursive = "childrecursive" 36 ChildPlayground = "childplayground" 37 ) 38 39 func TopRepo(basePath string) (string, error) { 40 const isBare = false 41 42 repoPath := filepath.Join(basePath, TopRepoDirName) 43 _, err := git.PlainInit(repoPath, isBare) 44 if err != nil { 45 return "", fmt.Errorf("failed to create repo %q: %w", repoPath, err) 46 } 47 48 return repoPath, nil 49 } 50 51 // ChildRepos creates child repos as defined and returns the path 52 // to the wrapping directory 53 func ChildRepos(basePath string) (childs []string, err error) { 54 defer errz.Recover(&err) 55 56 childs = []string{} 57 58 for _, child := range Childs { 59 err = createAndFillRepo(basePath, child) 60 errz.Fatal(err) 61 62 childs = append(childs, filepath.Join(basePath, child)) 63 } 64 65 return childs, nil 66 } 67 68 func RecursiveRepo(basePath string) (_ string, err error) { 69 defer errz.Recover(&err) 70 71 err = createAndFillRepo(basePath, ChildRecursive) 72 errz.Fatal(err) 73 74 path := filepath.Join(basePath, ChildRecursive) 75 b, err := bob.Bob(bob.WithDir(path)) 76 errz.Fatal(err) 77 78 err = b.Init() 79 errz.Fatal(err) 80 81 err = b.Add("https://github.com/pkg/errors.git", true) 82 errz.Fatal(err) 83 84 repo, err := git.PlainOpen(path) 85 if err != nil { 86 return "", fmt.Errorf("failed to open repo: %w", err) 87 } 88 89 wt, err := repo.Worktree() 90 if err != nil { 91 return "", fmt.Errorf("failed to get worktree: %w", err) 92 } 93 94 err = wt.AddGlob(".") 95 errz.Fatal(err) 96 97 err = commit(wt, "Add bob files") 98 errz.Fatal(err) 99 100 return path, nil 101 } 102 103 func PlaygroundRepo(basePath string) (_ string, err error) { 104 defer errz.Recover(&err) 105 106 err = createAndFillRepo(basePath, ChildPlayground) 107 errz.Fatal(err) 108 109 path := filepath.Join(basePath, ChildPlayground) 110 err = bob.CreatePlayground(path) 111 errz.Fatal(err) 112 113 repo, err := git.PlainOpen(path) 114 if err != nil { 115 return "", fmt.Errorf("failed to open repo: %w", err) 116 } 117 118 wt, err := repo.Worktree() 119 if err != nil { 120 return "", fmt.Errorf("failed to get worktree: %w", err) 121 } 122 123 err = wt.AddGlob(".") 124 errz.Fatal(err) 125 126 err = commit(wt, "Add playground files") 127 errz.Fatal(err) 128 129 return path, nil 130 } 131 132 // BaseTestStructure fill a existing directory with basic testing blueprints. 133 // The generated structure looks like: 134 // 135 // basePath/ 136 // basePath/top-repo 137 // basePath/top-repo/.git 138 // basePath/repos/child1/.git 139 // basePath/repos/child2/.git 140 // basePath/repos/child3/.git 141 // 142 // TODO: @leonklingele update this comment with the playground stuff. 143 // 144 // The top-repo is intended to call `bob init`. 145 // The child repos can be add to the top repo using `bob add` 146 // 147 func BaseTestStructure(basePath string) (topRepo string, childs []string, recursive, playgroundRepo string, err error) { 148 defer errz.Recover(&err) 149 150 if !filepath.IsAbs(basePath) { 151 return "", nil, "", "", fmt.Errorf("basePath must be absolut") 152 } 153 154 topRepo, err = TopRepo(basePath) 155 errz.Fatal(err) 156 157 childs, err = ChildRepos(filepath.Join(basePath, ChildReposDirName)) 158 errz.Fatal(err) 159 160 recursive, err = RecursiveRepo(basePath) 161 errz.Fatal(err) 162 163 playgroundRepo, err = PlaygroundRepo(basePath) 164 errz.Fatal(err) 165 166 return topRepo, childs, recursive, playgroundRepo, nil 167 }