Отправка на удаленный компьютер с базовой аутентификацией
В настоящее время я изо всех сил пытаюсь заставить git работать с частными репозиториями на GitHub. Хотя работа с командной строкой git работает должным образом (git push origin
) приведенный ниже фрагмент кода не работает. Последняя команда журнала возвращает следующий результат:
Error during push error=repository not found
Сам репозиторий существует, иначе нажатие из командной строки не сработает. Сначала я подумал, что это может быть связано с тем, что удаленный репозиторий пуст. Но даже если это не так, у меня та же ошибка. Учетные данные действительны, я их перепроверил.
Итак, должно быть что-то мне не хватает. Но поскольку я новичок в Go, мой опыт слишком мал, чтобы это можно было понять.
package git
import (
"io/ioutil"
"path/filepath"
"time"
"github.com/apex/log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/spf13/viper"
)
const gitDataDirectory = "./data/"
const defaultRemoteName = "origin"
// Commit creates a commit in the current repository
func Commit() {
repo, err := git.PlainOpen(gitDataDirectory)
if err != nil {
// Repository does not exist yet, create it
log.Info("The Git repository does not exist yet and will be created.")
repo, err = git.PlainInit(gitDataDirectory, false)
}
if err != nil {
log.Warn("The data folder could not be converted into a Git repository. Therefore, the versioning does not work as expected.")
return
}
w, _ := repo.Worktree()
log.Info("Committing new changes...")
w.Add(".gitignore")
w.Add("info.json")
w.Commit("test", &git.CommitOptions{
Author: &object.Signature{
Name: "test",
Email: "test@localhost",
When: time.Now(),
},
})
_, err = repo.Remote(defaultRemoteName)
if err != nil {
log.Info("Creating new Git remote named " + defaultRemoteName)
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: defaultRemoteName,
URLs: []string{"https://github.com/jlnostr/blub.git"},
})
if err != nil {
log.WithError(err).Warn("Error creating remote")
}
}
auth := &http.BasicAuth{
Username: "jlnostr",
Password: "[git_basic_auth_token]",
}
log.Info("Pushing changes to remote")
err = repo.Push(&git.PushOptions{
RemoteName: defaultRemoteName,
Auth: auth,
})
if err != nil {
log.WithError(err).Warn("Error during push")
}
}
2 ответа
Было несколько проблем:
Токен GitHub имел доступ только к общедоступным репозиториям, но я попытался отправить его в частный репозиторий. С использованием
repo
разрешения (вместоrepo:public
) помогло.Репо еще не было "действительно" инициализировано. Как упоминалось в @peakle, в этом случае помогло вытягивание перед нажатием.
Итак, ниже приведен полный рабочий пример инициализации частного репозитория с помощью go-git v5.
package git
import (
"io/ioutil"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
const gitDataDirectory = "./data/"
const defaultRemoteName = "origin"
var auth = &http.BasicAuth{
Username: "<username>",
Password: "<git_basic_auth_token>",
}
func createCommitOptions() *git.CommitOptions {
return &git.CommitOptions{
Author: &object.Signature{
Name: "Rick Astley",
Email: "never.gonna.give.you.up@localhost",
When: time.Now(),
},
}
}
// Commit creates a commit in the current repository
func Commit() {
err := initializeGitRepository()
if err != nil {
// logging: The folder could not be converted into a Git repository.
return
}
// Open after initialization
repo, _ := git.PlainOpen(gitDataDirectory)
w, _ := repo.Worktree()
status, _ := w.Status()
if status.IsClean() {
return
}
// Committing new changes
w.Add("<your_file>.txt")
w.Commit("test", createCommitOptions())
// Pushing to remote
err = repo.Push(&git.PushOptions{
RemoteName: defaultRemoteName,
Auth: auth,
})
}
func initializeGitRepository() error {
_, err := git.PlainOpen(gitDataDirectory)
if err == nil {
return nil
}
// The Git repository does not exist yet and will be created.
repo, err := git.PlainInit(gitDataDirectory, false)
if err != nil {
return err
}
// Writing default .gitignore with "media/" as first line
filename := filepath.Join(gitDataDirectory, ".gitignore")
err = ioutil.WriteFile(filename, []byte("media/"), 0644)
if err != nil {
return err
}
w, _ := repo.Worktree()
w.Add(".gitignore")
w.Commit("Initial commit", createCommitOptions())
return initializeRemote()
}
func initializeRemote() error {
repo, err := git.PlainOpen(gitDataDirectory)
if err != nil {
return err
}
_, err = repo.Remote(defaultRemoteName)
if err == nil {
// Repo already exists, skipping
return nil
}
w, err := repo.Worktree()
if err != nil {
return err
}
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
// Creating default remote
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: defaultRemoteName,
URLs: []string{"https://github.com/<user>/<repo>.git"},
Fetch: []config.RefSpec{refspec},
})
if err != nil {
// TODO
}
// Pulling from remote
w.Pull(&git.PullOptions{
RemoteName: defaultRemoteName,
Auth: auth,
})
return err
}
Я пробовал ваш код несколько раз с минимальными изменениями, как показано ниже, и он отлично работает в случае, когда репо также клонировано и git inited, зафиксируйте сообщение для утверждения: https://github.com/peakle/iptables-http-services/commit/56eba2fcc4fac790ad573942ab1b926ddadf0875
Я не могу воспроизвести вашу проблему, но, возможно, это поможет вам с ошибкой git, которая произошла в вашем случае: https://help.github.com/en/github/creating-cloning-and-archiving-repositories/error-repository-not-found, я думаю, у вас есть опечатка в имени репо или что-то похожее на случаи, описанные в ссылке выше.
и дополнительно для будущих улучшений вашей кодовой базы, у меня проблемы в случае, если git еще не создан, я добавляю код и блок TODO с рекомендациями, возможно, это может помочь вам в будущем:)
package main
import (
"fmt"
_ "io/ioutil"
"os"
_ "path/filepath"
"time"
"github.com/apex/log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
const gitDataDirectory = "/Users/peakle/projects/tests/iptables-http-services"
const defaultRemoteName = "origin"
func main() {
filename := "kek.txt"
file, _ := os.Create(fmt.Sprintf("%s/%s", gitDataDirectory, filename))
_, _ = file.Write([]byte("test string2"))
Commit(filename)
}
// Commit creates a commit in the current repository
func Commit(filename string) {
var isNewInit bool
repo, err := git.PlainOpen(gitDataDirectory)
if err != nil {
// Repository does not exist yet, create it
log.Info("The Git repository does not exist yet and will be created.")
repo, err = git.PlainInit(gitDataDirectory, false)
isNewInit = true
}
if err != nil {
log.Warn("The data folder could not be converted into a Git repository. Therefore, the versioning does not work as expected.")
return
}
w, _ := repo.Worktree()
log.Info("Committing new changes...")
if isNewInit {
err = w.AddGlob("*")
if err != nil {
fmt.Println(err)
}
//TODO if its new git init, need to add `git pull` command with remote branch
} else {
_, _ = w.Add(filename)
}
_, _ = w.Commit("test", &git.CommitOptions{
Author: &object.Signature{
Name: "peakle",
Email: "test@mail.com",
When: time.Now(),
},
})
_, err = repo.Remote(defaultRemoteName)
if err != nil {
log.Info("Creating new Git remote named " + defaultRemoteName)
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: defaultRemoteName,
URLs: []string{"https://github.com/Peakle/iptables-http-services.git"},
})
if err != nil {
log.WithError(err).Warn("Error creating remote")
}
}
auth := &http.BasicAuth{
Username: "peakle",
Password: "authtoken",
}
log.Info("Pushing changes to remote")
err = repo.Push(&git.PushOptions{
RemoteName: defaultRemoteName,
Auth: auth,
})
if err != nil {
log.WithError(err).Warn("Error during push")
}
}