]> arthur.barton.de Git - bup.git/commitdiff
ls: add --commit-hash and drop vfs nominal_oid
authorRob Browning <rlb@defaultvalue.org>
Sat, 9 Dec 2017 00:21:21 +0000 (18:21 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 17 Dec 2017 02:05:47 +0000 (20:05 -0600)
Report the tree hash for commits for ls -s, unless --commit-hash is
also specified, and add some initial tests.  This brings -s in line
with 38a49e9eb930e8a47e9de5a9715fad659a774e8b.

Report 0000000000000000000000000000000000000000 for items without a
hash so that the number of output fields doesn't vary in that case.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/ls.py
lib/bup/vfs2.py
t/test-ls.sh

index 0b5fb90426fdaca30753fb88361f8e8b55c2eea7..4c4d614d37bceaaa5f0b2d69e02895dbd3b077d0 100644 (file)
@@ -9,9 +9,18 @@ from bup import metadata, options, vfs2 as vfs
 from bup.repo import LocalRepo
 from helpers import columnate, istty1, last, log
 
+def item_hash(item, tree_for_commit):
+    """If the item is a Commit, return its commit oid, otherwise return
+    the item's oid, if it has one.
+
+    """
+    if tree_for_commit and isinstance(item, vfs.Commit):
+        return item.coid
+    return getattr(item, 'oid', None)
 
 def item_info(item, name,
               show_hash = False,
+              commit_hash=False,
               long_fmt = False,
               classification = None,
               numeric_ids = False,
@@ -21,8 +30,10 @@ def item_info(item, name,
 
     """
     result = ''
-    if show_hash and hasattr(item, 'oid'):
-        result += '%s ' % item.oid.encode('hex')
+    if show_hash:
+        oid = item_hash(item, commit_hash)
+        result += '%s ' % (oid.encode('hex') if oid
+                           else '0000000000000000000000000000000000000000')
     if long_fmt:
         meta = item.meta.copy()
         meta.path = name
@@ -43,6 +54,7 @@ optspec = """
 %sls [-a] [path...]
 --
 s,hash   show hash for each file
+commit-hash show commit hash instead of tree for commits (implies -s)
 a,all    show hidden files
 A,almost-all    show hidden files except . and ..
 l        use a detailed, long listing format
@@ -82,9 +94,13 @@ def do_ls(args, default='.', onabort=None, spec_prefix=''):
         elif option in ('-A', '--almost-all'):
             show_hidden = 'almost'
 
+    if opt.commit_hash:
+        opt.hash = True
+
     def item_line(item, name):
         return item_info(item, name,
                          show_hash = opt.hash,
+                         commit_hash=opt.commit_hash,
                          long_fmt = opt.l,
                          classification = classification,
                          numeric_ids = opt.numeric_ids,
index dfc747a4db7f6dbf858cbc3b984eec05823850de..5fbcc6a43a0cbcc26a03cbcdbbe8ea0a785c25b6 100644 (file)
@@ -40,9 +40,8 @@ fill_in_metadata_if_dir() or ensure_item_has_metadata()).
 Commit items represent commits (e.g. /.tag/some-commit or
 /foo/latest), and for most purposes, they appear as the underlying
 tree.  S_ISDIR(item_mode(item)) will return true for both tree Items
-and Commits and the commit's oid is the tree hash.  The commit hash
-will be item.coid, and nominal_oid(item) will return coid for commits,
-oid for everything else.
+and Commits and the commit's oid is the tree hash; the commit hash is
+item.coid.
 
 """
 
@@ -231,15 +230,6 @@ _root = Root(meta=default_dir_mode)
 _tags = Tags(meta=default_dir_mode)
 
 
-def nominal_oid(item):
-    """If the item is a Commit, return its commit oid, otherwise return
-    the item's oid, if it has one.
-
-    """
-    if isinstance(item, Commit):
-        return item.coid
-    return getattr(item, 'oid', None)
-
 def copy_item(item):
     """Return a completely independent copy of item, such that
     modifications will not affect the original.
index 8b87778c4f2cc55043e81dcca5ebea5b1d6edca2..88c1759476aecc9ada185c01694f86df1081fa88 100755 (executable)
@@ -32,6 +32,9 @@ WVPASS bup index src
 WVPASS bup save -n src -d 242312160 --strip src
 WVPASS bup tag some-tag src
 
+src_commit_hash=$(git log --format=%H -n1 src)
+src_tree_hash=$(git log --format=%T -n1 src)
+
 
 WVSTART "ls (short)"
 
@@ -205,6 +208,12 @@ WVPASSEQ "$(bup ls -l --numeric-ids src | cut -d' ' -f 1-2)" \
 "drwxr-xr-x 0/0
 drwxr-xr-x 0/0"
 
+WVPASSEQ "$(bup ls -ds "src/latest" | tr -s ' ' ' ')" \
+"$src_tree_hash src/latest"
+
+WVPASSEQ "$(bup ls -ds --commit-hash "src/latest" | tr -s ' ' ' ')" \
+"$src_commit_hash src/latest"
+
 
 WVSTART "ls (dates TZ != UTC)"
 export TZ=America/Chicago