6

I want to download single file from my git repository using python.

Currently I am using gitpython lib. Git clone is working fine with below code but I don't want to download entire directory.

import os
from git import Repo
git_url = 'stack@127.0.1.7:/home2/git/stack.git'
repo_dir = '/root/gitrepo/'
if __name__ == "__main__":
    Repo.clone_from(git_url, repo_dir, branch='master', bare=True)
    print("OK")
Nils Werner
  • 28,291
  • 6
  • 62
  • 82
Ravi Ranjan
  • 331
  • 1
  • 5
  • 10

4 Answers4

3

Don't think of a Git repo as a collection of files, but a collection of snapshots. Git doesn't allow you to select what files you download, but allows you to select how many snapshots you download:

git clone stack@127.0.1.7:/home2/git/stack.git

will download all snapshots for all files, while

git clone --depth 1 stack@127.0.1.7:/home2/git/stack.git

will only download the latest snapshot of all files. You will still download all files, but at least leave out all of their history.

Of these files you can simply select the one you want, and delete the rest:

import os
import git
import shutil
import tempfile

# Create temporary dir
t = tempfile.mkdtemp()
# Clone into temporary dir
git.Repo.clone_from('stack@127.0.1.7:/home2/git/stack.git', t, branch='master', depth=1)
# Copy desired file from temporary dir
shutil.move(os.path.join(t, 'setup.py'), '.')
# Remove temporary dir
shutil.rmtree(t)
Nils Werner
  • 28,291
  • 6
  • 62
  • 82
  • It's a collection of snapshots, not changesets. In one sense this does not matter, but in others it does, and since Git lets the implementation show (shine?) through, it matters when using Git. – torek Jul 09 '18 at 13:36
  • Ok, I have changed the wording – Nils Werner Jul 09 '18 at 13:40
  • is there any git command for download the single file without any script? – Ravi Ranjan Jul 11 '18 at 05:58
  • No. Even the command `git archive --remote` (which isn't available in `gitpython`) requires un-tar-ing the output. – Nils Werner Jul 11 '18 at 07:00
  • It's true that Git won't let you do this, but Github and Bitbucket do. See https://stackoverflow.com/a/4605068/733092 – Noumenon Jan 15 '20 at 05:48
2

You can also use subprocess in python:

import subprocess

args = ['git', 'clone', '--depth=1', 'stack@127.0.1.7:/home2/git/stack.git']
res = subprocess.Popen(args, stdout=subprocess.PIPE)
output, _error = res.communicate()

if not _error:
    print(output)
else:
    print(_error)

But your main problem remains still:

Git does not support downloading parts of the repository. You have to download all of it. But you should be able to do this with GitHub. Reference

Benyamin Jafari
  • 15,536
  • 14
  • 81
  • 116
0

I don't want to flag this as a direct duplicate, since it does not fully reflect the scope of this question, but part of what Lucifer said in his answer seems the way to go, according to this SO post. In short, git does not allow for a partial download, but certain providers (like GitHub) do, via raw content.
That being said, Python does provide quite a number of different libraries to download, with the best-known being urllib.request.

dennlinger
  • 6,287
  • 1
  • 23
  • 43
-2

You need to request the raw version of the file! You can get it from raw.github.com

Lucifer
  • 488
  • 4
  • 12