-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchangeset.c
177 lines (138 loc) · 4.83 KB
/
changeset.c
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
168
169
170
171
172
173
174
175
176
177
#include "changeset.h"
#include "database.h"
#include "emission.h"
#include "file.h"
#include "string_cache.h"
#include "utils.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int fuzz_span = 300;
int fuzz_gap = 300;
void changeset_init (changeset_t * cs)
{
cs->ready_index = SIZE_MAX;
cs->mark = 0;
cs->unready_count = 0;
cs->children = NULL;
cs->children_end = NULL;
cs->versions = NULL;
cs->versions_end = NULL;
cs->merge = NULL;
cs->merge_end = NULL;
}
static bool strings_match (const version_t * A, const version_t * B)
{
// We used to compare the CVS commitid here also. However, a user reported
// that they were seeing commits being broken up unnecessarily, and removing
// the commitid improved things.
return A->author == B->author
&& A->branch == B->branch
&& A->log == B->log
&& A->implicit_merge == B->implicit_merge;
}
static int version_compare (const version_t * A, version_t * B)
{
int r = cache_strcmp (A->author, B->author);
if (r != 0)
return r;
r = cache_strcmp (A->branch->tag, B->branch->tag);
if (r != 0)
return r;
if (A->implicit_merge != B->implicit_merge)
return B->implicit_merge - A->implicit_merge;
unsigned long Alh = string_hash_get (A->log);
unsigned long Blh = string_hash_get (B->log);
if (Alh != Blh)
return Alh < Blh ? -1 : 1;
r = cache_strcmp (A->log, B->log);
if (r != 0)
return r;
if (A->time != B->time)
return A->time < B->time ? -1 : 1;
if (A->file != B->file)
return A->file < B->file ? -1 : 1; // Files are sorted by now.
if (A == B)
return 0;
return A < B ? -1 : 1; // Versions are sorted by now.
}
static int version_compare_qsort (const void * AA, const void * BB)
{
return version_compare (* (version_t * const *) AA,
* (version_t * const *) BB);
}
static int version_compare_heap (const void * AA, const void * BB)
{
const version_t * A = AA;
const version_t * B = BB;
if (A->time != B->time)
return A->time > B->time;
if (A->file != B->file)
return A->file > B->file;
return strcmp (A->version, B->version);
}
static int compare_version_by_file (const void * AA, const void * BB)
{
const version_t * const * A = AA;
const version_t * const * B = BB;
if ((*A)->file < (*B)->file)
return -1;
if ((*A)->file > (*B)->file)
return 1;
return 0;
}
void create_changesets (database_t * db)
{
size_t total_versions = 0;
for (file_t * i = db->files; i != db->files_end; ++i)
total_versions += i->versions_end - i->versions;
if (total_versions == 0)
return;
version_t ** version_list = ARRAY_ALLOC (version_t *, total_versions);
version_t ** vp = version_list;
for (file_t * i = db->files; i != db->files_end; ++i)
for (version_t * j = i->versions; j != i->versions_end; ++j)
*vp++ = j;
assert (vp == version_list + total_versions);
qsort (version_list, total_versions, sizeof (version_t *),
version_compare_qsort);
changeset_t * current = database_new_changeset (db);
ARRAY_APPEND (current->versions, version_list[0]);
version_list[0]->commit = current;
current->time = version_list[0]->time;
current->type = ct_commit;
for (size_t i = 1; i < total_versions; ++i) {
version_t * next = version_list[i];
if (!strings_match (*current->versions, next)
|| next->time - current->time > fuzz_span
|| next->time - current->versions_end[-1]->time > fuzz_gap) {
ARRAY_TRIM (current->versions);
current = database_new_changeset (db);
current->time = next->time;
current->type = ct_commit;
}
ARRAY_APPEND (current->versions, version_list[i]);
version_list[i]->commit = current;
}
ARRAY_TRIM (current->versions);
free (version_list);
// Do a pass through the changesets; this breaks any cycles.
heap_t ready_versions;
heap_init (&ready_versions,
offsetof (version_t, ready_index), version_compare_heap);
prepare_for_emission (db, &ready_versions);
ssize_t emitted_changesets = 0;
changeset_t * changeset;
while ((changeset = next_changeset_split (db, &ready_versions))) {
changeset_emitted (db, &ready_versions, changeset);
++emitted_changesets;
}
// Sort the changeset version lists by file.
for (changeset_t ** i = db->changesets; i != db->changesets_end; ++i)
ARRAY_SORT ((*i)->versions, compare_version_by_file);
assert (heap_empty (&ready_versions));
assert (heap_empty (&db->ready_changesets));
assert (emitted_changesets == db->changesets_end - db->changesets);
heap_destroy (&ready_versions);
}