gitpython git authentication using user and password

后端 未结 3 1887
离开以前
离开以前 2021-01-01 18:57

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? W

3条回答
  •  离开以前
    2021-01-01 19:30

    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)
    

提交回复
热议问题