5

I am new to both Python and Git. I am in the process of writing a Python script which needs to do the same action as done by running the below git command from my workspace on a Linux server. (i.e. /local/mnt/workspace/)

git clone git://git.xyz.com/platform/manifest.git -b jb_2.5

I tried using Fab library however module fabric.api isn't installed so I couldn't proceed. Also,

import git
git.Git().clone("git://git.xyz.com/platform/manifest.git") 

didn't work.

Any other solutions to do this ?

user202729
  • 3,358
  • 3
  • 25
  • 36
user1429246
  • 323
  • 2
  • 4
  • 9
  • 1
    What does "doesn't work" mean? Does it get an error? If so, which line, and what's the traceback? Or does it just return but do nothing, or do the wrong thing? Or…? Meanwhile, what does "couldn't proceed" mean? Do you not know how to install fabric.api, or not want to? Why did you think you wanted it in the first place? – abarnert Feb 26 '13 at 01:44
  • I think, in the git module, the Repo object will provide method to clone an existing repository. So you might want to try - git.Repo().clone(). Ref - https://pythonhosted.org/GitPython/0.3.2/tutorial.html – Sumod Mar 04 '14 at 17:01
  • Does this answer your question? [Use Git commands within Python code](https://stackoverflow.com/questions/11113896/use-git-commands-within-python-code) – user202729 Aug 16 '21 at 14:02

3 Answers3

17

You can define a git function that allows you to make calls to git. Limiting the user to git commands is important for security purposes; otherwise asking for a git url and using other techniques could result in loss of data or other malicious attacks.

import subprocess

def git(*args):
    return subprocess.check_call(['git'] + list(args))

# examples
git("status")
git("clone", "git://git.xyz.com/platform/manifest.git", "-b", "jb_2.5")

Changing it to subprocess.check_output allows you to see the output git prints, instead of determining success (e.g. git("status") raises an exception if you're not in a git repo).


Side note: take a look at PIP which is designed to help install common packages.

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • 1
    You really don't want to try to define a `git` function that tries to simulate the API of the module he's trying to use. – abarnert Feb 26 '13 at 01:38
  • how will it behave when issuing interactive commands (like `git commit`)? – Throoze Mar 05 '15 at 15:05
  • @Throoze, git always provides a way to do something without an interactive command. e.g. `git("commit", "-m", "my commit message")` – Brigand Mar 05 '15 at 20:34
  • @FakeRainBrigand Thanks for your answer, but my question is more like what happens with the control when using subprocess. How does it handle standard input and background/foreground processes? Thanks :) – Throoze Mar 07 '15 at 05:49
  • Try it, I don't know. – Brigand Mar 07 '15 at 19:02
3

You can either shell out (via os.system or subprocess) or use the GitPython package.

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
1

Since you didn't show what the error was beyond "didn't work", it's hard to guess what exactly your problem was.

But I'm guessing the problem is that import git raised an ImportError, because you never installed the git module that you're trying to use.

If so, the exact same readme document that told you how to do git.Git().clone("git://git.xyz.com/platform/manifest.git") will also tell you how to install it.

But most likely, all you need is something like pip install pygit2 or pip install GitPython or something like that. (You may need sudo, and you may need to install pip before you can use it, and so on, but since we know nothing about your platform or your level of knowledge, there's no way to guess exactly what you need.)

abarnert
  • 354,177
  • 51
  • 601
  • 671