Skip to content

Commit 9dfe2fb

Browse files
committed
Use a single function for listing submodules
1 parent 09c1366 commit 9dfe2fb

File tree

6 files changed

+33
-27
lines changed

6 files changed

+33
-27
lines changed

.cppsm/testing/hello_and_then_some_test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ cppsm add equipment https://github.com/per-framework/dumpster.cpp.git v1
1010
cppsm add requires https://github.com/per-framework/dumpster.cpp.git v1
1111
cppsm add equipment https://github.com/per-framework/dumpster.cpp.git v1
1212

13+
cppsm upgrade
14+
1315
cppsm list
1416

1517
cppsm remove requires/polyfill.cpp/v1

commands/.settings

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,25 @@ if [ -z "$NUMBER_OF_PROCESSORS" ]; then
2323
fi
2424

2525
N_PARALLEL_UPDATE="${N_PARALLEL_UPDATE:-$NUMBER_OF_PROCESSORS}"
26+
27+
add-submodule-keys-and-paths-to() {
28+
local VAR="$1"
29+
local GITMODULES="$2"
30+
local KIND="$3"
31+
shift 3
32+
local IFS=$'\n'
33+
for LINE in $(git config \
34+
--file "$GITMODULES" \
35+
--get-regexp "^submodule\.($KIND)/.*\.path$" \
36+
| sed "$@"); do
37+
eval "$VAR+=('$LINE')"
38+
done
39+
}
40+
41+
add-submodule-paths-to() {
42+
add-submodule-keys-and-paths-to "$@" -e 's#^.*\.path ##g'
43+
}
44+
45+
add-submodule-keys-to() {
46+
add-submodule-keys-and-paths-to "$@" -e 's#^submodule\.##g' -e 's#\.path .*$##g'
47+
}

commands/add

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ else
3939
EXISTING="$DESIRED"
4040
fi
4141

42-
for SUBMODULE in $(git config \
43-
--file "$EXISTING/.gitmodules" \
44-
--name-only \
45-
--get-regexp '^submodule\.requires/.*\.path$'); do
46-
SUBMODULE_URL=$(git config --file "$EXISTING/.gitmodules" --get "${SUBMODULE%.path}.url")
47-
SUBMODULE_BRANCH=$(git config --file "$EXISTING/.gitmodules" --get "${SUBMODULE%.path}.branch")
42+
KEYS=()
43+
add-submodule-keys-to KEYS "$EXISTING/.gitmodules" requires
44+
45+
for KEY in "${KEYS[@]}"; do
46+
SUBMODULE_URL=$(git config --file "$EXISTING/.gitmodules" --get "submodule.$KEY.url")
47+
SUBMODULE_BRANCH=$(git config --file "$EXISTING/.gitmodules" --get "submodule.$KEY.branch")
4848
"$CPPSM/commands/add" "$KIND" "$SUBMODULE_URL" "$SUBMODULE_BRANCH"
4949
done

commands/list

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ tree() {
2424
local PARENT="$5"
2525

2626
local KEYS=()
27-
IFS=$'\n'
28-
for KEY in $(git config \
29-
--file "$GITMODULES" \
30-
--name-only \
31-
--get-regexp "^submodule\.($KIND)/.*\.path$"); do
32-
KEYS+=("$KEY")
33-
done
27+
add-submodule-keys-to KEYS "$GITMODULES" "$KIND"
3428

3529
if [ 0 = ${#KEYS[@]} ]; then
3630
CONNECT=''
@@ -58,8 +52,6 @@ tree() {
5852

5953
for ((i = 0; i < ${#KEYS[@]}; ++i)); do
6054
SUB=${KEYS[$i]}
61-
SUB="${SUB%.path}"
62-
SUB="${SUB#submodule.}"
6355

6456
KEY="${SUB#*/}"
6557

commands/update

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ fi
1717
git pull "${GIT_QUIET[@]}" --rebase
1818

1919
MODULES=(.cppsm)
20-
while IFS='' read -r line; do
21-
MODULES+=("$line")
22-
done < <(git config \
23-
--file .gitmodules \
24-
--get-regexp "^submodule\.(equipment|requires)/.*\.path$" \
25-
| sed -e 's#^.*\.path ##g')
20+
add-submodule-paths-to MODULES .gitmodules 'equipment|requires'
2621

2722
git submodule "${GIT_QUIET[@]}" update --jobs "$N_PARALLEL_UPDATE" --init -- "${MODULES[@]}"

commands/upgrade

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ EOF
1515
fi
1616

1717
MODULES=(.cppsm)
18-
while IFS='' read -r line; do
19-
MODULES+=("$line")
20-
done < <(git config \
21-
--file .gitmodules \
22-
--get-regexp "^submodule\.(equipment|requires)/.*\.path$" \
23-
| sed -e 's#^.*\.path ##g')
18+
add-submodule-paths-to MODULES .gitmodules 'equipment|requires'
2419

2520
git submodule "${GIT_QUIET[@]}" update --jobs "$N_PARALLEL_UPDATE" --remote -- "${MODULES[@]}"
2621
git add "${MODULES[@]}"

0 commit comments

Comments
 (0)