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?

Javier C.
  • 7,859
  • 5
  • 41
  • 53
TheZadok42
  • 147
  • 1
  • 9
  • Maybe using one GIT module for python! Have a look at [Python Git Module experiences?](https://stackoverflow.com/questions/1456269/python-git-module-experiences), there is some interesting answers! – F. Hauri - Give Up GitHub Apr 09 '18 at 05:58

3 Answers3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Where should I write this? I just ran it in the terminal and it didn't do much... – TheZadok42 Apr 09 '18 at 05:57
  • @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. – VonC Apr 09 '18 at 06:04
  • I'm using Linux (Arch with i3wm) and git (version 2.17.0) – TheZadok42 Apr 09 '18 at 06:09
  • @TheZadok42 Then use libsecret, as I documented in https://stackoverflow.com/a/13386417/6309 and https://stackoverflow.com/a/40312117/6309. Make sure your python program will run with the same user as the one used for that git configuration. – VonC Apr 09 '18 at 06:11
  • After I write the command, is there any farther setup that I should do? – TheZadok42 Apr 09 '18 at 06:12
  • @TheZadok42 First, check https://stackoverflow.com/a/40312117/6309 for more details. Second, do in command line from any folder: git ls-remote /url/of/remote/repo: if it requires authentication, it will query for your username/password and cache them. Then you can proceed with your python program. – VonC Apr 09 '18 at 06:14
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)  
Bas
  • 358
  • 3
  • 7
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()
prolix
  • 11
  • 2