Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Unified Diff: fiddle/go/named/named.go

Issue 1912793002: fiddle: Don't overwrite existing fiddle names unintentionally. (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « fiddle/go/fiddle/main.go ('k') | fiddle/go/named/named_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: fiddle/go/named/named.go
diff --git a/fiddle/go/named/named.go b/fiddle/go/named/named.go
index a8c8074dcb240387c7a52ab9fc8ef73f70e0137e..5dab096c8e0e91b582c8e64eb3849c46858ab3b2 100644
--- a/fiddle/go/named/named.go
+++ b/fiddle/go/named/named.go
@@ -2,6 +2,7 @@
package named
import (
+ "errors"
"fmt"
"path/filepath"
"regexp"
@@ -13,6 +14,8 @@ import (
)
var (
+ DuplicateNameErr = errors.New("Name already exists.")
+
// fiddleHashRe is used to validate fiddle hashes.
fiddleHashRe = regexp.MustCompile("^[0-9a-zA-Z]{32}$")
@@ -57,10 +60,11 @@ func New(st NameStore) *Named {
// Add a named fiddle.
//
-// name - The name of the fidde, w/o the @ prefix.
-// hash - The fiddle hash.
-// user - The email of the user that created the name.
-func (n *Named) Add(name, hash, user string) error {
+// name - The name of the fidde, w/o the @ prefix.
+// hash - The fiddle hash.
+// user - The email of the user that created the name.
+// overwrite - True if the write should proceed if the name already exists.
+func (n *Named) Add(name, hash, user string, overwrite bool) error {
if !fiddleNameRe.MatchString(name) {
return fmt.Errorf("Not a valid fiddle name %q", name)
}
@@ -69,6 +73,10 @@ func (n *Named) Add(name, hash, user string) error {
}
oldHash, err := n.DereferenceID("@" + name)
if err == nil {
rmistry 2016/04/25 12:59:18 I had to look at this a few times to see what it w
+ // This name exists already.
+ if !overwrite {
+ return DuplicateNameErr
+ }
if oldHash == hash {
// Don't bother writing if the hash is already correct.
return nil
« no previous file with comments | « fiddle/go/fiddle/main.go ('k') | fiddle/go/named/named_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698