From 92e240e868d4877287b162b4115ea449c552daba Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Wed, 3 Oct 2018 10:50:50 +0200
Subject: [PATCH] bin: Migrate swh-hashtree to swh-model

This is a temporary move.  This will eventually converge with
swh.model.cli as the directory hashing ability overlaps.  What's
missing in the cli for now is the ability to filter patterns.

This is mostly used to compute hashes on tree coming from multiple
dvcs. This imposes the need to be able to filter pattern like .svn,
.hg, etc...
---
 bin/swh-hashtree | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100755 bin/swh-hashtree

diff --git a/bin/swh-hashtree b/bin/swh-hashtree
new file mode 100755
index 00000000..faf258fd
--- /dev/null
+++ b/bin/swh-hashtree
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+# Use sample:
+# swh-hashtree --path . --ignore '.svn' --ignore '.git-svn' \
+#    --ignore-empty-folders
+# 38f8d2c3a951f6b94007896d0981077e48bbd702
+
+import click
+import os
+
+from swh.model import from_disk, hashutil
+
+
+def combine_filters(*filters):
+    """Combine several ignore filters"""
+    if len(filters) == 0:
+        return from_disk.accept_all_directories
+    elif len(filters) == 1:
+        return filters[0]
+
+    def combined_filter(*args, **kwargs):
+        return all(filter(*args, **kwargs) for filter in filters)
+
+    return combined_filter
+
+
+@click.command()
+@click.option('--path', default='.',
+              help='Optional path to hash.')
+@click.option('--ignore-empty-folder', is_flag=True, default=False,
+              help='Ignore empty folder.')
+@click.option('--ignore', multiple=True,
+              help='Ignore pattern.')
+def main(path, ignore_empty_folder=False, ignore=None):
+
+    filters = []
+    if ignore_empty_folder:
+        filters.append(from_disk.ignore_empty_directories)
+    if ignore:
+        filters.append(
+            from_disk.ignore_named_directories(
+                [os.fsencode(name) for name in ignore]
+            )
+        )
+
+    try:
+        d = from_disk.Directory.from_disk(path=os.fsencode(path),
+                                          dir_filter=combine_filters(*filters))
+        hash = d.hash
+    except Exception as e:
+        print(e)
+        return
+    else:
+        print(hashutil.hash_to_hex(hash))
+
+
+if __name__ == '__main__':
+    main()
-- 
GitLab