forked from google/go-licenses
/
git.go
98 lines (87 loc) · 2.96 KB
/
git.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package licenses
import (
"fmt"
"net/url"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/golang/glog"
git "gopkg.in/src-d/go-git.v4"
)
var (
gitRegexp = regexp.MustCompile(`^\.git$`)
// TODO(RJPercival): Support replacing "master" with Go Module version
gitRepoPathPrefixes = map[string]string{
"github.com": "blob/master/",
"bitbucket.org": "src/master/",
"go.googlesource.com": "+/refs/heads/master/",
"code.googlesource.com": "+/refs/heads/master/",
}
)
// GitRepo represents a Git repository that exists on disk locally.
type GitRepo struct {
dotGitPath string
}
// FindGitRepo finds the Git repository that contains the specified filePath
// by searching upwards through the directory tree for a ".git" directory.
func FindGitRepo(filePath string) (*GitRepo, error) {
path, err := findUpwards(filepath.Dir(filePath), gitRegexp, srcDirRegexps, nil)
if err != nil {
return nil, err
}
return &GitRepo{dotGitPath: path}, nil
}
// FileURL returns the URL of a file stored in a Git repository.
// It uses the URL of the specified Git remote repository to construct this URL.
// It supports repositories hosted on github.com, bitbucket.org and googlesource.com.
func (g *GitRepo) FileURL(filePath string, remote string) (*url.URL, error) {
relFilePath, err := filepath.Rel(filepath.Dir(g.dotGitPath), filePath)
if err != nil {
return nil, err
}
repoURL, err := gitRemoteURL(g.dotGitPath, remote)
if err != nil {
return nil, err
}
repoURL.Host = strings.TrimSuffix(repoURL.Host, ".")
repoURL.Path = strings.TrimSuffix(repoURL.Path, ".git")
if prefix, ok := gitRepoPathPrefixes[repoURL.Host]; ok {
repoURL.Path = path.Join(repoURL.Path, prefix, filepath.ToSlash(relFilePath))
} else {
return nil, fmt.Errorf("unrecognised Git repository host: %q", repoURL)
}
return repoURL, nil
}
func gitRemoteURL(repoPath string, remoteName string) (*url.URL, error) {
repo, err := git.PlainOpen(repoPath)
if err != nil {
return nil, err
}
remote, err := repo.Remote(remoteName)
if err != nil {
return nil, err
}
for _, urlStr := range remote.Config().URLs {
u, err := url.Parse(urlStr)
if err != nil {
glog.Warningf("Error parsing %q as URL from remote %q in Git repo at %q: %s", urlStr, remoteName, repoPath, err)
continue
}
return u, nil
}
return nil, fmt.Errorf("the Git remote %q does not have a valid URL", remoteName)
}