-
-
Notifications
You must be signed in to change notification settings - Fork 954
-
Hello. I recently came across this library for git on Python. I am working on a project where client needs to have a script which he can run to fetch/pull all the changes from my git repository.
The most important thing is that the computer will not have any SSH keys associated with gitlab therefore it will need to use username and password for authentication. I havent managed to find a way how to do this using GitPython.
I can use the following script to commit and push to the repository:
import git
full_local_path = "C:/Users/test/Desktop/WORK/Test/test_main/testfw"
branch_name = "test_branch"
repo = git.Repo(full_local_path)
origin = repo.remote(name="origin")
# COMMIT & PUSH
repo.index.commit("Some commit message123")
origin = repo.remote(name="origin")
origin.push()
print("Script complete")
The above script will work without any issues on the machine that has SSH keys for authentication.
However, my other Raspberry PI device will not have SHH keys and I want to be able to authenticate using username and password.
Currently, I manually have to fetch and pull all the changes as following:
My_raspberry@raspberrypi:~/DEV/testfw/fw $ git push
Username for 'https://xxx-gitlab.xxxxxx.lt': name.surname@xxxxxx.lt
Password for 'https://name.surname@xxxxxx.lt@xxx-gitlab.xxxxxx.lt':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 974 bytes | 487.00 KiB/s, done.
Total 8 (delta 7), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for test_branch, visit:
remote: https://xxx-gitlab.xxxxxx.lt/test/testfw/-/merge_requests/new?merge_request%5Bsource_branch%5D=test_branch
remote:
To https://xxx-gitlab.xxxxxx.lt/test/testfw.git
761a21c..c10b27e test_branch -> test_branch
and it will work fine without any issues. I was hoping to automate this process using GitPython.
Any help is appreciated
Beta Was this translation helpful? Give feedback.
All reactions
The tutorial contains an example for how to configure SSH connections. As for HTTPS, I'd expect such credentials to be stored and retrieved by git
's built-in credential helpers.
URLs are allowed to contain username and password, and these could be used through GitPython as well. It's also possible to call credential managers by hand and store a user and password for a URL programmatically. The format is specified in the git
documentation - please be aware though that no tooling exists to make interacting with them easier, at least not in GitPython.
I hope that helps.
Replies: 2 comments 2 replies
-
The tutorial contains an example for how to configure SSH connections. As for HTTPS, I'd expect such credentials to be stored and retrieved by git
's built-in credential helpers.
URLs are allowed to contain username and password, and these could be used through GitPython as well. It's also possible to call credential managers by hand and store a user and password for a URL programmatically. The format is specified in the git
documentation - please be aware though that no tooling exists to make interacting with them easier, at least not in GitPython.
I hope that helps.
Beta Was this translation helpful? Give feedback.
All reactions
-
@Byron
You mentioned I can use username and password within the URL. Isnt't that the solution to my problem or that still wont work?
Could you provide any code examples?
I assume something like that: (Notice I have full_url that includes my account username and password). How can I use this full url in GitPython?
import git
full_local_path = "/home/raspberrypi/DEV/testfw/fw"
username = yyyyyyy
password = yyyyyyyyyy
#I am not sure how to use full_url variable?
full_url= f"https://{username}:{password}@xxx.gitlab.xxxxxxxx.lt/testfw/fw.git"
repo = git.Repo(full_local_path)
repo.index.commit("Some commit message")
origin = repo.remote(name="origin")
origin.push()
Beta Was this translation helpful? Give feedback.
All reactions
-
GitPython allows to interact with git
mostly like one would on the git
command-line. I recommend trying to do what you want on in the terminal using git
and then transfer that knowledge to GitPython.
Beta Was this translation helpful? Give feedback.
All reactions
-
I can do everything I want using terminal easily.
See the logs below:
My_raspberry@raspberrypi:~/DEV/testfw/fw $ git push
Username for 'https://xxx-gitlab.xxxxxx.lt': name.surname@xxxxxx.lt
Password for 'https://name.surname@xxxxxx.lt@xxx-gitlab.xxxxxx.lt':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 974 bytes | 487.00 KiB/s, done.
Total 8 (delta 7), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for test_branch, visit:
remote: https://xxx-gitlab.xxxxxx.lt/test/testfw/-/merge_requests/new?merge_request%5Bsource_branch%5D=test_branch
remote:
To https://xxx-gitlab.xxxxxx.lt/test/testfw.git
761a21c..c10b27e test_branch -> test_branch
As you can see from above, I execute git push
command and I am prompted to type in username and password. I want to do the exact same thing in GitPython. Have you got any clues how can this be achieved?
Since you said that GitPython allows interacting with git
like git command line, there should be away to perform username and password authentication using https. It would be great if you could point me in the right direction and provide some example code
Beta Was this translation helpful? Give feedback.