11

I have just started with Python and my first assignment is to write a script to determine if a directory where it is ran is a git repository. A co-student suggested this code:

#! /usr/bin/env python

from subprocess import Popen, PIPE, STDOUT
if Popen(("git", "branch"), stderr=STDOUT, stdout=PIPE).returncode != 0:
    print("Nope!")
else:
    print("Yup!")

It should print an output depending on what the returncode of console command "git branch" is. However, the script did not work in the repository.

In any case, I would be grateful for any piece of advice regarding this.

The assignment also includes:

  • being able to use same script on Windows
  • eventual passing of the path to determine to the script without having to copy it to the target directory

Many thanks!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
NickyKatze
  • 133
  • 1
  • 2
  • 7

6 Answers6

23

Install gitpython, e.g pip install gitpython.

Then make a function like this:

import git

...

def is_git_repo(path):
    try:
        _ = git.Repo(path).git_dir
        return True
    except git.exc.InvalidGitRepositoryError:
        return False
Jacob Tomlinson
  • 3,341
  • 2
  • 31
  • 62
Pablo
  • 481
  • 5
  • 4
8

Close! Popen is a more complicated object that starts a process but requires other interaction to get information. In your case, you need to call wait() so that the Popen object waits for the program to complete to get the return code. You also risk the program hanging if the command returns too much information to fit in the pipe. Try 'call' (it calls wait for you) and send the command output to the bit bucket.

#! /usr/bin/env python

from subprocess import call, STDOUT
import os
if call(["git", "branch"], stderr=STDOUT, stdout=open(os.devnull, 'w')) != 0:
    print("Nope!")
else:
    print("Yup!")
tdelaney
  • 73,364
  • 6
  • 83
  • 116
8

While tdelaney's answer is correct, I would like to post a function which is more generalised and can be quickly copy-pasted into someone's script:

There are two requirements for the function:

import os
import subprocess

And the function is quite simple:

def is_git_directory(path = '.'):
    return subprocess.call(['git', '-C', path, 'status'], stderr=subprocess.STDOUT, stdout = open(os.devnull, 'w')) == 0
Pijusn
  • 11,025
  • 7
  • 57
  • 76
4

Wouldn't it be easier to just have python check and see if a folder named .git is present in the current running directory?

Serban Constantin
  • 3,316
  • 1
  • 18
  • 20
  • It would, possibly.I am searching for a way to make this work as desired (should also work on windows). How would you code your suggestion? – NickyKatze Oct 30 '13 at 21:56
  • 7
    That's not quite the same thing. If there's an empty `.git` directory this would say "yes" and git would say "no". If you're in a sub-directory of a repo, this would say "no" and git would say "yes". (There are also potential issues with environment variables.) – torek Oct 30 '13 at 23:03
  • And adding to @torek, if you do `if '.git' in folder` then you also check for folder names like `tim.github` etc. which probably you do not want. – Timo Apr 24 '21 at 10:45
1

The folder in question might also be within a git repo. For that reason I like to extract the root folder as well:

def getGitRoot(p):
    """Return None if p is not in a git repo, or the root of the repo if it is"""
    if call(["git", "branch"], stderr=STDOUT, stdout=open(os.devnull, 'w'), cwd=p) != 0:
        return None
    else:
        root = check_output(["git", "rev-parse", "--show-toplevel"], cwd=p)
        return root
Jon
  • 1,198
  • 7
  • 8
1

You could install GitPython and then you can apply this code

import git

def is_git_repo(path):
    try:
        _ = git.Repo(path).git_dir
        return True
    except git.exc.InvalidGitRepositoryError:
        return False
alexherm
  • 1,362
  • 2
  • 18
  • 31