微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

git filter-branch 调用的python 脚本下的git init 使用了错误的目录

如何解决git filter-branch 调用的python 脚本下的git init 使用了错误的目录

我运行 python 脚本的以下 MWE 来读取抛出提交并在其他地方创建另一个 git projec。

我这样调用这个脚本是为了遍历 git projectA 并在 bash 命令下创建另一个 git projectB

git filter-branch -f --tree-filter "python3 /media/sf_git/register-commits.py /home/mercury/splitted" --prune-empty --tag-name-filter cat -- --all

python3 的参数是在每次提交时运行的脚本以及它之后的路径是应该创建项目 B 的位置。

/media/sf_git/register-commits.py

import os
import sys


def git_init(module):
    os.system('git init ' + module)

def create_project(parent,module):
    os.chdir(parent)
    print('parent:',parent)
    git_init(module)
    if not os.path.exists(os.path.join(parent,module,'.git')):
        sys.exit('.git folder is not created.')


arg1 =  sys.argv[1]
if arg1 is None:
    sys.exit('The script argument is not provided')


commit_id = os.environ["GIT_COMMIT"]

module = 'projectB'
cwd = os.getcwd()

try:
    dst_module_path = os.path.join(arg1,module)
    if not os.path.exists(dst_module_path):
        create_project(arg1,module)
except Exception as e:
    print('Error: ' + str(e))
finally:
    os.chdir(cwd)

问题是 os.chdir 可以改变路径。我什至打印过。没错。但是 git init 命令在项目 A 而不是项目 B 的同一工作目录中运行。它给了我以下错误

WARNING: git-filter-branch has a glut of gotchas generating mangled history
     rewrites.  Hit Ctrl-C before proceeding to abort,then use an
     alternative filtering tool such as 'git filter-repo'
     (https://github.com/neWren/git-filter-repo/) instead.  See the
     filter-branch manual page for more details; to squelch this warning,set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

Rewrite 8a30d5630ab7ead31ecc3b30122054d27eec0dbe (1/3058) (0 seconds passed,remaining 0 predicted)
Reinitialized existing Git repository in /home/mercury/projectA/.git/
.git folder is not created.
parent: /home/mercury/splitted
tree filter Failed: python3 /media/sf_git/register-commits.py /home/mercury/splitted

它在 projectB 下创建一个文件/home/mercury/splitted,其中没有 .git 文件夹。

貌似还有另一个方面的问题,projectA 改了。因为我第二次运行脚本的时候,出现了错误

Proceeding with filter-branch...

You need to run this command from the toplevel of the working tree.

看起来项目A 受到了伤害。我所知道的唯一解决方法是从备份中复制 projectA 的 .git 文件夹。

使用 subprocess.Popen 给了我类似的结果:

def git_init(module):
    parent = os.getcwd()
    print('parent:',parent)
    proc = subprocess.Popen(['git','init',module],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,cwd=parent)
    p_status = proc.wait()
    (output,err) = proc.communicate()
    print(output)

输出

WARNING: git-filter-branch has a glut of gotchas generating mangled history
     rewrites.  Hit Ctrl-C before proceeding to abort,remaining 0 predicted)
parent: /home/mercury/splitted
parent: /home/mercury/splitted
b'Reinitialized existing Git repository in /home/mercury/projectA/.git/\n'
.git folder is not created.
tree filter Failed: python3 /media/sf_git/register-commits.py /home/mercury/splitted

奇怪的是,git 在 /home/mercury/splitted 中创建了一个文件夹,但尝试在 .git 下启动 /home/mercury/projectA

当我在正常的 python 环境下运行脚本时,一切正常。但是在 git filter-branch 下,即使工作目录更改得很好,路径也不适用于 git。除此之外,当 git init 应用于另一个目录时,projectA 似乎已损坏。

我不确定这是 git 问题还是 python 问题。

出了什么问题以及如何解决这个问题?

解决方法

怎么了...

一般情况下,在 git filter-branch 中,您不能在树过滤器中做两件事:

  1. 更改工作目录;
  2. 使用 Git 命令。

这不一定是一个排他性列表,幸运的是,有一些方法可以解决这两个问题。

以及如何解决这个问题?

更改目录的限制实际上特定于 在顶级 shell 中运行的 shell 命令(filter-branch eval 在这里是您的过滤器)。由于您正在启动一个完全独立的进程 python,它允许您更改工作目录。但值得一提的是这个问题,因为尝试优化您的过滤器可能会导致遇到它。

使用 Git 命令的限制是因为树过滤器专门用于让您使用 -Git 命令来重新处理每个提交的内容。使用 git filter-branch 只是为了检查每个提交的内容并不是这里的意图。

幸运的是,像这样运行 git init 有一个简单的解决方法:您只需要在调用 Git 时从环境中删除环境变量 GIT_DIR。如果您调用其他 Git 命令,则可能需要取消设置更多环境变量。

不过,总体而言,尚不清楚您为何要为此使用 git filter-branch。如果您想获取提交列表,正确的工具通常是 git rev-list。如果您想从这些提交中 获取文件,事情会变得更加复杂,但是 filter-branch 可能仍然不是正确的工具。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。