diff --git a/CONTRIBUTORS b/CONTRIBUTORS --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,3 +1,4 @@ +Antoine Cezar Daniele Serafini Ishan Bhanuka -Antoine Cezar +Kumar Shivendu diff --git a/swh/model/cli.py b/swh/model/cli.py --- a/swh/model/cli.py +++ b/swh/model/cli.py @@ -3,7 +3,9 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from enum import Enum import os +from pathlib import Path import sys from typing import Dict, List, Optional @@ -170,6 +172,16 @@ return swhid +class Color(Enum): + blue = "\033[94m" + green = "\033[92m" + end = "\033[0m" + + +def colorize(text, color): + return color.value + text + color.end.value + + @swh_cli_group.command(context_settings=CONTEXT_SETTINGS) @click.option( "--dereference/--no-dereference", @@ -184,6 +196,9 @@ default=True, help="show/hide file name (default: show)", ) +@click.option( + "--recursive", "-r", is_flag=True, help="Compute SWHID recursively", +) @click.option( "--type", "-t", @@ -210,7 +225,13 @@ ) @click.argument("objects", nargs=-1, required=True) def identify( - obj_type, verify, show_filename, follow_symlinks, objects, exclude_patterns, + obj_type, + verify, + show_filename, + recursive, + follow_symlinks, + objects, + exclude_patterns, ): """Compute the Software Heritage persistent identifier (SWHID) for the given source code object(s). @@ -232,8 +253,8 @@ swh:1:cnt:57b939c81bce5d06fa587df8915f05affbe22b82 sched/deadline.c \b - $ swh identify --no-filename /usr/src/linux/kernel/ - swh:1:dir:f9f858a48d663b3809c9e2f336412717496202ab + $ swh identify --R --no-filename /usr/src/linux/kernel/ + swh:1:dir: \b $ git clone --mirror https://forge.softwareheritage.org/source/helloworld.git @@ -246,14 +267,31 @@ if verify and len(objects) != 1: raise click.BadParameter("verification requires a single object") - results = zip( - objects, - map( - partial(identify_object, obj_type, follow_symlinks, exclude_patterns), - objects, - ), + partial_identify_obj = partial( + identify_object, obj_type, follow_symlinks, exclude_patterns ) + if recursive: + + def out(path: Path): + is_dir = path.is_dir() + colored_path = colorize(path.name, Color.blue if is_dir else Color.green) + swhid = partial_identify_obj(str(path)) + return f"{colored_path} {swhid}" + + for obj in list(objects): + obj = Path(obj) + for root, _, files in os.walk(str(obj)): + root = Path(root) + rel_depth = len(str(root.relative_to(obj)).split(os.sep)) + click.echo(f"{(rel_depth - 1) * ' │ '} {out(root)}") + for filename in files: + filepath = Path(os.path.join(root, filename)) + click.echo(f"{(rel_depth) * ' │ '} {out(filepath)}") + return + + results = zip(objects, map(partial_identify_obj, objects)) + if verify: swhid = next(results)[1] if str(verify) == swhid: