从git钩子中的Python子进程运行时,git worktree添加失败
我尝试为子进程设置cwd参数,手动创建目录,验证目录是否存在(该目录存在,但git worktree会在失败时自动将其删除),或使用shell = True。没有更改输出。
在CMD / PowerShell中运行'git worktree add C:/ Users / meh / Desktop / blah HEAD'效果很好。
git目录路径为'C:/ Users / meh / Desktop / blah'
temp_directory_path = "C:/Users/meh/Desktop/blah/abc"
print(temp_directory_path)
print(os.path.exists(temp_directory_path))
print(os.access(temp_directory_path, os.W_OK))
worktree_add_result = subprocess.run(["git", "worktree", "add", temp_directory_path, commit], capture_output=True, text=True, cwd="C:/Users/meh/Desktop/blah")
if worktree_add_result.returncode != 0:
print(worktree_add_result)
print(os.path.exists(temp_directory_path))
print(os.access(temp_directory_path, os.W_OK))
exit(1000)
没有手动创建的“ abc”目录的结果:
C:/Users/meh/Desktop/blah/abc
False
False
CompletedProcess(args=['git', 'worktree', 'add', 'C:/Users/meh/Desktop/blah/abc', '
HEAD'], returncode=128, stdout='Preparing worktree (detached HEAD cbfef18)\n', stderr="fatal: Unable to create 'C:/Users/meh/Desktop/blah/abc/.git/index.lock': No such file or directory\n")
False
False
具有手动创建的“ abc”目录的结果:
C:/Users/meh/Desktop/blah/abc
True
True
CompletedProcess(args=['git', 'worktree', 'add', 'C:/Users/meh/Desktop/blah/abc', '
HEAD'], returncode=128, stdout='Preparing worktree (detached HEAD cbfef18)\n', stderr="fatal: Unable to create 'C:/Users/meh/Desktop/blah/abc/.git/index.lock': No such file or directory\n")
False
False
预期结果:
Preparing worktree (detached HEAD cbfef18)
HEAD is now at cbfef18 commit message
答案 0 :(得分:0)
这不是Python问题,而是Git问题。我正在尝试从名为“ pre-commit”的git钩子运行“ git worktree add”。提交时,首先创建index.lock文件,然后运行我的脚本。 'git worktree add'由于某种原因无法复制此文件。