From f53989093669e5dadf88a9856bf469b2eae456d3 Mon Sep 17 00:00:00 2001
From: Stefano Zacchiroli <zack@upsilon.cc>
Date: Thu, 12 Jul 2018 15:00:24 +0200
Subject: [PATCH] swh-identify: show filename in output (by default)

with explicit option to hide it

Closes T1133
---
 swh/model/cli.py            | 11 +++++++++--
 swh/model/tests/test_cli.py | 34 ++++++++++++++++++++++++++++++++--
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/swh/model/cli.py b/swh/model/cli.py
index 5996d19a..6df219e6 100644
--- a/swh/model/cli.py
+++ b/swh/model/cli.py
@@ -4,6 +4,7 @@
 # See top-level LICENSE file for more information
 
 import click
+import locale
 import os
 import sys
 
@@ -39,10 +40,12 @@ def pid_of_dir(path):
               help='type of object to identify (default: auto)')
 @click.option('--verify', '-v', metavar='PID', type=PidParamType(),
               help='reference identifier to be compared with computed one')
+@click.option('--filename/--no-filename', 'show_filename', default=True,
+              help='show/hide file name (default: show)')
 @click.argument('object',
                 type=click.Path(exists=True, readable=True,
                                 allow_dash=True, path_type=bytes))
-def identify(type, verify, object):
+def identify(type, verify, show_filename, object):
     """Compute the Software Heritage persistent identifier (PID) for a given
     source code object.
 
@@ -88,7 +91,11 @@ def identify(type, verify, object):
             click.echo('PID mismatch: %s != %s' % (verify, pid))
             sys.exit(1)
     else:
-        click.echo(pid)
+        msg = pid
+        if show_filename:
+            encoding = locale.getpreferredencoding(do_setlocale=False)
+            msg = '%s\t%s' % (pid, object.decode(encoding))
+        click.echo(msg)
 
 
 if __name__ == '__main__':
diff --git a/swh/model/tests/test_cli.py b/swh/model/tests/test_cli.py
index 054cc0c7..d8e68b9e 100644
--- a/swh/model/tests/test_cli.py
+++ b/swh/model/tests/test_cli.py
@@ -23,6 +23,7 @@ class TestIdentify(DataMixin, unittest.TestCase):
         self.runner = CliRunner()
 
     def test_content_id(self):
+        """identify file content"""
         self.make_contents(self.tmpdir_name)
         for filename, content in self.contents.items():
             path = os.path.join(self.tmpdir_name, filename)
@@ -30,20 +31,48 @@ class TestIdentify(DataMixin, unittest.TestCase):
                                         ['--type', 'content', path])
 
             self.assertEqual(result.exit_code, 0)
-            self.assertEqual(result.output.rstrip(),
+            self.assertEqual(result.output.split()[0],
                              'swh:1:cnt:' + hash_to_hex(content['sha1_git']))
 
     def test_directory_id(self):
+        """identify an entire directory"""
         self.make_from_tarball(self.tmpdir_name)
         path = os.path.join(self.tmpdir_name, b'sample-folder')
         result = self.runner.invoke(cli.identify,
                                     ['--type', 'directory', path])
 
         self.assertEqual(result.exit_code, 0)
-        self.assertEqual(result.output.rstrip(),
+        self.assertEqual(result.output.split()[0],
                          'swh:1:dir:e8b0f1466af8608c8a3fb9879db172b887e80759')
 
+    def test_show_filename(self):
+        """filename is shown by default"""
+        self.make_contents(self.tmpdir_name)
+        for filename, content in self.contents.items():
+            path = os.path.join(self.tmpdir_name, filename)
+            result = self.runner.invoke(cli.identify,
+                                        ['--type', 'content', path])
+
+            self.assertEqual(result.exit_code, 0)
+            self.assertEqual(result.output.rstrip(),
+                             'swh:1:cnt:%s\t%s' %
+                             (hash_to_hex(content['sha1_git']), path.decode()))
+
+    def test_hide_filename(self):
+        """filename is hidden upon request"""
+        self.make_contents(self.tmpdir_name)
+        for filename, content in self.contents.items():
+            path = os.path.join(self.tmpdir_name, filename)
+            result = self.runner.invoke(cli.identify,
+                                        ['--type', 'content', '--no-filename',
+                                         path])
+
+            self.assertEqual(result.exit_code, 0)
+            self.assertEqual(result.output.rstrip(),
+                             'swh:1:cnt:' + hash_to_hex(content['sha1_git']))
+
     def test_auto_id(self):
+        """automatic object type: file or directory, depending on argument"""
         with tempfile.NamedTemporaryFile(prefix='swh.model.cli') as f:
             result = self.runner.invoke(cli.identify, [f.name])
             self.assertEqual(result.exit_code, 0)
@@ -55,6 +84,7 @@ class TestIdentify(DataMixin, unittest.TestCase):
             self.assertRegex(result.output, r'^swh:\d+:dir:')
 
     def test_verify_content(self):
+        """identifier verification"""
         self.make_contents(self.tmpdir_name)
         for filename, content in self.contents.items():
             expected_id = 'swh:1:cnt:' + hash_to_hex(content['sha1_git'])
-- 
GitLab