There is a requirement for the project to obtain a list of files that are push to the remote repository and to perform a specific analysis of the files. It's natural to think of using git hooks to trigger a script to get a list of files. More anxious to use this feature, use Python with some git commands to write a script out, and then think of a better way to modify the script.
#!/usr/bin/env python#coding=utf-8 "The script is called in a pre-receive or post-receive hook, or it can be used directly as a git hook if the hook is a shell script, You need to add the following code to call the script:while read line;do echo $line | python $PATH/pre-receive.pydone When a user executes git push, it triggers the main role of the script on the remote repository: Get a list of files submitted to the repository by the user, Submitter and time Information "import sys,subprocess__author__ = " Liuzhenwei "Class trigger (object):d ef __ Init__ (self): "' Initialize file list information, submitter information, commit time, current Operation branch ' ' self.pushauthor = '" "self.pushtime = " " self.filelist = []self.ref = "" Def __getgitinfo (self): "" Self.oldobject, Self.newobject,self.ref = sys.stdin.readline (). Strip (). Split (' ') def __getpushinfo (self): " The git show command gets the path of the push author, time, and file list file to a relative path relative to the version Coogan directory ' rev = subprocess. Popen (' git rev-list ' +self.newobject,shell=true,stdout=subprocess. PIPE) Revlist = rev.stdout.readlines () Revlist = [x.strip () for&nbsP;x in revlist] How many commits after #查找从上次提交self. Oldobject, that is, the object list for this push submission Indexold = revlist.index ( Self.oldobject) Pushlist = revlist[:indexold] #循环获取每次提交的文件列表for pobject in pushlist:p = subprocess. Popen (' git show ' +pobject,shell=true,stdout=subprocess. PIPE) Pipe = p.stdout.readlines () Pipe = [x.strip () for x in pipe] Self.pushauthor = pipe[1].strip ("Author:"). Strip () Self.pushtime = pipe[2].strip ("Date:"). Strip () self.fileList.extend ([ '/'. Join (Filename.split ("/") [1:]) for filename in pipe if filename.startswith ("+ + +") and not filename.endswith ("null")]) def Getgitpushinfo (self): "Returns the file list information, submitter information, commit time ' Self.__getgitinfo () self.__getpushinfo () print " Times: ", self.pushtimeprint "Author:",self.pushauthorprint "Ref:",self.refprint "Files:", self.filelistif __name__ == "__main__": T = trigGER () T.getgitpushinfo ()
This article is from the "Diannaowa" blog, make sure to keep this source http://diannaowa.blog.51cto.com/3219919/1671384
Use git hooks to get a list of submitted files in Python language