简体   繁体   English

使用python进行Git发布

[英]Git post commit with python

Is it possible with a post commit hook to feed all the filename with path of each added or modified files to a python script? 是否可以使用post commit挂钩将所有文件名以及每个添加或修改的文件的路径提供给python脚本?

I found some usefull library but they require to search in the repository. 我找到了一些有用的库,但是它们需要在存储库中进行搜索。 like this one : https://pythonhosted.org/GitPython/0.3.1/tutorial.html I want git to automatically communicate those filenames to my script. 像这样一个: https : //pythonhosted.org/GitPython/0.3.1/tutorial.html我希望git自动将那些文件名传达给我的脚本。

Thanks. 谢谢。

EDIT 编辑

Apparently I canget the last commit hash with : git rev-parse --verify HEAD 显然我可以使用以下命令获取最后的提交哈希:git rev-parse --verify HEAD

Then I can get the file list of this commit with git show --pretty="format:" --name-only 然后我可以使用git show --pretty =“ format:” --name-only获得此提交的文件列表。

however I only want the modified and added files, not the deleted ones. 但是我只想要修改和添加的文件,而不要删除的文件。

EDIT2 编辑2

Well I did not understood correctly what were git hooks. 好吧,我没有正确理解什么是git hooks。 Apparently I can put a python script in the hook folders and it will be executed. 显然,我可以将python脚本放在hook文件夹中,它将被执行。

All I have to do now is find a way to speak to git in bash from my python script to get my file list. 我现在要做的就是从python脚本中找到一种与bash中的git对话的方法,以获取文件列表。

Forget this post. 忘记这个帖子。

EDIT3 编辑3

Here are the commands I'm looking for: getting the last commit hash : 这是我要查找的命令:获取最后的提交哈希值:

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

output example : 输出示例:

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

getting the file list of this commit with information about their modification: 获取此提交的文件列表以及有关其修改的信息:

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

output example : 输出示例:

$ 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

So to do this kind of thing you have to make a python script and place it in the hook folder of the git repository, name your script post-commit. 因此,要执行此操作,您必须制作一个python脚本并将其放置在git存储库的hook文件夹中,并在提交后命名脚本。

Well that is the theory but I did not try yet, I just wrote this script. 那是理论,但是我还没有尝试,我只是写了这个脚本。

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM