Repository navigation
Expand file tree
/
Copy pathGitDiffProvider.cpp
More file actions
426 lines (396 loc) · 15.9 KB
/
Copy pathGitDiffProvider.cpp
File metadata and controls
426 lines (396 loc) · 15.9 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
#include "codex/GitDiffProvider.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QHash>
#include <QMetaObject>
#include <QPointer>
#include <QThreadPool>
#include <git2.h>
#include <algorithm>
#include <limits>
#include <memory>
#include <mutex>
namespace codexui::codex {
namespace {
constexpr std::size_t MaximumDiffBytes = 16U * 1024U * 1024U;
void ensureLibGit() {
static std::once_flag initialized;
std::call_once(initialized, [] { git_libgit2_init(); });
}
template <typename Type, void (*Free)(Type *)>
using GitPointer = std::unique_ptr<Type, decltype(Free)>;
QString gitError(const QString &fallback) {
const git_error *error = git_error_last();
return error && error->message ? QString::fromUtf8(error->message) : fallback;
}
QString text(const char *value) {
return value ? QString::fromUtf8(value) : QString{};
}
QString statusName(git_delta_t status) {
switch (status) {
case GIT_DELTA_ADDED:
return QStringLiteral("Added");
case GIT_DELTA_DELETED:
return QStringLiteral("Deleted");
case GIT_DELTA_RENAMED:
return QStringLiteral("Renamed");
case GIT_DELTA_COPIED:
return QStringLiteral("Copied");
case GIT_DELTA_UNTRACKED:
return QStringLiteral("Untracked");
case GIT_DELTA_TYPECHANGE:
return QStringLiteral("Type changed");
case GIT_DELTA_UNREADABLE:
return QStringLiteral("Unreadable");
case GIT_DELTA_CONFLICTED:
return QStringLiteral("Conflict");
default:
return QStringLiteral("Modified");
}
}
QString discoverRoot(const QString &directory) {
if (directory.isEmpty())
return {};
QString start = directory;
if (QFileInfo(start).isFile())
start = QFileInfo(start).absolutePath();
git_buf discovered = GIT_BUF_INIT;
const QByteArray encoded = QFile::encodeName(start);
if (git_repository_discover(&discovered, encoded.constData(), 0, nullptr) <
0) {
git_buf_dispose(&discovered);
return {};
}
git_repository *raw = nullptr;
const int opened = git_repository_open(&raw, discovered.ptr);
git_buf_dispose(&discovered);
if (opened < 0)
return {};
GitPointer<git_repository, git_repository_free> repository(raw,
git_repository_free);
if (git_repository_is_bare(repository.get()))
return {};
return QDir::cleanPath(text(git_repository_workdir(repository.get())));
}
GitPointer<git_tree, git_tree_free> headTree(git_repository *repository,
QString &error) {
git_reference *rawReference = nullptr;
const int headResult = git_repository_head(&rawReference, repository);
GitPointer<git_reference, git_reference_free> reference(rawReference,
git_reference_free);
if (headResult == GIT_EUNBORNBRANCH || headResult == GIT_ENOTFOUND)
return {nullptr, git_tree_free};
if (headResult < 0) {
error = gitError(QStringLiteral("Unable to read repository HEAD."));
return {nullptr, git_tree_free};
}
git_object *rawObject = nullptr;
if (git_reference_peel(&rawObject, reference.get(), GIT_OBJECT_TREE) < 0) {
error = gitError(QStringLiteral("Unable to read the HEAD tree."));
return {nullptr, git_tree_free};
}
return {reinterpret_cast<git_tree *>(rawObject), git_tree_free};
}
QString normalizedHint(QString path) {
path = QDir::fromNativeSeparators(std::move(path));
if (path.startsWith(QStringLiteral("a/")) ||
path.startsWith(QStringLiteral("b/")))
path.remove(0, 2);
return QDir::cleanPath(path);
}
bool containsHiddenDirectory(const QString &path) {
const QStringList parts = QDir::fromNativeSeparators(path).split(
QLatin1Char('/'), Qt::SkipEmptyParts);
return std::any_of(parts.begin(), parts.end(), [](const QString &part) {
return part.size() > 1 && part.startsWith(QLatin1Char('.'));
});
}
int repositoryPathScore(git_repository *repository, const QString &root,
QString path) {
path = normalizedHint(std::move(path));
if (QDir::isAbsolutePath(path)) {
path = QDir(root).relativeFilePath(path);
if (path == QStringLiteral("..") || path.startsWith(QStringLiteral("../")))
return 0;
}
const QByteArray encoded = QFile::encodeName(path);
unsigned int status = 0;
if (git_status_file(&status, repository, encoded.constData()) == 0)
return status == GIT_STATUS_CURRENT ? 1 : 2;
git_index *rawIndex = nullptr;
if (git_repository_index(&rawIndex, repository) == 0) {
GitPointer<git_index, git_index_free> index(rawIndex, git_index_free);
if (git_index_get_bypath(index.get(), encoded.constData(), 0))
return 1;
}
QString error;
GitPointer<git_tree, git_tree_free> tree = headTree(repository, error);
if (!tree)
return 0;
git_tree_entry *entry = nullptr;
const bool found =
git_tree_entry_bypath(&entry, tree.get(), encoded.constData()) == 0;
git_tree_entry_free(entry);
return found ? 1 : 0;
}
std::vector<int> rootHintScores(const QString &root,
const QStringList &directories,
const QStringList &paths) {
std::vector<int> scores(static_cast<std::size_t>(paths.size()), 0);
git_repository *raw = nullptr;
const QByteArray encodedRoot = QFile::encodeName(root);
if (git_repository_open(&raw, encodedRoot.constData()) < 0)
return scores;
GitPointer<git_repository, git_repository_free> repository(raw,
git_repository_free);
for (qsizetype index = 0; index < paths.size(); ++index) {
const QString &path = paths[index];
int score = repositoryPathScore(repository.get(), root, path);
for (const QString &directory : directories) {
if (score == 2)
break;
const QString absolute = QDir(directory).absoluteFilePath(path);
score = std::max(
score, repositoryPathScore(repository.get(), root, absolute));
}
scores[static_cast<std::size_t>(index)] = score;
}
return scores;
}
bool appendRepository(GitDiffSnapshot &snapshot, const QString &root,
GitDiffScope scope, GitDiffContext context,
const std::shared_ptr<std::atomic<std::uint64_t>> &clock,
std::uint64_t generation, std::size_t &retainedBytes) {
git_repository *rawRepository = nullptr;
const QByteArray encodedRoot = QFile::encodeName(root);
if (git_repository_open(&rawRepository, encodedRoot.constData()) < 0)
return false;
GitPointer<git_repository, git_repository_free> repository(rawRepository,
git_repository_free);
git_diff_options options{};
static_cast<void>(
git_diff_options_init(&options, GIT_DIFF_OPTIONS_VERSION));
options.flags = GIT_DIFF_INCLUDE_UNTRACKED |
GIT_DIFF_RECURSE_UNTRACKED_DIRS |
GIT_DIFF_SHOW_UNTRACKED_CONTENT |
GIT_DIFF_INCLUDE_TYPECHANGE |
GIT_DIFF_INCLUDE_TYPECHANGE_TREES |
GIT_DIFF_INCLUDE_UNREADABLE;
options.context_lines =
context == GitDiffContext::Compact
? 3
: std::numeric_limits<std::uint16_t>::max();
options.interhunk_lines = context == GitDiffContext::Compact ? 0 : 3;
QString treeError;
GitPointer<git_tree, git_tree_free> tree =
headTree(repository.get(), treeError);
if (!treeError.isEmpty()) {
snapshot.error = treeError;
return false;
}
git_diff *rawDiff = nullptr;
int result = 0;
if (scope == GitDiffScope::Unstaged)
result = git_diff_index_to_workdir(&rawDiff, repository.get(), nullptr,
&options);
else if (scope == GitDiffScope::Staged)
result = git_diff_tree_to_index(&rawDiff, repository.get(), tree.get(),
nullptr, &options);
else
result = git_diff_tree_to_workdir_with_index(
&rawDiff, repository.get(), tree.get(), &options);
if (result < 0) {
snapshot.error = gitError(QStringLiteral("Unable to calculate Git changes."));
return false;
}
GitPointer<git_diff, git_diff_free> diff(rawDiff, git_diff_free);
git_diff_find_options findOptions{};
static_cast<void>(
git_diff_find_options_init(&findOptions, GIT_DIFF_FIND_OPTIONS_VERSION));
findOptions.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
GIT_DIFF_FIND_FOR_UNTRACKED;
git_diff_find_similar(diff.get(), &findOptions);
const std::size_t count = git_diff_num_deltas(diff.get());
for (std::size_t index = 0; index < count; ++index) {
if (clock->load() != generation)
return false;
const git_diff_delta *delta = git_diff_get_delta(diff.get(), index);
if (!delta || delta->status == GIT_DELTA_UNMODIFIED ||
delta->status == GIT_DELTA_IGNORED)
continue;
GitDiffFile file;
file.repositoryRoot = root;
file.path = text(delta->new_file.path);
if (file.path.isEmpty())
file.path = text(delta->old_file.path);
file.absolutePath = QDir(root).absoluteFilePath(file.path);
file.previousPath = text(delta->old_file.path);
if (file.previousPath == file.path)
file.previousPath.clear();
file.status = statusName(delta->status);
file.binary = (delta->flags & GIT_DIFF_FLAG_BINARY) != 0;
git_patch *rawPatch = nullptr;
const int patchResult = git_patch_from_diff(&rawPatch, diff.get(), index);
GitPointer<git_patch, git_patch_free> patch(rawPatch, git_patch_free);
if (patchResult == 0 && patch) {
std::size_t additions = 0;
std::size_t deletions = 0;
git_patch_line_stats(nullptr, &additions, &deletions, patch.get());
file.additions = static_cast<int>(std::min<std::size_t>(
additions, static_cast<std::size_t>(std::numeric_limits<int>::max())));
file.deletions = static_cast<int>(std::min<std::size_t>(
deletions, static_cast<std::size_t>(std::numeric_limits<int>::max())));
git_buf rendered = GIT_BUF_INIT;
if (git_patch_to_buf(&rendered, patch.get()) == 0) {
if (retainedBytes + rendered.size <= MaximumDiffBytes) {
file.patch = QString::fromUtf8(
rendered.ptr, static_cast<qsizetype>(rendered.size));
retainedBytes += rendered.size;
} else {
snapshot.truncated = true;
file.patch = QStringLiteral(
"Diff omitted because the review exceeds the 16 MiB display limit.");
}
}
git_buf_dispose(&rendered);
}
snapshot.files.push_back(std::move(file));
}
return true;
}
GitDiffSnapshot collect(QString workspace, QStringList directories,
QStringList paths, QString selectedRepository,
bool includeHiddenRepositories, GitDiffScope scope,
GitDiffContext context,
const std::shared_ptr<std::atomic<std::uint64_t>> &clock,
std::uint64_t generation) {
GitDiffSnapshot snapshot;
snapshot.workspace = workspace.isEmpty()
? QString{}
: QDir::cleanPath(std::move(workspace));
snapshot.scope = scope;
snapshot.context = context;
if (!snapshot.workspace.isEmpty())
directories.prepend(snapshot.workspace);
directories.removeDuplicates();
QStringList roots;
QHash<QString, QStringList> rootDirectories;
for (const QString &directory : directories) {
if (clock->load() != generation)
return snapshot;
if (!includeHiddenRepositories && containsHiddenDirectory(directory))
continue;
const QString root = discoverRoot(directory);
if (root.isEmpty() ||
(!includeHiddenRepositories && containsHiddenDirectory(root)))
continue;
rootDirectories[root].push_back(directory);
if (!roots.contains(root))
roots.push_back(root);
}
if (roots.isEmpty()) {
snapshot.error = snapshot.workspace.isEmpty()
? QStringLiteral("Select a thread to inspect changes.")
: QStringLiteral("Change review requires a Git repository.");
return snapshot;
}
QStringList matched;
QHash<QString, std::vector<int>> hintScores;
for (const QString &root : roots)
hintScores.insert(root,
rootHintScores(root, rootDirectories[root], paths));
for (qsizetype pathIndex = 0; pathIndex < paths.size(); ++pathIndex) {
QStringList pathMatches;
int bestScore = 0;
for (const QString &root : roots) {
const int score =
hintScores[root][static_cast<std::size_t>(pathIndex)];
if (score > bestScore) {
bestScore = score;
pathMatches.clear();
}
if (score != 0 && score == bestScore)
pathMatches.push_back(root);
}
for (const QString &root : pathMatches) {
if (!matched.contains(root))
matched.push_back(root);
}
}
if (!matched.isEmpty())
roots = std::move(matched);
std::sort(roots.begin(), roots.end(), [](const QString &left,
const QString &right) {
return QString::localeAwareCompare(left, right) < 0;
});
snapshot.repositoryRoots = roots;
snapshot.repository = true;
if (!selectedRepository.isEmpty() && roots.contains(selectedRepository))
roots = {selectedRepository};
snapshot.repositoryRoot = roots.size() == 1 ? roots.front() : QString{};
std::size_t retainedBytes = 0;
for (const QString &root : roots) {
if (clock->load() != generation)
return snapshot;
appendRepository(snapshot, root, scope, context, clock, generation,
retainedBytes);
}
std::sort(snapshot.files.begin(), snapshot.files.end(),
[](const GitDiffFile &left, const GitDiffFile &right) {
if (left.repositoryRoot != right.repositoryRoot)
return QString::localeAwareCompare(left.repositoryRoot,
right.repositoryRoot) < 0;
return QString::localeAwareCompare(left.path, right.path) < 0;
});
return snapshot;
}
} // namespace
GitDiffProvider::GitDiffProvider(QObject *parent)
: QObject(parent),
generation(std::make_shared<std::atomic<std::uint64_t>>(0)) {
ensureLibGit();
}
GitDiffProvider::~GitDiffProvider() { cancel(); }
void GitDiffProvider::cancel() {
generation->fetch_add(1);
emit loadingChanged(false);
}
void GitDiffProvider::request(QString workspace,
QStringList candidateDirectories,
QStringList changedPaths,
QString selectedRepository,
bool includeHiddenRepositories,
GitDiffScope scope,
GitDiffContext context) {
const std::uint64_t requested = generation->fetch_add(1) + 1;
const auto clock = generation;
const QPointer<GitDiffProvider> receiver(this);
emit loadingChanged(true);
QThreadPool::globalInstance()->start(
[receiver, clock, requested, workspace = std::move(workspace),
candidateDirectories = std::move(candidateDirectories),
changedPaths = std::move(changedPaths),
selectedRepository = std::move(selectedRepository),
includeHiddenRepositories, scope,
context]() mutable {
GitDiffSnapshot snapshot = collect(
std::move(workspace), std::move(candidateDirectories),
std::move(changedPaths), std::move(selectedRepository),
includeHiddenRepositories, scope, context, clock, requested);
if (clock->load() != requested)
return;
QMetaObject::invokeMethod(
QCoreApplication::instance(),
[receiver, clock, requested, snapshot = std::move(snapshot)]() {
if (!receiver || clock->load() != requested)
return;
emit receiver->loadingChanged(false);
emit receiver->snapshotReady(snapshot);
},
Qt::QueuedConnection);
});
}
} // namespace codexui::codex