Beispiel #1
0
// gitAdd returns the tree object created by adding files to the index.
func gitAdd(repo *git.Repository, paths ...string) (*git.Tree, error) {
	indx, err := repo.Index()
	if err != nil {
		return nil, err
	}
	defer indx.Free()

	for _, file := range paths {
		err = indx.AddByPath(file)
		if err != nil {
			return nil, err
		}
	}

	err = indx.Write()
	if err != nil {
		return nil, err
	}

	treeID, err := indx.WriteTree()
	if err != nil {
		return nil, err
	}

	tree, err := repo.LookupTree(treeID)
	if err != nil {
		return nil, err
	}
	return tree, nil
}
Beispiel #2
0
func getSignatureFromLastCommit(repo *git.Repository) (*git.Signature, error) {
	master, err := repo.LookupReference(MasterRef)
	if err != nil {
		return nil, err
	}
	defer master.Free()

	objId := master.Target()
	c, err := repo.LookupCommit(objId)
	if err != nil {
		return nil, err
	}
	defer c.Free()

	return c.Committer(), nil
}
Beispiel #3
0
func gitCommit(repo *git.Repository, message string, tree *git.Tree, authorName, authorEmail string) error {
	commitSignature := &git.Signature{
		Name:  "shortbread",
		Email: "*****@*****.**",
		When:  time.Now(),
	}

	if authorName == "" {
		authorName = commitSignature.Name
	}

	if authorEmail == "" {
		authorEmail = commitSignature.Email
	}

	signature := &git.Signature{
		Name:  authorName,
		Email: authorEmail,
		When:  time.Now(),
	}

	parentCommit := make([]*git.Commit, 0)
	master, err := repo.LookupReference(MasterRef)
	if err != nil {
		_, err = repo.CreateCommit(MasterRef, signature, commitSignature, message, tree, parentCommit...)
		return err
	}
	defer master.Free()

	objId := master.Target()
	c, err := repo.LookupCommit(objId)
	if err != nil {
		return err
	}

	parentCommit = append(parentCommit, c)
	_, err = repo.CreateCommit(MasterRef, signature, commitSignature, message, tree, parentCommit...)
	return err
}
Beispiel #4
0
// Push has the same effect as 'git push origin master'
func Push(repo *git.Repository) error {
	remoteRepo, err := repo.LoadRemote("origin")
	if err != nil {
		return err
	}
	defer remoteRepo.Free()

	err = remoteRepo.SetPushRefspecs([]string{MasterRef + ":" + MasterRef})
	if err != nil {
		return err
	}

	credCallBack := &git.RemoteCallbacks{}
	credCallBack.CredentialsCallback = getCredentials
	err = remoteRepo.SetCallbacks(credCallBack)
	if err != nil {
		return err
	}

	err = remoteRepo.Save()
	if err != nil {
		return err
	}

	pushObj, err := remoteRepo.NewPush()
	if err != nil {
		return err
	}
	defer pushObj.Free()

	err = pushObj.AddRefspec(MasterRef + ":" + MasterRef)
	if err != nil {
		return err
	}

	err = pushObj.Finish()
	if err != nil {
		return err
	}

	ok := pushObj.UnpackOk()
	if !ok {
		return errors.New("objects from push not unpacked properly")

	}

	StatusForEachCallback := func(ref string, msg string) int {
		if msg != "" {
			return -1
		}
		return 0
	}

	err = pushObj.StatusForeach(StatusForEachCallback)
	if err != nil {
		return err
	}

	sig, err := getSignatureFromLastCommit(repo)
	if err != nil {
		return err
	}

	err = pushObj.UpdateTips(sig, "update by push")
	return nil
}