我有一個 git repo,其中在構建程序中會生成幾個工件。為了使事情簡單:
- 代碼生成產生例如
.cpp,.h檔案,也許一些其他型別的檔案; - Build 生成 .o 和 .so 檔案 最重要的是,repo 還存盤其他一些
.cpp/.h/.o/.so檔案作為跟蹤檔案。
.gitignore以這樣一種方式配置,可以git clean -dxf有效地產生一個干凈的回購。
但是,在某些情況下,我只想清理構建工件并保留生成的工件。排序做一個git干凈-dxf但只有當檔案型別包含.foo。
我已經研究了 git clean 的 --exclude 選項,但恐怕它不會完成這項作業,因為我只確切地知道我想要包含在 clean 中的檔案型別,但并非相反。
現在,我所擁有的最接近的東西類似于(可能在語法上不正確,但比給出了想法)
find -name "*.so" -or -name "*.os" | xargs -I {} sh -c "git ls-files --error-unmatch {} && rm {}"
我是否忽略了可以完成這項作業的 git 命令?
問候,
文森特
uj5u.com熱心網友回復:
考慮直接使用git clean:
排序做一個git干凈-dxf但只有當檔案型別包含.foo。
這將是git clean -dxf -- "*.foo"。在--這里不需要,它只是一般的好建議把pathspecs落后--的情況下,命令,否則可能將其視為選項。在某些情況下也不需要雙引號,但在其他情況下它們是必需的,并且不會造成傷害。1
(順便說.foo一句,這里是檔案擴展名而不是檔案型別,至少在檔案型別由其擴展名以外的其他東西確定的系統上。MacOS 在這里得到一些負面影響,因為它的 Unix 基礎不相信擴展名 -基于型別,但它的許多應用程式都可以。??)
1確切的細節取決于您的命令列解釋器,但顯然這CMD.EXE至少適用。我不知道 PowerShell。在 sh/bash 中,運算式 like*.foo將匹配檔案——嗯,檔案和子目錄,但目錄在許多方面與這里的檔案相同——在當前目錄中,名稱以 結尾.foo,除非它們被參考或沒有這樣的檔案。在這兩種情況下,文字字串*.foo被傳遞給 Git,允許Git以更適合您的任務的方式擴展它。
uj5u.com熱心網友回復:
我了解您的問題,也許我知道如何幫助您。
我讓你在這里我制作了一個 python3 片段:
import re
import subprocess
def format_output(output, regex=None):
assert type(output)==list, 'the output parameter need to be a list'
if not regex:
output.pop()
return output
data = list()
pattern = re.compile(regex)
for line in output:
match = pattern.search(line)
if match:
if pattern.groups > 0:
set = list()
for i in range(0, pattern.groups):
if match.group(i 1) is not None:
set.append(match.group(i 1))
data.append(set)
else:
data.append(line)
return data
def shell_output(command, regex=None):
output = subprocess.check_output(command)
lines = output.decode().split('\n')
return format_output(lines, regex)
使用命令 find( shell_output("find")) 你會得到一個包含 的串列splitted find's command output,添加regex parameter允許你filter results.
您可以制作一個完美的正則運算式find only the files that do NOT contain what you want以包含在清潔中
找到后,使用以下命令關閉腳本:
trash= shell_output("find", *YOURREGEX)
for stuff in trash
os.system("rm" stuff)
你終于有了你的清潔工;)
在這里你會找到documentation for making your regex:https : //docs.python.org/3/howto/regex.html
PS:find命令work on linux如果您在 Windows 上使用遞回目錄串列的命令。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398261.html
標籤:混帐
