Minecraft wiki entry for Anvil_mechanics, associated text: The target is repaired, adding the durability of the sacrifice plus a bonus of 12% of the maximum durability, up to the item's maximum durability. If the target item is undamaged, there is no charge for repair, otherwise, the cost is 2 levels.
If the sacrifice has enchantments, it also tries to combine the sacrifice's enchantments onto the target. Regardless of whether any enchantments on the target are actually changed, the cost is based on the total enchantments on the target and sacrifice. For each enchantment on the sacrifice:
If the target has the enchantment as well...
and the sacrifice level is greater, the target is raised to the sacrifice's level.
and the sacrifice level is equal, the target gains one level, unless it is already at the maximum level for that enchantment.
and the sacrifice level is less, nothing changes on the target.
If the target does not have the enchantment, it gains all levels of that enchantment, unless it already has an incompatible enchantment. Enchantments are incompatible if both are in one of the following groups:
Sword: Sharpness, Smite, and Bane of Arthropods
Tool: Fortune and Silk Touch (as of Java version 1.12.2 you can combine these; the sacrifice item's enchantment is lost)
Armor: Protection, Fire Protection, Blast Protection, Projectile Protection
Boots: Depth Strider and Frost Walker
Bow: Infinity and Mending
Crossbow: Multishot and Piercing
Trident: Loyalty and Riptide or Channeling and Riptide
Books: Silk Touch and Looting or Silk Touch and Luck of the Sea (and all of the above).