1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package main
import (
"io/ioutil"
"log"
"os"
git "github.com/libgit2/git2go"
)
// echo "https://remote.url.com" | gitaly-remote /path/to/repository remote-name
func main() {
if n := len(os.Args); n != 3 {
log.Fatalf("invalid number of arguments, expected 2, got %d", n-1)
}
repoPath := os.Args[1]
remoteName := os.Args[2]
remoteURLBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error while reading remote URL: %v", err)
}
remoteURL := string(remoteURLBytes)
err = addOrUpdate(repoPath, remoteName, remoteURL)
if err != nil {
log.Fatalf("error while setting remote: %v", err)
}
}
//addOrUpdate adds, or updates if exits, the remote to repository
func addOrUpdate(repoPath string, name, url string) error {
if err := add(repoPath, name, url); err != nil {
return update(repoPath, name, url)
}
return nil
}
func add(repoPath string, name, url string) error {
git2repo, err := openGit2GoRepo(repoPath)
if err != nil {
return err
}
_, err = git2repo.Remotes.Create(name, url)
return err
}
func update(repoPath string, name, url string) error {
git2repo, err := openGit2GoRepo(repoPath)
if err != nil {
return err
}
return git2repo.Remotes.SetUrl(name, url)
}
func openGit2GoRepo(repoPath string) (*git.Repository, error) {
git2repo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
}
return git2repo, nil
}
|