-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgit-https-to-ssh
More file actions
executable file
·59 lines (47 loc) · 1.38 KB
/
Copy pathgit-https-to-ssh
File metadata and controls
executable file
·59 lines (47 loc) · 1.38 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
#!/bin/bash
#
# Converts all git remotes from HTTPS to SSH in repositories under a directory.
# Usage:
# git-https-to-ssh [--dry-run|-n] [directory]
#
# Default directory: ~/dev
set -eu
dry_run=false
dir=""
for arg in "$@"; do
case "$arg" in
--dry-run|-n)
dry_run=true
;;
*)
dir="$arg"
;;
esac
done
dir="${dir:-$HOME/dev}"
if [[ ! -d "$dir" ]]; then
echo "Error: Directory '$dir' does not exist" >&2
exit 1
fi
if ! command -v gh >/dev/null 2>&1; then
echo "Error: gh CLI not found. Install from https://cli.github.com/" >&2
exit 1
fi
echo "Scanning for git repositories in $dir..."
$dry_run && echo "(Dry run - no changes will be made)"
find "$dir" -name .git -type d 2>/dev/null | while read -r gitdir; do
repo="${gitdir%/.git}"
git -C "$repo" remote -v 2>/dev/null | grep ' (fetch)$' | while read -r name url _; do
if [[ "$url" =~ ^https://github\.com/ ]]; then
new_url=$(gh repo view "$url" --json sshUrl -q '.sshUrl' 2>/dev/null) || continue
[[ -z "$new_url" ]] && continue
echo "Converting: $repo"
echo " $name: $url -> $new_url"
if ! $dry_run; then
git -C "$repo" remote set-url "$name" "$new_url"
fi
fi
done
done
echo "Done."
$dry_run && echo "(Dry run: no actual changes performed)"