5
\$\begingroup\$

The code should do the following in order:

  1. It should download/clone the public Github repository locally.
  2. It should remove all the git history (and branches)
  3. Use the Github API to create a new Github repository initialized with the input github repository content that you downloaded locally. The new repository should be named using name supplied. I am able to do steps 1 and 3 but asks for log-in 2 times. I am not able to initialize the new remote repo with local repo. local_repo = repo1 how? And removing git history? where can I find git history in the cloned repo.
import git,os,tempfile,os,fnmatch,sys
from github import Github

username = sys.argv[1]
password = sys.argv[2]
input_repo_url = sys.argv[3]
output_repo_name = sys.argv[4]

tempdir=tempfile.mkdtemp(prefix="",suffix="")
predictable_filename = "myfile"
saved_umask = os.umask(77)

path = os.path.join(tempdir,predictable_filename)
print("Cloning the repository at "+path)

local_repo = git.Repo.clone_from(input_repo_url,path, branch="master")
print("Clone successful!")

g = Github(username,password)
user = g.get_user()
repo1 = user.create_repo(output_repo_name)
print("New repository created at "+username+" account ")
print(repo1)

target_url = "https://github.com/"+username+"/"+output_repo_name+".git"
print(target_url)

print("Pushing cloned repo to target repo")
local_repo.create_remote("new",url=target_url)
local_repo.git.push("new")
print("Success!!")
\$\endgroup\$
2
  • \$\begingroup\$ I'll happily provide feedback about the script itself, but the larger question is - why? Why would you want to delete all of the history behind a repository when forking a new repo, and why shouldn't a new repo be just that -- a fork? \$\endgroup\$
    – Reinderien
    Commented Oct 27, 2020 at 17:00
  • \$\begingroup\$ Thanks for your reply! . Its a part of an assignment which is supposed to help me understand git and python. So I have to clone a public repo locally, remove history and then push it as a new repository to a remote user's account. How can I use the same git object that i used to create a new remote repo and initialize it with local repo? Because of the 2 objects, 2 log ins are being called. \$\endgroup\$
    – Krishna
    Commented Oct 27, 2020 at 17:08

2 Answers 2

1
\$\begingroup\$

Octal permission fields

77

is likely incorrect. What you've written is 77 in decimal, but this would work out to 115 in octal. If you wanted 077 in octal, you would actually need to write 0o077.

Pathlib

Consider replacing os.path with use of pathlib.

Argument parsing

The argv parsing that you've done is fragile - it will fail with an invalid-index exception if the user provides insufficient parameters. Consider using argparse instead.

Also: do not ask for a password as part of a command line, since it will be stored in the user's shell history. Accept it from an environmental variable, a secure wallet, or from stdin via getpass (not input).

String interpolation

"https://github.com/"+username+"/"+output_repo_name+".git"

is more easily written as

f'https://github.com/{username}/{output_repo_name}.git'
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your suggestion, I will change them. And what about pushing a local repo to remote repo? \$\endgroup\$
    – Krishna
    Commented Oct 28, 2020 at 10:17
0
\$\begingroup\$

Comment your code

This is a more general tip, but it is very good practice to put comments in your code to make it more readable and understandable

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.