[英]git init under python script called by git filter-branch uses wrong directory
我運行 python 腳本的以下 MWE 來讀取 throw 提交並在其他地方創建另一個 git 項目。
我以這種方式調用此腳本來遍歷 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
它在/home/mercury/splitted
splitted 下創建一個空文件夾projectB
,其中沒有.git
文件夾。
看起來還有另一個問題是項目A被改變了。 因為當我第二次運行腳本時,出現了錯誤
Proceeding with filter-branch...
You need to run this command from the toplevel of the working tree.
看起來項目A受到了傷害。 我知道的唯一解決方法是從備份中復制.git
的 .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)
output
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)
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
splitted 內創建了一個文件夾,但嘗試在/home/mercury/projectA
.git
當我在正常的 python 環境下運行腳本時,一切都很好。 但是在git filter-branch
下,即使工作目錄更改得很好,路徑也不適用於git
。 除此之外,當git init
應用於另一個目錄時,projectA 似乎已損壞。
我不確定這是git
問題還是python
問題。
出了什么問題以及如何解決此問題?
怎么了...
一般來說,在git filter-branch
中的樹形過濾器中有兩件事您不能做:
這不一定是一個獨家列表,幸運的是,有一些方法可以解決這兩個問題。
以及如何解決這個問題?
更改目錄的限制實際上特定於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 可能仍然不是正確的工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.