Skip to content

Commit

Permalink
tag-cmd: Some fixups
Browse files Browse the repository at this point in the history
* Make some error messages nicer in telling the tag name that was used.

* Move tag listing code in git.py and use this code in tag-cmd and vfs.py

* Make tag-cmd output the list of tags on stdout instead of stderr

* Don't error out when more than 2 arguments are given. When there are less
  than 2, be nice and show the usage.

* In tag-cmd, catch GitErrors that come out of git.rev_parse()

Signed-off-by: Gabriel Filion <lelutin@gmail.com>
  • Loading branch information
Gabriel Filion authored and apenwarr committed Jan 3, 2011
1 parent 376d069 commit 891e5f3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
30 changes: 16 additions & 14 deletions cmd/tag-cmd.py
Expand Up @@ -35,33 +35,35 @@
try:
os.unlink(tag_file)
except OSError, e:
log("bup: error: unable to delete tag: %s" % e)
log("bup: error: unable to delete tag '%s': %s" % (opt.delete, e))
sys.exit(1)

sys.exit(0)

tags = []
for (t, dummy) in git.list_refs():
if t.startswith('refs/tags/'):
tags.append(t[10:])
tags = [t for sublist in git.tags().values() for t in sublist]

if not extra:
for t in tags:
log("%s\n" % t)
print t
sys.exit(0)
elif len(extra) != 2:
log('bup: error: no ref or hash given.')
sys.exit(1)
elif len(extra) < 2:
o.fatal('no commit ref or hash given.')

tag_name = extra[0]
commit = extra[1]
debug1("from args: tag name = %s; commit = %s\n" % (tag_name, commit))
(tag_name, commit) = extra[:2]
if not tag_name:
o.fatal("tag name must not be empty.")
debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))

if tag_name in tags:
log("bup: error: tag '%s' already exists" % tag_name)
sys.exit(1)

hash = git.rev_parse(commit)
try:
hash = git.rev_parse(commit)
except git.GitError, e:
log("bup: error: %s" % e)
sys.exit(2)

if not hash:
log("bup: error: commit %s not found." % commit)
sys.exit(2)
Expand All @@ -75,7 +77,7 @@
try:
tag = file(tag_file, 'w')
except OSError, e:
log('bup: error: could not create tag %s: %s' % (tag_name, e))
log("bup: error: could not create tag '%s': %s" % (tag_name, e))
sys.exit(3)

tag.write(hash.encode('hex'))
Expand Down
15 changes: 14 additions & 1 deletion lib/bup/git.py
Expand Up @@ -140,7 +140,7 @@ def _decode_packobj(buf):
class PackIdx:
def __init__(self):
assert(0)

def find_offset(self, hash):
"""Get the offset of an object inside the index file."""
idx = self._idx_from_hash(hash)
Expand Down Expand Up @@ -1031,3 +1031,16 @@ def join(self, id):
yield d
except StopIteration:
log('booger!\n')

def tags():
"""Return a dictionary of all tags in the form {hash: [tag_names, ...]}."""
tags = {}
for (n,c) in list_refs():
if n.startswith('refs/tags/'):
name = n[10:]
if not c in tags:
tags[c] = []

tags[c].append(name) # more than one tag can point at 'c'

return tags
9 changes: 1 addition & 8 deletions lib/bup/vfs.py
Expand Up @@ -477,14 +477,7 @@ def __init__(self, parent, name, hash):
def _mksubs(self):
self._subs = {}

tags = {}
for (n,c) in git.list_refs():
if n.startswith('refs/tags/'):
name = n[10:]
if not c in tags:
tags[c] = []

tags[c].append(name)
tags = git.tags()

revs = list(git.rev_list(self.hash.encode('hex')))
for (date, commit) in revs:
Expand Down
4 changes: 2 additions & 2 deletions t/test.sh
Expand Up @@ -208,9 +208,9 @@ WVPASSEQ "$(sha1sum <$D/a)" "$(sha1sum <$D/a.new)"
WVSTART "tag"
WVFAIL bup tag -d v0.n 2>/dev/null
WVFAIL bup tag v0.n non-existant 2>/dev/null
WVPASSEQ "$(bup tag 2>&1)" ""
WVPASSEQ "$(bup tag)" ""
WVPASS bup tag v0.1 master
WVPASSEQ "$(bup tag 2>&1)" "v0.1"
WVPASSEQ "$(bup tag)" "v0.1"
WVPASS bup tag -d v0.1

# This section destroys data in the bup repository, so it is done last.
Expand Down

0 comments on commit 891e5f3

Please sign in to comment.