| 1 |
package core |
| 2 |
|
| 3 |
/* Branch represents a named line of development. |
| 4 |
* Uses SVN-style global revision numbering - a branch is just a name, |
| 5 |
* revisions are still globally sequential (r123 on port, r124 on feature/x). */ |
| 6 |
|
| 7 |
type Branch struct { |
| 8 |
Name string `json:"name"` // "port", "feature/foo", "release/1.0" |
| 9 |
HeadRev int64 `json:"head_rev"` // current tip revision number |
| 10 |
CreatedAt int64 `json:"created_at"` // unix timestamp |
| 11 |
CreatedFrom int64 `json:"created_from"` // revision number this branch was created from |
| 12 |
} |
| 13 |
|
| 14 |
// DefaultBranchName is the default branch name (like SVN's trunk, but we call it "port") |
| 15 |
const DefaultBranchName = "port" |
| 16 |
|
| 17 |
// BranchNameValid checks if a branch name is valid |
| 18 |
func BranchNameValid(name string) bool { |
| 19 |
if len(name) == 0 || len(name) > 255 { |
| 20 |
return false |
| 21 |
} |
| 22 |
|
| 23 |
/* NOTE(kroot): branch names follow simple rules: |
| 24 |
* - no leading/trailing slashes |
| 25 |
* - no double slashes |
| 26 |
* - no special chars except / - _ . |
| 27 |
* - no reserved names */ |
| 28 |
|
| 29 |
for i, r := range name { |
| 30 |
switch { |
| 31 |
case r >= 'a' && r <= 'z': |
| 32 |
continue |
| 33 |
case r >= 'A' && r <= 'Z': |
| 34 |
continue |
| 35 |
case r >= '0' && r <= '9': |
| 36 |
continue |
| 37 |
case r == '-' || r == '_' || r == '.': |
| 38 |
continue |
| 39 |
case r == '/': |
| 40 |
/* no leading slash */ |
| 41 |
if i == 0 { |
| 42 |
return false |
| 43 |
} |
| 44 |
/* no trailing slash */ |
| 45 |
if i == len(name)-1 { |
| 46 |
return false |
| 47 |
} |
| 48 |
/* no double slashes */ |
| 49 |
if i > 0 && name[i-1] == '/' { |
| 50 |
return false |
| 51 |
} |
| 52 |
continue |
| 53 |
default: |
| 54 |
return false |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return true |
| 59 |
} |
| 60 |
|