Skip to main content
 首页 » 编程设计

python之Cygwin下的Git P4 : Submit fails with "Patch does not apply"

2024年10月25日32mq0036

我正在尝试在 Cygwin 下使用 git-p4。工作流程的“克隆”和“ rebase ”部分似乎工作正常,但我无法“提交”。我猜它可能与行结束约定有关。我看过 this git-p4 issue及其链接的项目,但行尾和空白配置的操作到目前为止是不成功的。我配置中的注意事项:

(1) 我正在使用 bash shell 函数技巧来使路径正常工作:

$ type p4 
p4 is a function 
p4 ()  
{  
    P4=`which p4`; 
    PWD=$(cygpath -wa .) "${P4}" "$@" 
} 

(2) 我已经尝试了 git 的 autocrlf 值的所有设置——无论(真、假、输入)如何,结果都是失败。目前正在尝试“false”,因为这在与软件仓库进行差异化时最有意义。

(3) 我也玩过 p4 clientspec 的 lineend 值;目前正在尝试“unix”,Cygwin 的 b/c 和在 OSX 下运行的 P4 沙箱服务器。

测试很简单。 repo 协议(protocol)包含一个文件,foo1。 depot 版本看起来像(使用 od -c 输出):

0000000   f   o   o   1  \n  \n 
0000006 

本地 git 提交的版本如下所示:

0000000   f   o   o   1       i   s       t   h   e       o   n   e   ! 
0000020  \n  \n 
0000022 

我在 git-p4 中的 applyCommit() 方法中添加了一些额外的诊断输出。结合使用 --verbose 运行提交:

 $ git p4 submit --verbose 
 Reading pipe: git name-rev HEAD 
 Reading pipe: ['git', 'config', 'git-p4.allowSubmit'] 
 Reading pipe: git rev-parse --symbolic --remotes 
 Reading pipe: git rev-parse p4/master 
 Reading pipe: git cat-file commit ce414288d1b5d52dbad20c1a29f1875cfff7c281 
 Reading pipe: git cat-file commit HEAD~0 
 Reading pipe: git cat-file commit HEAD~1 
 Reading pipe: ['git', 'config', 'git-p4.conflict'] 
 Origin branch is remotes/p4/master 
 Reading pipe: ['git', 'config', '--bool', 'git-p4.useclientspec'] 
 Opening pipe: ['p4', '-G', 'where', '//depot/foo/...'] 
 Perforce checkout for depot path //depot/foo/ located at c:\git\foo\ 
 Synchronizing p4 checkout... 
 ... - file(s) up-to-date. 
 Opening pipe: p4 -G opened ... 
 Reading pipe: ['git', 'rev-list', '--no-merges', 'remotes/p4/master..master'] 
 Reading pipe: ['git', 'config', '--bool', 'git-p4.skipUserNameCheck'] 
 Reading pipe: ['git', 'config', 'git-p4.detectRenames'] 
 Reading pipe: ['git', 'config', 'git-p4.detectCopies'] 
 Reading pipe: ['git', 'config', '--bool', 'git-p4.detectCopiesHarder'] 
 Reading pipe: ['git', 'show', '-s', '--format=format:%h %s', '2303176ae8c575313616ae2c4a35358258742598'] 
 Applying 2303176 updating foo1 
 Opening pipe: p4 -G users 
 Reading pipe: ['git', 'log', '--max-count=1', '--format=%ae', '2303176ae8c575313616ae2c4a35358258742598'] 
 Reading pipe: git diff-tree -r  "2303176ae8c575313616ae2c4a35358258742598^" "2303176ae8c575313616ae2c4a35358258742598" 
 //depot/foo/foo1#1 - opened for edit 
 Sanity: git diff-tree --full-index -p "2303176ae8c575313616ae2c4a35358258742598" | git apply --verbose --check - 
 Checking patch foo1... 
 error: while searching for: 
 foo1 
 
 
 error: patch failed: foo1:1 
 error: foo1: patch does not apply 
 Unfortunately applying the change failed! 
 Reading pipe: ['git', 'config', '--bool', 'git-p4.attemptRCSCleanup'] 
 //depot/foo/foo1#1 - was edit, reverted 

注意“Sanity:”诊断行。这是在 applyCommit() 方法中失败的 tryPatchCmd 的值。如果我在 bash 命令行中执行语句的第一部分,我会看到:

 2303176ae8c575313616ae2c4a35358258742598 
 diff --git a/foo1 b/foo1 
 index 630baf44b0874b3319c2814399f0b03106912183..4c23e4512b3347ec31068e464b64cbd99851cc9a 100644 
 --- a/foo1 
 +++ b/foo1 
 @@ -1,2 +1,2 @@ 
 -foo1 
 +foo1 is the one! 

将其通过管道传递到命令的第二部分不会导致错误。我很困惑为什么命令在 Python 脚本中使用 os.system() 执行时会失败,但在其他情况下会成功。想法?

请您参考如下方法:

我相信我已经解决了这个问题。问题有两个。第一个大错——没有仔细阅读手册。 documentation in the Submit section状态:

Submitting changes from a Git repository back to the p4 repository requires a separate p4 client workspace. This should be specified using the P4CLIENT environment variable or the Git configuration variable git-p4.client. The p4 client must exist, but the client root will be created and populated if it does not already exist.

我花了一些时间才明白这实际上意味着 两个 物理工作副本 -- 一个用于 git,一个用于 p4。 git-p4 脚本在尝试在同一个文件夹中执行 git 和 p4 操作时感到窒息,并且这种就地操作不能很好地与 Perforce 的 checkout 模型一起使用。我通过在 Mac 上执行相同的测试工作流程发现了这一点,该工作流程 [更清楚] 错误为“无法破坏可写文件”。通过在 p4 clientspec 中调整“clobber”,我能够在 Mac 和 Cygwin 中提交工作,这让我走上了正确的道路。

在 this Perforce page 上有关于设置双工作区结构的详细演练。 .

第二:有趣的是,我似乎仍然需要 --ignore-whitespace 作为 Cygwin 环境中 git apply 逻辑的一部分。这是我愿意接受的 git-p4 hack。