Git [1] sürümümde, her Git alt modülünde a nameve a bulunur path. Mutlaka aynı olmaları gerekmez [2] . Her ikisini de güvenilir bir şekilde elde etmek, önce alt modülleri kontrol etmeden ( git update --init), zor bir sihirbazlık parçasıdır.
Alt modülün bir listesini alın names
Bunu git configveya başka bir gitkomutu kullanarak bunu başarmanın bir yolunu bulamadım . Bu nedenle regex'e geri döndük .gitmodules(süper çirkin). Ancak git, alt modül için izin verilen olası kod alanını sınırladığı için biraz güvenli görünüyor names. Buna ek olarak, muhtemelen bu listeyi daha fazla kabuk işleme için kullanmak istediğinizden, NULL-bytes ( \0) ile ayrı girişlerin altındaki çözüm .
$ sed -nre \
's/^\[submodule \"(.*)\"]$/\1\x0/p' \
"$(git rev-parse --show-toplevel)/.gitmodules" \
| tr -d '\n' \
| xargs -0 -n1 printf "%b\0"
Ve senaryonuzda:
#!/usr/bin/env bash
while IFS= read -rd '' submodule_name; do
echo submodule name: "${submodule_name}"
done < <(
sed -nre \
's/^\[submodule \"(.*)\"]$/\1\x0/p' \
"$(git rev-parse --show-toplevel)/.gitmodules" \
| tr -d '\n' \
| xargs -0 -n1 printf "%b\0"
)
Not : read -rd ''gerektirir bashve çalışmaz sh.
Alt modülün bir listesini alın paths
Benim yaklaşımda denemek değil çıktı işlemek için git config --get-regexpbirlikte awk, tr, sed, ... ama bunun yerine sıfır bayt geri ayrılmış geçmektedir git config --get. Bu, alt modüldeki yeni satırlar, boşluklar ve diğer özel karakterlerle (örn. Unicode) ilgili sorunları önlemek içindir paths. Buna ek olarak, muhtemelen bu listeyi daha fazla kabuk işleme için kullanmak istediğinizden, NULL-bytes ( \0) ile ayrı girişlerin altındaki çözüm .
$ git config --null --file .gitmodules --name-only --get-regexp '\.path$' \
| xargs -0 -n1 git config --null --file .gitmodules --get
Örneğin, bir Bash betiğinde şunları yapabilirsiniz:
#!/usr/bin/env bash
while IFS= read -rd '' submodule_path; do
echo submodule path: "${submodule_path}"
done < <(
git config --null --file .gitmodules --name-only --get-regexp '\.path$' \
| xargs -0 -n1 git config --null --file .gitmodules --get
)
Not : read -rd ''gerektirir bashve çalışmaz sh.
Dipnotlar
[1] Git sürümü
$ git --version
git version 2.22.0
[2] Ayırmalı namevepath
Test havuzunu ayarlayın:
$ git init test-name-path
$ cd test-name-path/
$ git checkout -b master
$ git commit --allow-empty -m 'test'
$ git submodule add ./ submodule-name
Cloning into '/tmp/test-name-path/submodule-name'...
done.
$ ls
submodule-name
$ cat .gitmodules
[submodule "submodule-name"]
path = submodule-name
url = ./
Yapmak nameve ayrıştırmak için alt modülü taşıyın path:
$ git mv submodule-name/ submodule-path
$ ls
submodule-path
$ cat .gitmodules
[submodule "submodule-name"]
path = submodule-path
url = ./
$ git config --file .gitmodules --get-regexp '\.path$'
submodule.submodule-name.path submodule-path
Test yapmak
Test havuzunu ayarlayın:
$ git init test
$ cd test/
$ git checkout -b master
$ git commit --allow-empty -m 'test'
$
$ git submodule add ./ simplename
Cloning into '/tmp/test/simplename'...
done.
$
$ git submodule add ./ 'name with spaces'
Cloning into '/tmp/test/name with spaces'...
done.
$
$ git submodule add ./ 'future-name-with-newlines'
Cloning into '/tmp/test/future-name-with-newlines'...
done.
$ git mv future-name-with-newlines/ 'name
> with
> newlines'
$
$ git submodule add ./ 'name-with-unicode-💩'
Cloning into '/tmp/test/name-with-unicode-💩'...
done.
$
$ git submodule add ./ sub/folder/submodule
Cloning into '/tmp/test/sub/folder/submodule'...
done.
$
$ git submodule add ./ name.with.dots
Cloning into '/tmp/test/name.with.dots'...
done.
$
$ git submodule add ./ 'name"with"double"quotes'
Cloning into '/tmp/test/name"with"double"quotes'...
done.
$
$ git submodule add ./ "name'with'single'quotes"
Cloning into '/tmp/test/name'with'single'quotes''...
done.
$ git submodule add ./ 'name]with[brackets'
Cloning into '/tmp/test/name]with[brackets'...
done.
$ git submodule add ./ 'name-with-.path'
Cloning into '/tmp/test/name-with-.path'...
done.
.gitmodules:
[submodule "simplename"]
path = simplename
url = ./
[submodule "name with spaces"]
path = name with spaces
url = ./
[submodule "future-name-with-newlines"]
path = name\nwith\nnewlines
url = ./
[submodule "name-with-unicode-💩"]
path = name-with-unicode-💩
url = ./
[submodule "sub/folder/submodule"]
path = sub/folder/submodule
url = ./
[submodule "name.with.dots"]
path = name.with.dots
url = ./
[submodule "name\"with\"double\"quotes"]
path = name\"with\"double\"quotes
url = ./
[submodule "name'with'single'quotes"]
path = name'with'single'quotes
url = ./
[submodule "name]with[brackets"]
path = name]with[brackets
url = ./
[submodule "name-with-.path"]
path = name-with-.path
url = ./
Alt modülün listesini alın names
$ sed -nre \
's/^\[submodule \"(.*)\"]$/\1\x0/p' \
"$(git rev-parse --show-toplevel)/.gitmodules" \
| tr -d '\n' \
| xargs -0 -n1 printf "%b\0" \
| xargs -0 -n1 echo submodule name:
submodule name: simplename
submodule name: name with spaces
submodule name: future-name-with-newlines
submodule name: name-with-unicode-💩
submodule name: sub/folder/submodule
submodule name: name.with.dots
submodule name: name"with"double"quotes
submodule name: name'with'single'quotes
submodule name: name]with[brackets
submodule name: name-with-.path
Alt modülün listesini alın paths
$ git config --null --file .gitmodules --name-only --get-regexp '\.path$' \
| xargs -0 -n1 git config --null --file .gitmodules --get \
| xargs -0 -n1 echo submodule path:
submodule path: simplename
submodule path: name with spaces
submodule path: name
with
newlines
submodule path: name-with-unicode-💩
submodule path: sub/folder/submodule
submodule path: name.with.dots
submodule path: name"with"double"quotes
submodule path: name'with'single'quotes
submodule path: name]with[brackets
submodule path: name-with-.path
git submodulevarsayımsal birgit submodule listdavranış sergilemeyi beklediğim gibi davrandığını gösteriyor - hiçbir argüman olmadan ne olduğunu kontrol etmeyi hiç düşünmedimgit submodule. (Başlangıçta yanlış 'Paylaş' bağlantısını aldığım için bu bağlantıyı test ettiğim için sevindim!)