In [1]:
# %%bash
# git add --all :/
# git commit -a -m 'bugfix: _root() otherwise name conflict between attribute and callable'
In [2]:
!date #last update
In [3]:
# Change work directory to project root
import gitpath #pip install git+https://github.com/ruxi/python-gitpath.git
import os.path
rootpath = gitpath.root()
os.chdir(rootpath)
os.getcwd()
Out[3]:
created: Thu Nov 3 00:45:51 CDT 2016
Gitpath by Maximilian Nöthe is a function that return the git repo's root directory. Source code here:
https://github.com/MaxNoe/python-gitpath
This notebook describes adding gitpath into ruxitools. Currently gitpath is not on pypi, which adds complexity when trying to reproduce an anaconda environment with it included in the environment.yml
In [4]:
os.makedirs(os.path.join('ruxitools','gitpath'), exist_ok=True)
In [ ]:
# %load ruxitools/gitpath/__init__.py
#!/usr/bin/env python
# The MIT License (MIT)
# Copyright (c) 2015 Maximilian Nöthe
# https://github.com/MaxNoe/python-gitpath
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from subprocess import check_output, CalledProcessError
from functools import lru_cache
import os.path
@lru_cache(maxsize=1)
def root():
''' returns the absolute path of the repository root '''
try:
base = check_output(['git', 'rev-parse', '--show-toplevel'])
except CalledProcessError:
raise IOError('Current working directory is not a git repository')
return base.decode('utf-8').strip()
def abspath(relpath):
''' returns the absolute path for a path given relative to the root of
the git repository
'''
return os.path.join(root(), relpath)