Skip to content

Commit e9d6abb

Browse files
committed
update docs
1 parent 032b93c commit e9d6abb

8 files changed

Lines changed: 60 additions & 16 deletions

File tree

docs/Agents.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ spark git issues # 从 Markdown 文档/任务创建 GitHub Issue
102102
```bash
103103
spark git update -p /path/to/repos
104104
spark git update -p ~/workspace -p ~/projects
105+
spark git update --ssh # 强制通过 SSH 更新(HTTPS 不稳定时使用)
105106
```
106107

107-
详细文档: [docs/usage/update.md](docs/usage/update.md)
108+
详细文档: [docs/usage/git.md](docs/usage/git.md)
108109

109110
#### `spark git submodule`
110111
将本地 Git 仓库添加为子模块,或将远程仓库克隆为子模块。

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ go test ./internal/git/... -v -run TestFunctionName
6969

7070
| Command | Description |
7171
|---------|-------------|
72-
| `spark git update` | Update all repos to latest version |
72+
| `spark git update [--ssh]` | Update all repos to latest version (`--ssh` 强制 SSH) |
7373
| `spark git submodule add [-p <path>]` | Add existing repos as submodules |
7474
| `spark git sync [repo]` | Sync all submodules to latest |
7575
| `spark git gitcode [-p <path>]` | Add Gitcode remote to repos |

docs/features/git.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
### 多仓库批量更新
1010

11-
扫描配置目录下所有 Git 仓库,执行批量 `git pull`。适合同时维护多个仓库的日常更新。
11+
扫描配置目录下所有 Git 仓库,执行批量 `git fetch --all && git pull`。适合同时维护多个仓库的日常更新。当 HTTPS 连接不稳定时,可加 `--ssh` 强制通过 SSH 更新(仅本次有效,不修改仓库配置的远程地址)
1212

1313
```bash
1414
spark git update -p ~/workspace
15+
spark git update -p ~/workspace --ssh
1516
```
1617

1718
### Submodule 管理

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ go test ./internal/git/... -v -run TestFunctionName
7474

7575
| Command | Description |
7676
|---------|-------------|
77-
| `spark git update` | Update all repos to latest version |
77+
| `spark git update [--ssh]` | Update all repos to latest version (`--ssh` 强制 SSH) |
7878
| `spark git submodule add [-p <path>]` | Add existing repos as submodules |
7979
| `spark git sync [repo]` | Sync all submodules to latest |
8080
| `spark git gitcode [-p <path>]` | Add Gitcode remote to repos |

docs/quick-start/getting-started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ github_owner: your-username
2929
3030
```bash
3131
spark git update -p ~/workspace # 更新所有仓库
32+
spark git update -p ~/workspace --ssh # HTTPS 不稳定时强制通过 SSH 更新
3233
spark git init --owner variableway # 初始化仓库并创建 GitHub 远程
3334
spark git submodule add -p /path/to/repos # 添加仓库为子模块
3435
spark git batch-clone variableway -o ./repos # 克隆组织仓库

docs/spec/git.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ spark git
1414

1515
## spark git update
1616

17-
更新所有 Git 仓库到最新版本。扫描配置中 `repo-path` 下的所有仓库,执行 `git pull`
17+
更新所有 Git 仓库到最新版本。扫描配置中 `repo-path` 下的所有仓库,执行 `git fetch --all && git pull`
1818

1919
```
20-
spark git update [-p <path>]
20+
spark git update [-p <path>] [--ssh]
2121
```
2222

2323
| 标志 | 类型 | 默认值 | 必填 | 说明 |
2424
|------|------|--------|------|------|
2525
| `-p, --path` | stringSlice | `["."]` || 包含 Git 仓库的目录路径 |
26+
| `--ssh` | bool | `false` || 强制通过 SSH 更新(临时将 HTTPS GitHub URL 改写为 SSH,不修改仓库配置) |
2627

2728
无参数。
2829

internal/git/submodule.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,13 @@ func rewriteGitModulesURLs(repoPath string) error {
193193
}
194194

195195
func httpsToSSH(url string) string {
196-
if strings.HasPrefix(url, "https://github.com/") {
197-
return "git@github.com:" + strings.TrimPrefix(url, "https://github.com/")
196+
if strings.HasPrefix(url, "https://") {
197+
withoutScheme := strings.TrimPrefix(url, "https://")
198+
if idx := strings.Index(withoutScheme, "/"); idx != -1 {
199+
host := withoutScheme[:idx]
200+
path := withoutScheme[idx+1:]
201+
return "git@" + host + ":" + path
202+
}
198203
}
199204
return url
200205
}

internal/git/updater.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ import (
88
"strings"
99
)
1010

11+
// httpsHost extracts the host from an HTTPS URL, or empty string if not HTTPS.
12+
func httpsHost(url string) string {
13+
if !strings.HasPrefix(url, "https://") {
14+
return ""
15+
}
16+
withoutScheme := strings.TrimPrefix(url, "https://")
17+
if idx := strings.Index(withoutScheme, "/"); idx != -1 {
18+
return withoutScheme[:idx]
19+
}
20+
return withoutScheme
21+
}
22+
1123
// UpdateRepository updates a git repository to the latest version.
12-
// When useSSH is true, HTTPS GitHub remote URLs are forced to SSH for this
24+
// When useSSH is true, HTTPS remote URLs are forced to SSH for this
1325
// update via a transient url.insteadOf config override, without modifying the
1426
// repository's configured remote URLs.
1527
func UpdateRepository(repoPath string, useSSH bool) error {
@@ -22,15 +34,38 @@ func UpdateRepository(repoPath string, useSSH bool) error {
2234
}
2335
currentBranch := strings.TrimSpace(string(branch))
2436

25-
// gitArgs builds a git command scoped to the repo, prepending an SSH
26-
// url.insteadOf override when useSSH is set so HTTPS GitHub URLs are
27-
// transparently rewritten to SSH for the duration of this command only.
37+
// Build url.insteadOf overrides for all HTTPS remotes when useSSH is set.
38+
var sshOverrides []string
39+
if useSSH {
40+
remotesCmd := exec.Command("git", "remote")
41+
remotesCmd.Dir = repoPath
42+
remotesOut, err := remotesCmd.Output()
43+
if err == nil {
44+
for _, remote := range strings.Split(strings.TrimSpace(string(remotesOut)), "\n") {
45+
if remote == "" {
46+
continue
47+
}
48+
urlCmd := exec.Command("git", "remote", "get-url", remote)
49+
urlCmd.Dir = repoPath
50+
urlOut, err := urlCmd.Output()
51+
if err != nil {
52+
continue
53+
}
54+
remoteURL := strings.TrimSpace(string(urlOut))
55+
if host := httpsHost(remoteURL); host != "" {
56+
sshOverrides = append(sshOverrides,
57+
"-c", fmt.Sprintf("url.git@%s:.insteadOf=https://%s/", host, host))
58+
}
59+
}
60+
}
61+
}
62+
63+
// gitArgs builds a git command scoped to the repo, prepending SSH
64+
// url.insteadOf overrides when useSSH is set.
2865
gitArgs := func(subcmd ...string) *exec.Cmd {
2966
args := subcmd
30-
if useSSH {
31-
args = append([]string{
32-
"-c", "url.git@github.com:.insteadOf=https://github.com/",
33-
}, args...)
67+
if len(sshOverrides) > 0 {
68+
args = append(sshOverrides, args...)
3469
}
3570
c := exec.Command("git", args...)
3671
c.Dir = repoPath

0 commit comments

Comments
 (0)