Ejemplo n.º 1
0
// Switch a repository to the empty branch, which will be created
// if it does not exist.
func switchToEmptyBranch(r *git.Repo) error {
	contents := bytes.NewBufferString("This branch intentionally left blank\n")
	readme := filepath.Join(r.WorkDir, "README.empty-branch")
	if ref, err := r.Ref("empty-branch"); err == nil {
		return ref.Checkout()
	}
	cmd, _, _ := r.Git("checkout", "--orphan", "empty-branch")
	if cmd.Run() != nil {
		return fmt.Errorf("Could not create empty-branch")
	}
	cmd, _, _ = r.Git("rm", "-r", "--cached", ".")
	if cmd.Run() != nil {
		return fmt.Errorf("Could not remove index from empty-branch")
	}
	cmd, _, _ = r.Git("clean", "-f", "-x", "-d")
	if cmd.Run() != nil {
		return fmt.Errorf("Could not clean working tree for empty-branch")
	}
	if ioutil.WriteFile(readme, contents.Bytes(), os.FileMode(0644)) != nil {
		return fmt.Errorf("Could not create README.empty-branch")
	}
	cmd, _, _ = r.Git("add", "README.empty-branch")
	if cmd.Run() != nil {
		return fmt.Errorf("Could not add README.empty-branch to empty-branch")
	}
	cmd, _, _ = r.Git("commit", "-m", "Created empty branch.")
	if cmd.Run() != nil {
		return fmt.Errorf("Could not create initial commit to empty-branch")
	}
	return nil
}
Ejemplo n.º 2
0
func updateRepo(repo *git.Repo, branch string) {
	Info.Print("Updating GIT repo")
	repo.Checkout(branch)
	repo.Fetch([]string{"origin"})
	res, err := repo.Ref("remotes/origin/" + branch)
	if err != nil {
		Error.Fatal("Failed to get ref to origin/master: ", err)
	}
	currentRes, err := repo.CurrentRef()
	if err != nil {
		Error.Fatal("Failed to get ref to master: ", err)
	}
	err = currentRes.MergeWith(res)
	if err != nil {
		Error.Fatal("Failed to merge to master: ", err)
	}
}