Git,cmd,PowerShellはgitコマンドラインの違いを実行し,Python呼び出しにより一括引き出しを実現する.

22452 ワード

GitHub Repoアドレス:スターへようこそ
Git,cmd,PowerShellはgitコマンドラインの実行をサポートしていますが、操作にはわずかな違いがあります.ここでは、需要1と需要2を例に説明する.
需要一:引き出しは含まれる.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命令行重み付け知っている仲間がいたら伝言エリアへようこそ
以上の2つのニーズからgitコマンドが異なるツールで実行されるのか、いくつかの違いがあることがわかりますので、PythonでGitを呼び出す過程でgitpythonモジュールで通常のコマンドを使用できるほか、cmdでgitpythonと組み合わせて使用することができ、複雑なgitコマンドをサポートすることができます.現在のニーズの3つを例に挙げます.
需要3:引き出しは含まれる.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))