繁体   English   中英

使用Python提取最后修改日期,git存储库中文件的作者

[英]Extracting last modified date, author of files in git repository using python

好的,我一直在努力从远程git存储库中提取数据,并使用Python脚本根据文件的最后修改日期生成一个csv报告,列出文件。 我已经能够使用子流程获取最新的代码,并且能够生成报告。 这两个函数的代码片段如下:

> import subprocess 
> process = subprocess.Popen("git pull",stdout=subprocess.PIPE)
> output = process.communicate()[0]

用于csv生成

> with open('excelout1.csv', 'w') as csv_file:
>     wr = csv.writer(csv_file, delimiter=',')
>     for row in myfilelist:
>         wr.writerow(row)

所以现在,我正在获取所有文件的最后修改日期,但事实是,生成的日期显然是本地存储库中文件的更新日期,即,当我进行最新的提取时。 我想要的是REMOTE存储库中每个文件的最后修改日期和作者。

使用Git bash生成最后修改日期的命令是git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort 我想知道如何在python脚本中使用此命令。 我是python的新手,可以提供任何帮助。

编辑 :Mufeed的建议后正在使用当前代码

import os, csv, glob, time
import pandas as pd
import subprocess

process = subprocess.Popen("git pull", stdout=subprocess.PIPE)
output = process.communicate()[0]
p = subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort'],cwd = "C:\Users\sherin.sunny\git\ng-ui",shell=True) 
print(p)

print ('-'*60)  # just vanity
date_file_list = []
for dirpath, dirs, files in os.walk(".\src\\"):
    # select the type of file, for instance *.jpg or all files *.*
    for file in glob.glob(dirpath + '/*.component.ts'):

        stats = os.stat(file)

        lastmod_date = time.localtime(stats[8])

        date_file_tuple = lastmod_date, file
        date_file_list.append(date_file_tuple)

#print date_file_list  # test
date_file_list.sort()
date_file_list.reverse()  # newest mod date now first
print ("%-40s %s" % ("filename:", "last modified:"))
myfilelist = []
for file in date_file_list:
    # extract just the filename
    folder, file_name = os.path.split(file[1])
    # convert date tuple to MM/DD/YYYY HH:MM:SS format
    file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0])
    myfilelist.append([file_name, file_date])
with open('excelout1.csv', 'w') as csv_file:
    wr = csv.writer(csv_file, delimiter=',')
    for row in myfilelist:
        wr.writerow(row)

我不知道我是否正确理解了您的问题。 检查下面的代码段。 相同的子流程模块将输出作为问题描述。

import subprocess
p = subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git 
log -1 --format="%ai {}" {} | sort'],cwd = "path\to\directory",shell=True) 
#cwd = change working directory   
print(p)

输出

b'2018-06-23 09:42:40 -0700 CONTRIBUTING.md\n
2018-06-23 09:42:40 -0700 data_reader.py\n
2018-06-23 09:42:40 -0700 LICENSE\n
2018-06-23 09:43:37 -0700 README.md\n'

subprocess.check_output用于将输出存储到变量,以便您可以从中提取所需的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM