How can I rename github remote branch using PyGithub python API ?
Reference from official PyGithub - https://github.com/PyGithub/PyGithub
https://pygithub.readthedocs.io/en/latest/apis.html
from github import Github
# using username and password
g = Github("user", "password")
# or using an access token
g = Github("access_token")
# get repo
r = g.get_organization('org').get_repo('name')
# Git reference of the branch that you wish to delete
src = r.get_git_ref("heads/source branch name")
# Create new branch from old branch
r.create_git_ref("refs/heads/new branch name", sha=src.object.sha)
# Delete old branch reference
src.delete()
Related
I have an existing repo which I connected to my gitlab via ssh
I am just trying to create my branch in sourcetree so that i push my changes there but in getting an error "not a valid object name master"
is there any part that i skipped because my created repository cant create new branches? thanks for reading any help will be great.
TLDR: created and linked via gitlab repo in sourcetree with an existing one, cant create branch to push
Check first if you are creating your branch from an empty repository: there should be at least one commit.
If the repository is not empty, check if:
your remote GitLab repository expects a default branch 'main'
your local repository uses master as its main branch
If it is the case, rename your local branch
cd /path/to/local/repository
git branch -m master main
And make sure any future local repository starts with main:
git config --global init.defaultbranch main
Try again your branch creation in SourceTree.
Is there a way that I can get the most recent commit on a remote repository using gitpython?
I do not want to perform operations like a pull or merge on my local branch. I also do not want to depend on the master branch on my local to get this information. All I have is a valid repo out there, and I am using repo.remotes.origin.url to get the information.
With just the repo URL, can I get the most recent commit on that repository?
Using gitpython, you can't do this without a local clone. Git is a distributed system, so it's designed for users to operate on their local repos. These answer gives some decent explanations and alternatives:
https://stackoverflow.com/a/28524371/5752730
https://stackoverflow.com/a/19391097/5752730
https://stackoverflow.com/a/16579859/5752730
Using gitpython - requires local repo
You can do a shallow clone (for speed), get latest commit SHA using git rev-parse or git ls-remote, then delete the local repo.
import git
from pathlib import Path
repo_url = 'https://github.com/path/to/your/repo.git'
local_repo_dir = Path('/path/to/your/repo')
# delete the repo if it exists, perform shallow clone, get SHA, delete repo
local_repo_dir.unlink(missing_ok=True)
repo = git.Repo.clone_from(repo_url, local_repo_dir, depth=1)
sha = repo.rev_parse('origin/master')
local_repo_dir.unlink()
print(sha)
Using python subprocess - does not require local repo
This simpler solution uses git ls-remote, which does not require a local clone. The following uses subprocess to get the SHA-1 of the given branch from the remote repo without a local clone. Note that the SHA needs to be extracted from the output response by splitting at the first tab.
import subprocess
import re
repo_url = 'https://github.com/path/to/your/repo.git'
process = subprocess.Popen(["git", "ls-remote", repo_url], stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
sha = re.split(r'\t+', stdout.decode('ascii'))[0]
print(sha)
You can do it with gitpython without creating a local repository first:
remote_heads = git.cmd.Git().ls_remote(repo_url, heads=True)
Is there a way to get a list of files and directories in the gitlab repo using python? So if I use the gitlab repository url as my source, can I traverse and get a list of all files and directories/ sub-directories within the repo? Like using os.walk but for a web url.
Can you try python-gitlab?
Use repository_tree method can do it for you.
Here is the code
import gitlab
gl = gitlab.Gitlab.from_config()
# Get a project by ID
project_id = 851
project = gl.projects.get(project_id)
branch = {your branch}
directory = {your directory ref to root directory of the repository}
project.repository_tree(direcotry, ref=branch)
For more details, please visit python-gitlab
I have three existing projects that I want to put in one group in GitLab.
I did the following steps:
Create a new group supi
Go to project A => Settings => Advanced => Transfer project => Select namespace => Group supi
(Now Project A's url moved from git#gitlab.com:myname/project-a to git#gitlab.com:mygroup/project-a)
I changed the url inside of my local copy from project A git remote set-url origin git#gitlan.com:mygroup/project-a
I thought that everything will work from here, however, when I call git pull from my local project A I get this:
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
What does it mean?
This is the content of my .git/config file:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git#gitlab.com:mygroup/project-a.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
Found the issue, I had no files in the repository. Adding a readme.md made it work.
I have a GitLab project that utilises GitLab CI.
The project also uses submodules, both the project and it's submodules are under the same GitLab account.
Here is my .gitmodules file
[submodule "proto_contracts"]
path = proto_contracts
url = https://gitlab.com/areller/proto_contracts.git
I also have this piece in the .gitlab-ci.yml file
variables:
GIT_SUBMODULE_STRATEGY: recursive
However, when i run the CI I get this error
fatal: could not read Username for 'https://gitlab.com': No such device or address
Both the project and the submodules are in a private repository so you would expect to be prompted for authentication, but as I've mentioned, the project and the submodule are under the same account and one of the runner's jobs is to clone the original repository
So it's odd that it's unable to reach the submodule
Is there a way around it?
You must use relative URLs for submodules. Update your .gitmodules as follow:
[submodule "proto_contracts"]
path = proto_contracts
url = ../../areller/proto_contracts.git
Further reading: Using Git submodules with GitLab CI | GitLab Docs