【问题标题】:Extracting last modified date, author of files in git repository using python使用python提取最后修改日期,git存储库中文件的作者
【发布时间】:2018-06-27 18:27:41
【问题描述】:

好的,我一直致力于从远程 git 存储库中提取数据,并使用 Python 脚本根据文件的最后修改日期生成 csv 报告列表文件。我已经能够使用 subprocess 获取最新代码,并且还能够生成报告。这两个函数的代码sn-p如下:

> 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)

所以现在,我得到了所有文件的最后修改日期,但问题是,生成的日期是我本地 repo 中的文件更新的时间,即当我接受最新的 pull 时,很明显。我想要的是远程存储库中每个文件的最后修改日期和作者。

使用 Git bash 生成上次修改日期的命令是 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)

【问题讨论】:

  • 为什么不使用子流程模块本身呢? subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {}'],shell=True)
  • @mufeed 默认不使用 git bash
  • 不知道我理解的对不对。但是要得到他想要的结果,我提到的代码就足够了吧?执行该代码后,我得到了正确的输出。如果我理解错了,请告诉我。
  • 感谢@Peter 的澄清。我现在明白了。

标签: python git gitpython


【解决方案1】:

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

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 用于将输出存储到变量中,以便您可以从中提取所需的值。

【讨论】:

  • 我收到以下错误文件名、目录名或卷标语法不正确。 subprocess.CalledProcessError: 命令'['cd C:/Users/sherin.sunny/git/ng-ui/;git ls-files -z | xargs -0 -n1 I{}-- git log -1 --format="%ai {}" {} | sort']' 返回非零退出状态 1. 我给的目录名正确吗?
  • 我猜您在使用 Windows 系统时指定路径时需要使用反斜杠。我的代码是在linux中执行的。试试 cd C:\Users\sherin.sunny\git\ng-ui
  • 我将您更新的代码与正斜杠和反斜杠一起使用。两者都出现错误 For forward: 文件名、目录名或卷标语法不正确。 Traceback(最近一次调用最后一次): subprocess.CalledProcessError: Command '['git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort']' 返回非零退出状态 1。
  • 对于反斜杠:文件“check_fileage.py”,第 12 行 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) ^ SyntaxError: (unicode error) 'unicodeescape ' 编解码器无法解码位置 2-3 中的字节:截断 \UXXXXXXXX 转义
  • 你能用双反斜杠检查吗?例如:C:\\Users\\sherin.sunny\\git\\ng-ui
猜你喜欢
  • 2021-12-07
  • 2021-11-16
  • 2012-10-17
  • 1970-01-01
  • 2019-02-26
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多