Python3 script to find a particular branch existence and last commit date in git lab

--

GitPython has references to all the branches. A user can iterate to check if the branch is part of that reference or not.

pip3 install GitPython

git required imports

from git import Repo
import git

last commit date required imports

from time import strftime, localtime
import datetime

Below command downloads repo to local and gets all remote branches
Repo.clone_from(repoName, “C:\\”+folderNameLocal,branch=’master’)
repo = git.Repo(folderNameLocal)
remote_refs = repo.remote().refs

then iterate over remote branches then look for your specific branch

for refs in remote_refs:
#below if condition will check for given branch present in remote ref
if (refs.name.lower()).startswith(‘origin/abc’):
print(refs.name.lower())

Command to get Last commit date

datetime.datetime.fromtimestamp(refs.commit.committed_date)

full script can be found here

https://github.com/sudhast1910/pythonutilities/blob/main/findbranchandlastcommit_gitlab.py

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response