GitHub Repo 地址:欢迎给星
----------以下正文----------
Git,cmd,PowerShell都支持git命令行的执行,但是他们在操作上会有细微差别。现以需求一和需求二为例进行说明。
需求一:拉取只包含.md文件和.yml文件的一个Repo
· 通过git实现:echo后面的目标文件必须要加''(引号)。
cd
cd D:test
mkdir repo_file
cd repo_file
git init
git remote add origin https://github.com/microsoftdocs/oufr-dev-docs.git
git config core.sparsecheckout true
echo '*.md' >> .git/info/sparse-checkout
echo '*.yml' >> .git/info/sparse-checkout
· 通过cmd实现:echo后面的目标文件不能加''(引号)。
cd
cd D:test
mkdir repo_file
cd repo_file
git init
git remote add origin https://github.com/microsoftdocs/oufr-dev-docs.git
git config core.sparsecheckout true
echo *.md >> .git/info/sparse-checkout
echo *.yml >> .git/info/sparse-checkout
· python调用cmd方法实现拉取只包含.md文件和.yml文件的Repo
repo_file = '自定义一个repo_file'
RepoFullName = '自定义一个RepoFullName'
cmd1 = "cd "
cmd2 = "cd C:ContributorCommitsResults"
cmd3 = "cd repo_list"
cmd4 = "mkdir {}".format(repo_file)
cmd5 = "cd {}".format(repo_file)
cmd6 = "git init"
cmd7 = "git remote add origin https://github.com/{}.git".format(RepoFullName)
cmd8 = "git config core.sparsecheckout true"
cmd9 = "echo *.md >> .git/info/sparse-checkout"
cmd10 = "echo *.yml >> .git/info/sparse-checkout"
cmd = cmd1 + " && " + cmd2 + " && " + cmd3 + " && " + cmd4 + " && " +
cmd5 + " && " + cmd6 + " && " + cmd7 + " && " + cmd8 + " && " +
cmd9 + " && " + cmd10
需求二:拉取需求一中的Repo后,通过git log指定输出内容,排序并去重
· 通过git实现:git的排序去重命令为 | sort | uniq
git log --pretty=format:"%an" "D:repo_listmicrosoftdocs_open_specs_windows.md" | sort | uniq > D:/log_data/log.csv
· 通过PowerShell实现:git的去重命令为 | sort -unique
git log --pretty=format:"%an" "D:repo_listmicrosoftdocs_open_specs_windows.md" | sort -unique > D:/log_data/log.csv
· cmd暂未发现去重命令实现调用git命令行去重
如果有知道的小伙伴欢迎再留言区留言
通过以上两个需求可以看出git命令在不同的工具下执行还是有一些区别的,所以我们在通过Python去调用Git的过程中,除了可以通过gitpython模块使用常规命令外,还可以通过cmd的方式配合gitpython一起使用,可以支持较为复杂的git命令。现已需求三为例:
需求三:拉取只包含.md文件和.yml文件的1000个Repo,且输出每一篇文章的Github URL和贡献者人数,及贡献者明细
python代码如下:
import subprocess
import sys
import pandas as pd
import time
import os
from git import Repo,Git
import csv
current_1 = pd.read_csv(r'C:ContributorCommitsProgramrepo_list.csv')
current_2 = pd.read_csv(r'C:ContributorCommitsProgram404_repo_list.csv')
current_3 = pd.read_csv(r'C:ContributorCommitsProgramcan_not_find_remote_master_repo_list.csv')
list_404 = []
for i in range(13):
list_404.append(current_2.iloc[i][0])
list_can_not_find_remote_master = []
for i in range(4):
list_can_not_find_remote_master.append(current_3.iloc[i][0])
def RunShellWithReturnCode(command,print_output=True,universal_newlines=True):
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=universal_newlines)
if print_output:
output_array = []
while True:
line = p.stdout.readline()
if not line:
break
print(line.strip("/n"))
output_array.append(line)
output ="".join(output_array)
else:
output = p.stdout.read()
p.wait()
errout = p.stderr.read()
if print_output and errout:
print(sys.stderr, errout)
p.stdout.close()
p.stderr.close()
return output,p.returncode
# 拉取指定所需要的文件的Repo
RepoFullName = current_1.iloc[i][0]
RepoFullName_split = RepoFullName.split("/")
repo_file = RepoFullName_split[0]+"_"+RepoFullName_split[1]
cmd1 = "cd "
cmd2 = "cd C:ContributorCommitsResults"
cmd3 = "cd repo_list"
cmd4 = "mkdir {}".format(repo_file)
cmd5 = "cd {}".format(repo_file)
cmd6 = "git init"
cmd7 = "git remote add origin https://github.com/{}.git".format(RepoFullName)
cmd8 = "git config core.sparsecheckout true"
cmd9 = "echo *.md >> .git/info/sparse-checkout"
cmd10 = "echo *.yml >> .git/info/sparse-checkout"
cmd = cmd1 + " && " + cmd2 + " && " + cmd3 + " && " + cmd4 + " && " +
cmd5 + " && " + cmd6 + " && " + cmd7 + " && " + cmd8 + " && " +
cmd9 + " && " + cmd10
cmd_remote = cmd + " && " + "git pull origin live"
if RepoFullName in list_404:
print("The {} repo is 404 repo {} ".format(i,RepoFullName))
with open(r"C:ContributorCommitsProgramrepo_data_log.csv", "w+", newline='',encoding='utf-8') as f:
f.write("The {} repo is 404 repo {}".format(i,RepoFullName))
print("-"*100)
continue
elif RepoFullName in list_can_not_find_remote_master:
print("The {} repo is can_not_find_remote_master repo {} ".format(i,RepoFullName))
print("***Star to clone the Repo {}".format(RepoFullName))
RunShellWithReturnCode(cmd_remote)
print("***Successful to create the file and configure")
print("***Successful to clone the Repo {}".format(RepoFullName))
with open(r"C:ContributorCommitsProgramrepo_data_log.csv", "w+", newline='',encoding='utf-8') as f:
f.write("The {} can_not_find_remote_master repo {} finished".format(i,RepoFullName))
print("-"*100)
else:
try:
print("***Star to clone the Repo {}".format(RepoFullName))
RunShellWithReturnCode(cmd)
print("***Successful to create the file and configure")
print("***Star to pull the Repo {}".format(RepoFullName))
r = Repo(r'C:ContributorCommitsResultsrepo_list{}'.format(repo_file)) # 创建一个操作对象
r.remote().pull('master')
print("The {} repo {} finished".format(i,RepoFullName))
with open(r"C:ContributorCommitsProgramrepo_data_log.csv", "w+", newline='',encoding='utf-8') as f:
f.write("The {} repo {} finished".format(i,RepoFullName))
print("-"*100)
except:
print("There is something wrong with {} repo {}".format(i,RepoFullName))
with open(r"C:ContributorCommitsProgramrepo_data_log.csv", "w+", newline='',encoding='utf-8') as f:
f.write("There is something wrong with {} repo {}".format(i,RepoFullName))
print("-"*100)