lockPath relativizes the given filepath such that it is relative to the root path of the repository it is contained within, taking into account the working directory of the caller. lockPaths also respects different filesystem directory separators, so that a Windows path of "\foo\bar" will be normal
(data *lockData, file string)
| 111 | // - File to lock: ./baz |
| 112 | // - Resolved path bar/baz |
| 113 | func lockPath(data *lockData, file string) (string, error) { |
| 114 | var abs string |
| 115 | var err error |
| 116 | |
| 117 | if filepath.IsAbs(file) { |
| 118 | abs, err = tools.CanonicalizeSystemPath(file) |
| 119 | if err != nil { |
| 120 | return "", errors.New(tr.Tr.Get("unable to canonicalize path %q: %v", file, err)) |
| 121 | } |
| 122 | } else { |
| 123 | abs = filepath.Join(data.workingDir, file) |
| 124 | } |
| 125 | path, err := filepath.Rel(data.rootDir, abs) |
| 126 | if err != nil { |
| 127 | return "", err |
| 128 | } |
| 129 | |
| 130 | path = filepath.ToSlash(path) |
| 131 | if strings.HasPrefix(path, "../") { |
| 132 | return "", errors.New(tr.Tr.Get("unable to canonicalize path %q", path)) |
| 133 | } |
| 134 | |
| 135 | if stat, err := os.Stat(abs); err == nil && stat.IsDir() { |
| 136 | return path, errors.New(tr.Tr.Get("cannot lock directory: %s", file)) |
| 137 | } |
| 138 | |
| 139 | return filepath.ToSlash(path), nil |
| 140 | } |
| 141 | |
| 142 | func init() { |
| 143 | RegisterCommand("lock", lockCommand, func(cmd *cobra.Command) { |
no test coverage detected