summaryrefslogtreecommitdiff
path: root/readonly.go
blob: 27e23eea852d8324a005ecd4ab9dce6caa211619 (plain)
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
package forgepb

import "strings"

func (all *Repos) UpdateGoPath(name string, gopath string) bool {
	oldr := all.DeleteByPath(name)
	if oldr == nil {
		// nothing to update
		return false
	}

	// update gopath and append it back to the list
	oldr.GoPath = gopath
	return all.Append(oldr)
}

// returns true if gopath is readonly()
func (all *Repos) IsReadOnly(gopath string) bool {
	var match *Repo

	loop := all.SortByPath() // get the list of repos
	for loop.Scan() {
		r := loop.Repo()
		if r.GoPath == gopath {
			// exact gopath match
			if r.Writable {
				return false
			}
			if r.ReadOnly {
				return true
			}
			// private is assumed to be r/w unless above is specifically set
			if r.Private {
				return false
			}
		}
		// search for potential dir matches
		if r.Directory {
			// test the dir
			if strings.HasPrefix(gopath, r.GoPath) {
				match = r
			}
		}
	}

	// take the settings from the directory match
	if match.Writable {
		return false
	}
	if match.ReadOnly {
		return true
	}
	// private is assumed to be r/w unless above is specifically set
	if match.Private {
		return false
	}

	// always assume readonly
	return true
}

// is this a non-publishable repo?
func (all *Repos) IsPrivate(gopath string) bool {
	var match *Repo

	loop := all.SortByPath() // get the list of repos
	for loop.Scan() {
		r := loop.Repo()
		if r.GoPath == gopath {
			// if private is set here, then ok, otherwise
			// still check if a Directory match exists
			if r.Private {
				return true
			}
		}
		// search for potential dir matches
		if r.Directory {
			// test the dir
			if strings.HasPrefix(gopath, r.GoPath) {
				match = r
			}
		}
	}

	if match.Private {
		return true
	}

	// otherwise, assume not private
	return true
}