MCPcopy
hub / github.com/redspread/spread / InitProject

Function InitProject

pkg/project/project.go:28–67  ·  view source on GitHub ↗

InitProject creates a new Spread project including initializing a Git repository on disk. A target must be specified.

(target string)

Source from the content-addressed store, hash-verified

26// InitProject creates a new Spread project including initializing a Git repository on disk.
27// A target must be specified.
28func InitProject(target string) (*Project, error) {
29 // Check if path is specified
30 if len(target) == 0 {
31 return nil, ErrEmptyPath
32 }
33
34 // Get absolute path to directory
35 target, err := filepath.Abs(target)
36 if err != nil {
37 return nil, fmt.Errorf("could not resolve '%s': %v", target, err)
38 }
39
40 // Check if directory exists
41 if _, err = os.Stat(target); err == nil {
42 return nil, fmt.Errorf("'%s' already exists", target)
43 } else if !os.IsNotExist(err) {
44 return nil, err
45 }
46
47 // Create .spread directory in target directory
48 if err = os.MkdirAll(target, 0755); err != nil {
49 return nil, fmt.Errorf("could not create repo directory: %v", err)
50 }
51
52 // Create bare Git repository in .spread directory with the directory name "git"
53 gitDir := filepath.Join(target, GitDirectory)
54 repo, err := git.InitRepository(gitDir, true)
55 if err != nil {
56 return nil, fmt.Errorf("Could not create Object repository: %v", err)
57 }
58
59 // Create .gitignore file in directory ignoring Git repository
60 ignoreName := filepath.Join(target, ".gitignore")
61 ignoreData := fmt.Sprintf("/%s", GitDirectory)
62 ioutil.WriteFile(ignoreName, []byte(ignoreData), 0755)
63 return &Project{
64 Path: target,
65 repo: repo,
66 }, nil
67}
68
69// OpenProject attempts to open the project at the given path.
70func OpenProject(target string) (*Project, error) {

Callers 4

InitGlobalFunction · 0.85
TestInitProjectNoPathFunction · 0.85
TestInitProjectRelativeFunction · 0.85
TestInitProjectAbsoluteFunction · 0.85

Calls

no outgoing calls

Tested by 3

TestInitProjectNoPathFunction · 0.68
TestInitProjectRelativeFunction · 0.68
TestInitProjectAbsoluteFunction · 0.68