golang.org/x/build@v0.0.0-20240506185731-218518f32b70/devapp/title.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "path" 9 "strings" 10 ) 11 12 // ParsePrefixedChangeTitle parses a prefixed change title. 13 // It returns a list of paths from the prefix joined with root, and the remaining change title. 14 // It does not try to verify whether each path is an existing Go package. 15 // 16 // Supported forms include: 17 // 18 // "root", "import/path: change title" -> ["root/import/path"], "change title" 19 // "root", "path1, path2: change title" -> ["root/path1", "root/path2"], "change title" # Multiple comma-separated paths. 20 // 21 // If there's no path prefix (preceded by ": "), title is returned unmodified 22 // with a paths list containing root: 23 // 24 // "root", "change title" -> ["root"], "change title" 25 // 26 // If there's a branch prefix in square brackets, title is returned with said prefix: 27 // 28 // "root", "[branch] path: change title" -> ["root/path"], "[branch] change title" 29 func ParsePrefixedChangeTitle(root, prefixedTitle string) (paths []string, title string) { 30 // Parse branch prefix in square brackets, if any. 31 // E.g., "[branch] path: change title" -> "[branch] ", "path: change title". 32 var branch string // "[branch] " or empty string. 33 if strings.HasPrefix(prefixedTitle, "[") { 34 if idx := strings.Index(prefixedTitle, "] "); idx != -1 { 35 branch, prefixedTitle = prefixedTitle[:idx+len("] ")], prefixedTitle[idx+len("] "):] 36 } 37 } 38 39 // Parse the rest of the prefixed change title. 40 // E.g., "path1, path2: change title" -> ["path1", "path2"], "change title". 41 idx := strings.Index(prefixedTitle, ": ") 42 if idx == -1 { 43 return []string{root}, branch + prefixedTitle 44 } 45 prefix, title := prefixedTitle[:idx], prefixedTitle[idx+len(": "):] 46 if strings.ContainsAny(prefix, "{}") { 47 // TODO: Parse "image/{png,jpeg}" as ["image/png", "image/jpeg"], maybe? 48 return []string{path.Join(root, strings.TrimSpace(prefix))}, branch + title 49 } 50 paths = strings.Split(prefix, ",") 51 for i := range paths { 52 paths[i] = path.Join(root, strings.TrimSpace(paths[i])) 53 } 54 return paths, branch + title 55 }