-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-url-swap
executable file
·44 lines (33 loc) · 1.28 KB
/
git-url-swap
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
#!/bin/python3
import subprocess
import sys
cur_url = None
final_url = None
# attempt to get current url
try:
cur_url = subprocess.check_output(["git", "config", "--get", "remote.origin.url"]).decode('utf-8').strip()
except Exception:
print("Please run this script from a valid git directory!")
sys.exit()
# make sure that the url is from github.com, the site this script was designed for
if cur_url.find("github.com") == -1:
print("this script does not work on repositories hosted through non-github services.")
sys.exit()
cur_url = cur_url.replace("://www.", "://")
# determine whether remote is currently https or ssh
if cur_url.find("https") != -1:
final_url = cur_url.replace("https://github.com/", "[email protected]:")
print(cur_url + " -> " + final_url)
elif cur_url.find("http") != -1:
final_url = cur_url.replace("http://github.com/", "[email protected]:")
print(cur_url + " -> " + final_url)
elif cur_url.find("[email protected]:") != -1:
final_url = cur_url.replace("[email protected]:", "https://github.com/")
print(cur_url + " -> " + final_url)
else:
print("could not parse url \"" + cur_url + "\"")
try:
subprocess.call(["git", "remote", "set-url", "origin", final_url])
except Exception:
print("could not set new URL")
# vim: syntax=python