0

I am trying to copy .git folder of private repo of any user when access token is provided using python. However, I could not be able to download it. I found bash script to download the file but I wanted to retrieve only .git folders. This is what I was trying

python script

TOKEN = "tokensoicanaccessprivaterepo"
OWNER=""
REPO=""

API_URL=f"https://api.github.com/repos/{OWNER}/{REPO}?access_token={TOKEN}"
req_json = requests.get(API_URL).json()
# git_url = req_json['ssh_url']

# git clone https://${TOKEN}:x-oauth-basic@github.com/{OWNER}/{REPO}.git

os.system(f"git clone https://${TOKEN}@github.com/{OWNER}/{REPO}.git")

the way I am doing asks for password event after providing personal access token with repo and user scope while cloning.

I have looked into various resources but I did not find the way to copy '.git' folder of private repo of any user with the github provider token.

UPDATE

scope

enter image description here

oguz ismail
  • 1
  • 16
  • 47
  • 69
Serenity
  • 3,884
  • 6
  • 44
  • 87
  • 1
    You know that the `.git` folder *is* the whole repository right? It's not just metadata, it contains the whole history of the repository. So what you're trying to do is simply a bare clone (bare means without a working directory in this case). – Joachim Sauer Jun 07 '21 at 07:42
  • Yes that is what I meant to say. I want to download a bare clone of repository using the token. Sorry for not being explicit. – Serenity Jun 07 '21 at 07:53
  • ...so use `git clone --bare`? – 1615903 Jun 07 '21 at 07:57
  • It asks me a password even after passing token while cloning `git clone https://${TOKEN}@github.com/{OWNER}/{REPO}.git`. – Serenity Jun 07 '21 at 08:03
  • `:x-oauth-basic` is in the commented command, but missed in the actual command. In gitlab it's like `git clone https://oauth2:${TOKEN}@gitlab.com/repo`. I think it should follow a similar syntax for github. – ElpieKay Jun 07 '21 at 08:03
  • That I already tried and it said `remote: Repository not found. fatal: Authentication failed for` so I commented it. It seems like I am missing something important – Serenity Jun 07 '21 at 08:18
  • Maybe the token's owner does not have access to the repository. – ElpieKay Jun 07 '21 at 08:30
  • Do you mean the repo scope for that token? If its regarding scope then I have updated my questions with the screenshot of scope provided for that token. – Serenity Jun 07 '21 at 08:38
  • According to [the docs](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token#using-a-token-on-the-command-line) personal access tokens need both the user name *and* the token value (as the password). – Joachim Sauer Jun 07 '21 at 08:48
  • See if https://stackoverflow.com/a/42175489/6330106 helps. – ElpieKay Jun 07 '21 at 09:11
  • @ElpieKay As per the answer, I have already given the full access to repo scope. But it's still not working. I have attached an screenshot of the scope I have given. – Serenity Jun 07 '21 at 09:33
  • @JoachimSauer but this command `git clone https://${TOKEN}:x-oauth-basic@github.com/{OWNER}/{REPO}.git --bare` did not ask for username. How can I now give username? – Serenity Jun 07 '21 at 09:34
  • 1
    @Serenity: there the token is used as a user name and `x-oauth-basic` as the "password". You never explicitly mentioned which kind of token it is, but if it's a personal access token then the documentation I linked above should help. – Joachim Sauer Jun 07 '21 at 09:47
  • LOL there is a simple typo in the command which I could not notice. There should not be $ before {TOKEN} as I am using f string. Thank you everyone for your help and support. – Serenity Jun 07 '21 at 09:50
  • Thank you @ElpieKay for an alternative. This one worked for me after fixing the typo. `clone = f"git clone https://{TOKEN}:x-oauth-basic@github.com/{OWNER}/{REPO}.git --bare"; os.system(clone)` – Serenity Jun 07 '21 at 09:52

1 Answers1

1

In order to make a clone without working directory, use --bare:

git clone --bare https://{TOKEN}:x-oauth-basic@github.com/{OWNER}/{REPO}.git

Also, as pointed out by you in the comments:

there is a simple typo in the command which I could not notice. There should not be $ before {TOKEN} as I am using f string.

And finally, ensure that your access token has the repo scope.

1615903
  • 32,635
  • 12
  • 70
  • 99
  • 1
    Thank you for putting it as an answer. Do you think it's better to put the note regarding 'repo scope'? This will then help to be a complete solution, I think. – Serenity Jun 09 '21 at 00:25