GITLAB iLM - Institut Lumière Matière

Skip to content
Snippets Groups Projects
Commit f5e615cd authored by Olivier VINCENT's avatar Olivier VINCENT
Browse files

Initial commit for git-tools project.

parents
Branches
Tags
No related merge requests found
*egg-info*
*pycache*
*.eggs*
LICENSE 0 → 100644
BSD 3-Clause License
Copyright (c) 2020, Olivier VINCENT
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Tools for using GIT in python. Right now, mostly for getting commit hash names corresponding to files in a repo.
Requirements:
- Python >= 3.6
- gitpython (https://gitpython.readthedocs.io)
\ No newline at end of file
"""Git tools in python."""
from .gittools import current_commit_hash, file_in_commit, parent_repo
__version__ = 0.1
"""Git tools for Python."""
from warnings import warn
from pathlib import Path
from git import Repo
from git import InvalidGitRepositoryError
def file_in_commit(file, commit):
"""Return True if file in tree of commit, False if not."""
fileabs = Path(file).resolve() # absolute path of filename
rootabs = Path(commit.repo.working_dir).resolve() # path of root of repo
localname = str(fileabs.relative_to(rootabs)) # name of file in the repo
if not fileabs.exists():
raise FileNotFoundError(f'File {fileabs} does not exist')
try:
commit.tree[localname]
except KeyError: # in this case the file is not in the commit
return False
else:
return True
def parent_repo(file):
"""Return repository object if file is in a subfolder of a git repo."""
filepath = Path(file)
try:
repo = Repo(filepath, search_parent_directories=True)
except InvalidGitRepositoryError:
warn('No git repository found. Returning None.')
return None
else:
return repo
def current_commit_hash(file, dirtyok=False):
"""Return HEAD commit hash corresponding to file if it's in a GIT repo."""
repo = parent_repo(file)
if not dirtyok and repo.is_dirty():
raise Exception("Dirty repo, please commit recent changes first.")
commit = repo.head.commit
assert file_in_commit(file, commit), "File is not in the HEAD commit."
return str(commit)
setup.py 0 → 100644
from setuptools import setup, find_packages
import gittools
with open("README.md", "r") as f:
long_description = f.read()
setup(
name='gittools',
version=gittools.__version__,
author='Olivier Vincent',
author_email='olivier.vincent@univ-lyon1.fr',
url='https://cameleon.univ-lyon1.fr/ovincent/git-tools',
description='Tools for git in python',
long_description=long_description,
long_description_content_type="text/markdown",
packages=find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
],
setup_requires=['gitpython'],
python_requires='>=3.6'
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment