【发布时间】:2019-11-23 18:51:05
【问题描述】:
我正在开发 https://github.com/cdr/sshcode 的分支,更具体地说,我正在开发 PR 以向程序添加 git4win/msys2 支持
目前问题源于这两个函数
func gitbashWindowsDir(dir string) string {
if dir == "~" { //Special case
return "~/"
}
mountPoints := gitbashMountPointsAndHome()
// Apply mount points
absDir, _ := filepath.Abs(dir)
absDir = filepath.ToSlash(absDir)
for _, mp := range mountPoints {
if strings.HasPrefix(absDir, mp[0]) {
resolved := strings.Replace(absDir, mp[0], mp[1], 1)
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
return resolved
}
}
return dir
}
// This function returns an array with MINGW64 mount points including relative home dir
func gitbashMountPointsAndHome() [][]string {
// Initialize mount points with home dir
mountPoints := [][]string{{filepath.ToSlash(os.Getenv("HOME")), "~"}}
// Load mount points
out, err := exec.Command("mount").Output()
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(out), "\n")
var mountRx = regexp.MustCompile(`^(.*) on (.*) type`)
for _, line := range lines {
extract := mountRx.FindStringSubmatch(line)
if len(extract) > 0 {
mountPoints = append(mountPoints, []string{extract[1], extract[2]})
}
res = strings.TrimPrefix(dir, line)
}
// Sort by size to get more restrictive mount points first
sort.Slice(mountPoints, func(i, j int) bool {
return len(mountPoints[i][0]) > len(mountPoints[j][0])
})
return mountPoints
}
这是如何使用的,在 msys2/git4win 上,你给 gitbashWindowsDir("/Workspace") 它应该返回 /Workspace 因为它处理 msys2/git4win 处理路径的非正统方式。
当你给它gitbashWindowsDir("Workspace/") 时,它会返回与echo $PWD/Workspace/ 基本相同的输出,但采用窗口格式
我正在开发一个使用 strings.Prefix 东西的第一步补丁,
到目前为止,这就是我为这个补丁所拥有的东西
package main
import (
"fmt"
"os"
)
func main() {
mydir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
fmt.Println(os.Getenv("PWD"))
fmt.Println(mydir)
}
我想检查输入是否有前缀/,如果有,只需将其作为字符串返回,这似乎是gitbashWindowsDir("/Workspace") 返回//Workspace 的简单修复
但我认为最困难的部分是gitbashWindowsDir("Workspace/"),因为它返回与echo $PWD/Workspace/ 相同的输出,但采用Windows 格式(Z:\Workspace\)
______________________________________________ ______________________________________________
更新,我已经得到前缀修剪工作,(愚蠢的简单) 但是现在我遇到了这个问题
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"go.coder.com/flog"
)
func main() {
fmt.Println("RESOLVED: ", gitbashWindowsDir(os.Args[1]))
fmt.Println("RESOLVED: ", gitbashWindowsDir("C:\\msys64\\Workspace"))
}
func gitbashWindowsDir(dir string) string {
// if dir is left empty, line82:main.go will set it to `~`, this makes it so that
// if dir is `~`, return `~/` instead of continuing with the gitbashWindowsDir()
// function.
if dir == "~" {
return "~/"
}
mountPoints := gitbashMountPointsAndHome()
// Apply mount points
absDir, _ := filepath.Abs(dir)
absDir = filepath.ToSlash(absDir)
for _, mp := range mountPoints {
if strings.HasPrefix(absDir, mp[0]) {
resolved := strings.Replace(absDir, mp[0], mp[1], 1)
if strings.HasPrefix(resolved, "//") {
resolved = strings.TrimPrefix(resolved, "/")
flog.Info("DEBUG: strings.TrimPrefix")
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
flog.Info("'%s'", resolved)
return resolved
}
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
return resolved
}
}
return dir
}
// This function returns an array with MINGW64 mount points including relative home dir
func gitbashMountPointsAndHome() [][]string {
mountPoints := [][]string{{filepath.ToSlash(os.Getenv("HOME")), "~"}}
// Load mount points
out, err := exec.Command("mount").Output()
if err != nil {
//log.Error(err)
log.Println(err)
}
lines := strings.Split(string(out), "\n")
var mountRx = regexp.MustCompile(`^(.*) on (.*) type`)
for _, line := range lines {
extract := mountRx.FindStringSubmatch(line)
if len(extract) > 0 {
mountPoints = append(mountPoints, []string{extract[1], extract[2]})
}
}
// Sort by size to get more restrictive mount points first
sort.Slice(mountPoints, func(i, j int) bool {
return len(mountPoints[i][0]) > len(mountPoints[j][0])
})
return mountPoints
}
当它运行时,它会返回
merith@DESKTOP-BQUQ80R MINGW64 /z/sshcode
$ go run ../debug.go Workspace
2019-11-27 10:49:38 INFO Resolved windows path 'Workspace' to '/z/sshcode/Workspace
RESOLVED: /z/sshcode/Workspace
2019-11-27 10:49:38 INFO DEBUG: strings.TrimPrefix
2019-11-27 10:49:38 INFO Resolved windows path 'C:\msys64\Workspace' to '/Workspace
2019-11-27 10:49:38 INFO '/Workspace'
RESOLVED: /Workspace
现在我需要弄清楚如何检测和删除/*/ 前缀,以便/z/sshcode/Workspace 变为Workspace/
【问题讨论】: