Connection refused when tried to clone from github Enterprise - python

An error I met when I use git API
from git import Repo
from github import Github
Connecting to git.xxxxx.com (port 9418) ... fatal: unable to connect to git.xxxxx.com:
git.xxxxx.com[0: 141.113.0.105]: errno=Connection refused
I can clone code from Enterprise github by command
git clone https://[name]:[token]#git.git.xxxx.com/xxx.git
But I'm confusing the way of clone in git API
def walk_githubprojects(token, organization):
client = Github(base_url='https://git.xxx.com/api/v3', login_or_token=token)
user=client.get_user().get_repos()
for repo in client.get_organization(organization).get_repos():
print(repo.name)
print(repo.git_url)
Repo.clone_from(repo.git_url, 'my_path')
I can get repo names under organization, the script should clone repos to my local dir

The solution is:
from git import Repo
from github import Github
def walk_githubprojects(token, organization):
client = Github(base_url='https://git.xxxxx.com/api/v3', login_or_token=token)
user=client.get_user().get_repos()
for repo in client.get_organization(organization).get_repos():
HTTPS_REMOTE_URL = f'https://[name]:[token]#git.xxxxx.com/{repo.full_name}'
Repo.clone_from(HTTPS_REMOTE_URL, f'/xxxx/{repo.name}')

Related

How to connect to Azure Devops git repo through databricks?

I want to create a python notebook in databricks that will do the following -
Connect to Azure Devops Git repo
Make couple of changes in a yaml file
Commit the changes in the master branch
Push the changes back to repo
I tried the below code to achieve step 1:
import git
repo_url = <repo url>
pat_token = <token>
dbfs_path ='<dbfs_path>'
repo = git.Repo.clone_from(repo_url, dbfs_path, branch='master')
But I got the below error:
GitCommandError: Cmd('git') failed due to: exit code(128)
cmdline: git clone --branch=master -v <repo_url> <dbfs_path>
stderr: 'Cloning into <dbfs_path>...
fatal: could not read Username for <repo_url>: No such device or address
I am not sure where to provide the username.

git submodule update via ssh error

I cloned the application commcare-hq after installing python and django in my cpanel. here's the link: https://github.com/dimagi/commcare-hq but whenever i enter the following command
git submodule update --init --recursive
i get the following error
fatal: clone of 'git://github.com/dimagi/xml2json.git' into submodule path
'/home/hcdcnetl/myProject/commcare-hq/submodules/xml2json' failed
Failed to clone 'submodules/xml2json'. Retry scheduled
Cloning into '/home/hcdcnetl/myProject/commcare-
hq/corehq/apps/hqmedia/static/hqmedia/MediaUploader'...
fatal: unable to connect to github.com:
github.com[0: 192.30.253.113]: errno=Connection refused
github.com[1: 192.30.253.112]: errno=Connection refused
git://github.com is not an SSH URL, it is a Git-protocol URL.
Try
git config --global url."git#github.com/".insteadOf git://github.com/
Any git://github.com/ will be replaced by the SSH URL git#github.com/...

Push to remote repository

I have two repositories on github, using gitpython I'm trying to push a file from one repository to another remote repository. I've managed to do it using git but struggling with the gitpython code.
git remote add remote_to_push git#bitbucket...
git fetch remote_to_push
git checkout remote_to_push/master
git add file_to_push
git commit -m "pushing file"
git push remote_to_push HEAD:master
I've managed to create a repo object of the remote I think with the following
from git import Repo
repo = Repo('path/to/other/git/repo')
remote = repo.remotes.origin
I can't figure out how to add something to then push, if i call
remote.add("file_to_push")
Then I get errors about the create() function
TypeError: create() takes exactly 4 arguments (2 given)
Trying to follow what they have done in How to push to remote repo with GitPython with
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))
I assume it should work with using master and master as the remote branches as they both must exist but it's giving me the error
stderr: 'error: src refspec master does not match any.'
Thanks
Solved it.
First created a remote of the other repo
git remote add remote_to_push git#bitbucket...
Then the gitpython code
from git import Repo
repo = Repo('path/to/other/git/repo') #create repo object of the other repository
repo.git.checkout('remote_to_push/master') #checkout to a branch linked to the other repo
file = 'path/to/file' #path to file to push
repo.git.add(file) # same as git add file
repo.git.commit(m = "commit message") # same as git commit -m "commit message"
repo.git.push('remote_to_push', 'HEAD:master') # git push remote_to_push HEAD:master
Aside from the docs, I found the following to be quite helpful if anyone's struggling with gitpython as I found it quite a pain
Python Git Module experiences?
http://sandlininc.com/?p=801
Git push via GitPython
How to push to remote repo with GitPython

Serve repository using git daemon with GitPython

I'm trying to initialize a git repo and then serve it using the git daemon. Everything with GitPython.
Initializing the repo works:
temp_dir = '/tmp/something'
repo = git.Repo.init(temp_dir)
Starting the daemon as well:
gd = Git().daemon(temp_dir, enable='receive-pack', listen='127.0.0.1', port=GIT_DAEMON_PORT,as_process=True, export_all=True)
gd.proc.wait()
But I'm not able to access the repo:
git clone git://127.0.0.1:9418/something
Cloning into 'something'...
fatal: remote error: access denied or repository not exported: /something
I'm not sure if I have to initialize the repo as bare, or if I have to specify the base_path when starting the git daemon... tried it all. Does anybody have some pointers?
PS: I've seen a similar approach here:
https://github.com/gitpython-developers/GitPython/blob/master/git/test/lib/helper.py
Figured it out, Thanks to user3159253.
import git
import tempfile
tmpdir = tempfile.TemporaryDirectory(suffix='.git')
repo = git.Repo.init(tmpdir.name, shared=True, bare=True)
repo.daemon_export = True
gd = git.Git().daemon(tmpdir.name,
enable='receive-pack',
listen='127.0.0.1',
port=9418,
as_process=True,
verbose=True
)
gd.proc.wait()
Now you can clone that repo using:
$git clone git://127.0.0.1:9418/tmp/<name-of-tmpdir>.git

Unique git url for github repo with multiple branches?

I'm looking at a flask app which I would like to import into an openshift project (https://github.com/lpolepeddi/intro-to-flask) . This contains a a series of 'checkpoints'(branches) as part of a tut. I want to grab the final code which is at https://github.com/lpolepeddi/intro-to-flask/tree/20_visibility_control for the starting point of a project . Is the a way to get a unique git url for this branch of the form
https://github.com/lpolepeddi/intro-to-flask.git
So that I can pull it in with a command like:
git remote add upstream -m master https://github.com/shekhargulati/flask-login-openshift-quickstart.git
Although as far as I know "single branch GitHub url" doesn't exist, you could just clone entire repo and later change branch:
git clone https://github.com/lpolepeddi/intro-to-flask.git
cd intro-to-flask
git checkout 20_visibility_control
or as stated in this answer, clone only one branch:
git clone --branch 20_visibility_control --single-branch https://github.com/lpolepeddi/intro-to-flask.git

Categories