forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
51 lines (47 loc) · 964 Bytes
/
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
package git
import (
"bufio"
"io"
"net/url"
"path"
"strings"
)
// NameFromRepositoryURL suggests a name for a repository URL based on the last
// segment of the path, or returns false
func NameFromRepositoryURL(url *url.URL) (string, bool) {
// from path
if len(url.Path) > 0 {
base := path.Base(url.Path)
if len(base) > 0 && base != "/" {
if ext := path.Ext(base); ext == ".git" {
base = base[:len(base)-4]
}
return base, true
}
}
return "", false
}
type ChangedRef struct {
Ref string
Old string
New string
}
func ParsePostReceive(r io.Reader) ([]ChangedRef, error) {
refs := []ChangedRef{}
scan := bufio.NewScanner(r)
for scan.Scan() {
segments := strings.Split(scan.Text(), " ")
if len(segments) != 3 {
continue
}
refs = append(refs, ChangedRef{
Ref: segments[2],
Old: segments[0],
New: segments[1],
})
}
if err := scan.Err(); err != nil && err != io.EOF {
return nil, err
}
return refs, nil
}