Skip to content

Commit

Permalink
Replace 040000 and 0100644 constants with GIT_MODE_{TREE,FILE}
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
apenwarr committed Feb 20, 2011
1 parent 252c21d commit 48c6f48
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cmd/index-cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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())
Expand Down
13 changes: 7 additions & 6 deletions cmd/save-cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -237,7 +239,7 @@ def wantrecurse_during(ent):
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
Expand All @@ -246,9 +248,8 @@ def wantrecurse_during(ent):
# 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:
Expand Down Expand Up @@ -287,7 +288,7 @@ def wantrecurse_during(ent):
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),
Expand Down
13 changes: 8 additions & 5 deletions lib/bup/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)


Expand All @@ -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):
Expand Down
14 changes: 9 additions & 5 deletions lib/bup/hashsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
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.
Expand Down Expand Up @@ -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

Expand All @@ -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])
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions lib/bup/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def invalidate(self):
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
Expand Down
18 changes: 9 additions & 9 deletions lib/bup/vfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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

Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -384,11 +385,10 @@ def _mksubs(self):
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):
Expand All @@ -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 = {}
Expand Down Expand Up @@ -436,21 +436,21 @@ def _mksubs(self):
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


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 = {}
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand Down
4 changes: 4 additions & 0 deletions t/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 48c6f48

Please sign in to comment.