Bazaar Version Control System

Ignore merge operation for given extension

Asked by WillemD on 2010-03-04

I have text files created by a 3rd party tool that I'd like to keep in bzr. When two guys work on this file, I expect bzr will auto-merge the two files and the 3rd party tool will not function anymore. I can identify these files by some extension (e.g *.3rd)

Is there some way to tell bzr to handle "*.3rd" files like it handles images? Essentially, no merging will take place. I assume the version that was checked in last will be kept.

Question information

Language:
English Edit question
Status:
Solved
For:
Bazaar Edit question
Assignee:
No assignee Edit question
Solved by:
Andrew Bennetts
Solved:
2010-03-05
Last query:
2010-03-05
Last reply:
2010-03-05
Best Andrew Bennetts (spiv) said : #1

> Is there some way to tell bzr to handle "*.3rd" files like it handles images?

To make bzr handle these files the way it would handle images or other binaries is easy to do with a simple plugin:

---- merge_3rd.py ----
"""Custom 'merge' logic for *.3rd files.

Always conflicts if both branches have changed the file.
"""

from bzrlib.merge import AbstractPerFileMerger, Merger

def merge_3rd_files_hook(merger):
    """Hook to merge *.3rd files"""
    return Merge3rdFiles(merger)

class Merge3rdFiles(AbstractPerFileMerger):

    def filename_matches(self, params):
        inventory = self.merger.this_tree.inventory
        filename = inventory[params.file_id].name
        if filename.endswith('.3rd'):
            return filename

    def merge_contents(self, params):
        """Merge the contents of a single file."""
        # First, check whether this custom merge logic should be used. We
        # expect most files should not be merged by this handler.
        if (
            # OTHER is a straight winner, rely on default merge.
            params.winner == 'other' or
            # THIS and OTHER aren't both files.
            not params.is_file_merge() or
            # The filename doesn't match *.3rd
            not self.filename_matches(params)):
            return 'not_applicable', None
        return 'conflicted', params.this_lines

Merger.hooks.install_named_hook(
    'merge_file_content', merge_3rd_files_hook, '*.3rd file merge')
---- END ----

Put that file in ~/.bazaar/plugins or your system-wide bzrlib/plugins directory.

This requires bzr 2.1 or newer.

Martin Pool (mbp) said : #2

https://bugs.edge.launchpad.net/bzr/+bug/532435 asks for better documentation of this

WillemD (willem-duminy) said : #3

Thanks Andrew Bennetts, that solved my question.