github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt/utils/utils.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package utils contains utility functions for use throughout git-dolt. 16 package utils 17 18 import ( 19 "fmt" 20 "os/exec" 21 "regexp" 22 "strings" 23 ) 24 25 // EnsureSuffix adds a suffix to a string if not already present. 26 func EnsureSuffix(s string, suffix string) string { 27 if !strings.HasSuffix(s, suffix) { 28 return s + suffix 29 } 30 return s 31 } 32 33 // LastSegment gets the last segment of a slash-separated string. 34 func LastSegment(s string) string { 35 tokens := strings.Split(s, "/") 36 return tokens[len(tokens)-1] 37 } 38 39 var hashRegex = regexp.MustCompile(`[0-9a-v]{32}`) 40 41 // CurrentRevision gets the commit hash of the currently checked-out revision of 42 // the dolt repo at the given dirname. 43 func CurrentRevision(dirname string) (string, error) { 44 cmd := exec.Command("dolt", "log", "-n", "1") 45 cmd.Dir = dirname 46 out, err := cmd.Output() 47 if err != nil { 48 return "", fmt.Errorf("error running dolt log to find current revision: %v", err) 49 } 50 return hashRegex.FindString(string(out)), nil 51 }