fix(api): restore API path polluted by MSYS2/Git Bash on Windows
在 Windows Git Bash (MSYS2) 环境下,gitlink-cli api GET /v1/owner/repo 的路径参数会被 MSYS2 自动改写为类似 C:/Program Files/Git/v1/owner/repo 的 Windows 路径(MSYS2 把 /v1 当成 Unix 路径转换为 Git 安装目录),导致 API 调用 404。 修复:在 runAPI 中检测路径首部是否为盘符(^[A-Za-z]:/), 若是则按常见 API 前缀(/v1/ /v2/ /api/ /users/ /projects/) 定位并还原原始 API 路径。 影响所有 Windows Git Bash 用户,跨平台兼容性修复。
This commit is contained in:
parent
9749a4c832
commit
b0424aa6d8
|
|
@ -7,6 +7,7 @@ import (
|
|||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -60,6 +61,10 @@ func validateAPIArgs(c *cobra.Command, args []string) error {
|
|||
return cobra.ExactArgs(2)(c, args)
|
||||
}
|
||||
|
||||
// msysPathRe matches Windows drive-letter prefixes produced by MSYS2/Git Bash
|
||||
// path conversion, e.g. "C:/Program Files/Git/v1/owner/repo" for input "/v1/owner/repo".
|
||||
var msysPathRe = regexp.MustCompile(`^[A-Za-z]:/`)
|
||||
|
||||
func runAPI(c *cobra.Command, args []string) error {
|
||||
batchFile, _ := c.Flags().GetString("batch-file")
|
||||
if batchFile != "" {
|
||||
|
|
@ -69,6 +74,18 @@ func runAPI(c *cobra.Command, args []string) error {
|
|||
method := strings.ToUpper(args[0])
|
||||
path := args[1]
|
||||
|
||||
// Fix MSYS2/Git Bash path auto-conversion on Windows:
|
||||
// "/v1/owner/repo" is rewritten to "C:/Program Files/Git/v1/owner/repo".
|
||||
// Detect the drive-letter prefix and restore the original API path.
|
||||
if msysPathRe.MatchString(path) {
|
||||
for _, prefix := range []string{"/v1/", "/v2/", "/api/", "/users/", "/projects/"} {
|
||||
if idx := strings.Index(path, prefix); idx >= 0 {
|
||||
path = path[idx:]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue