Revert "skip remote creation"

This reverts commit e4bee821d8.
This commit is contained in:
Tim Schubert 2022-06-02 21:26:52 +02:00
parent 8deec15978
commit 1fbca6145d
Signed by: dadada
GPG key ID: EEB8D1CE62C4DFEA

32
main.go
View file

@ -16,13 +16,14 @@ import (
"errors" "errors"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http" githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
) )
const ( const (
defaultRemoteName = "origin" defaultRemoteName = "pad-archiver"
) )
var ( var (
@ -101,14 +102,32 @@ func download(
return padfile, nil return padfile, nil
} }
func createRemote(
repo *git.Repository,
remote string,
url string,
) (*git.Remote, error) {
newRemote, err := repo.Remote(remote)
if err != nil {
log.Printf("Creating new git remote %s with URL %s", remote, url)
return repo.CreateRemote(&config.RemoteConfig{
Name: remote,
URLs: []string{url},
})
} else {
log.Printf("Using remote %s with URL %s", remote, url)
}
return newRemote, nil
}
func push( func push(
auth *githttp.BasicAuth, auth *githttp.BasicAuth,
r *git.Repository, r *git.Repository,
remote *string, remote string,
) error { ) error {
return r.Push(&git.PushOptions{ return r.Push(&git.PushOptions{
RemoteName: *remote, RemoteName: remote,
Auth: auth, Auth: auth,
}) })
} }
@ -141,7 +160,7 @@ func main() {
"", "",
"password", "password",
) )
remote := flag.String( remoteUrl := flag.String(
"remote", "remote",
defaultRemoteName, defaultRemoteName,
"remote", "remote",
@ -197,7 +216,10 @@ func main() {
} }
if *doPush == true { if *doPush == true {
if err := push(auth, repo, remote); err != nil { if _, err := createRemote(repo, defaultRemoteName, *remoteUrl); err != nil {
log.Fatalf("%s", err)
}
if err := push(auth, repo, defaultRemoteName); err != nil {
if err == git.NoErrAlreadyUpToDate { if err == git.NoErrAlreadyUpToDate {
log.Println("Already up-to-date") log.Println("Already up-to-date")
} else { } else {