I wanted to get git commit information in csv format With the --pretty = format option of git log, I couldn't get it in the format I wanted, so I output the log once and then format the log.
OS Mac Catalina Pycharm CE python 3.7
A log file is output when you hit it with a console such as GitBash.
--date-order --date=format:'%Y/%m/%d %H:%M:%S' > git.log```
##### Expected git log to capture
#### **` git.log`**
```log
commit f36da445d06d2db7b4f08a508be835f5464ded
Author: nomori<[email protected]>
Date:   2020/10/10 23:50:29
    first commit.
A	.gitignore
A	perse_git_log.py
 perse_git_log.py
import re
import csv
import os
COMMIT_ID = 'commit '
STATUS_ADD = 'A	'
STATUS_MOD = 'M	'
STATUS_DEL = 'D	'
GIT_AUTHOR = 'Author: '
GIT_DATE = 'Date:   '
path = './input/git.log'
#Read git log file.
array_commit_info = []
with open(path) as git_log_file:
    data = git_log_file.readlines()
for item in data:
    #Removed trailing line feed code.
    item = item.replace('\n', '')
    if COMMIT_ID in item:
        #Get the hash ID of the commit.
        commit_id = item.replace(COMMIT_ID, '')
    elif GIT_AUTHOR in item:
        #Get committed user information.
        author_tmp = item.replace(GIT_AUTHOR, '')
        #Delete the email address part.
        author = re.sub(' +<.*>', '', author_tmp)
    elif GIT_DATE in item:
        #Get commit date and time.
        date = item.replace(GIT_DATE, '')
    else:
        #Get file change history.
        file_status = item[0:2]
        if file_status == STATUS_ADD or file_status == STATUS_MOD or file_status == STATUS_DEL:
            #Get file name excluding Git status.
            file_name = item[2:]
            #Hold information in an array for output.
            array_commit_info.append([commit_id, author, date, file_name])
print(array_commit_info)
#Output in CSV format.
file_path = './output/'
if not os.path.exists(file_path):
    os.mkdir(file_path)
output_filename = file_path + 'git_output.csv'
with open(output_filename, 'w') as f:
    writer = csv.writer(f)
    #Output header information.
    writer.writerow(['COMMIT_ID', 'AUTHOR', 'DATE', 'COMMIT_FILE_NAME'])
    for line_data in array_commit_info:
        #Output commit information.
        writer.writerow(line_data)
 git_output.csv
f36da445d06d2db7b4f08a508be835f5464ded,nomori,2020/10/10 23:50:29,.gitignore
f36da445d06d2db7b4f08a508be835f5464ded,nomori,2020/10/10 23:50:29,perse_git_log.py
--Reading and writing (creating / adding) files with Python https://note.nkmk.me/python-file-io-open-with/ --Python string manipulation master https://qiita.com/tomotaka_ito/items/594ee1396cf982ba9887 --Remove some strings in Python (strip, etc.) https://note.nkmk.me/python-str-remove-strip/ -[Python3] How to use configparser and sample https://www.mathkuro.com/python/configperser/ --Python configuration file management summary https://kodocode.net/python-begin-settings/
Recommended Posts