Skip to content

Instantly share code, notes, and snippets.

@mwhooker
Created August 2, 2011 02:43
Show Gist options
  • Save mwhooker/1119495 to your computer and use it in GitHub Desktop.
Save mwhooker/1119495 to your computer and use it in GitHub Desktop.
print timestamp of last revision for each file in bzr branch
"""Takes a list of files under bazaar version control and lists path and
timestamp of last commit.
Usage: bzr_tip.py $(find . -name "*.py") | sort -n
line output format is <timestamp> <file>@<last revision>
"""
import sys
import itertools
from contextlib import contextmanager
from bzrlib.branch import BzrBranch
from bzrlib.log import show_log, LogFormatter, make_log_rev_iterator
@contextmanager
def branch_lock_read(branch):
branch.lock_read()
try:
yield
finally:
branch.unlock()
class LogCollector(LogFormatter):
def __init__(self, *args, **kwargs):
self.curr_file = None
super(LogCollector, self).__init__(*args, **kwargs)
def log_revision(self, revision):
output = "%s %s@%s\n" % (
revision.rev.timestamp,
self.curr_file,
revision.revno
)
self.to_file.write(output)
my_branch = BzrBranch.open('.')
tree = my_branch.basis_tree()
lf = LogCollector(sys.stdout)
ids = itertools.imap(lambda path: tree.path2id(path), sys.argv[1:])
ids = itertools.ifilter(lambda id: id, ids)
with branch_lock_read(my_branch):
for (path, entry) in tree.iter_entries_by_dir(ids):
lf.curr_file = path
show_log(my_branch, lf, specific_fileid=entry.file_id, limit=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment