Search code examples
pythongitpython

GitPython git diff for the last so many days


I've been trying to implement this git syntax git diff HEAD 'HEAD@{7 day ago}' to this diffs = REPO.git.diff('HEAD') GitPython code for some time now without a success.

Couldn't find a helpful document.

How can I look for the diffs of last 7 days with GitPython

Note: Python version: 3.4


Solution

  • I think GitPython doesn't support this command so I've used an other approach.

    git_cmd = "git diff HEAD 'HEAD@{7 day ago}'"
    kwargs = {}
    kwargs['stdout'] = subprocess.PIPE
    kwargs['stderr'] = subprocess.PIPE
    kwargs['cwd'] = '/path/to/repo/'
    proc = subprocess.Popen(shlex.split(git_cmd), **kwargs)
    (stdout_str, stderr_str) = proc.communicate()
    return_code = proc.wait()
    
    decoded_list = stdout_str.decode('utf-8')
    

    With this way I was able to achieve what I'm looking for.

    Credits to: https://stackoverflow.com/a/15315706/5415084