Exemple #1
0
func resetIndexFromCommit(repo *libgit.Repository, commitId string) error {
	// If the index doesn't exist, idx is a new index, so ignore
	// the path error that ReadIndex is returning
	idx, _ := ReadIndex(repo)
	com, err := repo.GetCommit(commitId)
	if err != nil {
		fmt.Printf("%s\n", err)
		return err
	}
	treeId := com.TreeId()
	tree := libgit.NewTree(repo, treeId)
	if tree == nil {
		panic("Error retriving tree for commit")
	}
	idx.ResetIndex(repo, tree)
	writeIndex(repo, idx, "index")
	return nil
}
Exemple #2
0
func Merge(repo *libgit.Repository, args []string) {
	if len(args) != 1 {
		fmt.Fprintf(os.Stderr, "Usage: go-git merge branchname\n")
		return
	}
	commit, err := repo.GetCommitOfBranch(args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Invalid branch: %s\n", args[0])
		return
	}
	head, err := getHeadId(repo)
	if err != nil {
		panic(err)
	}

	// The current branch is an ancestor of HEAD. This
	// is a fast-forward.
	if headCom, err := repo.GetCommit(head); err == nil {
		if isAncestor(headCom, fmt.Sprintf("%s", commit.Id)) {
			fmt.Fprintf(os.Stderr, "Already up-to-date.\n")
			return
		}
	}

	// Head is an ancestor of the current branch.
	if isAncestor(commit, head) {
		hb := getHeadBranch(repo)
		newId := fmt.Sprintf("%s", commit.Id)

		ioutil.WriteFile(".git/refs/heads/"+hb, []byte(newId), 0644)
		resetIndexFromCommit(repo, newId)
		fmt.Printf("Hooray! A Fast forward on %s! New should should be %s\n", hb, commit.Id)
		return
	}

	panic("Only fast forward commits are currently supported.")
}