| 1 |
#include "GitDiffOutputParser.h" |
| 2 |
#include <wx/tokenzr.h> |
| 3 |
|
| 4 |
GitDiffOutputParser::GitDiffOutputParser() {} |
| 5 |
|
| 6 |
GitDiffOutputParser::~GitDiffOutputParser() {} |
| 7 |
|
| 8 |
void GitDiffOutputParser::GetDiffMap(const wxString& rawDiff, wxStringMap_t& M, wxArrayString* commitMessage) const |
| 9 |
{ |
| 10 |
wxStringTokenizer tokenizer(rawDiff, wxT("\n"), wxTOKEN_RET_EMPTY_ALL); |
| 11 |
wxString currentFile; |
| 12 |
wxString currentDiff; |
| 13 |
const wxString diffPrefix = "diff --git a/"; |
| 14 |
bool foundFirstDiff = false; |
| 15 |
eGitDiffStates state = kLookingForFileName; // Searching for file name |
| 16 |
wxString empty; |
| 17 |
wxString& line = empty; |
| 18 |
bool nextToken = true; |
| 19 |
while(tokenizer.HasMoreTokens() || nextToken == false) { |
| 20 |
if(nextToken) { |
| 21 |
line = tokenizer.GetNextToken(); |
| 22 |
} |
| 23 |
nextToken = true; |
| 24 |
|
| 25 |
if(commitMessage && !foundFirstDiff) { |
| 26 |
// The git blame and git CommitList need to display the commit message |
| 27 |
// This can be found at the top of the output, before the first "diff " |
| 28 |
if(!line.StartsWith("diff ")) { |
| 29 |
commitMessage->Add(line + "\n"); |
| 30 |
continue; |
| 31 |
} else { |
| 32 |
foundFirstDiff = true; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
switch(state) { |
| 37 |
case kLookingForFileName: { |
| 38 |
if(line.StartsWith(diffPrefix)) { |
| 39 |
int where = line.Find(diffPrefix); |
| 40 |
line = line.Mid(where + diffPrefix.length()); |
| 41 |
where = line.Find(" b/"); |
| 42 |
if(where != wxNOT_FOUND) { |
| 43 |
line = line.Mid(0, where); |
| 44 |
} |
| 45 |
currentFile = line; |
| 46 |
state = kLookingForDiff; |
| 47 |
} |
| 48 |
} break; |
| 49 |
case kLookingForDiff: { |
| 50 |
if(line.StartsWith(diffPrefix)) { |
| 51 |
// Add the current outpt to the result output and continue |
| 52 |
M[currentFile] = currentDiff; |
| 53 |
currentDiff.Clear(); |
| 54 |
currentFile.Clear(); |
| 55 |
|
| 56 |
// Reset the scanner loop |
| 57 |
state = kLookingForFileName; |
| 58 |
nextToken = |
| 59 |
false; // This will make sure we process this line again, however, in the other state switch case |
| 60 |
} else { |
| 61 |
currentDiff << line << "\n"; |
| 62 |
} |
| 63 |
} break; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Add any leftovers |
| 68 |
if(!currentFile.IsEmpty()) { |
| 69 |
M[currentFile] = currentDiff; |
| 70 |
} |
| 71 |
} |