Edgewall Software

Ticket #897: trac-post-commit-hook.svn.diff

File trac-post-commit-hook.svn.diff, 3.1 KB (added by ivanoe, 7 years ago)

svn diff version of post-commit

  • trac-post-commit-hook

     
    2929#  
    3030# It should be called from the 'post-commit' script in Subversion, such as 
    3131# via: 
     32#  
     33# LINUX: 
    3234# 
    3335# REPOS="$1" 
    3436# REV="$2" 
     
    4244#  -u "$AUTHOR"    \ 
    4345#  -m "$LOG" 
    4446# 
     47# WINDOWS: sample of post-commit.bat (must use temporary files for the log message) 
     48# 
     49##SET REPOS=%1 
     50##SET REV=%2 
     51## 
     52##::----------------------------- 
     53##::Call the TRAC post-commit hook 
     54##:: 
     55##SET TRAC_ENV=C:\somewhere\trac\project 
     56##SET LOG_FILE=%TEMP%.\svnfileR-%REV% 
     57##SET AUT_FILE=%TEMP%.\svnfileA-%REV% 
     58## 
     59##svnlook log -r %REV% %REPOS%>%LOG_FILE% 
     60##svnlook author -r %REV% %REPOS%>%AUT_FILE% 
     61## 
     62##:: SET THE AUTHOR FROM THE FILE. The file is expected to contain only one line with this value 
     63##FOR /F %%A IN (%AUT_FILE%) DO SET AUTHOR=%%A 
     64## 
     65##python [trac-path]\contrib\trac-post-commit-hook.py -p "%TRAC_ENV%" -r "%REV%" -u "%AUTHOR%" -m "file:%LOG_FILE%" 
     66##DEL %LOG_FILE% 
     67##DEL %AUT_FILE% 
     68##:: 
     69##::----------------------------- 
     70## 
     71# 
    4572# It searches commit messages for text in the form of: 
    4673#   command #1 
    4774#   command #1, #2 
     
    114141    def __init__(self, project=options.project, author=options.user, rev=options.rev, msg=options.msg): 
    115142        self.author = author 
    116143        self.rev = rev 
     144        msg = self._readFromFile( msg ) 
    117145        self.msg = "(In [%s]) %s" % (rev, msg) 
    118146        self.now = int(time.time())  
    119147        self.con = sqlite.connect(os.path.join(project, 'db', 'trac.db'), autocommit=0)  
     
    150178            cur.execute("INSERT INTO ticket_change (ticket, time, author, field, oldvalue, newvalue) VALUES (%s, %s, %s, %s, %s, %s)", 
    151179                        int(tkt), self.now, self.author, 'comment', '', self.msg) 
    152180 
     181    def _readFromFile(self, message): 
     182        """ Ivan Melnychuk: SOLUTION FOR WINDOWS SYSTEMS: 
     183        Windows does not support returning values from script. 
     184        Therefore temporary files may be used to keep some information like commit message 
     185        If the 'message' starts with 'file:', and the file with indicated name exists, then read the text message from the file rather then using the text directly 
     186        Even though actual message also may start with such prefix, it is very unlikely that the file exists with such a name by accident 
     187        """ 
     188        if message[:5] == 'file:' and os.path.exists( message[5:] ): 
     189            f = open( message[5:], 'r' ) 
     190            message = f.read() 
     191            f.close() 
     192            return message 
     193        return message; 
     194 
    153195    def verifyDatabaseVersion(self): 
    154196        """Make sure the database uses a known schema version""" 
    155197        cursor = self.con.cursor() 
     
    157199                       'name=%s AND value=%s', 'database_version', '7') 
    158200        if not cursor.fetchone(): 
    159201            raise Exception('Expected Trac database version 7') 
    160                          
     202 
    161203if __name__ == "__main__": 
    162204    if len(sys.argv) < 5: 
    163205        print "For usage: %s --help" % (sys.argv[0]) 
    164206    else: 
    165207        CommitHook() 
    166      
     208