From eed7e5d51451186430f4265cd023815670120c15 Mon Sep 17 00:00:00 2001 From: James Cor Date: Thu, 25 Jul 2024 16:06:12 -0700 Subject: [PATCH] make tags case insensitive (#8153) --- go/cmd/dolt/commands/sql.go | 2 +- go/libraries/doltcore/doltdb/doltdb.go | 15 ++++---- .../doltcore/sqle/database_provider.go | 14 ++++---- .../doltcore/sqle/enginetest/dolt_queries.go | 34 +++++++++++++++++++ 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/go/cmd/dolt/commands/sql.go b/go/cmd/dolt/commands/sql.go index 379db9cb4d0..4240829c85d 100644 --- a/go/cmd/dolt/commands/sql.go +++ b/go/cmd/dolt/commands/sql.go @@ -906,7 +906,7 @@ func getDBBranchFromSession(sqlCtx *sql.Context, qryist cli.Queryist) (db string // It is possible to `use mydb/branch`, and as far as your session is concerned your database is mydb/branch. We // allow that, but also want to show the user the branch name in the prompt. So we munge the DB in this case. - if strings.HasSuffix(db, "/"+branch) { + if strings.HasSuffix(strings.ToLower(db), strings.ToLower("/"+branch)) { db = db[:len(db)-len(branch)-1] } } diff --git a/go/libraries/doltcore/doltdb/doltdb.go b/go/libraries/doltcore/doltdb/doltdb.go index 82d269834bb..28ad921342b 100644 --- a/go/libraries/doltcore/doltdb/doltdb.go +++ b/go/libraries/doltcore/doltdb/doltdb.go @@ -980,15 +980,14 @@ func (ddb *DoltDB) GetBranchesByNomsRoot(ctx context.Context, nomsRoot hash.Hash // HasBranch returns whether the DB has a branch with the name given, case-insensitive. Returns the case-sensitive // matching branch if found, as well as a bool indicating if there was a case-insensitive match, and any error. func (ddb *DoltDB) HasBranch(ctx context.Context, branchName string) (string, bool, error) { - branchName = strings.ToLower(branchName) branches, err := ddb.GetRefsOfType(ctx, branchRefFilter) if err != nil { return "", false, err } for _, b := range branches { - if strings.ToLower(b.GetPath()) == branchName { - return b.GetPath(), true, nil + if path := b.GetPath(); strings.EqualFold(path, branchName) { + return path, true, nil } } @@ -1064,19 +1063,19 @@ func (ddb *DoltDB) GetTags(ctx context.Context) ([]ref.DoltRef, error) { } // HasTag returns whether the DB has a tag with the name given -func (ddb *DoltDB) HasTag(ctx context.Context, tagName string) (bool, error) { +func (ddb *DoltDB) HasTag(ctx context.Context, tagName string) (string, bool, error) { tags, err := ddb.GetRefsOfType(ctx, tagsRefFilter) if err != nil { - return false, err + return "", false, err } for _, t := range tags { - if t.GetPath() == tagName { - return true, nil + if path := t.GetPath(); strings.EqualFold(path, tagName) { + return path, true, nil } } - return false, nil + return "", false, nil } type TagWithHash struct { diff --git a/go/libraries/doltcore/sqle/database_provider.go b/go/libraries/doltcore/sqle/database_provider.go index 12ba86629af..8abe6ca7855 100644 --- a/go/libraries/doltcore/sqle/database_provider.go +++ b/go/libraries/doltcore/sqle/database_provider.go @@ -930,13 +930,13 @@ func revisionDbType(ctx *sql.Context, srcDb dsess.SqlDatabase, revSpec string) ( return dsess.RevisionTypeBranch, caseSensitiveBranchName, nil } - isTag, err := isTag(ctx, srcDb, resolvedRevSpec) + caseSensitiveTagName, isTag, err := isTag(ctx, srcDb, resolvedRevSpec) if err != nil { return dsess.RevisionTypeNone, "", err } if isTag { - return dsess.RevisionTypeTag, resolvedRevSpec, nil + return dsess.RevisionTypeTag, caseSensitiveTagName, nil } if doltdb.IsValidCommitHash(resolvedRevSpec) { @@ -1401,21 +1401,21 @@ func isRemoteBranch(ctx context.Context, ddbs []*doltdb.DoltDB, branchName strin } // isTag returns whether a tag with the given name is in scope for the database given -func isTag(ctx context.Context, db dsess.SqlDatabase, tagName string) (bool, error) { +func isTag(ctx context.Context, db dsess.SqlDatabase, tagName string) (string, bool, error) { ddbs := db.DoltDatabases() for _, ddb := range ddbs { - tagExists, err := ddb.HasTag(ctx, tagName) + tName, tagExists, err := ddb.HasTag(ctx, tagName) if err != nil { - return false, err + return "", false, err } if tagExists { - return true, nil + return tName, true, nil } } - return false, nil + return "", false, nil } // revisionDbForBranch returns a new database that is tied to the branch named by revSpec diff --git a/go/libraries/doltcore/sqle/enginetest/dolt_queries.go b/go/libraries/doltcore/sqle/enginetest/dolt_queries.go index 14a1031b58d..fa96f343c85 100644 --- a/go/libraries/doltcore/sqle/enginetest/dolt_queries.go +++ b/go/libraries/doltcore/sqle/enginetest/dolt_queries.go @@ -4619,6 +4619,40 @@ var DoltTagTestScripts = []queries.ScriptTest{ }, }, }, + { + Name: "dolt-tag: case insensitive", + SetUpScript: []string{ + "CREATE TABLE test(pk int primary key);", + "CALL DOLT_COMMIT('-Am','created table test');", + "CALL DOLT_TAG('ABC');", + "INSERT INTO test VALUES (0),(1),(2);", + "CALL DOLT_COMMIT('-am','inserted rows into test');", + }, + Assertions: []queries.ScriptTestAssertion{ + { + Query: "SELECT tag_name FROM dolt_tags", + Expected: []sql.Row{ + {"ABC"}, + }, + }, + { + Query: "select * from test;", + Expected: []sql.Row{ + {0}, + {1}, + {2}, + }, + }, + { + Query: "use mydb/abc;", + Expected: []sql.Row{}, + }, + { + Query: "select * from test;", + Expected: []sql.Row{}, + }, + }, + }, } var DoltRemoteTestScripts = []queries.ScriptTest{