使用python进行Git post提交

时间:2014-02-24 17:44:22

标签: python git python-2.7 hook githooks

是否可以使用post commit hook将所有文件名与每个添加或修改过的文件的路径一起提供给python脚本?

我找到了一些有用的库但他们需要在存储库中搜索。 像这样:https://pythonhosted.org/GitPython/0.3.1/tutorial.html 我希望git自动将这些文件名传递给我的脚本。

感谢。

修改

显然我可以使用以下命令获取最后一个提交哈希: git rev-parse - 验证HEAD

然后我可以获取此提交的文件列表 git show --pretty =“format:” - name-only

但是我只想要修改和添加的文件,而不是删除的文件。

EDIT2

嗯,我没有正确理解什么是git hooks。 显然,我可以将一个python脚本放在钩子文件夹中,然后执行它。

现在我要做的就是从我的python脚本中找到一种用bash与git对话的方法来获取我的文件列表。

忘了这篇文章。

EDIT3

以下是我正在寻找的命令: 获取最后一次提交哈希:

git log -n 1 HEAD --pretty=format:"%H"

输出示例:

$ git log -n 1 HEAD --pretty=format:"%H"
42e6783eeda7ff56a02eab07f1ec4ba6e19212b6

获取此提交的文件列表,其中包含有关其修改的信息:

git show --pretty="format:" --name-status <hash>

输出示例:

$ git show --pretty="format:" --name-status 42e6783eeda7ff56a02eab07f1ec4ba6e19212b6
M       admin/flexAdmin/src/main/flex/business/AdminController.as
D       admin/flexAdmin/src/main/flex/linky/business/command/GetContenuFamilleATCommand.as
D       admin/flexAdmin/src/main/flex/linky/business/event/GetContenuFamilleATEvent.as
M       admin/flexAdmin/src/main/flex/model/ModelLocator.as

1 个答案:

答案 0 :(得分:0)

所以要做这种事情你必须制作一个python脚本并将它放在git存储库的hook文件夹中,命名你的脚本后提交。

那就是理论,但我还没试过,我刚写了这个剧本。

#!/usr/bin/env python   1
# --*-- encoding: iso-8859-1 --*--

import subprocess
import sys, os

DELETED_FILE_TOKEN = "D\t"

def execute_cmd(full_cmd, cwd=None):
    """Execute a git command"""

    process = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
    (stdoutdata, stderrdata) = process.communicate(None)
    if 0 != process.wait():
        raise Exception("Could not execute git command")

    return (stdoutdata.strip(), stderrdata.strip())

def get_last_commit_hash():
    """Get the hash of the last commited commit"""
    return execute_cmd("git log -n 1 HEAD --pretty=format:\"%H\"")[0]

def get_commit_file_list(hash):
    """Get the list of files impacted by a commit, each file is preceded by
    x\t where x can be A for added, D for deleted, M for modified"""
    file_list = execute_cmd("git show --pretty=\"format:\" --name-status "+hash)[0]
    return file_list.split("\n");

def remove_unwanted_files(file_list):
    """remove the x\t from the file names and remove the file that have
    been deleted"""
    cleaned_file_list = list()
    for file in file_list:
        if not file[:2] == DELETED_FILE_TOKEN:
            cleaned_file_list.append(file[2:])
    return cleaned_file_list

def get_script_current_path():
    """get the scrypt current path (to lacalise the repository and open files)"""
    pathname = os.path.dirname(sys.argv[0])
    return os.path.abspath(pathname)    

def get_hash():
    """Allow you to launch the script in command line with any hash"""
if len(sys.argv) > 1:
    return sys.argv[1]
else:
    return get_last_commit_hash()

hash = get_hash();
file_list = get_commit_file_list(hash)
file_list = remove_unwanted_files(file_list)

#here file_list contains the modified and added files