libgit2 / git2go

Git to Go; bindings for libgit2. Like McDonald's but tastier.
MIT License
1.93k stars 316 forks source link

How to pass the equivalent of git push -o <string>? #953

Open MatejLach opened 1 year ago

MatejLach commented 1 year ago

On the Git CLI, I can use git push -o <option> for example git push -o merge_request.create to supply string options with push that are then processed on the server by i.e. the post-receive hook to perform additional actions, like create a pull request automatically after the push etc. See GitLab's options as an example.

My failed attempt to recreate this with git2go :

package main

import (
    git "github.com/libgit2/git2go/v34"
    log "github.com/sirupsen/logrus"
)

func main() {
    repo, err := git.OpenRepository("/home/ml/Projects/demo")
    if err != nil {
        log.Fatal(err)
    }
    remote, err := repo.Remotes.Lookup("origin")
    if err != nil {
        remote, err = repo.Remotes.Create("origin", repo.Path())
        if err != nil {
            log.Fatal(err)
        }
    }

    // ref
    ref, err := repo.Head()
    if err != nil {
        log.Fatal(err)
    }

    // Get the name
    branch := ref.Branch()
    branchName, err := branch.Name()
    if err != nil {
        log.Fatal(err)
    }

    if err := remote.Push([]string{"refs/heads/" + branchName}, &git.PushOptions{
        RemoteCallbacks: git.RemoteCallbacks{
            CredentialsCallback: func(url string, username_from_url string, allowed_types git.CredentialType) (*git.Credential, error) {
                return git.NewCredentialUserpassPlaintext("username", "pass")
            },
        },
        Headers: []string{"merge_request.create:true"},
    }); err != nil {
        log.Fatal(err)
    }
}

Is there a way to do this?