Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork937
How to check whether the username and password can access the git repository#1959
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
How to use gitpython to check whether the configured username and password can access the git repository? I just wanted to test the connectivity. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 4 comments 1 reply
-
You can do that with |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
@Byron is suggesting something like this: With GitPythonNote: The importtempfilefromgitimportRepodefcheck_git_access(repo_url,username,password):try:withtempfile.TemporaryDirectory()astmpdirname:repo_url_with_creds=f"https://{username}:{password}@{repo_url.split('://')[-1]}"repo=Repo.init(tmpdirname,bare=True)# Add a remote named 'origin' with the URL that includes the username and password# to the repo initialized in the temp dir. This is only used to check access to# the remote repository.origin=repo.create_remote('origin',url=repo_url_with_creds)# Attempt to list the references from the remote repository# If this succeeds, it means the credentials are correct and access is granted.refs=origin.ls()print("Access granted")returnTrueexceptExceptionase:# If there is any exception, it indicates that access was deniedprint(f"Access denied:{e}")returnFalse# Example usagerepo_url="gitee.com/guang11cheng/all_docs.git"username="your_username"password="your_password"check_git_access(repo_url,username,password) HTTP Request Only Without Git Command ExecutionYou might be able to use something like Basic Authentication and avoid Git commands entirely though, e.g. importrequestsfromrequests.authimportHTTPBasicAuthdeftest_git_access(repo_url,username,password):try:response=requests.get(repo_url,auth=HTTPBasicAuth(username,password))ifresponse.status_code==200:return"Access granted: Repository is reachable."elifresponse.status_code==401:return"Access denied: Invalid credentials."else:returnf"Error: Unexpected status code{response.status_code}."exceptrequests.exceptions.RequestExceptionase:returnf"Error:{str(e)}"# Example usagerepo_url="https://gitee.com/guang11cheng/all_docs.git"username="your_username"password="your_password"output=test_git_access(repo_url,username,password)print(output) That could probably be done in client-side JavaScript too |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
After I verified it, neither way works. The first way, I randomly change the username, password and repo_url, all display 'Access granted' |
BetaWas this translation helpful?Give feedback.
All reactions
-
origin.ls() |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #1958 on August 22, 2024 17:02.