Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
Created June 21, 2012 04:54
Show Gist options
  • Save wonderful-panda/2963903 to your computer and use it in GitHub Desktop.
Save wonderful-panda/2963903 to your computer and use it in GitHub Desktop.
bzr delta
from bzrlib.commands import plugin_cmds
plugin_cmds.register_lazy('cmd_delta', [],
'bzrlib.plugins.delta.delta')
bzrのプラグインフォルダにdeltaというフォルダを作り、上の2ファイルを置く
で、
$ bzr delta ブランチのパス -r -3..-1 --output 出力先
とか
import os, errno
from bzrlib import osutils, errors
from bzrlib.commands import Command
from bzrlib.option import (Option, ListOption)
class cmd_delta(Command):
"""
Export delta between 2 revisions
"""
takes_args = ["location?"]
takes_options = [
'verbose', 'revision',
Option('output', type=str, short_name='o', help='output directory'),
]
def run(self, location=None, revision=None, output=None, verbose=None):
if not output:
raise errors.BzrError("output does not specified")
if os.path.exists(output):
raise errors.BzrError("%s is already exists" % output)
self.location = location or "."
self.output = output
from bzrlib.diff import get_trees_and_branches_to_diff_locked
cleanup = []
try:
old_tree, new_tree = \
get_trees_and_branches_to_diff_locked(
None, revision, location, location, cleanup.append)[:2]
changes = new_tree.iter_changes(old_tree, require_versioned=True)
for (file_id, paths, _, versioned, _, name, kind, _) in changes:
if versioned[1] and kind[1] == 'file':
self.write_file(file_id, new_tree, paths[1])
finally:
while cleanup:
cleanup.pop()()
def write_file(self, file_id, tree, relpath):
full_path = osutils.pathjoin(self.output, relpath)
if self.outf:
self.outf.write("writing %s\n" % full_path)
parent_dir = osutils.dirname(full_path)
try:
os.makedirs(parent_dir)
except OSError, e:
if e.errno != errno.EEXIST:
raise
source = tree.get_file(file_id, relpath)
try:
target = open(full_path, 'wb')
try:
osutils.pumpfile(source, target)
finally:
target.close()
finally:
source.close()
try:
mtime = tree.get_file_mtime(file_id)
except errors.FileTimestampUnavailable:
pass
else:
os.utime(full_path, (mtime, mtime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment