]> arthur.barton.de Git - bup.git/commitdiff
Replace 040000 and 0100644 constants with GIT_MODE_{TREE,FILE}
authorAvery Pennarun <apenwarr@gmail.com>
Sun, 20 Feb 2011 02:02:12 +0000 (18:02 -0800)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 20 Feb 2011 05:38:28 +0000 (21:38 -0800)
Those constants were scattered in *way* too many places.  While we're there,
fix the inconsistent usage of strings vs. ints when specifying the file
mode; there's no good reason to be passing strings around (except that I
foolishly did that in the original code in version 0.01).

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
cmd/index-cmd.py
cmd/save-cmd.py
lib/bup/git.py
lib/bup/hashsplit.py
lib/bup/index.py
lib/bup/vfs.py
t/test.sh

index a04355096406c837ef29911387aceb3597f04a03..89854113aa66ddbd62cd50e22fdfa9cb61a5202e 100755 (executable)
@@ -2,7 +2,7 @@
 import sys, stat, time, os
 from bup import options, git, index, drecurse
 from bup.helpers import *
-
+from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
 
 class IterHelper:
     def __init__(self, l):
@@ -57,7 +57,7 @@ def update_index(top, excluded_paths):
     hashgen = None
     if opt.fake_valid:
         def hashgen(name):
-            return (0100644, index.FAKE_SHA)
+            return (GIT_MODE_FILE, index.FAKE_SHA)
 
     total = 0
     bup_dir = os.path.abspath(git.repo())
index 7219ab4b55441293136d74f58bfa99193aef1702..4b8ca121624f06bce7765a3d7eaa66a04e9ac76e 100755 (executable)
@@ -2,6 +2,7 @@
 import sys, stat, time, math
 from bup import hashsplit, git, options, index, client
 from bup.helpers import *
+from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
 
 
 optspec = """
@@ -99,8 +100,9 @@ def _pop(force_tree):
     shalist = shalists.pop()
     tree = force_tree or w.new_tree(shalist)
     if shalists:
-        shalists[-1].append(('40000',
-                             git.mangle_name(part, 040000, 40000),
+        shalists[-1].append((GIT_MODE_TREE,
+                             git.mangle_name(part,
+                                             GIT_MODE_TREE, GIT_MODE_TREE),
                              tree))
     else:  # this was the toplevel, so put it back for sanity
         shalists.append(shalist)
@@ -237,7 +239,7 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
             if lastskip_name and lastskip_name.startswith(ent.name):
                 ent.invalidate()
             else:
-                ent.validate(040000, newtree)
+                ent.validate(GIT_MODE_TREE, newtree)
             ent.repack()
         if exists and wasmissing:
             count += oldsize
@@ -246,9 +248,8 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
     # it's not a directory
     id = None
     if hashvalid:
-        mode = '%o' % ent.gitmode
         id = ent.sha
-        shalists[-1].append((mode, 
+        shalists[-1].append((ent.gitmode, 
                              git.mangle_name(file, ent.mode, ent.gitmode),
                              id))
     else:
@@ -287,7 +288,7 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
                 add_error(Exception('skipping special file "%s"' % ent.name))
                 lastskip_name = ent.name
         if id:
-            ent.validate(int(mode, 8), id)
+            ent.validate(mode, id)
             ent.repack()
             shalists[-1].append((mode,
                                  git.mangle_name(file, ent.mode, ent.gitmode),
index d94de38a3dd800f96979068e37752476c8fc0fdb..3d8b9dab5c9caf29433ea904d61dcb82a3c1dc03 100644 (file)
@@ -129,7 +129,8 @@ def calc_hash(type, content):
 
 def _shalist_sort_key(ent):
     (mode, name, id) = ent
-    if stat.S_ISDIR(int(mode, 8)):
+    assert(mode+0 == mode)
+    if stat.S_ISDIR(mode):
         return name + '/'
     else:
         return name
@@ -141,11 +142,12 @@ def tree_encode(shalist):
     l = []
     for (mode,name,bin) in shalist:
         assert(mode)
-        assert(mode != '0')
-        assert(mode[0] != '0')
+        assert(mode+0 == mode)
         assert(name)
         assert(len(bin) == 20)
-        l.append('%s %s\0%s' % (mode,name,bin))
+        s = '%o %s\0%s' % (mode,name,bin)
+        assert(s[0] != '0')  # 0-padded octal is not acceptable in a git tree
+        l.append(s)
     return ''.join(l)
 
 
@@ -157,9 +159,10 @@ def tree_decode(buf):
         assert(z > 0)
         spl = buf[ofs:ofs+z].split(' ', 1)
         assert(len(spl) == 2)
+        mode,name = spl
         sha = buf[ofs+z+1:ofs+z+1+20]
         ofs += z+1+20
-        yield (spl[0], spl[1], sha)
+        yield (int(mode, 8), name, sha)
 
 
 def _encode_packobj(type, content):
index 1819294d1aa7d2029203316d78f9c300bc75b7c8..2b2163b99899ce03a491fecab72a164b9c249f67 100644 (file)
@@ -8,6 +8,10 @@ MAX_PER_TREE = 256
 progress_callback = None
 fanout = 16
 
+GIT_MODE_FILE = 0100644
+GIT_MODE_TREE = 040000
+assert(GIT_MODE_TREE != 40000)  # 0xxx should be treated as octal
+
 # The purpose of this type of buffer is to avoid copying on peek(), get(),
 # and eat().  We do copy the buffer contents on put(), but that should
 # be ok if we always only put() large amounts of data at a time.
@@ -133,7 +137,7 @@ def _squish(maketree, stacks, n):
         elif stacks[i]:
             (shalist, size) = _make_shalist(stacks[i])
             tree = maketree(shalist)
-            stacks[i+1].append(('40000', tree, size))
+            stacks[i+1].append((GIT_MODE_TREE, tree, size))
         stacks[i] = []
         i += 1
 
@@ -145,12 +149,12 @@ def split_to_shalist(makeblob, maketree, files,
     if not fanout:
         shal = []
         for (sha,size,level) in sl:
-            shal.append(('100644', sha, size))
+            shal.append((GIT_MODE_FILE, sha, size))
         return _make_shalist(shal)[0]
     else:
         stacks = [[]]
         for (sha,size,level) in sl:
-            stacks[0].append(('100644', sha, size))
+            stacks[0].append((GIT_MODE_FILE, sha, size))
             if level:
                 _squish(maketree, stacks, level)
         #log('stacks: %r\n' % [len(i) for i in stacks])
@@ -165,9 +169,9 @@ def split_to_blob_or_tree(makeblob, maketree, files, keep_boundaries):
     if len(shalist) == 1:
         return (shalist[0][0], shalist[0][2])
     elif len(shalist) == 0:
-        return ('100644', makeblob(''))
+        return (GIT_MODE_FILE, makeblob(''))
     else:
-        return ('40000', maketree(shalist))
+        return (GIT_MODE_TREE, maketree(shalist))
 
 
 def open_noatime(name):
index f35fdc87db14c89a118892a05cc898e5e9079402..7483407faf204123449c76c6c4fa87bed2bc65fe 100644 (file)
@@ -136,6 +136,7 @@ class Entry:
     def validate(self, gitmode, sha):
         assert(sha)
         assert(gitmode)
+        assert(gitmode+0 == gitmode)
         self.gitmode = gitmode
         self.sha = sha
         self.flags |= IX_HASHVALID|IX_EXISTS
index 0a17918e878410d8701c8b3da4082e1b9cc3d96a..454355337c4b06bde89f231657be66647a7fea14 100644 (file)
@@ -6,6 +6,7 @@ and abstracts internal name mangling and storage from the exposition layer.
 import os, re, stat, time
 from bup import git
 from helpers import *
+from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
 
 EMPTY_SHA='\0'*20
 
@@ -46,7 +47,7 @@ def _treeget(hash):
 
 
 def _tree_decode(hash):
-    tree = [(int(name,16),stat.S_ISDIR(int(mode,8)),sha)
+    tree = [(int(name,16),stat.S_ISDIR(mode),sha)
             for (mode,name,sha)
             in _treeget(hash)]
     assert(tree == list(sorted(tree)))
@@ -384,11 +385,10 @@ class Dir(Node):
             type = it.next()
         assert(type == 'tree')
         for (mode,mangled_name,sha) in git.tree_decode(''.join(it)):
-            mode = int(mode, 8)
             name = mangled_name
             (name,bupmode) = git.demangle_name(mangled_name)
             if bupmode == git.BUP_CHUNKED:
-                mode = 0100644
+                mode = GIT_MODE_FILE
             if stat.S_ISDIR(mode):
                 self._subs[name] = Dir(self, name, mode, sha)
             elif stat.S_ISLNK(mode):
@@ -408,7 +408,7 @@ class CommitDir(Node):
     the number of commits grows big.
     """
     def __init__(self, parent, name):
-        Node.__init__(self, parent, name, 040000, EMPTY_SHA)
+        Node.__init__(self, parent, name, GIT_MODE_TREE, EMPTY_SHA)
 
     def _mksubs(self):
         self._subs = {}
@@ -436,13 +436,13 @@ class CommitDir(Node):
 class CommitList(Node):
     """A list of commits with hashes that start with the current node's name."""
     def __init__(self, parent, name):
-        Node.__init__(self, parent, name, 040000, EMPTY_SHA)
+        Node.__init__(self, parent, name, GIT_MODE_TREE, EMPTY_SHA)
         self.commits = {}
 
     def _mksubs(self):
         self._subs = {}
         for (name, (hash, date)) in self.commits.items():
-            n1 = Dir(self, name, 040000, hash)
+            n1 = Dir(self, name, GIT_MODE_TREE, hash)
             n1.ctime = n1.mtime = date
             self._subs[name] = n1
 
@@ -450,7 +450,7 @@ class CommitList(Node):
 class TagDir(Node):
     """A directory that contains all tags in the repository."""
     def __init__(self, parent, name):
-        Node.__init__(self, parent, name, 040000, EMPTY_SHA)
+        Node.__init__(self, parent, name, GIT_MODE_TREE, EMPTY_SHA)
 
     def _mksubs(self):
         self._subs = {}
@@ -472,7 +472,7 @@ class BranchList(Node):
     /.commit/??/ . The symlink is named after the commit date.
     """
     def __init__(self, parent, name, hash):
-        Node.__init__(self, parent, name, 040000, hash)
+        Node.__init__(self, parent, name, GIT_MODE_TREE, hash)
 
     def _mksubs(self):
         self._subs = {}
@@ -514,7 +514,7 @@ class RefList(Node):
     that are reachable via a ref (e.g. a branch).  See CommitDir for details.
     """
     def __init__(self, parent):
-        Node.__init__(self, parent, '/', 040000, EMPTY_SHA)
+        Node.__init__(self, parent, '/', GIT_MODE_TREE, EMPTY_SHA)
 
     def _mksubs(self):
         self._subs = {}
index 9b5e55aadc6c9f3ef85a0710e45e6e70ee0b1b76..6097143025b3711c497e74d7e7ff84a327d28f18 100755 (executable)
--- a/t/test.sh
+++ b/t/test.sh
@@ -105,6 +105,10 @@ d/
 a
 ./"
 WVPASS bup save -t $D/d
+WVPASS bup index --fake-invalid $D/d/z
+WVPASS bup save -t $D/d/z
+WVPASS bup save -t $D/d/z  # test regenerating trees when no files are changed
+WVPASS bup save -t $D/d
 WVPASSEQ "$(cd $D && bup index -m)" \
 "f
 a