[Go] use git hooks to get a list of submitted files in Python language

Source: Internet
Author: User
Tags using git git commands git hooks

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.