Skip to content

Commit 065699e

Browse files
committed
remote: add AddRemote and RemoveRemote functions
1 parent 03ef311 commit 065699e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

remote.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2019 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
import "strings"
8+
9+
// RemoveRemote removes a remote from given repository path if exists.
10+
func RemoveRemote(repoPath, remote string) error {
11+
_, err := NewCommand("remote", "rm", remote).RunInDir(repoPath)
12+
if err != nil && !strings.Contains(err.Error(), "fatal: No such remote") {
13+
return err
14+
}
15+
return nil
16+
}
17+
18+
// AddRemoteOptions contains options to add a remote address.
19+
type AddRemoteOptions struct {
20+
Mirror bool
21+
}
22+
23+
// AddRemote adds a new remote
24+
func AddRemote(repoPath, remote, addr string, opts AddRemoteOptions) error {
25+
cmd := NewCommand("remote", "add", remote)
26+
if opts.Mirror {
27+
cmd.AddArguments("--mirror")
28+
}
29+
_, err := cmd.AddArguments(addr).RunInDir(repoPath)
30+
return err
31+
}

0 commit comments

Comments
 (0)