-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_manager.py
More file actions
593 lines (492 loc) · 22.2 KB
/
Copy pathproject_manager.py
File metadata and controls
593 lines (492 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
"""项目管理 | Project Manager
管理 git-based 项目:创建(clone)、列表、查询。
Stores project metadata in ~/.niuma/niuma.db."""
import os
import sqlite3
import subprocess
import sys
from pathlib import Path
_PROJECTS_DIR = Path.home() / ".niuma" / "projects"
_DB_PATH = Path.home() / ".niuma" / "niuma.db"
def _ensure_dirs() -> None:
_PROJECTS_DIR.mkdir(parents=True, exist_ok=True)
def _get_db() -> sqlite3.Connection:
_ensure_dirs()
db = sqlite3.connect(str(_DB_PATH))
db.row_factory = sqlite3.Row
db.execute("PRAGMA journal_mode=WAL")
db.execute("""
CREATE TABLE IF NOT EXISTS projects (
name TEXT PRIMARY KEY,
git_url TEXT NOT NULL,
local_path TEXT NOT NULL,
created_at TEXT DEFAULT (datetime('now'))
)
""")
db.commit()
return db
def list_projects() -> list[dict]:
"""返回所有项目的 [{name, git_url, local_path, created_at}, ...]。"""
db = _get_db()
rows = db.execute("SELECT * FROM projects ORDER BY created_at DESC").fetchall()
db.close()
return [dict(r) for r in rows]
def get_project(name: str) -> dict | None:
"""获取单个项目信息,不存在返回 None。"""
db = _get_db()
row = db.execute("SELECT * FROM projects WHERE name = ?", (name,)).fetchone()
db.close()
return dict(row) if row else None
def create_project(name: str, git_url: str, proxy: str = "") -> dict:
"""创建新项目:git clone → 写入 DB。返回项目 dict。失败时抛出 RuntimeError。"""
_ensure_dirs()
# 检查名称是否重复
existing = get_project(name)
if existing:
raise RuntimeError(
f"项目 '{name}' 已存在 | Project '{name}' already exists.\n"
f"路径 | path: {existing['local_path']}"
)
# 检查 git URL 是否已被其他项目使用
db = _get_db()
dup = db.execute("SELECT name FROM projects WHERE git_url = ?", (git_url,)).fetchone()
db.close()
if dup:
raise RuntimeError(
f"该 git 地址已被项目 '{dup['name']}' 使用 | "
f"Git URL already used by project '{dup['name']}'"
)
local_path = _PROJECTS_DIR / name
# git clone
print(f" Cloning {git_url} ...")
clone_cmd = ["git", "clone", git_url, str(local_path)]
if proxy:
clone_cmd = ["git", "clone", "-c", f"http.proxy={proxy}", "-c", f"https.proxy={proxy}", git_url, str(local_path)]
result = subprocess.run(clone_cmd, capture_output=True, text=True, encoding="utf-8", errors="replace")
if result.returncode != 0:
# 清理失败的克隆目录
if local_path.exists():
import shutil
shutil.rmtree(local_path, ignore_errors=True)
raise RuntimeError(
f"Git clone 失败 | failed:\n{result.stderr.strip()}\n"
f"如果是网络问题,尝试设置代理后重试 | If network issue, retry with proxy"
)
# 写入 DB
db = _get_db()
db.execute(
"INSERT INTO projects (name, git_url, local_path) VALUES (?, ?, ?)",
(name, git_url, str(local_path)),
)
db.commit()
db.close()
print(f" [OK] 项目已创建 | project created: {local_path}")
return {"name": name, "git_url": git_url, "local_path": str(local_path)}
def get_project_path(name: str) -> Path | None:
"""返回项目本地路径,不存在返回 None。"""
proj = get_project(name)
if not proj:
return None
p = Path(proj["local_path"])
return p if p.exists() else None
def delete_project(name: str) -> None:
"""删除项目:从 DB 中移除记录。不删除本地文件(安全考虑)。"""
db = _get_db()
db.execute("DELETE FROM projects WHERE name = ?", (name,))
db.commit()
db.close()
# ═══════════════════════════════════════════════════════════════
# Git 操作 —— 强/弱模型通过 git 分支通信
# ═══════════════════════════════════════════════════════════════
GIT_AUTHOR_COMPILER = ("强模型", "niuma@compiler")
GIT_AUTHOR_WORKER = ("弱模型", "niuma@worker")
GIT_AUTHOR_REVIEWER = ("强模型", "niuma@compiler")
BRANCH_PREFIX = "niuma"
def _zh() -> bool:
"""当前是否为中文语言。"""
import config as _cfg
return _cfg.get_lang() != "en"
def msg_requirement(desc: str) -> str:
"""需求确认的 commit message。"""
short = desc.strip().replace("\n", " ")[:60]
if _zh():
return f"需求确认: {short}"
return f"Requirement confirmed: {short}"
def msg_compiler(nodes: int, summary: str = "") -> str:
"""编译器完成 DAG 的 commit message。"""
if _zh():
extra = f" — {summary}" if summary else ""
return f"任务分解完成,拆成了 {nodes} 个模块{extra}"
extra = f" — {summary}" if summary else ""
return f"Task decomposed into {nodes} modules{extra}"
def msg_worker(node_id: str, name: str, passed: bool, iterations: int) -> str:
"""Worker 完成节点的 commit message。"""
status = "完成了" if passed else "未能完成"
if _zh():
return f"完成了模块 {name}({node_id}),尝试了 {iterations} 次" if passed else \
f"未能完成模块 {name}({node_id}),尝试了 {iterations} 次"
return f"{'Completed' if passed else 'Failed'} module {name}({node_id}) after {iterations} attempt(s)"
def msg_review(passed: bool, score: int, summary: str) -> str:
"""审核结论的 commit message。"""
s = f" [{score}/10]" if score > 0 else ""
if _zh():
if passed:
return f"审查通过{s}: {summary}" if summary else f"审查通过{s}"
return f"审查未通过{s}: {summary}" if summary else f"审查未通过{s}"
if passed:
return f"Review passed{s}: {summary}" if summary else f"Review passed{s}"
return f"Review failed{s}: {summary}" if summary else f"Review failed{s}"
def git_run(repo_path: str | Path, args: list[str], check: bool = True) -> str:
"""在指定仓库中运行 git 命令,返回 stdout。失败时抛出 RuntimeError。"""
result = subprocess.run(
["git"] + args,
cwd=str(repo_path),
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
if check and result.returncode != 0:
raise RuntimeError(
f"git {' '.join(args)} 失败 | failed:\n{result.stderr.strip()}"
)
return result.stdout.strip()
def create_task_branch(repo_path: str | Path, task_id: str) -> str:
"""创建任务分支 niuma/<task-id>,始终从 main 分叉。自动清理旧任务分支。"""
git_run(repo_path, ["checkout", "main"])
# 删除所有旧 niuma 分支(一个任务 = 一个分支)
for old in list_task_branches(repo_path):
git_run(repo_path, ["branch", "-D", old], check=False)
branch = f"{BRANCH_PREFIX}/{task_id}"
git_run(repo_path, ["checkout", "-b", branch])
return branch
def get_current_branch(repo_path: str | Path) -> str:
return git_run(repo_path, ["branch", "--show-current"])
def branch_exists(repo_path: str | Path, branch: str) -> bool:
try:
git_run(repo_path, ["rev-parse", "--verify", branch])
return True
except RuntimeError:
return False
def switch_branch(repo_path: str | Path, branch: str) -> None:
git_run(repo_path, ["checkout", branch])
def commit_file(
repo_path: str | Path,
filepath: str,
content: str,
author: tuple[str, str],
message: str,
) -> None:
"""写文件 + git add + git commit --author。filepath 相对于 repo_path。"""
full_path = Path(repo_path) / filepath
if full_path.exists() and full_path.read_text(encoding="utf-8") == content:
return
full_path.parent.mkdir(parents=True, exist_ok=True)
full_path.write_text(content, encoding="utf-8")
git_run(repo_path, ["add", filepath])
git_run(repo_path, [
"commit",
"-m", message,
f"--author={author[0]} <{author[1]}>",
])
def commit_existing(
repo_path: str | Path,
filepath: str,
author: tuple[str, str],
message: str,
) -> None:
"""对已存在的文件执行 git add + git commit。"""
git_run(repo_path, ["add", filepath])
git_run(repo_path, [
"commit",
"-m", message,
f"--author={author[0]} <{author[1]}>",
])
def read_file_from_branch(repo_path: str | Path, branch: str, filepath: str) -> str:
"""从指定分支读取文件内容。"""
return git_run(repo_path, ["show", f"{branch}:{filepath}"])
def merge_to_main(repo_path: str | Path, branch: str) -> bool:
"""将分支合并到 main。返回 True 表示成功。不自动 push。"""
try:
current = get_current_branch(repo_path)
git_run(repo_path, ["checkout", "main"])
git_run(repo_path, ["merge", "--no-ff", branch, "-m", f"Merge {branch}: task completed"])
git_run(repo_path, ["branch", "-d", branch])
return True
except RuntimeError:
# 合并冲突时恢复原分支
try:
git_run(repo_path, ["merge", "--abort"], check=False)
git_run(repo_path, ["checkout", current], check=False)
except Exception:
pass
return False
def list_task_branches(repo_path: str | Path) -> list[str]:
"""列出所有 niuma/ 开头的分支。"""
output = git_run(repo_path, ["branch", "--list", f"{BRANCH_PREFIX}/*"], check=False)
if not output:
return []
return [b.strip().lstrip("* ") for b in output.splitlines() if b.strip()]
def get_niuma_log(repo_path: str | Path, max_count: int = 20) -> list[str]:
"""返回 niuma 任务分支的 git log 摘要。"""
output = git_run(repo_path, [
"log", "--oneline", "--all", "--graph",
f"--max-count={max_count}",
], check=False)
if not output:
return []
return output.splitlines()
# ═══════════════════════════════════════════════════════════════
# Git 凭据检测 — 帮助用户配置 HTTPS/SSH 认证
# ═══════════════════════════════════════════════════════════════
def detect_git_protocol(url: str) -> str:
"""检测 git URL 协议类型。返回 'https' | 'ssh' | 'unknown'。"""
url = url.strip()
if url.startswith("https://") or url.startswith("http://"):
return "https"
if url.startswith("git@") or url.startswith("ssh://"):
return "ssh"
return "unknown"
def parse_git_host(url: str) -> dict:
"""从 git URL 解析主机和服务信息。返回 {host, service, is_known}。"""
import re
url = url.strip()
host = ""
service = "unknown"
if url.startswith("https://") or url.startswith("http://"):
m = re.search(r'https?://([^/]+)', url)
host = m.group(1) if m else ""
elif url.startswith("git@"):
m = re.search(r'git@([^:]+)', url)
host = m.group(1) if m else ""
if "github.com" in host:
service = "GitHub"
elif "gitlab" in host:
service = "GitLab"
elif "gitee.com" in host:
service = "Gitee"
elif "bitbucket" in host:
service = "Bitbucket"
return {"host": host, "service": service, "is_known": service != "unknown"}
def check_git_credential_helper() -> dict:
"""检测 git credential helper 配置。返回 {configured, helper, scope}。"""
import subprocess as _sp
result = {"configured": False, "helper": "", "scope": ""}
for scope in ["--global", "--local", "--system"]:
try:
r = _sp.run(
["git", "config", scope, "credential.helper"],
capture_output=True, text=True,
)
helper = r.stdout.strip()
if helper:
result["configured"] = True
result["helper"] = helper
result["scope"] = scope
break
except Exception:
pass
return result
def check_ssh_keys() -> dict:
"""检测 SSH 密钥状态。返回 {has_key, keys, agent_running}。"""
from pathlib import Path as _Path
ssh_dir = _Path.home() / ".ssh"
result = {"has_key": False, "keys": [], "agent_running": False}
if ssh_dir.exists():
for f in ssh_dir.iterdir():
name = f.name
# 私钥文件:id_* 但不含 .pub
if name.startswith("id_") and not name.endswith(".pub") and not name.endswith(".ppk"):
result["keys"].append(str(f))
result["has_key"] = True
# 检查 ssh-agent
import os as _os
result["agent_running"] = bool(_os.environ.get("SSH_AUTH_SOCK"))
return result
def get_credential_setup_guide(protocol: str, host_info: dict) -> list[str]:
"""根据协议和主机返回凭据配置的分步指南。返回步骤列表。"""
steps: list[str] = []
service = host_info["service"]
host = host_info["host"]
if protocol == "https":
steps.append(f"检测到 HTTPS 协议 — 需要配置 git 凭据存储")
steps.append("")
cred = check_git_credential_helper()
if cred["configured"]:
steps.append(f"[OK] 凭据助手已配置: {cred['helper']} ({cred['scope']})")
steps.append("")
return steps
steps.append("[!!] git 凭据存储未配置 — 强弱模型将无法自动提交和推送")
steps.append("")
if service == "GitHub":
steps.append("推荐方案(GitHub Personal Access Token):")
steps.append(" 1. 打开 https://github.com/settings/tokens")
steps.append(" 2. 点击 'Generate new token (classic)'")
steps.append(" 3. 勾选 'repo' 权限(完整仓库访问)")
steps.append(" 4. 生成后复制 token(只显示一次!)")
steps.append(" 5. 回到这里:用 token 当密码,用户名填你的 GitHub 用户名")
steps.append("")
elif service == "GitLab":
steps.append("推荐方案(GitLab Personal Access Token):")
steps.append(f" 1. 打开 https://{host}/-/user_settings/personal_access_tokens")
steps.append(" 2. 勾选 'read_repository' + 'write_repository'")
steps.append(" 3. 生成后复制 token")
steps.append("")
elif service == "Gitee":
steps.append("推荐方案(Gitee 私人令牌):")
steps.append(f" 1. 打开 https://gitee.com/profile/personal_access_tokens")
steps.append(" 2. 勾选 'projects' 权限")
steps.append(" 3. 生成后复制 token")
steps.append("")
steps.append("配置 git 凭据存储:")
steps.append("")
steps.append(" 方式 1(推荐 — 长期存储,明文到磁盘):")
steps.append(" git config --global credential.helper store")
steps.append(" # 下次 git clone/push 时输入用户名和 token,系统会记住")
steps.append("")
steps.append(" 方式 2(内存缓存 1 小时 — 更安全,每次重启需重新输入):")
steps.append(" git config --global credential.helper 'cache --timeout=3600'")
steps.append("")
steps.append(" 方式 3(仅本次会话,最强的安全但最不方便):")
steps.append(" # 每次 git clone/push 都需要手动输入用户名和 token")
steps.append("")
elif protocol == "ssh":
steps.append(f"检测到 SSH 协议 — 需要配置 SSH 密钥")
steps.append("")
ssh = check_ssh_keys()
if ssh["has_key"]:
steps.append(f"[OK] 找到 SSH 密钥: {', '.join(ssh['keys'])}")
if not ssh["agent_running"]:
steps.append("[!!] ssh-agent 未运行,建议启动:")
steps.append(" eval \"$(ssh-agent -s)\" && ssh-add ~/.ssh/id_rsa")
steps.append("")
if service in ("GitHub", "GitLab", "Gitee"):
steps.append(f"确保 SSH 公钥已添加到 {service}:")
if service == "GitHub":
steps.append(" https://github.com/settings/keys")
elif service == "GitLab":
steps.append(f" https://{host}/-/user_settings/ssh_keys")
elif service == "Gitee":
steps.append(" https://gitee.com/profile/sshkeys")
steps.append(f" 公钥内容: cat ~/.ssh/id_rsa.pub (或 id_ed25519.pub)")
steps.append("")
if not ssh["has_key"]:
steps.append("[!!] 未找到 SSH 密钥,需要生成:")
steps.append(" ssh-keygen -t ed25519 -C \"niuma@worker\"")
steps.append(" eval \"$(ssh-agent -s)\" && ssh-add ~/.ssh/id_ed25519")
steps.append(f" 然后把公钥添加到 {service}(见上方链接)")
steps.append("")
steps.append("配置完成后,牛马会引导你输入凭据并保存。")
return steps
def seed_git_credentials(host: str, username: str, token: str) -> tuple[bool, str]:
"""用 git credential approve 将凭据写入已配置的 credential helper。
支持所有 helper(store / cache / osxkeychain / manager)。
返回 (成功, 详情)。"""
import subprocess as _sp
input_data = (
f"protocol=https\n"
f"host={host}\n"
f"username={username}\n"
f"password={token}\n\n"
)
try:
import os as _os
env = {**_os.environ, "GIT_TERMINAL_PROMPT": "0"}
r = _sp.run(
["git", "credential", "approve"],
input=input_data,
capture_output=True,
text=True,
timeout=10,
env=env,
)
if r.returncode == 0:
# 额外:直接写入 ~/.git-credentials 作为保底
cred_file = Path.home() / ".git-credentials"
cred_line = f"https://{username}:{token}@{host}\n"
try:
existing = cred_file.read_text(encoding="utf-8") if cred_file.exists() else ""
if cred_line.strip() not in existing:
cred_file.write_text(existing + cred_line, encoding="utf-8")
except Exception:
pass
return True, "凭据已保存到 git 凭据管理器 | Credential saved"
else:
# git credential approve 失败,直接写文件
cred_file = Path.home() / ".git-credentials"
cred_line = f"https://{username}:{token}@{host}\n"
try:
existing = cred_file.read_text(encoding="utf-8") if cred_file.exists() else ""
if cred_line.strip() not in existing:
cred_file.write_text(existing + cred_line, encoding="utf-8")
return True, "凭据已写入 ~/.git-credentials"
except Exception as e2:
return False, f"凭据保存失败 | Failed: {str(e2)[:200]}"
except _sp.TimeoutExpired:
return False, "凭据保存超时 — credential helper 可能未正确配置"
except FileNotFoundError:
return False, "git 命令不可用 | git not available"
except Exception as e:
return False, f"凭据保存异常 | Error: {str(e)[:200]}"
def verify_git_access(url: str, proxy: str = "") -> tuple[bool, str]:
"""通过 git ls-remote 验证能否访问远程仓库。返回 (成功, 详情)。"""
import subprocess as _sp
import os as _os
cmd = ["git", "ls-remote", "--heads", url]
if proxy:
cmd = ["git", "-c", f"http.proxy={proxy}", "-c", f"https.proxy={proxy}", "ls-remote", "--heads", url]
env = {**_os.environ, "GIT_TERMINAL_PROMPT": "0"}
try:
r = _sp.run(cmd, capture_output=True, text=True, timeout=30, env=env)
if r.returncode == 0:
return True, r.stderr.strip() or "OK"
else:
err = r.stderr.strip()
if "Could not resolve host" in err:
return False, f"无法解析主机 | Cannot resolve host — 检查网络或代理设置"
if "Authentication failed" in err or "403" in err or "fatal: could not read" in err:
return False, f"认证失败 | Authentication failed — 检查凭据配置"
if "Permission denied" in err:
return False, f"权限不足 | Permission denied — 检查 SSH 密钥或 token 权限"
return False, err[:200]
except _sp.TimeoutExpired:
return False, "连接超时 | Connection timed out — 检查网络或代理设置"
except Exception as e:
return False, str(e)[:200]
def push_branch(repo_path: str | Path, branch: str, proxy: str = "") -> tuple[bool, str]:
"""Push 指定分支到 origin。返回 (成功与否, 输出信息)。"""
if proxy:
cmd = ["-c", f"http.proxy={proxy}", "-c", f"https.proxy={proxy}", "push", "origin", branch]
else:
cmd = ["push", "origin", branch]
result = subprocess.run(
["git"] + cmd,
cwd=str(repo_path),
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
output = result.stderr.strip() or result.stdout.strip()
return result.returncode == 0, output
def get_all_branches_log(repo_path: str | Path) -> str:
"""返回当前仓库的完整 git log(含所有 niuma 分支)。"""
output = git_run(repo_path, [
"log", "--oneline", "--graph", "--all", "--max-count=50",
], check=False)
return output if output else ""
def get_branch_log(repo_path: str | Path, branch: str = "") -> str:
"""返回指定分支的完整 git log(含时间)。"""
target = [branch] if branch else []
output = git_run(repo_path, [
"log", "--format=%h %ad %s",
"--date=format:%m-%d %H:%M",
] + target, check=False)
return output if output else ""
def delete_task_branch(repo_path: str | Path, branch: str) -> bool:
"""删除一个 niuma 任务分支。返回是否成功。"""
try:
git_run(repo_path, ["branch", "-D", branch])
return True
except RuntimeError:
return False