Repository navigation
Expand file tree
/
Copy pathclean-git-config.sh
More file actions
executable file
·154 lines (121 loc) · 3.48 KB
/
Copy pathclean-git-config.sh
File metadata and controls
executable file
·154 lines (121 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [[ -z "${ROOT_DIR}" ]]; then
echo "Error: not inside a Git repository."
exit 1
fi
if [[ "$(pwd)" != "${ROOT_DIR}" ]]; then
echo "Error: run this script from the repository root: ${ROOT_DIR}"
exit 1
fi
CURRENT_BRANCH="$(git branch --show-current)"
if [[ -z "${CURRENT_BRANCH}" ]]; then
echo "Error: unable to determine current branch."
exit 1
fi
KEYS=(
"branch.${CURRENT_BRANCH}.github-pr-owner-number"
"branch.${CURRENT_BRANCH}.github-pr-base-branch"
)
print_effective_values() {
local key="$1"
local values
values="$(git config --get-all "${key}" 2>/dev/null || true)"
if [[ -z "${values}" ]]; then
echo " effective values: none"
return
fi
echo " effective values:"
while IFS= read -r line; do
echo " - ${line}"
done <<< "${values}"
}
print_all_origins() {
local key="$1"
local origins
origins="$(git config --show-origin --show-scope --get-all "${key}" 2>/dev/null || true)"
if [[ -z "${origins}" ]]; then
echo " origins: none"
return
fi
echo " origins:"
while IFS= read -r line; do
echo " - ${line}"
done <<< "${origins}"
}
count_effective_values() {
local key="$1"
local count
count="$(git config --get-all "${key}" 2>/dev/null | wc -l | tr -d ' ')"
echo "${count}"
}
echo "Repository root : ${ROOT_DIR}"
echo "Current branch : ${CURRENT_BRANCH}"
echo
echo "Configuration audit (all sources):"
duplicates_found=0
for key in "${KEYS[@]}"; do
echo
echo "Key: ${key}"
print_all_origins "${key}"
print_effective_values "${key}"
count="$(count_effective_values "${key}")"
if [[ "${count}" -gt 1 ]]; then
duplicates_found=1
echo " status: duplicate values detected (${count})"
else
echo " status: ok (${count})"
fi
done
echo
if [[ "${duplicates_found}" -eq 0 ]]; then
echo "No duplicates detected for the current branch keys. Nothing to clean."
exit 0
fi
echo "The script will perform this cleanup after confirmation:"
echo " 1) keep one canonical value per key"
echo " 2) remove the same key from local/worktree/global scopes"
echo " 3) re-add the canonical value in local scope (.git/config)"
echo
read -r -p "Proceed with cleanup? [y/N] " answer
if [[ ! "${answer}" =~ ^[Yy]$ ]]; then
echo "Cleanup canceled."
exit 0
fi
echo
echo "Applying cleanup..."
for key in "${KEYS[@]}"; do
canonical="$(git config --get-all "${key}" 2>/dev/null | head -n 1 || true)"
if [[ -z "${canonical}" ]]; then
echo "- ${key}: no value found, skipping"
continue
fi
git config --local --unset-all "${key}" 2>/dev/null || true
git config --worktree --unset-all "${key}" 2>/dev/null || true
git config --global --unset-all "${key}" 2>/dev/null || true
git config --local --add "${key}" "${canonical}"
echo "- ${key}: cleaned, canonical value restored in local scope"
done
echo
echo "Post-cleanup verification:"
remaining_duplicates=0
for key in "${KEYS[@]}"; do
echo
echo "Key: ${key}"
print_all_origins "${key}"
print_effective_values "${key}"
count="$(count_effective_values "${key}")"
if [[ "${count}" -gt 1 ]]; then
remaining_duplicates=1
echo " status: still duplicated (${count})"
else
echo " status: ok (${count})"
fi
done
echo
if [[ "${remaining_duplicates}" -eq 1 ]]; then
echo "Warning: duplicates still exist in effective config (possibly system scope or include files)."
echo "Inspect with: git config --show-origin --show-scope --list"
exit 2
fi