예제 #1
0
func checkProject(t *testing.T, target string) {
	// check that project directory was created
	checkPath(t, target, true)

	// check .gitignore
	ignorePath := filepath.Join(target, ".gitignore")
	checkPath(t, ignorePath, false)

	// check git directory
	gitDir := filepath.Join(target, GitDirectory)
	checkPath(t, gitDir, true)
	repo, err := git.OpenRepository(gitDir)
	assert.NoError(t, err)

	assert.True(t, filepath.Clean(repo.Path()) == gitDir)
}
예제 #2
0
파일: repo.go 프로젝트: zmarcantel/hearth
// Open the managed repository. Opens the config, gets the repo directory, and
// fills all the necessary data
func Open() (Repository, error) {
	var repo Repository
	conf, err := config.Open()
	if err != nil {
		return repo, err
	}

	conf_path := conf.BaseDirectory
	if strings.HasPrefix(conf_path, "~/") {
		conf_path = path.Join(os.Getenv("HOME"), conf_path[2:])
	}

	repo_raw, err := git.OpenRepository(conf_path)
	if err != nil {
		return repo, fmt.Errorf("could not open git repository: %s", err)
	}
	repo = Repository{repo_raw, conf_path, conf}

	return repo, nil
}
예제 #3
0
파일: gitfuse.go 프로젝트: bachue/pages
func (gitfs *GitFs) getMasterTreeFromRepoWithoutCache(repoPath string) (*libgit2.Repository, *libgit2.Branch, *libgit2.Commit, *libgit2.Tree, func(), error) {
	repo, err := libgit2.OpenRepository(repoPath)
	if err != nil {
		gitfs.logger.Debugf("Failed to open Git Repository %s due to %s", repoPath, err)
		return nil, nil, nil, nil, nil, err
	}
	gitfs.logger.Debugf("Open Git Repository %s", repoPath)
	masterBranch, err := repo.LookupBranch("master", libgit2.BranchLocal)
	if err != nil {
		gitfs.logger.Errorf("Failed to get master branch of Git Repository %s due to %s", repoPath, err)
		repo.Free()
		return nil, nil, nil, nil, nil, err
	}
	gitfs.logger.Debugf("Got master branch of Git Repository %s", repoPath)
	targetCommit, err := repo.LookupCommit(masterBranch.Target())
	if err != nil {
		gitfs.logger.Errorf("Failed to get commit from master branch of Git Repository %s due to %s", repoPath, err)
		masterBranch.Free()
		repo.Free()
		return nil, nil, nil, nil, nil, err
	}
	gitfs.logger.Debugf("Got commit %s from master branch of Git Repository %s", targetCommit.Id().String(), repoPath)
	targetTree, err := targetCommit.Tree()
	if err != nil {
		gitfs.logger.Errorf("Failed to get tree of commit %s from Git Repository %s due to %s", targetCommit.Id().String(), repoPath, err)
		targetCommit.Free()
		masterBranch.Free()
		repo.Free()
		return nil, nil, nil, nil, nil, err
	}
	gitfs.logger.Debugf("Got tree %s from master branch of Git Repository %s", targetTree.Id().String(), repoPath)
	cleaner := func() {
		targetTree.Free()
		targetCommit.Free()
		masterBranch.Free()
		repo.Free()
	}
	return repo, masterBranch, targetCommit, targetTree, cleaner, nil
}
예제 #4
0
파일: project.go 프로젝트: redspread/spread
// OpenProject attempts to open the project at the given path.
func OpenProject(target string) (*Project, error) {
	// Check if path is specified
	if len(target) == 0 {
		return nil, ErrEmptyPath
	}

	// check that path exists and is dir
	if fileInfo, err := os.Stat(target); err != nil {
		return nil, err
	} else if !fileInfo.IsDir() {
		return nil, ErrPathNotDir
	}

	gitDir := filepath.Join(target, GitDirectory)
	repo, err := git.OpenRepository(gitDir)
	if err != nil {
		return nil, fmt.Errorf("failed to open Git repository: %v", err)
	}

	return &Project{
		Path: target,
		repo: repo,
	}, nil
}
예제 #5
0
func TestGitFsReadLink(t *testing.T) {
	gitfs, cleaner := setupGitFsTest(t)
	defer cleaner()

	realpath, err := os.Readlink(gitfs.GitFsDir + "/pry")
	assert.NotNil(t, err)
	pathError, ok := err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.EINVAL)

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry")
	assert.NotNil(t, err)
	pathError, ok = err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.EINVAL)

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry/Gemfile")
	assert.NotNil(t, err)
	pathError, ok = err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.EINVAL)

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry/bin")
	assert.NotNil(t, err)
	pathError, ok = err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.EINVAL)

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry/bin/pry")
	assert.NotNil(t, err)
	pathError, ok = err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.EINVAL)

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry/bin/pry.unexisted")
	assert.NotNil(t, err)
	pathError, ok = err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.ENOENT)

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry/bin/pry.unexisted")
	assert.NotNil(t, err)
	pathError, ok = err.(*os.PathError)
	assert.True(t, ok)
	assert.EqualValues(t, pathError.Err, unix.ENOENT)

	repo, err := libgit2.OpenRepository(gitfs.GitRepoDir + "/pry/ruby-pry.git")
	assert.Nil(t, err)
	defer repo.Free()

	newBlobContent := []byte("bin/pry")
	newBlobId, err := repo.CreateBlobFromBuffer(newBlobContent)
	assert.Nil(t, err)

	branch, err := repo.LookupBranch("master", libgit2.BranchLocal)
	assert.Nil(t, err)
	defer branch.Free()

	parentCommit, err := repo.LookupCommit(branch.Target())
	assert.Nil(t, err)
	defer parentCommit.Free()

	parentTree, err := parentCommit.Tree()
	assert.Nil(t, err)
	defer parentTree.Free()

	builder, err := repo.TreeBuilderFromTree(parentTree)
	assert.Nil(t, err)
	defer builder.Free()

	err = builder.Insert("pry.symlink", newBlobId, int(libgit2.FilemodeLink))
	assert.Nil(t, err)

	newTreeId, err := builder.Write()
	assert.Nil(t, err)

	newTree, err := repo.LookupTree(newTreeId)
	assert.Nil(t, err)
	defer newTree.Free()

	_, err = repo.CreateCommit(
		branch.Reference.Name(),
		&libgit2.Signature{Name: "testuser", Email: "*****@*****.**", When: time.Now()},
		&libgit2.Signature{Name: "testuser", Email: "*****@*****.**", When: time.Now()},
		"This is a symlink test", newTree, parentCommit)
	assert.Nil(t, err)
	gitfs.cache.Purge() // Refresh Cache

	realpath, err = os.Readlink(gitfs.GitFsDir + "/pry/ruby-pry/pry.symlink")
	assert.Nil(t, err)
	assert.EqualValues(t, realpath, "bin/pry")
}