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 pre-receive or post-receive hooks, 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 a git push, the script's main role is triggered on the remote repository: Gets the list of files submitted to the repository by the user, the submitter and the time information" " Importsys,subprocess__author__="Liuzhenwei" classTrigger (object):def __init__(self):" "Initialize file list information, submitter information, commit time, branch of current operation" "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):" "git show command gets the path of the push author, time, and file list file relative to the version Coogan directory relative to the path" "Rev= subprocess. Popen ('git rev-list'+self.newobject,shell=true,stdout=subprocess. PIPE) Revlist=rev.stdout.readlines () revlist= [X.strip () forXinchRevlist]#find out how many commits have been submitted since the last commit self.oldobject, that is, the object list of this push submissionIndexold =Revlist.index (self.oldobject) pushlist=Revlist[:indexold]#loop to get a list of files submitted each time forPobjectinchpushlist:p= subprocess. Popen ('git show'+pobject,shell=true,stdout=subprocess. PIPE) Pipe=p.stdout.readlines () pipe= [X.strip () forXinchPipe] Self.pushauthor= Pipe[1].strip ("Author:"). Strip () Self.pushtime= Pipe[2].strip ("Date:"). Strip () Self.fileList.extend (['/'. Join (Filename.split ("/") [1:]) forFileNameinchPipeifFilename.startswith ("+++") and notFilename.endswith ("NULL")]) defGetgitpushinfo (self):" "returns file list information, submitter information, submission time" "Self .__getgitinfo() self.__getpushinfo() Print "Time :", Self.pushtimePrint "Author:", Self.pushauthorPrint "Ref:", Self.refPrint "Files:", Self.filelistif __name__=="__main__": T=Trigger () t.getgitpushinfo ( )
Transferred from: http://diannaowa.blog.51cto.com/3219919/1671384
[Go] use git hooks to get a list of submitted files in Python language