-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh-init
More file actions
executable file
·89 lines (71 loc) · 2.79 KB
/
gh-init
File metadata and controls
executable file
·89 lines (71 loc) · 2.79 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
#!/usr/bin/env python3
import argparse
import os
import subprocess
from pathlib import Path
def run(cmd, check=True):
subprocess.run(cmd, shell=True, check=check)
def github_repo_exists(username, repo_name):
try:
subprocess.run(
f"gh repo view {username}/{repo_name}",
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True
)
return True
except subprocess.CalledProcessError:
return False
def main():
parser = argparse.ArgumentParser(description="Initialize a local and remote GitHub repository.")
parser.add_argument('-p', '--path', help='Local path to initialize (default: current dir)', default=os.getcwd())
parser.add_argument('-n', '--name', help='Repository name (default: current dir name)', default=None)
parser.add_argument('--no-gitignore', action='store_true', help='Skip creating .gitignore')
parser.add_argument('--no-readme', action='store_true', help='Skip creating README.md')
parser.add_argument('-P', '--public', action='store_true', help='Make the GitHub repo public')
parser.add_argument('-d', '--description', help='Short description for the GitHub repo', default="")
args = parser.parse_args()
path = Path(args.path).resolve()
repo_name = args.name or path.name
username = subprocess.check_output("git config --global github.user", shell=True, text=True).strip()
if not username:
print("Error: 'github.user' not set in git config.")
exit(1)
if github_repo_exists(username, repo_name):
print(f"Error: GitHub repository '{username}/{repo_name}' already exists. Choose a different name.")
exit(1)
path.mkdir(parents=True, exist_ok=True)
os.chdir(path)
run("git init")
readme = path / "README.md"
gitignore = path / ".gitignore"
if not args.no_readme:
if not readme.exists():
readme.write_text(f"## {repo_name}\nNew repo\n\n---\n")
run("git add README.md")
if not args.no_gitignore:
if not gitignore.exists():
gitignore.write_text("""## default .gitignore
*.swp
*.swo
*.bak
*.tmp
*.log
.DS_Store
Thumbs.db
.env
node_modules/
dist/
__pycache__/
""")
run("git add .gitignore")
visibility = "--public" if args.public else "--private"
run(f'gh repo create "{repo_name}" {visibility} --description "{args.description}" --source=. --remote=origin')
current_branch = subprocess.check_output("git symbolic-ref --short HEAD", shell=True, text=True).strip()
if readme.exists() or gitignore.exists():
run('git commit -m "gh-init"')
run(f"git push -u origin {current_branch}")
print(f"Initialized GitHub repo '{username}/{repo_name}' in {path}")
if __name__ == "__main__":
main()