Skip to content

Commit

Permalink
Add utility for getting relative path of file based on git repo root …
Browse files Browse the repository at this point in the history
…location

(refs idaholab#16410)
  • Loading branch information
aeslaughter committed Jan 12, 2021
1 parent b593b2d commit ff93047
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/mooseutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .mooseutils import generate_filebase, recursive_update, fuzzyEqual, fuzzyAbsoluteEqual
from .gitutils import is_git_repo, git_commit, git_commit_message, git_merge_commits, git_ls_files
from .gitutils import git_root_dir, git_init_submodule, git_submodule_status, git_version
from .gitutils import git_authors, git_lines, git_committers
from .gitutils import git_authors, git_lines, git_committers, git_localpath
from .message import mooseDebug, mooseWarning, mooseMessage, mooseError
from .MooseException import MooseException
from .eval_path import eval_path
Expand Down
7 changes: 7 additions & 0 deletions python/mooseutils/gitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,10 @@ def git_committers(loc=os.getcwd(), *args):
items = line.split("\t", 1)
counts[items[1]] = int(items[0])
return counts

def git_localpath(filename):
"""
Return the path from the root of the repository.
"""
root = git_root_dir(os.path.dirname(filename))
return os.path.relpath(filename, root)
4 changes: 4 additions & 0 deletions python/mooseutils/tests/test_gitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def testGitLines(self):
self.assertTrue(counts['Andrew E. Slaughter'] > 0)
self.assertEqual(n_with_blank, sum(list(counts.values())))

def testGitLocalPath(self):
filename = os.path.abspath(__file__)
local = mooseutils.git_localpath(filename)
self.assertEqual(local, 'python/mooseutils/tests/test_gitutils.py')

if __name__ == '__main__':
unittest.main(verbosity=2, buffer=True)

0 comments on commit ff93047

Please sign in to comment.