feat(repo): 新增 +clone 使用 git 克隆仓库(对标 gh repo clone)
This commit is contained in:
parent
c09645da62
commit
b9cc72d81a
|
|
@ -220,6 +220,10 @@ gitlink-cli repo +list
|
|||
# View repository info
|
||||
gitlink-cli repo +info --owner Gitlink --repo forgeplus
|
||||
|
||||
# Clone a repository with git (mirrors `gh repo clone`)
|
||||
gitlink-cli repo +clone --owner Gitlink --repo forgeplus
|
||||
gitlink-cli repo +clone --owner Gitlink --repo forgeplus -d ./forgeplus -b develop
|
||||
|
||||
# Read repository README
|
||||
gitlink-cli repo +readme --owner Gitlink --repo forgeplus --ref master
|
||||
|
||||
|
|
|
|||
|
|
@ -231,6 +231,10 @@ gitlink-cli repo +list
|
|||
# 查看仓库信息
|
||||
gitlink-cli repo +info --owner Gitlink --repo forgeplus
|
||||
|
||||
# 使用 git 克隆仓库(对标 `gh repo clone`)
|
||||
gitlink-cli repo +clone --owner Gitlink --repo forgeplus
|
||||
gitlink-cli repo +clone --owner Gitlink --repo forgeplus -d ./forgeplus -b develop
|
||||
|
||||
# 读取仓库 README
|
||||
gitlink-cli repo +readme --owner Gitlink --repo forgeplus --ref master
|
||||
|
||||
|
|
|
|||
|
|
@ -81,6 +81,8 @@
|
|||
"cmd.release.list.short": "List releases",
|
||||
"cmd.release.short": "Release operations",
|
||||
"cmd.release.view.short": "View release details",
|
||||
"cmd.repo.clone.long": "Resolve the repository clone URL through the platform API and run `git clone`, mirroring `gh repo clone`. Git progress goes to stderr; the JSON/table result stays on stdout.",
|
||||
"cmd.repo.clone.short": "Clone a repository with git",
|
||||
"cmd.repo.create.short": "Create a new repository",
|
||||
"cmd.repo.delete.short": "Delete a repository",
|
||||
"cmd.repo.fork.short": "Fork a repository",
|
||||
|
|
@ -113,6 +115,7 @@
|
|||
"error.dataset.delete_confirm": "dataset attachment deletion is destructive; run --dry-run first, then pass --yes to confirm",
|
||||
"error.missing_required_flag": "required flag --{name} is missing",
|
||||
"error.profile.user_required": "could not determine target user; pass --user or run gitlink-cli auth login",
|
||||
"error.repo.clone_url_missing": "repository response did not include a clone_url; check the owner/repo",
|
||||
"error.unsupported_language": "unsupported language: {lang}",
|
||||
"flag.api.batch_continue_on_error": "Continue running remaining batch requests after a failure",
|
||||
"flag.api.batch_dry_run": "Preview batch requests without sending remote requests",
|
||||
|
|
@ -209,6 +212,8 @@
|
|||
"flag.release.target": "Target branch",
|
||||
"flag.repo": "Repository name (auto-detected from git remote)",
|
||||
"flag.repo.category": "Filter: manage/mirror/sync/fork/all (default: manage)",
|
||||
"flag.repo.clone_branch": "Branch to check out after cloning",
|
||||
"flag.repo.clone_dir": "Target directory (defaults to the repository name)",
|
||||
"flag.repo.description": "Repository description",
|
||||
"flag.repo.name": "Repository name",
|
||||
"flag.repo.private": "Make repository private (true/false)",
|
||||
|
|
|
|||
|
|
@ -81,6 +81,8 @@
|
|||
"cmd.release.list.short": "列出发布",
|
||||
"cmd.release.short": "发布操作",
|
||||
"cmd.release.view.short": "查看发布详情",
|
||||
"cmd.repo.clone.long": "通过平台 API 解析仓库克隆地址并执行 `git clone`,对标 `gh repo clone`。git 进度输出到 stderr,JSON/表格结果保持在 stdout。",
|
||||
"cmd.repo.clone.short": "使用 git 克隆仓库",
|
||||
"cmd.repo.create.short": "创建新仓库",
|
||||
"cmd.repo.delete.short": "删除仓库",
|
||||
"cmd.repo.fork.short": "Fork 仓库",
|
||||
|
|
@ -113,6 +115,7 @@
|
|||
"error.dataset.delete_confirm": "删除数据集附件具有破坏性;请先 --dry-run 预览,再传 --yes 确认",
|
||||
"error.missing_required_flag": "缺少必需参数 --{name}",
|
||||
"error.profile.user_required": "无法确定目标用户;请通过 --user 指定,或先运行 gitlink-cli auth login 登录",
|
||||
"error.repo.clone_url_missing": "仓库响应缺少 clone_url,请检查 owner/repo 是否正确",
|
||||
"error.unsupported_language": "不支持的语言:{lang}",
|
||||
"flag.api.batch_continue_on_error": "批处理请求失败后继续执行后续请求",
|
||||
"flag.api.batch_dry_run": "预览批处理请求,不发送远端请求",
|
||||
|
|
@ -209,6 +212,8 @@
|
|||
"flag.release.target": "目标分支",
|
||||
"flag.repo": "仓库名称(自动从 git remote 检测)",
|
||||
"flag.repo.category": "筛选:manage/mirror/sync/fork/all(默认:manage)",
|
||||
"flag.repo.clone_branch": "克隆后检出的分支",
|
||||
"flag.repo.clone_dir": "目标目录(默认使用仓库名)",
|
||||
"flag.repo.description": "仓库描述",
|
||||
"flag.repo.name": "仓库名称",
|
||||
"flag.repo.private": "设为私有仓库(true/false)",
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/i18n"
|
||||
"github.com/gitlink-org/gitlink-cli/internal/output"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||||
)
|
||||
|
||||
|
|
@ -42,6 +46,51 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
|
|||
return ctx.Output(env)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "clone",
|
||||
Description: tr.T("cmd.repo.clone.short"),
|
||||
Long: tr.T("cmd.repo.clone.long"),
|
||||
Flags: []common.Flag{
|
||||
{Name: "dir", Short: "d", Usage: tr.T("flag.repo.clone_dir")},
|
||||
{Name: "branch", Short: "b", Usage: tr.T("flag.repo.clone_branch")},
|
||||
},
|
||||
Run: func(ctx *common.RuntimeContext) error {
|
||||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||||
return err
|
||||
}
|
||||
env, err := ctx.CallAPI("GET", ctx.RepoPath(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, _ := env.Data.(map[string]interface{})
|
||||
cloneURL, _ := data["clone_url"].(string)
|
||||
if cloneURL == "" {
|
||||
return errors.New(tr.T("error.repo.clone_url_missing"))
|
||||
}
|
||||
args := []string{"clone", cloneURL}
|
||||
if branch := ctx.Arg("branch"); branch != "" {
|
||||
args = append(args, "--branch", branch)
|
||||
}
|
||||
if dir := ctx.Arg("dir"); dir != "" {
|
||||
args = append(args, dir)
|
||||
}
|
||||
gitCmd := exec.Command("git", args...)
|
||||
gitCmd.Stdout = os.Stderr
|
||||
gitCmd.Stderr = os.Stderr
|
||||
if err := gitCmd.Run(); err != nil {
|
||||
return fmt.Errorf("git clone failed: %w", err)
|
||||
}
|
||||
dest := ctx.Arg("dir")
|
||||
if dest == "" {
|
||||
dest = strings.TrimSuffix(cloneURL[strings.LastIndex(cloneURL, "/")+1:], ".git")
|
||||
}
|
||||
return ctx.Output(output.SuccessEnvelope(map[string]interface{}{
|
||||
"message": "cloned",
|
||||
"clone_url": cloneURL,
|
||||
"dir": dest,
|
||||
}, nil))
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "info",
|
||||
Description: tr.T("cmd.repo.info.short"),
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/client"
|
||||
|
|
@ -634,3 +637,47 @@ func assertEqual(t *testing.T, got interface{}, want interface{}) {
|
|||
t.Fatalf("got %v (%T), want %v (%T)", got, got, want, want)
|
||||
}
|
||||
}
|
||||
|
||||
// --- clone ---
|
||||
|
||||
func TestRepoCloneRunsGit(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not available")
|
||||
}
|
||||
src := t.TempDir()
|
||||
for _, args := range [][]string{
|
||||
{"init", "-q", "--initial-branch=master", src},
|
||||
{"-C", src, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "init"},
|
||||
} {
|
||||
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
|
||||
t.Fatalf("git %v: %v (%s)", args, err, out)
|
||||
}
|
||||
}
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/owner/repo.json" {
|
||||
t.Fatalf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
writeJSON(t, w, map[string]interface{}{"identifier": "repo", "clone_url": src})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
dest := filepath.Join(t.TempDir(), "cloned-repo")
|
||||
if err := runShortcut(t, server, "clone", map[string]string{"dir": dest}); err != nil {
|
||||
t.Fatalf("clone shortcut failed: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dest, ".git")); err != nil {
|
||||
t.Fatalf("expected cloned repo at %s: %v", dest, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoCloneMissingCloneURL(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]interface{}{"identifier": "repo"})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
if err := runShortcut(t, server, "clone", map[string]string{}); err == nil {
|
||||
t.Fatal("expected error when clone_url missing")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue