gitpython git authentication using user and password

荒凉一梦 提交于 2020-05-11 04:15:32

问题


I'm using GitPython but did not find a way to push to repo using username and password. Can anybody send me a working example or give me some pointer about how to do it? What I need to do is: add a file to the repository, push it using the username and password provided.


回答1:


This is what I used for myself for pulling

pull.py

#! /usr/bin/env python3

import git
import os
from getpass import getpass

project_dir = os.path.dirname(os.path.abspath(__file__))
os.environ['GIT_ASKPASS'] = os.path.join(project_dir, 'askpass.py')
os.environ['GIT_USERNAME'] = username
os.environ['GIT_PASSWORD'] = getpass()
g = git.cmd.Git('/path/to/some/local/repo')
g.pull()

askpass.py (similar to this one)

This is in the same directory as pull.py and is not limited to Github only.

#!/usr/bin/env python3
#
# Short & sweet script for use with git clone and fetch credentials.
# Requires GIT_USERNAME and GIT_PASSWORD environment variables,
# intended to be called by Git via GIT_ASKPASS.
#

from sys import argv
from os import environ

if 'username' in argv[1].lower():
    print(environ['GIT_USERNAME'])
    exit()

if 'password' in argv[1].lower():
    print(environ['GIT_PASSWORD'])
    exit()

exit(1)



回答2:


I found this working solution:

  1. create a script like this: ask_pass.py
  2. before to execute push assign the environment vars:
   os.environment['GIT_ASKPASS']= <full path to your script>
   os.environment['GIT_USERNAME'] = <committer username>
   os.environment['GIT_PASSWORD'] = <the password>

and anything works fine.



来源:https://stackoverflow.com/questions/44784828/gitpython-git-authentication-using-user-and-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!