diff --git a/README.md b/README.md index c33220f..fe62b85 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Options: -g, --graphmode TEXT GraphMode: scattered | clustered [default: scattered] -o, --out TEXT Image filename [default: graph] -u, --with-url Add repo URLs [default: False] + -r, --relative-path Show relative path [default: False] --help Show this message and exit. ``` @@ -28,6 +29,9 @@ $ submodulegraph # Print the structure of the submodules in text in the console, including repo URLs. $ submodulegraph -u +# Print the structure of the submodules in text in the console, including repo URLs and relative paths of submodule directories. +$ submodulegraph -u -r + # Create a png of the submodule structure where all the submodules are listed separately. $ submodulegraph -m png diff --git a/submodulegraph.py b/submodulegraph.py index 34421d7..0407a1c 100644 --- a/submodulegraph.py +++ b/submodulegraph.py @@ -68,20 +68,23 @@ def parseGitModuleFile(file): res.append((p, u)) return res -def parse(path, url=None): +def parse(path, relpath=None, url=None): + if relpath: + name = os.path.normpath(os.path.relpath(path, start=relpath)) + else: + name = os.path.basename(os.path.normpath(path)) + if os.path.isfile(os.path.join(path, '.gitmodules')) is False: - return Tree({'name': os.path.basename(os.path.normpath(path)), - 'path': path, 'url': url}) + return Tree({'name': name, 'path': path, 'url': url}) - tree = Tree({'name': os.path.basename(os.path.normpath(path)), - 'path': path, 'url': url}) + tree = Tree({'name': name, 'path': path, 'url': url}) moduleFile = os.path.join(path, '.gitmodules') if os.path.isfile(moduleFile) is True: subs = parseGitModuleFile(moduleFile) for p, u in subs: newPath = os.path.join(path, p) - newTree = parse(newPath, u) + newTree = parse(newPath, relpath=relpath, url=u) tree.createChild(newTree) return tree @@ -102,10 +105,17 @@ def parse(path, url=None): default=False, is_flag=True, show_default=True, help="Add repo URLs") +@click.option('-r', '--relative-path', + default=False, is_flag=True, + show_default=True, + help="Show relative path") @click.argument('repo') -def main(mode, repo, graphmode, out, with_url): +def main(mode, repo, graphmode, out, with_url, relative_path): root = repo - tree = parse(root) + if relative_path: + tree = parse(root, relpath=root) + else: + tree = parse(root) if mode == 'text': tree.print(with_url=with_url)