read-tree: move merge functions to the library