Skip to content

Commit 872ef34

Browse files
authored
Merge pull request python-geeks#323 from Remakh/main
Fork and Clone a Repository
2 parents eab1dfa + 63c665f commit 872ef34

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

fork_and_clone/Fnc.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import subprocess
2+
import sys
3+
from github import Github
4+
5+
6+
def fork():
7+
8+
print("To quit at any point click ctrl-c")
9+
accOrOrg = input(
10+
"Fork to your profile or an organization"
11+
"(type org for organization, acc for profile): ")
12+
while (accOrOrg != "acc" and accOrOrg != "org"):
13+
accOrOrg = input("invalid answer: acc or org: ")
14+
username = input("Your github username: ")
15+
password = input("Your github password: ")
16+
try:
17+
g = Github(username, password)
18+
user = g.get_user()
19+
repo = input(
20+
"Name of repo you want to fork (in the form owner/repo): ")
21+
if (accOrOrg == "acc"):
22+
user.create_fork(g.get_repo(repo))
23+
else:
24+
org = input("name of your organization: ")
25+
org = g.get_organization(org)
26+
org.create_fork(g.get_repo(repo))
27+
clone(username, repo)
28+
except Exception as e:
29+
print("ERROR: " + str(e))
30+
print("You can try again or click ctrl-c and exit")
31+
fork()
32+
33+
34+
def clone(repo):
35+
36+
location = input(
37+
"Path for cloning repo into"
38+
"(default is current directory): ")
39+
try:
40+
if len(location.strip()) != 0:
41+
arr = repo.split("/")
42+
subprocess.run(
43+
"git clone https://github.com/" + repo + ".git " + location
44+
+ "\\" + arr[0] + "\\" + arr[1], shell=True, check=True)
45+
else:
46+
subprocess.run(
47+
"git clone https://github.com/" + repo + ".git",
48+
shell=True, check=True)
49+
50+
except Exception as e:
51+
print("ERROR: " + str(e))
52+
fnc = input(
53+
"To start from the forking type fork, to start from"
54+
"cloning type clone, to exit press ctrl-c: ").lower()
55+
if fnc == "fork":
56+
fork()
57+
elif fnc == "clone":
58+
clone(repo)
59+
else:
60+
print("Invalid Input: Exiting Now")
61+
sys.exit()
62+
63+
64+
if __name__ == "__main__":
65+
fork()

fork_and_clone/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Fork and Clone a Repository
2+
3+
A python script that forks and clones a repository.
4+
5+
- You can fork into your own account or an organizations account.
6+
- You can choose a path to clone the repository into (default is the repository of the script)
7+
- You need to install the PyGithub wrapper library for the Github API. you need to have Git installed in your system so you can git clone, and you need Python 3.0 and above
8+
- To get PyGithub, simply pip install pygithub
9+
- To install Git please visit https://git-scm.com/downloads
10+
11+
Usage:
12+
13+
- Type into the command line Python (or Python3 if you have multple versions of python) Fnc.py and the script will prompt you for instructions
14+
- Example: Python Fnc.py

fork_and_clone/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
git = 2.20.0
2+
PyGithub = 1.5.3

0 commit comments

Comments
 (0)