Skip to content
/ nway Public

Algorithms for n-way diffing and merging of files with a common ancestor

License

Notifications You must be signed in to change notification settings

lyxell/nway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

n-way merge with common ancestor

nway

Algorithms to perform n-way diff and merge of files with a common ancestor, based on the 3-way diff approach described in A Formal Investigation of Diff3 by Khanna, Kunal and Pierce and the shortest edit script algorithm described in An O(ND) difference algorithm and its variations by Myers.

Distributed as a single-header C++17 library.

Examples

Merge strings

#include <cassert>
#include <iostream>
#include <string>

#include <nway.h>

using namespace std::string_literals;

int main(int argc, char** argv) {
    auto diff = nway::diff("hello world"s, {"hxxllo world"s,
                                            "hello wyyrld"s,
                                            "hello wyyrld!!"s,
                                            "a hello world"s});
    assert(!nway::has_conflict(diff));
    // prints "a hxxllo wyyrld!!"
    std::cout << nway::merge(diff) << std::endl;
    return 0;
}

Merge files

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

#include <nway.h>

using namespace std::string_literals;

int main(int argc, char** argv) {

    using file = std::vector<std::string>;

    file original = {
        "a",
        "b",
        "c",
        "d",
        "e",
    };

    // deletes third line
    file a = {
        "a",
        "b",
        "d",
        "e",
    };

    // changes fifth line
    file b = {
        "a",
        "b",
        "c",
        "d",
        "x",
    };

    // changes first line
    file c = {
        "x",
        "b",
        "c",
        "d",
        "e",
    };

    auto diff = nway::diff(original, {a, b, c});
    assert(!nway::has_conflict(diff));
    // prints
    // x
    // b
    // d
    // x
    for (auto line : nway::merge(diff)) {
        std::cout << line << std::endl;
    }
    return 0;
}

About

Algorithms for n-way diffing and merging of files with a common ancestor

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages