示例#1
0
文件: repo.go 项目: zmarcantel/hearth
// Create a new repository in the given path and fill it with a remote:origin if given.
// This also creates the hearth config (and all other InitFiles()) in the repo.
func Create(path, origin string) (Repository, error) {
	if _, err := os.Stat(path); err == nil {
		return Repository{}, fmt.Errorf("%s already exists.", path)
	}

	repo_raw, err := git.InitRepository(path, false)
	if err != nil {
		return Repository{}, err
	}
	repo := Repository{repo_raw, path, config.Config{}}

	if err := repo.InitFiles(); err != nil {
		return repo, err
	}

	if len(origin) == 0 {
		fmt.Println("WARN: origin not provided. must be added before issuing a save command")
	} else {
		_, err := repo.Remotes.Create("origin", origin)
		if err != nil {
			log.Fatalf("could not add remote:origin to repo: %s", err.Error())
		}
	}

	return repo, nil
}
示例#2
0
func create_origin_repo(t *testing.T) (Repository, string) {
	repo_path := temp_dir()

	r, err := git.InitRepository(repo_path, true)
	check_fatal(t, err)

	return Repository{r, repo_path, config.Config{}}, repo_path
}
示例#3
0
// InitProject creates a new Spread project including initializing a Git repository on disk.
// A target must be specified.
func InitProject(target string) (*Project, error) {
	// Check if path is specified
	if len(target) == 0 {
		return nil, ErrEmptyPath
	}

	// Get absolute path to directory
	target, err := filepath.Abs(target)
	if err != nil {
		return nil, fmt.Errorf("could not resolve '%s': %v", target, err)
	}

	// Check if directory exists
	if _, err = os.Stat(target); err == nil {
		return nil, fmt.Errorf("'%s' already exists", target)
	} else if !os.IsNotExist(err) {
		return nil, err
	}

	// Create .spread directory in target directory
	if err = os.MkdirAll(target, 0755); err != nil {
		return nil, fmt.Errorf("could not create repo directory: %v", err)
	}

	// Create bare Git repository in .spread directory with the directory name "git"
	gitDir := filepath.Join(target, GitDirectory)
	repo, err := git.InitRepository(gitDir, true)
	if err != nil {
		return nil, fmt.Errorf("Could not create Object repository: %v", err)
	}

	// Create .gitignore file in directory ignoring Git repository
	ignoreName := filepath.Join(target, ".gitignore")
	ignoreData := fmt.Sprintf("/%s", GitDirectory)
	ioutil.WriteFile(ignoreName, []byte(ignoreData), 0755)
	return &Project{
		Path: target,
		repo: repo,
	}, nil
}