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

Open
opened 2023-06-17 11:53:07 -05:00 by MatejLach · 0 comments
MatejLach commented 2023-06-17 11:53:07 -05:00 (Migrated from github.com)

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?

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](https://docs.gitlab.com/ee/user/project/push_options.html) 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?
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: jcarr/git2go#953
No description provided.