Git,cmd,PowerShell执行git命令行的区别,及通过Python调用实现批量拉取。

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

 	cmd1 = "cd \\"
    cmd2 = "cd C:\\ContributorCommits\\Results"
    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_list\microsoftdocs_open_specs_windows.md" | sort | uniq > D:/log_data/log.csv

· 通过PowerShell实现:git的去重命令为 | sort -unique

git log --pretty=format:"%an" "D:\repo_list\microsoftdocs_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:\ContributorCommits\Program\repo_list.csv')
current_2 = pd.read_csv(r'C:\ContributorCommits\Program\404_repo_list.csv')
current_3 = pd.read_csv(r'C:\ContributorCommits\Program\can_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
for i in range(1000):
    RepoFullName = current_1.iloc[i][0]
    RepoFullName_split = RepoFullName.split("/")
    repo_file = RepoFullName_split[0]+"_"+RepoFullName_split[1]
    cmd1 = "cd \\"
    cmd2 = "D:"
    cmd3 = "cd test\\Results\\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

    if RepoFullName in list_404:
        print("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)
        r.remote().pull('live')
        print("***Successful to create the file and configure")
        print("***Successful to clone the Repo {}".format(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'D:\test\Results\repo_list\{}'.format(repo_file)) # 创建一个操作对象
            r.remote().pull('master')
            print("The  {} repo {} finished".format(i,RepoFullName))
            print("-"*100)
        except:
            print("There is something wrong with repo {}".format(RepoFullName))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值