| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <git2.h> |
| 4 |
|
| 5 |
namespace vcs |
| 6 |
{ |
| 7 |
|
| 8 |
namespace git |
| 9 |
{ |
| 10 |
|
| 11 |
// Represents a set of changes between two states of a repository |
| 12 |
class Diff final |
| 13 |
{ |
| 14 |
private: |
| 15 |
git_diff* _diff; |
| 16 |
|
| 17 |
public: |
| 18 |
Diff(git_diff* diff) : |
| 19 |
_diff(diff) |
| 20 |
{} |
| 21 |
|
| 22 |
~Diff() |
| 23 |
{ |
| 24 |
git_diff_free(_diff); |
| 25 |
} |
| 26 |
|
| 27 |
bool containsFile(const std::string& relativePath) |
| 28 |
{ |
| 29 |
FileSearch search{ relativePath, false }; |
| 30 |
git_diff_foreach(_diff, Diff::searchForFile, nullptr, nullptr, nullptr, &search); |
| 31 |
|
| 32 |
return search.found; |
| 33 |
} |
| 34 |
|
| 35 |
private: |
| 36 |
struct FileSearch |
| 37 |
{ |
| 38 |
std::string path; |
| 39 |
bool found; |
| 40 |
}; |
| 41 |
|
| 42 |
static int searchForFile(const git_diff_delta* delta, float progress, void* payload) |
| 43 |
{ |
| 44 |
auto* search = reinterpret_cast<FileSearch*>(payload); |
| 45 |
|
| 46 |
if (delta->new_file.path == search->path) |
| 47 |
{ |
| 48 |
search->found = true; |
| 49 |
} |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
} |