Merge pull request '修复 Kubernetes 工作区无法保存 Git 版本' (#18) from codex/fix-workspace-git-ownership into main
Verify control plane / verify (push) Canceled after 0s
Verify control plane / verify (push) Canceled after 0s
Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/18
This commit was merged in pull request #18.
This commit is contained in:
commit
318b7d8086
2 files changed
+30
-4
No files matched your search
+17
-4
@@ -274,11 +274,17 @@ try:
|
||||
result={'base64':base64.b64encode(output.getvalue()).decode(),'files':count}
|
||||
elif name in ('version_save','version_list'):
|
||||
def git(*arguments):
|
||||
return subprocess.run(['git','-c','core.hooksPath=/dev/null',*arguments],cwd=root,capture_output=True,text=True,timeout=25)
|
||||
# Kubernetes owns the mount directory; Git should trust only this workspace.
|
||||
return subprocess.run(
|
||||
['git', '-c', f'safe.directory={root.as_posix()}', '-c', 'core.hooksPath=/dev/null', *arguments],
|
||||
cwd=root, capture_output=True, text=True, timeout=25,
|
||||
)
|
||||
if name=='version_save':
|
||||
message=values.get('message','')
|
||||
if not isinstance(message,str) or not message or len(message)>200: raise ValueError('Invalid version message')
|
||||
git('init');git('config','user.name','Workspace');git('config','user.email','workspace@localhost')
|
||||
for arguments in [('init',), ('config', 'user.name', 'Workspace'), ('config', 'user.email', 'workspace@localhost')]:
|
||||
if git(*arguments).returncode:
|
||||
raise ValueError('Git initialization failed')
|
||||
ignore=root/'.git/info/exclude'
|
||||
if ignore.parent.is_symlink() or ignore.is_symlink(): raise ValueError('Invalid Git metadata path')
|
||||
with ignore.open('a') as f:f.write('\nnode_modules/\n.venv/\n.env\n.env.*\n!.env.example\n')
|
||||
@@ -286,8 +292,15 @@ try:
|
||||
if added.returncode: raise ValueError('Git staging failed')
|
||||
committed=git('commit','-m',message)
|
||||
if committed.returncode and 'nothing to commit' not in committed.stdout: raise ValueError('Git commit failed')
|
||||
log=git('log','-30','--format=%H%x09%ct%x09%s')
|
||||
result={'versions':[dict(zip(['commit','timestamp','message'],line.split('\t',2))) for line in log.stdout.splitlines() if '\t' in line]}
|
||||
result={'versions': []}
|
||||
if (root / '.git').exists() and git('show-ref', '--head', '--quiet').returncode != 1:
|
||||
log=git('log','-30','--format=%H%x09%ct%x09%s')
|
||||
if log.returncode:
|
||||
raise ValueError('Git history unavailable')
|
||||
result['versions'] = [
|
||||
dict(zip(['commit', 'timestamp', 'message'], line.split('\t', 2)))
|
||||
for line in log.stdout.splitlines() if '\t' in line
|
||||
]
|
||||
elif name=='list_files':
|
||||
target=path(values.get('path','.'))
|
||||
result={'files':[{'name':p.name,'directory':p.is_dir(),'symlink':p.is_symlink()} for p in sorted(target.iterdir())][:1000]}
|
||||
|
||||
@@ -41,6 +41,19 @@ class GatewayTests(unittest.TestCase):
|
||||
(root / "large.txt").write_bytes(b"x" * 64001)
|
||||
self.assertIn("error", self.execute_file_tool(root, "read_file", {"path": "large.txt"}))
|
||||
|
||||
def test_git_history_distinguishes_empty_failed_and_saved_versions(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
self.assertEqual([], self.execute_file_tool(root, "version_list", {})["versions"])
|
||||
subprocess.run(["git", "init"], cwd=root, check=True, capture_output=True)
|
||||
self.assertEqual([], self.execute_file_tool(root, "version_list", {})["versions"])
|
||||
(root / "index.html").write_text("<h1>Versioned source</h1>")
|
||||
saved = self.execute_file_tool(root, "version_save", {"message": "Initial version"})
|
||||
self.assertEqual("Initial version", saved["versions"][0]["message"])
|
||||
self.assertEqual(saved, self.execute_file_tool(root, "version_list", {}))
|
||||
(root / ".git/config").write_text("[invalid")
|
||||
self.assertEqual("Git history unavailable", self.execute_file_tool(root, "version_list", {})["error"])
|
||||
|
||||
def test_source_snapshot_checks_start_and_excludes_credentials_and_local_data(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
|
||||
Reference in new issue
Block a user