본문 바로가기

개발하자

파이썬을 사용하여 깃 푸시 프로세스를 자동화하는 방법은 무엇입니까?

반응형

파이썬을 사용하여 깃 푸시 프로세스를 자동화하는 방법은 무엇입니까?

나는 파이썬을 사용하여 프로세스를 자동화하려고 한다.

나는 명령 뒤에 사용자 이름과 비밀번호를 입력하는 것을 제외하고 모두 자동화에 성공했다.

지금까지 제 코드는 다음과 같습니다:

import subprocess
import sys

add: str = sys.argv[1]
commit: str = sys.argv[2]
branch: str = sys.argv[3]


def run_command(command: str):
    print(command)
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    print(str(process.args))
    if command.startswith("git push"):
        output, error = process.communicate()
    else:
        output, error = process.communicate()
    try:
        output = bytes(output).decode()
        error = bytes(error).decode()
        if not output:
            print("output: " + output)
        print("error: " + error)
    except TypeError:
        print()


def main():
    global add
    global commit
    global branch
    if add == "" or add == " ":
        add = "."
    if branch == "":
        branch = "master"
    print("add: '" + add + "' commit: '" + commit + "' branch: '" + branch + "'")

    command = "git add " + add
    run_command(command)

    commit = commit.replace(" ", "''")
    command = 'git commit -m "' + commit + '"'
    run_command(command)

    command = "git push origin " + branch
    run_command(command)


if __name__ == '__main__':
    main()

사령부에 정보를 보낼 방법이 있나요?




가능한 경우 자격 증명 도우미를 사용하여 해당 정보(원격 URL에 연결된 자격 증명)를 캐시합니다. 및 "를 확인하십시오.

git config --global credential.helper

그러면 해당 정보를 입력할 필요가 전혀 없습니다.




이렇게 해결했습니다:

# make sure to cd into the git repo foler

import subprocess
import sys
import os


msg = input('Type the commit message (+ ENTER):') 
repo_directory = os.getcwd()

subprocess.run(["git", "add", "."], cwd=repo_directory)
# commit file
subprocess.run(["git", "commit", "-m", msg], cwd=repo_directory)
# push
subprocess.run(["git", "push"], cwd=repo_directory)  



깃 파이썬 라이브러리

from git import repo

repo = Repo('PATH/directory')
repo.git.add('file.txt')
repo.index.commit('commit message')
origin =
repo.remote(name='origin')
origin.push()

반응형