Repository navigation
Expand file tree
/
Copy pathremote.go
More file actions
62 lines (58 loc) · 1.59 KB
/
Copy pathremote.go
File metadata and controls
62 lines (58 loc) · 1.59 KB
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
package git
import (
"context"
"strings"
)
// PushRejectedNonFastForward reports whether a failed push was refused because
// the branch is not a fast-forward of its remote — the situation after a
// squash or rebase rewrites history, where a --force-with-lease push is needed.
func PushRejectedNonFastForward(err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
for _, marker := range []string{
"non-fast-forward",
"updates were rejected",
"failed to push some refs",
"fetch first",
"tip of your current branch is behind",
} {
if strings.Contains(msg, marker) {
return true
}
}
return false
}
// repoWebURL turns the default remote into a web URL when possible.
func (r Runner) repoWebURL(ctx context.Context) string {
remote := r.defaultRemote(ctx)
if remote == "" {
return ""
}
raw := strings.TrimSpace(r.bestEffort(ctx, "remote", "get-url", remote))
return remoteToWebURL(raw)
}
// remoteToWebURL turns common Git remote formats into plain https URLs.
func remoteToWebURL(raw string) string {
raw = strings.TrimSpace(raw)
raw = strings.TrimSuffix(raw, ".git")
if raw == "" {
return ""
}
if strings.HasPrefix(raw, "http://") || strings.HasPrefix(raw, "https://") {
return raw
}
if strings.HasPrefix(raw, "git@") {
withoutUser := strings.TrimPrefix(raw, "git@")
parts := strings.SplitN(withoutUser, ":", 2)
if len(parts) == 2 {
return "https://" + parts[0] + "/" + parts[1]
}
}
if strings.HasPrefix(raw, "ssh://git@") {
withoutScheme := strings.TrimPrefix(raw, "ssh://git@")
return "https://" + withoutScheme
}
return raw
}