-
Notifications
You must be signed in to change notification settings - Fork 0
/
borrowed_codehost.go
50 lines (44 loc) · 1.17 KB
/
borrowed_codehost.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
// Copyright (C) 2018 The Go Authors. All rights reserved.
// Copyright (C) 2021 Ambassador Labs
//
// Use of this source code is governed by a BSD-style
// license that can be found in the NOTICE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
// This file is a verbatim subset of Go 1.17
// cmd/go/internal/modfetch/codehost/codehost.go
package main
import (
"io/fs"
)
// AllHex reports whether the revision rev is entirely lower-case hexadecimal digits.
func AllHex(rev string) bool {
for i := 0; i < len(rev); i++ {
c := rev[i]
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' {
continue
}
return false
}
return true
}
// ShortenSHA1 shortens a SHA1 hash (40 hex digits) to the canonical length
// used in pseudo-versions (12 hex digits).
func ShortenSHA1(rev string) string {
if AllHex(rev) && len(rev) == 40 {
return rev[:12]
}
return rev
}
// UnknownRevisionError is an error equivalent to fs.ErrNotExist, but for a
// revision rather than a file.
type UnknownRevisionError struct {
Rev string
}
func (e *UnknownRevisionError) Error() string {
return "unknown revision " + e.Rev
}
func (UnknownRevisionError) Is(err error) bool {
return err == fs.ErrNotExist
}