-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgith
More file actions
executable file
·164 lines (107 loc) · 4.46 KB
/
gith
File metadata and controls
executable file
·164 lines (107 loc) · 4.46 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
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import requests, json, subprocess, os, re, webbrowser, sys
from os import path
config_path = path.expanduser('~/.config/gith')
user_auth = ('', '')
username = ''
def init():
# load username, email and access token from config file if it
# exists, otherwise we ask the user for email and access token
# and fetch the username.
if path.exists(config_path):
# reads an <e-mail> <personal access token> <username> combo in the following format
# email@email.com abe5d7edafi564da84dasf45648asd5s4d4fa5aa UserName
f = open(config_path)
creds = f.read().split(' ', 2)
f.close()
user_auth = (creds[0], creds[1])
username = creds[2]
return (user_auth, username)
else:
email = input('Insert your e-mail: ')
token = input('Insert a GitHub personal access token: ')
user_auth = (email, token)
response = requests.get('https://api.github.com/user', auth = user_auth)
if response.status_code == 200:
username = response.json()['login']
f = open(config_path, 'x')
# save email, access token, and username
f.write(f'{email} {token} {username}')
f.close()
return (user_auth, username)
def list_repos(user_auth):
response = requests.get('https://api.github.com/user/repos?sort=created', auth = user_auth)
if response.status_code == 200:
json = response.json()
for repo in json:
print(repo['html_url'])
def clone_repo(username, repo_name):
subprocess.check_call(['git', 'clone', f'https://github.com/{username}/{repo_name}'])
def view_curr_repo():
try:
out = subprocess.check_output(['git', 'remote', 'show', 'origin', '-n'], text = True)
match = re.search('Fetch URL: (.+)', out.splitlines()[1])
if match != None:
url = match.group(1)
webbrowser.open_new_tab(url)
else:
print('Could not figure out the URL of the origin remote')
except:
print('Not a git repository.')
def create_repo(user_auth, username, private):
try:
subprocess.check_output(['git', 'status'])
except:
print('Not a git repository. Initialize a repository with \'git init\' first.')
return
# get the name of the current working directory
working_dir_name = os.getcwd().split('/')[-1]
request_body = {
'name': working_dir_name,
'private': private,
'auto_init': False,
}
response = requests.post('https://api.github.com/user/repos',
auth = user_auth,
json = request_body
)
if True or response.status_code == 201:
print('Create successful.')
repo_url = response.json()['html_url']
subprocess.check_output(['git', 'remote', 'add', 'origin', repo_url])
subprocess.check_output(['git', 'push', '--set-upstream', 'origin', 'master'])
else:
print('Failed to create repo.')
print(response.status_code)
print(response.text)
def show_help():
print('GitHub helper commands:')
print('')
print(' list : See a list of all your repositories.')
print('')
print(' view : Open the current repository\'s GitHub page')
print(' Remote \'origin\' must be the GitHub repo.')
print('')
print(' clone <repo name> : Clone one of your repositories by name.')
print(' Example: gith clone my-repository')
print('')
print(' create [--private/-p] : Create a GitHub repository for the current Git repository.')
print(' The name of the current folder will be used as the repository\'s name.')
print(' Example: gith create --private')
if __name__ == '__main__':
(user_auth, username) = init()
if len(sys.argv) >= 2:
if sys.argv[1] == 'list':
list_repos(user_auth)
elif sys.argv[1] == 'clone' and len(sys.argv) >= 3:
clone_repo(username, sys.argv[2])
elif sys.argv[1] == 'view':
view_curr_repo()
elif sys.argv[1] == 'create':
create_repo(user_auth, username, len(sys.argv) >= 3 and (sys.argv[2] == '-p' or sys.argv[2] == '--private'))
elif sys.argv[1] == 'help':
show_help()
else:
show_help()
else:
show_help()