Implement forcing origin to newly created repo
This commit is contained in:
82
main.go
82
main.go
@@ -7,9 +7,12 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/config"
|
||||
"github.com/joho/godotenv"
|
||||
"golang.design/x/clipboard"
|
||||
)
|
||||
@@ -63,11 +66,14 @@ func main() {
|
||||
}
|
||||
|
||||
var name string
|
||||
var private bool
|
||||
var private, init, forceInit bool
|
||||
flag.StringVar(&name, "name", "", "Name of the repository")
|
||||
flag.StringVar(&name, "n", "", "Name of the repository (shorthand)")
|
||||
flag.BoolVar(&private, "private", false, "Make the repository private")
|
||||
flag.BoolVar(&private, "p", false, "Make the repository private (shorthand)")
|
||||
flag.BoolVar(&init, "init", false, "Also add remote to the new repo")
|
||||
flag.BoolVar(&init, "i", false, "Also add remote to the new repo (shorthand)")
|
||||
flag.BoolVar(&forceInit, "f", false, "Force assignment of the remote (deletes existing origin)")
|
||||
flag.Parse()
|
||||
|
||||
if len(flag.Args()) > 0 {
|
||||
@@ -86,15 +92,69 @@ func main() {
|
||||
}
|
||||
client.SetBasicAuth("dave", "D7u@NHh^9d33ue!xVAEu")
|
||||
|
||||
repo, _, err := client.CreateRepo(gitea.CreateRepoOption{
|
||||
Name: name,
|
||||
Private: private,
|
||||
DefaultBranch: "master",
|
||||
})
|
||||
if err != nil {
|
||||
Error.Fatalf("Error creating repository: %v", err)
|
||||
os.Exit(1)
|
||||
repo, _, err := client.GetRepo("dave", name)
|
||||
if err == nil {
|
||||
log.Printf("Repository already exists at:\n%s", repo.CloneURL)
|
||||
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
|
||||
} else {
|
||||
repo, _, err = client.CreateRepo(gitea.CreateRepoOption{
|
||||
Name: name,
|
||||
Private: private,
|
||||
DefaultBranch: "master",
|
||||
})
|
||||
if err != nil {
|
||||
Error.Fatalf("Error creating repository: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Repository created at:\n%s", repo.CloneURL)
|
||||
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
|
||||
}
|
||||
|
||||
if init {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
Error.Fatalf("Error getting current working directory: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
cwd = path.Clean(cwd)
|
||||
|
||||
var localRepo *git.Repository
|
||||
_, err = os.Stat(path.Join(cwd, ".git"))
|
||||
if err == nil {
|
||||
localRepo, err = git.PlainOpen(cwd)
|
||||
if err != nil {
|
||||
Error.Fatalf("Error opening git repository: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
localRepo, err = git.PlainInit(cwd, false)
|
||||
if err != nil {
|
||||
Error.Fatalf("Error initializing git repository: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
_, err = localRepo.Remote("origin")
|
||||
if err == nil {
|
||||
if forceInit {
|
||||
err = localRepo.DeleteRemote("origin")
|
||||
if err != nil {
|
||||
Error.Fatalf("Error deleting remote from local repository: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
Error.Fatalf("Remote origin already exists in local repository")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = localRepo.CreateRemote(&config.RemoteConfig{
|
||||
Name: "origin",
|
||||
URLs: []string{repo.CloneURL},
|
||||
})
|
||||
if err != nil {
|
||||
Error.Fatalf("Error adding remote to local repository: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Remote added to local repository")
|
||||
}
|
||||
log.Printf("Repository created at:\n%s", repo.CloneURL)
|
||||
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
|
||||
}
|
||||
|
Reference in New Issue
Block a user