Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

gitpython - Python - how to check whether the path is under a git repo? and extract the repo name

I'm trying to develop a Gatekeeping script in python rollin.py with the following requirements:

  1. Assume user would launch the rollin.py script from anywhere within his local git cloned area to push his commits to the master repo

  2. Now, rollin.py script would

    • clone a repo from master
    • pull user commits and merges into the cloned repo
    • Run the compliance tests
    • If passes, then push those changes to master repo else discard and notify the user

Now within rolloin.py script, how would I check the the repo name and user's git clone path? (since user can launch the rolloin.py script from anywhere in his local area)

Is there any existing function or method available? otherwise I'm thinking to implement reverse recursive search from cwd to locate the .git and and then url from it's config file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the GitPython library

You can achieve this with the already authored GitPython module. Read the docs.

$ pip install GitPython

Python snippet to print the base git path and the origin remote url.

import git

# Raises InvalidGitRepositoryError when not in a repo
repo = git.Repo(".", search_parent_directories=True) 
print "Location "+ repo.working_tree_dir
print "Remote: " + repo.remote("origin").url

To create a new git repo

import git 

newRepo = git.Repo.init("my-git-repo", mkdir=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...