Commit Graph

2 Commits

Author SHA1 Message Date
Dennis Ameling
d7a3a090df Update formatting for Go 1.17
From the 1.17 release notes (https://golang.org/doc/go1.17#gofmt): gofmt (and go fmt) now synchronizes //go:build lines with // +build lines.

More info about this change can be found at https://golang.org/design/draft-gobuild
2021-08-17 20:24:58 +02:00
brian m. carlson
a66274014c
tools: add a function to properly canonicalize paths
Git consistently uses canonicalized paths internally.  This is for many
reasons, but mostly to verify that a single path is within a repository.
In order to interoperate properly with Git, we need to canonicalize
paths and do it in the same way as Git.

On Unix systems, to canonicalize a path, it is sufficient to make the
path absolute and then resolve any symlinks.  Go provides two functions
to do these two steps, filepath.Abs and filepath.EvalSymlinks, and they
work as advertised.

Windows, however, has much more complex path handling and these
functions do not handle all cases.  The typical way to canonicalize
paths on Windows is using GetFinalPathNameByHandle, and this is the
technique Git uses.

Go, however, does not provide a general API to canonicalize paths,
unlike Rust's std::fs::canonicalize and similar functionality in myriad
other languages.  Therefore, in order to get this working on Windows,
let's add a function to canonicalize paths in the appropriate system
way, one for Unix systems and one for Windows.  The code comes from Go's
standard library, so update the copyright and license accordingly.

Update the CanonicalizePath function to use the new function.  We
duplicate the Abs call because we an absolute path in CanonicalizePath
in some cases even if the path is missing, whereas the new function
needs to do it in all cases because we will use it other situations in
the future.  This should be a simple string check, so it should not
involve an extra system call or other overhead.
2021-03-01 22:10:19 +00:00