-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_creator.py
124 lines (101 loc) · 4.27 KB
/
repo_creator.py
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
import os
import subprocess
from datetime import datetime
from github import Github
from dotenv import load_dotenv
import argparse
import webbrowser
load_dotenv()
def repo_exists(user, repo_name):
try:
user.get_repo(repo_name)
return True
except:
return False
def git_commands(directory, commit_message, private, use_ssh):
os.chdir(directory)
if os.path.exists('.git'):
print("A .git directory is already present in this directory.")
try:
upstream_url = subprocess.check_output(['git', 'config', '--get', 'remote.origin.url'])
upstream_url = upstream_url.decode('utf-8').strip()
print(f"The .git in this directory points to the following upstream: {upstream_url}")
except subprocess.CalledProcessError:
print("Failed to retrieve upstream information.")
exit()
if not any(os.listdir(directory)) or os.listdir(directory) == ['..git']:
readme_path = os.path.join(directory, 'README.md')
project_name = os.path.basename(directory)
with open(readme_path, 'w') as readme:
readme.write(project_name)
print("Created README.md in empty directory.")
commands = [
'git init',
'git add .',
f'git commit -m "{commit_message}"'
]
for cmd in commands:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
process.communicate()
token = os.getenv('YOUR_GITHUB_PERSONAL_ACCESS_TOKEN')
if not token:
raise ValueError("GitHub token not found in .env file")
github = Github(token)
user = github.get_user()
repo_name = os.path.basename(directory)
repo = user.create_repo(repo_name, private=private)
if use_ssh:
remote_url = f'[email protected]:{user.login}/{repo_name}.git'
else:
remote_url = f'https://github.com/{user.login}/{repo_name}.git'
commands_after_repo_creation = [
f'git remote add origin {remote_url}',
'git branch -M master',
'git push -u origin master'
]
for cmd in commands_after_repo_creation:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
process.communicate()
def create_and_push_repo(use_tk, alias_mode):
try:
directory = None
if alias_mode:
directory = os.getcwd()
elif use_tk:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
directory = filedialog.askdirectory(title="Select Folder")
root.quit()
else:
directory = os.path.normpath(os.path.expanduser(input("Please enter the path to the folder: ")))
if not directory:
return
token = os.getenv('YOUR_GITHUB_PERSONAL_ACCESS_TOKEN')
github = Github(token)
user = github.get_user()
repo_name = os.path.basename(directory)
if repo_exists(user, repo_name):
print(f"Repository '{repo_name}' already exists on GitHub. Exiting...")
exit()
public_response = input("Do you want the repository to be public? [y/N]: ").strip() or "n"
public = public_response.lower() == 'y'
ssh_response = input("Do you want to use SSH (instead of HTTPS) to push to GitHub? [y/N]: ").strip() or "n"
use_ssh = ssh_response.lower() == 'y'
commit_message = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
git_commands(directory, commit_message, private=not public, use_ssh=use_ssh)
print("Repository created and pushed!")
open_in_browser = input("Do you want to open the repository in your browser? [Y/n]: ").strip().lower()
if open_in_browser == 'y':
repo_url = f'https://github.com/{user.login}/{repo_name}'
webbrowser.open(repo_url)
except KeyboardInterrupt:
print("\nOperation interrupted by user. Exiting...")
exit()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="GitHub Repo Upstreamer")
parser.add_argument("--tk", action="store_true", help="Use tkinter for file dialog")
parser.add_argument("--alias", action="store_true", help="Use current directory as the repository folder")
args = parser.parse_args()
create_and_push_repo(args.tk, args.alias)