summaryrefslogtreecommitdiffstats
path: root/src/merge.c
blob: 059523ec2590db1bc123a97565d6ad2826eb598f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "md5util.h"
#include "diff.h"
#include "dbutil.h"
#include "package.h"
#include "util.h"

struct cb_info {
    struct syrep_db_context *c1;
    struct syrep_db_context *c2;
    const char *root;
};

static int conflict_phase(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
    struct cb_info *cb_info = p;
    struct syrep_md md1, md2;
    int f1, f2;
    char path[PATH_MAX+1];

    assert(ddb && name && de && p);

    if (de->action != DIFF_CONFLICT)
        return 0;

    if ((f1 = get_current_md_by_name(cb_info->c1, name, &md1)) < 0)
        return -1;

    if ((f2 = get_current_md_by_name(cb_info->c2, name, &md2)) < 0)
        return -1;
    
    snprintf(path, sizeof(path), "%s/%s", cb_info->root, name->path);

    if (f1 && f2) {
        char d1[SYREP_DIGESTLENGTH*2+1], d2[SYREP_DIGESTLENGTH*2+1];
        
        fhex(md1.digest, SYREP_DIGESTLENGTH, d1);
        fhex(md2.digest, SYREP_DIGESTLENGTH, d2);
        d1[SYREP_DIGESTLENGTH*2] = d2[SYREP_DIGESTLENGTH*2] = 0;
        
        fprintf(stderr, "CONFLICT: New file '%s' apparead: %s in A vs. %s in B\n", path, d1, d2);
    } else if (f1 || f2) {
        char d[SYREP_DIGESTLENGTH*2+1];
        fhex(f1 ? md1.digest : md2.digest, SYREP_DIGESTLENGTH, d);
        d[SYREP_DIGESTLENGTH*2] = 0;

        fprintf(stderr, "CONFLICT: File '%s' (%s) appeared in %c, removed in %c\n", path, d, f1 ? 'A' : 'B', f2 ? 'A' : 'B');
    }
    
    return 0;
}

static int copy_phase(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
    struct cb_info *cb_info = p;
    struct syrep_name name2;
    struct syrep_md md;
    char path[PATH_MAX+1];
    int f;
    char d[SYREP_DIGESTLENGTH*2+1];

    assert(ddb && name && de && p);

    if (de->action != DIFF_COPY)
        return 0;

    if (de->repository == cb_info->c2)
        return 0;

    snprintf(path, sizeof(path), "%s/%s", cb_info->root, name->path);

    if ((f = get_current_md_by_name(cb_info->c1, name, &md)) < 0)
        return -1;

    if (!f) {
        fprintf(stderr, "Diff invalid!\n");
        return -1;
    }

    fhex(md.digest, SYREP_DIGESTLENGTH, d);
    d[SYREP_DIGESTLENGTH*2] = 0;

    if ((f = get_current_name_by_md(cb_info->c2, &md, &name2)) < 0)
        return -1;
    
    if (f) {
        char path2[PATH_MAX+1];
        snprintf(path2, sizeof(path2), "%s/%s", cb_info->root, name2.path);
        fprintf(stderr, "COPY: Linking existing file <%s> to <%s>.\n", path2, path);

        if (copy_or_link_file(path2, path) < 0)
            return -1;
        
    } else {
        const char* a;

        if ((a = package_get_item(cb_info->c1->package, d, 0))) {
            fprintf(stderr, "COPY: Copying file <%s> from patch.\n", path);
            
            if (copy_or_link_file(a, path) < 0)
                return -1;
        } else
            fprintf(stderr, "COPY: File <%s> is missing.\n", path);
    }

    return 0;
}

static int delete_phase(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
    struct cb_info *cb_info = p;
    char path[PATH_MAX+1];

    assert(ddb && name && de && p);
    
    if (de->action != DIFF_DELETE)
        return 0;

    if (de->repository == cb_info->c1)
        return 0;

    snprintf(path, sizeof(path), "%s/%s", cb_info->root, name->path);

    fprintf(stderr, "DELETE: Deleting file <%s>\n", path);

    if (unlink(path) < 0) {
        fprintf(stderr, "unlink(%s): %s\n", path, strerror(errno));
        return -1;
    }
    
    return 0;
}

/* Merges c1 into c2 in directory "root" */
int merge(struct syrep_db_context *c1, struct syrep_db_context *c2, const char* root) {
    struct cb_info cb_info;
    DB *ddb = NULL;
    int r = -1;

    memset(&cb_info, 0, sizeof(cb_info));
    cb_info.c1 = c1;
    cb_info.c2 = c2;
    cb_info.root = root;

    if (!(ddb = make_diff(c1, c2)))
        goto finish;

    if (diff_foreach(ddb, conflict_phase, &cb_info) < 0)
        goto finish;

    if (diff_foreach(ddb, copy_phase, &cb_info) < 0)
        goto finish;

    if (diff_foreach(ddb, delete_phase, &cb_info) < 0)
        goto finish;

    r = 0;
    
finish:
    if (ddb)
        ddb->close(ddb, 0);
    
    return r;
}