4

I'm trying to automate the git push processes using python.

I've succeeded automating all except entering the username and password after the git push command.

This is my code so far:

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

Is there any way to send the information to the command?

1

3 Answers 3

2

If possible, use a credential helper in order to cache that information (credentials associated to a remote URL).
Check the gitcredential section and "Git Tools - Credential Storage".

git config --global credential.helper

That way, you won't have to enter that information at all.

Sign up to request clarification or add additional context in comments.

6 Comments

Where should I write this? I just ran it in the terminal and it didn't do much...
@TheZadok42 if it does not return anything, that means there is no credential helper set up. What is your OS? And Git version? Depending on that, you can set up one.
I'm using Linux (Arch with i3wm) and git (version 2.17.0)
@TheZadok42 Then use libsecret, as I documented in stackoverflow.com/a/13386417/6309 and stackoverflow.com/a/40312117/6309. Make sure your python program will run with the same user as the one used for that git configuration.
After I write the command, is there any farther setup that I should do?
|
1

This is how I solved it:

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

Comments

0

GitPython library

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.