Sync translations with Transifex
[TortoiseGit.git] / src / TortoiseMerge / DiffData.cpp
blob6f6380d8cb2d5df178f7977cb569f295518199f1
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2014 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #pragma warning(push)
21 #include "diff.h"
22 #pragma warning(pop)
23 #include "TempFile.h"
24 #include "registry.h"
25 #include "resource.h"
26 #include "DiffData.h"
27 #include "UnicodeUtils.h"
28 #include "svn_dso.h"
29 #include "MovedBlocks.h"
31 #pragma warning(push)
32 #pragma warning(disable: 4702) // unreachable code
33 int CDiffData::abort_on_pool_failure (int /*retcode*/)
35 abort ();
36 return -1;
38 #pragma warning(pop)
40 CDiffData::CDiffData(void)
41 : m_bViewMovedBlocks(false)
42 , m_bPatchRequired(false)
44 apr_initialize();
45 svn_dso_initialize2();
47 m_bBlame = false;
49 m_sPatchOriginal = _T(": original");
50 m_sPatchPatched = _T(": patched");
53 CDiffData::~CDiffData(void)
55 apr_terminate();
58 void CDiffData::SetMovedBlocks(bool bViewMovedBlocks/* = true*/)
60 m_bViewMovedBlocks = bViewMovedBlocks;
63 int CDiffData::GetLineCount() const
65 int count = (int)m_arBaseFile.GetCount();
66 if (count < m_arTheirFile.GetCount())
67 count = m_arTheirFile.GetCount();
68 if (count < m_arYourFile.GetCount())
69 count = m_arYourFile.GetCount();
70 return count;
73 svn_diff_file_ignore_space_t CDiffData::GetIgnoreSpaceMode(DWORD dwIgnoreWS) const
75 switch (dwIgnoreWS)
77 case 0:
78 return svn_diff_file_ignore_space_none;
79 case 1:
80 return svn_diff_file_ignore_space_all;
81 case 2:
82 return svn_diff_file_ignore_space_change;
83 default:
84 return svn_diff_file_ignore_space_none;
88 svn_diff_file_options_t * CDiffData::CreateDiffFileOptions(DWORD dwIgnoreWS, bool bIgnoreEOL, apr_pool_t * pool)
90 svn_diff_file_options_t * options = svn_diff_file_options_create(pool);
91 options->ignore_eol_style = bIgnoreEOL;
92 options->ignore_space = GetIgnoreSpaceMode(dwIgnoreWS);
93 return options;
96 bool CDiffData::HandleSvnError(svn_error_t * svnerr)
98 TRACE(_T("diff-error in CDiffData::Load()\n"));
99 TRACE(_T("diff-error in CDiffData::Load()\n"));
100 CStringA sMsg = CStringA(svnerr->message);
101 while (svnerr->child)
103 svnerr = svnerr->child;
104 sMsg += _T("\n");
105 sMsg += CStringA(svnerr->message);
107 CString readableMsg = CUnicodeUtils::GetUnicode(sMsg);
108 m_sError.Format(IDS_ERR_DIFF_DIFF, (LPCTSTR)readableMsg);
109 svn_error_clear(svnerr);
110 return false;
113 void CDiffData::TieMovedBlocks(int from, int to, apr_off_t length)
115 for(int i=0; i<length; i++, to++, from++)
117 int fromIndex = m_YourBaseLeft.FindLineNumber(from);
118 int toIndex = m_YourBaseRight.FindLineNumber(to);
119 m_YourBaseLeft.SetMovedIndex(fromIndex, toIndex, true);
120 m_YourBaseRight.SetMovedIndex(toIndex, fromIndex, false);
122 toIndex = m_YourBaseBoth.FindLineNumber(to);
123 if (toIndex < 0)
124 return;
125 while((toIndex < m_YourBaseBoth.GetCount())&&
126 ((m_YourBaseBoth.GetState(toIndex) != DIFFSTATE_ADDED)&&
127 (!m_YourBaseBoth.IsMoved(toIndex))||(m_YourBaseBoth.IsMovedFrom(toIndex))||
128 (m_YourBaseBoth.GetLineNumber(toIndex) != to)))
130 toIndex++;
133 fromIndex = m_YourBaseBoth.FindLineNumber(from);
134 if (fromIndex < 0)
135 return;
136 while((fromIndex < m_YourBaseBoth.GetCount())&&
137 ((m_YourBaseBoth.GetState(fromIndex) != DIFFSTATE_REMOVED)&&
138 (!m_YourBaseBoth.IsMoved(fromIndex))||(!m_YourBaseBoth.IsMovedFrom(fromIndex))||
139 (m_YourBaseBoth.GetLineNumber(fromIndex) != from)))
141 fromIndex++;
143 if ((fromIndex < m_YourBaseBoth.GetCount())&&(toIndex < m_YourBaseBoth.GetCount()))
145 m_YourBaseBoth.SetMovedIndex(fromIndex, toIndex, true);
146 m_YourBaseBoth.SetMovedIndex(toIndex, fromIndex, false);
151 bool CDiffData::CompareWithIgnoreWS(CString s1, CString s2, DWORD dwIgnoreWS) const
153 if (dwIgnoreWS == 2)
155 s1 = s1.TrimLeft(_T(" \t"));
156 s2 = s2.TrimLeft(_T(" \t"));
158 else
160 s1 = s1.TrimRight(_T(" \t"));
161 s2 = s2.TrimRight(_T(" \t"));
164 return s1 != s2;
167 void CDiffData::StickAndSkip(svn_diff_t * &tempdiff, apr_off_t &original_length_sticked, apr_off_t &modified_length_sticked) const
169 if((m_bViewMovedBlocks)&&(tempdiff->type == svn_diff__type_diff_modified))
171 svn_diff_t * nextdiff = tempdiff->next;
172 while((nextdiff)&&(nextdiff->type == svn_diff__type_diff_modified))
174 original_length_sticked += nextdiff->original_length;
175 modified_length_sticked += nextdiff->modified_length;
176 tempdiff = nextdiff;
177 nextdiff = tempdiff->next;
182 BOOL CDiffData::Load()
184 m_arBaseFile.RemoveAll();
185 m_arYourFile.RemoveAll();
186 m_arTheirFile.RemoveAll();
188 m_YourBaseBoth.Clear();
189 m_YourBaseLeft.Clear();
190 m_YourBaseRight.Clear();
192 m_TheirBaseBoth.Clear();
193 m_TheirBaseLeft.Clear();
194 m_TheirBaseRight.Clear();
196 m_Diff3.Clear();
198 CRegDWORD regIgnoreWS = CRegDWORD(_T("Software\\TortoiseGitMerge\\IgnoreWS"));
199 CRegDWORD regIgnoreEOL = CRegDWORD(_T("Software\\TortoiseGitMerge\\IgnoreEOL"), TRUE);
200 CRegDWORD regIgnoreCase = CRegDWORD(_T("Software\\TortoiseGitMerge\\CaseInsensitive"), FALSE);
201 CRegDWORD regIgnoreComments = CRegDWORD(_T("Software\\TortoiseGitMerge\\IgnoreComments"), FALSE);
202 DWORD dwIgnoreWS = regIgnoreWS;
203 bool bIgnoreEOL = ((DWORD)regIgnoreEOL)!=0;
204 BOOL bIgnoreCase = ((DWORD)regIgnoreCase)!=0;
205 bool bIgnoreComments = ((DWORD)regIgnoreComments)!=0;
207 // The Subversion diff API only can ignore whitespaces and eol styles.
208 // It also can only handle one-byte charsets.
209 // To ignore case changes or to handle UTF-16 files, we have to
210 // save the original file in UTF-8 and/or the letters changed to lowercase
211 // so the Subversion diff can handle those.
212 CString sConvertedBaseFilename = m_baseFile.GetFilename();
213 CString sConvertedYourFilename = m_yourFile.GetFilename();
214 CString sConvertedTheirFilename = m_theirFile.GetFilename();
216 m_baseFile.StoreFileAttributes();
217 m_theirFile.StoreFileAttributes();
218 m_yourFile.StoreFileAttributes();
219 //m_mergedFile.StoreFileAttributes();
221 bool bBaseNeedConvert = false;
222 bool bTheirNeedConvert = false;
223 bool bYourNeedConvert = false;
224 bool bBaseIsUtf8 = false;
225 bool bTheirIsUtf8 = false;
226 bool bYourIsUtf8 = false;
228 if (IsBaseFileInUse())
230 if (!m_arBaseFile.Load(m_baseFile.GetFilename()))
232 m_sError = m_arBaseFile.GetErrorString();
233 return FALSE;
235 bBaseNeedConvert = bIgnoreCase || bIgnoreComments || (m_arBaseFile.NeedsConversion()) || !m_rx._Empty();
236 bBaseIsUtf8 = (m_arBaseFile.GetUnicodeType()!=CFileTextLines::ASCII) || bBaseNeedConvert;
239 if (IsTheirFileInUse())
241 // m_arBaseFile.GetCount() is passed as a hint for the number of lines in this file
242 // It's a fair guess that the files will be roughly the same size
243 if (!m_arTheirFile.Load(m_theirFile.GetFilename(), m_arBaseFile.GetCount()))
245 m_sError = m_arTheirFile.GetErrorString();
246 return FALSE;
248 bTheirNeedConvert = bIgnoreCase || bIgnoreComments || (m_arTheirFile.NeedsConversion()) || !m_rx._Empty();
249 bTheirIsUtf8 = (m_arTheirFile.GetUnicodeType()!=CFileTextLines::ASCII) || bTheirNeedConvert;
252 if (IsYourFileInUse())
254 // m_arBaseFile.GetCount() is passed as a hint for the number of lines in this file
255 // It's a fair guess that the files will be roughly the same size
256 if (!m_arYourFile.Load(m_yourFile.GetFilename(), m_arBaseFile.GetCount()))
258 m_sError = m_arYourFile.GetErrorString();
259 return FALSE;
261 bYourNeedConvert = bIgnoreCase || bIgnoreComments || (m_arYourFile.NeedsConversion()) || !m_rx._Empty();
262 bYourIsUtf8 = (m_arYourFile.GetUnicodeType()!=CFileTextLines::ASCII) || bYourNeedConvert;
265 // in case at least one of the files got converted or is UTF8
266 // we have to convert all non UTF8 (ASCII) files
267 // otherwise one file might be in ANSI and the other in UTF8 and we'll end up
268 // with lines marked as different throughout the files even though the lines
269 // would show no change at all in the viewer.
271 // convert all files we need to
272 bool bIsUtf8 = bBaseIsUtf8 || bTheirIsUtf8 || bYourIsUtf8; // any file end as UTF8
273 bBaseNeedConvert |= (IsBaseFileInUse() && !bBaseIsUtf8 && bIsUtf8);
274 if (bBaseNeedConvert)
276 sConvertedBaseFilename = CTempFiles::Instance().GetTempFilePathString();
277 m_baseFile.SetConvertedFileName(sConvertedBaseFilename);
278 m_arBaseFile.Save(sConvertedBaseFilename, true, true, 0, bIgnoreCase, m_bBlame
279 , bIgnoreComments, m_CommentLineStart, m_CommentBlockStart, m_CommentBlockEnd
280 , m_rx, m_replacement);
282 bYourNeedConvert |= (IsYourFileInUse() && !bYourIsUtf8 && bIsUtf8);
283 if (bYourNeedConvert)
285 sConvertedYourFilename = CTempFiles::Instance().GetTempFilePathString();
286 m_yourFile.SetConvertedFileName(sConvertedYourFilename);
287 m_arYourFile.Save(sConvertedYourFilename, true, true, 0, bIgnoreCase, m_bBlame
288 , bIgnoreComments, m_CommentLineStart, m_CommentBlockStart, m_CommentBlockEnd
289 , m_rx, m_replacement);
291 bTheirNeedConvert |= (IsTheirFileInUse() && !bTheirIsUtf8 && bIsUtf8);
292 if (bTheirNeedConvert)
294 sConvertedTheirFilename = CTempFiles::Instance().GetTempFilePathString();
295 m_theirFile.SetConvertedFileName(sConvertedTheirFilename);
296 m_arTheirFile.Save(sConvertedTheirFilename, true, true, 0, bIgnoreCase, m_bBlame
297 , bIgnoreComments, m_CommentLineStart, m_CommentBlockStart, m_CommentBlockEnd
298 , m_rx, m_replacement);
301 // Calculate the number of lines in the largest of the three files
302 int lengthHint = GetLineCount();
306 m_YourBaseBoth.Reserve(lengthHint);
307 m_YourBaseLeft.Reserve(lengthHint);
308 m_YourBaseRight.Reserve(lengthHint);
310 m_TheirBaseBoth.Reserve(lengthHint);
311 m_TheirBaseLeft.Reserve(lengthHint);
312 m_TheirBaseRight.Reserve(lengthHint);
314 catch (CMemoryException* e)
316 e->GetErrorMessage(m_sError.GetBuffer(255), 255);
317 m_sError.ReleaseBuffer();
318 e->Delete();
319 return FALSE;
322 apr_pool_t * pool = NULL;
323 apr_pool_create_ex (&pool, NULL, abort_on_pool_failure, NULL);
325 // Is this a two-way diff?
326 if (IsBaseFileInUse() && IsYourFileInUse() && !IsTheirFileInUse())
328 if (!DoTwoWayDiff(sConvertedBaseFilename, sConvertedYourFilename, dwIgnoreWS, bIgnoreEOL, pool))
330 apr_pool_destroy (pool); // free the allocated memory
331 return FALSE;
335 if (IsBaseFileInUse() && IsTheirFileInUse() && !IsYourFileInUse())
337 ASSERT(FALSE);
340 // Is this a three-way diff?
341 if (IsBaseFileInUse() && IsTheirFileInUse() && IsYourFileInUse())
343 m_Diff3.Reserve(lengthHint);
345 if (!DoThreeWayDiff(sConvertedBaseFilename, sConvertedYourFilename, sConvertedTheirFilename, dwIgnoreWS, bIgnoreEOL, !!bIgnoreCase, pool))
347 apr_pool_destroy (pool); // free the allocated memory
348 return FALSE;
352 // free the allocated memory
353 apr_pool_destroy (pool);
355 m_arBaseFile.RemoveAll();
356 m_arYourFile.RemoveAll();
357 m_arTheirFile.RemoveAll();
359 return TRUE;
362 bool
363 CDiffData::DoTwoWayDiff(const CString& sBaseFilename, const CString& sYourFilename, DWORD dwIgnoreWS, bool bIgnoreEOL, apr_pool_t * pool)
365 svn_diff_file_options_t * options = CreateDiffFileOptions(dwIgnoreWS, bIgnoreEOL, pool);
366 // convert CString filenames (UTF-16 or ANSI) to UTF-8
367 CStringA sBaseFilenameUtf8 = CUnicodeUtils::GetUTF8(sBaseFilename);
368 CStringA sYourFilenameUtf8 = CUnicodeUtils::GetUTF8(sYourFilename);
370 svn_diff_t * diffYourBase = NULL;
371 svn_error_t * svnerr = svn_diff_file_diff_2(&diffYourBase, sBaseFilenameUtf8, sYourFilenameUtf8, options, pool);
373 if (svnerr)
374 return HandleSvnError(svnerr);
376 tsvn_svn_diff_t_extension * movedBlocks = NULL;
377 if(m_bViewMovedBlocks)
378 movedBlocks = MovedBlocksDetect(diffYourBase, dwIgnoreWS, pool); // Side effect is that diffs are now splitted
380 svn_diff_t * tempdiff = diffYourBase;
381 LONG baseline = 0;
382 LONG yourline = 0;
383 while (tempdiff)
385 svn_diff__type_e diffType = tempdiff->type;
386 // Side effect described above overcoming - sticking together
387 apr_off_t original_length_sticked = tempdiff->original_length;
388 apr_off_t modified_length_sticked = tempdiff->modified_length;
389 StickAndSkip(tempdiff, original_length_sticked, modified_length_sticked);
391 for (int i=0; i<original_length_sticked; i++)
393 if (baseline >= m_arBaseFile.GetCount())
395 m_sError.LoadString(IDS_ERR_DIFF_NEWLINES);
396 return false;
398 const CString& sCurrentBaseLine = m_arBaseFile.GetAt(baseline);
399 EOL endingBase = m_arBaseFile.GetLineEnding(baseline);
400 if (diffType == svn_diff__type_common)
402 if (yourline >= m_arYourFile.GetCount())
404 m_sError.LoadString(IDS_ERR_DIFF_NEWLINES);
405 return false;
407 const CString& sCurrentYourLine = m_arYourFile.GetAt(yourline);
408 EOL endingYours = m_arYourFile.GetLineEnding(yourline);
409 if (sCurrentBaseLine != sCurrentYourLine)
411 bool changedWS = false;
412 if (dwIgnoreWS == 2 || dwIgnoreWS == 3)
413 changedWS = CompareWithIgnoreWS(sCurrentBaseLine, sCurrentYourLine, dwIgnoreWS);
414 if (changedWS || dwIgnoreWS == 0)
416 // one-pane view: two lines, one 'removed' and one 'added'
417 m_YourBaseBoth.AddData(sCurrentBaseLine, DIFFSTATE_REMOVEDWHITESPACE, yourline, endingBase, HIDESTATE_SHOWN, -1);
418 m_YourBaseBoth.AddData(sCurrentYourLine, DIFFSTATE_ADDEDWHITESPACE, yourline, endingYours, HIDESTATE_SHOWN, -1);
420 else
422 m_YourBaseBoth.AddData(sCurrentYourLine, DIFFSTATE_NORMAL, yourline, endingBase, HIDESTATE_HIDDEN, -1);
425 else
427 m_YourBaseBoth.AddData(sCurrentYourLine, DIFFSTATE_NORMAL, yourline, endingBase, HIDESTATE_HIDDEN, -1);
429 yourline++; //in both files
431 else
432 { // small trick - we need here a baseline, but we fix it back to yourline at the end of routine
433 m_YourBaseBoth.AddData(sCurrentBaseLine, DIFFSTATE_REMOVED, -1, endingBase, HIDESTATE_SHOWN, -1);
435 baseline++;
437 if (diffType == svn_diff__type_diff_modified)
439 for (int i=0; i<modified_length_sticked; i++)
441 if (m_arYourFile.GetCount() > yourline)
443 m_YourBaseBoth.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_ADDED, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
445 yourline++;
448 tempdiff = tempdiff->next;
451 HideUnchangedSections(&m_YourBaseBoth, NULL, NULL);
453 tempdiff = diffYourBase;
454 baseline = 0;
455 yourline = 0;
456 while (tempdiff)
458 if (tempdiff->type == svn_diff__type_common)
460 for (int i=0; i<tempdiff->original_length; i++)
462 const CString& sCurrentYourLine = m_arYourFile.GetAt(yourline);
463 EOL endingYours = m_arYourFile.GetLineEnding(yourline);
464 const CString& sCurrentBaseLine = m_arBaseFile.GetAt(baseline);
465 EOL endingBase = m_arBaseFile.GetLineEnding(baseline);
466 if (sCurrentBaseLine != sCurrentYourLine)
468 bool changedWS = false;
469 if (dwIgnoreWS == 2 || dwIgnoreWS == 3)
470 changedWS = CompareWithIgnoreWS(sCurrentBaseLine, sCurrentYourLine, dwIgnoreWS);
471 if (changedWS || dwIgnoreWS == 0)
473 m_YourBaseLeft.AddData(sCurrentBaseLine, DIFFSTATE_WHITESPACE, baseline, endingBase, HIDESTATE_SHOWN, -1);
474 m_YourBaseRight.AddData(sCurrentYourLine, DIFFSTATE_WHITESPACE, yourline, endingYours, HIDESTATE_SHOWN, -1);
476 else
478 m_YourBaseLeft.AddData(sCurrentBaseLine, DIFFSTATE_NORMAL, baseline, endingBase, HIDESTATE_HIDDEN, -1);
479 m_YourBaseRight.AddData(sCurrentYourLine, DIFFSTATE_NORMAL, yourline, endingYours, HIDESTATE_HIDDEN, -1);
482 else
484 m_YourBaseLeft.AddData(sCurrentBaseLine, DIFFSTATE_NORMAL, baseline, endingBase, HIDESTATE_HIDDEN, -1);
485 m_YourBaseRight.AddData(sCurrentYourLine, DIFFSTATE_NORMAL, yourline, endingYours, HIDESTATE_HIDDEN, -1);
487 baseline++;
488 yourline++;
491 if (tempdiff->type == svn_diff__type_diff_modified)
493 // now we trying to stick together parts, that were splitted by MovedBlocks
494 apr_off_t original_length_sticked = tempdiff->original_length;
495 apr_off_t modified_length_sticked = tempdiff->modified_length;
496 StickAndSkip(tempdiff, original_length_sticked, modified_length_sticked);
498 apr_off_t original_length = original_length_sticked;
499 for (int i=0; i<modified_length_sticked; i++)
501 if (m_arYourFile.GetCount() > yourline)
503 EOL endingYours = m_arYourFile.GetLineEnding(yourline);
504 m_YourBaseRight.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_ADDED, yourline, endingYours, HIDESTATE_SHOWN, -1);
505 if (original_length-- <= 0)
507 m_YourBaseLeft.AddEmpty();
509 else
511 EOL endingBase = m_arBaseFile.GetLineEnding(baseline);
512 m_YourBaseLeft.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_REMOVED, baseline, endingBase, HIDESTATE_SHOWN, -1);
513 baseline++;
515 yourline++;
518 apr_off_t modified_length = modified_length_sticked;
519 for (int i=0; i<original_length_sticked; i++)
521 if ((modified_length-- <= 0)&&(m_arBaseFile.GetCount() > baseline))
523 EOL endingBase = m_arBaseFile.GetLineEnding(baseline);
524 m_YourBaseLeft.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_REMOVED, baseline, endingBase, HIDESTATE_SHOWN, -1);
525 m_YourBaseRight.AddEmpty();
526 baseline++;
530 tempdiff = tempdiff->next;
532 // add last (empty) lines if needed - diff don't report those
533 if (m_arBaseFile.GetCount() > baseline)
535 if (m_arYourFile.GetCount() > yourline)
537 // last line is missing in both files add them to end and mark as no diff
538 m_YourBaseLeft.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_NORMAL, baseline, m_arBaseFile.GetLineEnding(baseline), HIDESTATE_SHOWN, -1);
539 m_YourBaseRight.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_NORMAL, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
540 yourline++;
541 baseline++;
543 else
545 viewdata oViewData(m_arBaseFile.GetAt(baseline), DIFFSTATE_REMOVED, baseline, m_arBaseFile.GetLineEnding(baseline), HIDESTATE_SHOWN);
546 baseline++;
548 // find first EMPTY line in last blok
549 int nPos = m_YourBaseLeft.GetCount();
550 while (--nPos>=0 && m_YourBaseLeft.GetState(nPos)==DIFFSTATE_EMPTY) ;
551 if (++nPos<m_YourBaseLeft.GetCount())
553 m_YourBaseLeft.SetData(nPos, oViewData);
555 else
557 m_YourBaseLeft.AddData(oViewData);
558 m_YourBaseRight.AddEmpty();
562 else if (m_arYourFile.GetCount() > yourline)
564 viewdata oViewData(m_arYourFile.GetAt(yourline), DIFFSTATE_ADDED, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN);
565 yourline++;
567 // try to move last line higher
568 int nPos = m_YourBaseRight.GetCount();
569 while (--nPos>=0 && m_YourBaseRight.GetState(nPos)==DIFFSTATE_EMPTY) ;
570 if (++nPos<m_YourBaseRight.GetCount())
572 m_YourBaseRight.SetData(nPos, oViewData);
574 else
576 m_YourBaseLeft.AddEmpty();
577 m_YourBaseRight.AddData(oViewData);
582 // Fixing results for conforming moved blocks
584 while(movedBlocks)
586 tempdiff = movedBlocks->base;
587 if(movedBlocks->moved_to != -1)
589 // set states in a block original:length -> moved_to:length
590 TieMovedBlocks((int)tempdiff->original_start, movedBlocks->moved_to, tempdiff->original_length);
592 if(movedBlocks->moved_from != -1)
594 // set states in a block modified:length -> moved_from:length
595 TieMovedBlocks(movedBlocks->moved_from, (int)tempdiff->modified_start, tempdiff->modified_length);
597 movedBlocks = movedBlocks->next;
600 // replace baseline with the yourline in m_YourBaseBoth
601 /* yourline = 0;
602 for(int i=0; i<m_YourBaseBoth.GetCount(); i++)
604 DiffStates state = m_YourBaseBoth.GetState(i);
605 if((state == DIFFSTATE_REMOVED)||(state == DIFFSTATE_MOVED_FROM))
607 m_YourBaseBoth.SetLineNumber(i, -1);
609 else
611 yourline++;
613 }//*/
615 TRACE(_T("done with 2-way diff\n"));
617 HideUnchangedSections(&m_YourBaseLeft, &m_YourBaseRight, NULL);
619 return true;
622 bool
623 CDiffData::DoThreeWayDiff(const CString& sBaseFilename, const CString& sYourFilename, const CString& sTheirFilename, DWORD dwIgnoreWS, bool bIgnoreEOL, bool bIgnoreCase, apr_pool_t * pool)
625 // the following three arrays are used to check for conflicts even in case the
626 // user has ignored spaces/eols.
627 CStdDWORDArray m_arDiff3LinesBase;
628 CStdDWORDArray m_arDiff3LinesYour;
629 CStdDWORDArray m_arDiff3LinesTheir;
630 #define AddLines(baseline, yourline, theirline) m_arDiff3LinesBase.Add(baseline), m_arDiff3LinesYour.Add(yourline), m_arDiff3LinesTheir.Add(theirline)
631 int lengthHint = GetLineCount();
633 m_arDiff3LinesBase.Reserve(lengthHint);
634 m_arDiff3LinesYour.Reserve(lengthHint);
635 m_arDiff3LinesTheir.Reserve(lengthHint);
637 CRegDWORD contextLines = CRegDWORD(_T("Software\\TortoiseGitMerge\\ContextLines"), 3);
638 svn_diff_file_options_t * options = CreateDiffFileOptions(dwIgnoreWS, bIgnoreEOL, pool);
640 // convert CString filenames (UTF-16 or ANSI) to UTF-8
641 CStringA sBaseFilenameUtf8 = CUnicodeUtils::GetUTF8(sBaseFilename);
642 CStringA sYourFilenameUtf8 = CUnicodeUtils::GetUTF8(sYourFilename);
643 CStringA sTheirFilenameUtf8 = CUnicodeUtils::GetUTF8(sTheirFilename);
645 svn_diff_t * diffTheirYourBase = NULL;
646 svn_error_t * svnerr = svn_diff_file_diff3_2(&diffTheirYourBase, sBaseFilenameUtf8, sTheirFilenameUtf8, sYourFilenameUtf8, options, pool);
647 if (svnerr)
648 return HandleSvnError(svnerr);
650 svn_diff_t * tempdiff = diffTheirYourBase;
651 LONG baseline = 0;
652 LONG yourline = 0;
653 LONG theirline = 0;
654 LONG resline = 0;
655 // common viewdata
656 const viewdata emptyConflictEmpty(_T(""), DIFFSTATE_CONFLICTEMPTY, DIFF_EMPTYLINENUMBER, EOL_NOENDING, HIDESTATE_SHOWN);
657 const viewdata emptyIdenticalRemoved(_T(""), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, EOL_NOENDING, HIDESTATE_SHOWN);
658 while (tempdiff)
660 if (tempdiff->type == svn_diff__type_common)
662 ASSERT((tempdiff->latest_length == tempdiff->modified_length) && (tempdiff->modified_length == tempdiff->original_length));
663 for (int i=0; i<tempdiff->original_length; i++)
665 if ((m_arYourFile.GetCount() > yourline)&&(m_arTheirFile.GetCount() > theirline))
667 AddLines(baseline, yourline, theirline);
669 m_Diff3.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_NORMAL, resline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_HIDDEN, -1);
670 m_YourBaseBoth.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_NORMAL, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_HIDDEN, -1);
671 m_TheirBaseBoth.AddData(m_arTheirFile.GetAt(theirline), DIFFSTATE_NORMAL, theirline, m_arTheirFile.GetLineEnding(theirline), HIDESTATE_HIDDEN, -1);
673 baseline++;
674 yourline++;
675 theirline++;
676 resline++;
680 else if (tempdiff->type == svn_diff__type_diff_common)
682 ASSERT(tempdiff->latest_length == tempdiff->modified_length);
683 //both theirs and yours have lines replaced
684 for (int i=0; i<tempdiff->original_length; i++)
686 if (m_arBaseFile.GetCount() > baseline)
688 AddLines(baseline, yourline, theirline);
690 m_Diff3.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, m_arBaseFile.GetLineEnding(baseline), HIDESTATE_SHOWN, -1);
691 m_YourBaseBoth.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, EOL_NOENDING, HIDESTATE_SHOWN, -1);
692 m_TheirBaseBoth.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, EOL_NOENDING, HIDESTATE_SHOWN, -1);
694 baseline++;
697 for (int i=0; i<tempdiff->modified_length; i++)
699 if ((m_arYourFile.GetCount() > yourline)&&(m_arTheirFile.GetCount() > theirline))
701 AddLines(baseline, yourline, theirline);
703 m_Diff3.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_IDENTICALADDED, resline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
704 m_YourBaseBoth.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_IDENTICALADDED, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
705 m_TheirBaseBoth.AddData(m_arTheirFile.GetAt(theirline), DIFFSTATE_IDENTICALADDED, resline, m_arTheirFile.GetLineEnding(theirline), HIDESTATE_SHOWN, -1);
707 yourline++;
708 theirline++;
709 resline++;
713 else if (tempdiff->type == svn_diff__type_conflict)
715 apr_off_t length = max(tempdiff->original_length, tempdiff->modified_length);
716 length = max(tempdiff->latest_length, length);
717 apr_off_t original = tempdiff->original_length;
718 apr_off_t modified = tempdiff->modified_length;
719 apr_off_t latest = tempdiff->latest_length;
721 apr_off_t originalresolved = 0;
722 apr_off_t modifiedresolved = 0;
723 apr_off_t latestresolved = 0;
725 LONG base = baseline;
726 LONG your = yourline;
727 LONG their = theirline;
728 if (tempdiff->resolved_diff)
730 originalresolved = tempdiff->resolved_diff->original_length;
731 modifiedresolved = tempdiff->resolved_diff->modified_length;
732 latestresolved = tempdiff->resolved_diff->latest_length;
734 for (int i=0; i<length; i++)
736 EOL endingBase = m_arBaseFile.GetCount() > baseline ? m_arBaseFile.GetLineEnding(baseline) : EOL_AUTOLINE;
737 if (original)
739 if (m_arBaseFile.GetCount() > baseline)
741 AddLines(baseline, yourline, theirline);
743 m_Diff3.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, endingBase , HIDESTATE_SHOWN, -1);
744 m_YourBaseBoth.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, endingBase , HIDESTATE_SHOWN, -1);
745 m_TheirBaseBoth.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_IDENTICALREMOVED, DIFF_EMPTYLINENUMBER, endingBase , HIDESTATE_SHOWN, -1);
748 else if ((originalresolved)||((modifiedresolved)&&(latestresolved)))
750 AddLines(baseline, yourline, theirline);
752 m_Diff3.AddData(emptyIdenticalRemoved);
753 if ((latestresolved)&&(modifiedresolved))
755 m_YourBaseBoth.AddData(emptyIdenticalRemoved);
756 m_TheirBaseBoth.AddData(emptyIdenticalRemoved);
759 if (original)
761 original--;
762 baseline++;
764 if (originalresolved)
765 originalresolved--;
767 if (modified)
769 modified--;
770 theirline++;
772 if (modifiedresolved)
773 modifiedresolved--;
774 if (latest)
776 latest--;
777 yourline++;
779 if (latestresolved)
780 latestresolved--;
782 original = tempdiff->original_length;
783 modified = tempdiff->modified_length;
784 latest = tempdiff->latest_length;
785 baseline = base;
786 yourline = your;
787 theirline = their;
788 if (tempdiff->resolved_diff)
790 originalresolved = tempdiff->resolved_diff->original_length;
791 modifiedresolved = tempdiff->resolved_diff->modified_length;
792 latestresolved = tempdiff->resolved_diff->latest_length;
794 for (int i=0; i<length; i++)
796 if ((latest)||(modified))
798 AddLines(baseline, yourline, theirline);
800 m_Diff3.AddData(_T(""), DIFFSTATE_CONFLICTED, resline, EOL_NOENDING, HIDESTATE_SHOWN, -1);
802 resline++;
805 if (latest)
807 if (m_arYourFile.GetCount() > yourline)
809 m_YourBaseBoth.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_CONFLICTADDED, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
812 else if ((latestresolved)||(modified)||(modifiedresolved))
814 m_YourBaseBoth.AddData(emptyConflictEmpty);
816 if (modified)
818 if (m_arTheirFile.GetCount() > theirline)
820 m_TheirBaseBoth.AddData(m_arTheirFile.GetAt(theirline), DIFFSTATE_CONFLICTADDED, theirline, m_arTheirFile.GetLineEnding(theirline), HIDESTATE_SHOWN, -1);
823 else if ((modifiedresolved)||(latest)||(latestresolved))
825 m_TheirBaseBoth.AddData(emptyConflictEmpty);
827 if (original)
829 original--;
830 baseline++;
832 if (originalresolved)
833 originalresolved--;
834 if (modified)
836 modified--;
837 theirline++;
839 if (modifiedresolved)
840 modifiedresolved--;
841 if (latest)
843 latest--;
844 yourline++;
846 if (latestresolved)
847 latestresolved--;
850 else if (tempdiff->type == svn_diff__type_diff_modified)
852 //deleted!
853 for (int i=0; i<tempdiff->original_length; i++)
855 if ((m_arBaseFile.GetCount() > baseline)&&(m_arYourFile.GetCount() > yourline))
857 AddLines(baseline, yourline, theirline);
859 m_Diff3.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_THEIRSREMOVED, DIFF_EMPTYLINENUMBER, m_arBaseFile.GetLineEnding(baseline), HIDESTATE_SHOWN, -1);
860 m_YourBaseBoth.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_NORMAL, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
861 m_TheirBaseBoth.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_THEIRSREMOVED, DIFF_EMPTYLINENUMBER, EOL_NOENDING, HIDESTATE_SHOWN, -1);
863 baseline++;
864 yourline++;
867 //added
868 for (int i=0; i<tempdiff->modified_length; i++)
870 if (m_arTheirFile.GetCount() > theirline)
872 AddLines(baseline, yourline, theirline);
874 m_Diff3.AddData(m_arTheirFile.GetAt(theirline), DIFFSTATE_THEIRSADDED, resline, m_arTheirFile.GetLineEnding(theirline), HIDESTATE_SHOWN, -1);
875 m_YourBaseBoth.AddEmpty();
876 m_TheirBaseBoth.AddData(m_arTheirFile.GetAt(theirline), DIFFSTATE_THEIRSADDED, theirline, m_arTheirFile.GetLineEnding(theirline), HIDESTATE_SHOWN, -1);
878 theirline++;
879 resline++;
883 else if (tempdiff->type == svn_diff__type_diff_latest)
885 //YOURS differs from base
887 for (int i=0; i<tempdiff->original_length; i++)
889 if ((m_arBaseFile.GetCount() > baseline)&&(m_arTheirFile.GetCount() > theirline))
891 AddLines(baseline, yourline, theirline);
893 m_Diff3.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_YOURSREMOVED, DIFF_EMPTYLINENUMBER, m_arBaseFile.GetLineEnding(baseline), HIDESTATE_SHOWN, -1);
894 m_YourBaseBoth.AddData(m_arBaseFile.GetAt(baseline), DIFFSTATE_YOURSREMOVED, DIFF_EMPTYLINENUMBER, m_arBaseFile.GetLineEnding(baseline), HIDESTATE_SHOWN, -1);
895 m_TheirBaseBoth.AddData(m_arTheirFile.GetAt(theirline), DIFFSTATE_NORMAL, theirline, m_arTheirFile.GetLineEnding(theirline), HIDESTATE_HIDDEN, -1);
897 baseline++;
898 theirline++;
901 for (int i=0; i<tempdiff->latest_length; i++)
903 if (m_arYourFile.GetCount() > yourline)
905 AddLines(baseline, yourline, theirline);
907 m_Diff3.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_YOURSADDED, resline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
908 m_YourBaseBoth.AddData(m_arYourFile.GetAt(yourline), DIFFSTATE_IDENTICALADDED, yourline, m_arYourFile.GetLineEnding(yourline), HIDESTATE_SHOWN, -1);
909 m_TheirBaseBoth.AddEmpty();
911 yourline++;
912 resline++;
916 else
918 TRACE(_T("something bad happened during diff!\n"));
920 tempdiff = tempdiff->next;
922 } // while (tempdiff)
924 if ((options->ignore_space != svn_diff_file_ignore_space_none)||(bIgnoreCase)||(bIgnoreEOL))
926 // If whitespaces are ignored, a conflict could have been missed
927 // We now go through all lines again and check if they're identical.
928 // If they're not, then that means it is a conflict, and we
929 // mark the conflict with the proper colors.
930 for (long i=0; i<m_Diff3.GetCount(); ++i)
932 DiffStates state1 = m_YourBaseBoth.GetState(i);
933 DiffStates state2 = m_TheirBaseBoth.GetState(i);
935 if (((state1 == DIFFSTATE_IDENTICALADDED)||(state1 == DIFFSTATE_NORMAL))&&
936 ((state2 == DIFFSTATE_IDENTICALADDED)||(state2 == DIFFSTATE_NORMAL)))
938 LONG lineyour = m_arDiff3LinesYour.GetAt(i);
939 LONG linetheir = m_arDiff3LinesTheir.GetAt(i);
940 LONG linebase = m_arDiff3LinesBase.GetAt(i);
941 if ((lineyour < m_arYourFile.GetCount()) &&
942 (linetheir < m_arTheirFile.GetCount()) &&
943 (linebase < m_arBaseFile.GetCount()))
945 if (((m_arYourFile.GetLineEnding(lineyour)!=m_arBaseFile.GetLineEnding(linebase))&&
946 (m_arTheirFile.GetLineEnding(linetheir)!=m_arBaseFile.GetLineEnding(linebase))&&
947 (m_arYourFile.GetLineEnding(lineyour)!=m_arTheirFile.GetLineEnding(linetheir))) ||
948 ((m_arYourFile.GetAt(lineyour).Compare(m_arBaseFile.GetAt(linebase))!=0)&&
949 (m_arTheirFile.GetAt(linetheir).Compare(m_arBaseFile.GetAt(linebase))!=0)&&
950 (m_arYourFile.GetAt(lineyour).Compare(m_arTheirFile.GetAt(linetheir))!=0)))
952 m_Diff3.SetState(i, DIFFSTATE_CONFLICTED_IGNORED);
953 m_YourBaseBoth.SetState(i, DIFFSTATE_CONFLICTADDED);
954 m_TheirBaseBoth.SetState(i, DIFFSTATE_CONFLICTADDED);
960 ASSERT(m_Diff3.GetCount() == m_YourBaseBoth.GetCount());
961 ASSERT(m_TheirBaseBoth.GetCount() == m_YourBaseBoth.GetCount());
963 TRACE(_T("done with 3-way diff\n"));
965 HideUnchangedSections(&m_Diff3, &m_YourBaseBoth, &m_TheirBaseBoth);
967 return true;
970 void CDiffData::HideUnchangedSections(CViewData * data1, CViewData * data2, CViewData * data3) const
972 if (data1 == NULL)
973 return;
975 CRegDWORD contextLines = CRegDWORD(_T("Software\\TortoiseGitMerge\\ContextLines"), 1);
977 if (data1->GetCount() > 1)
979 HIDESTATE lastHideState = data1->GetHideState(0);
980 if (lastHideState == HIDESTATE_HIDDEN)
982 data1->SetLineHideState(0, HIDESTATE_MARKER);
983 if (data2)
984 data2->SetLineHideState(0, HIDESTATE_MARKER);
985 if (data3)
986 data3->SetLineHideState(0, HIDESTATE_MARKER);
988 for (int i = 1; i < data1->GetCount(); ++i)
990 HIDESTATE hideState = data1->GetHideState(i);
991 if (hideState != lastHideState)
993 if (hideState == HIDESTATE_SHOWN)
995 // go back and show the last 'contextLines' lines to "SHOWN"
996 int lineback = i - 1;
997 int stopline = lineback - (int)(DWORD)contextLines;
998 while ((lineback >= 0)&&(lineback > stopline))
1000 data1->SetLineHideState(lineback, HIDESTATE_SHOWN);
1001 if (data2)
1002 data2->SetLineHideState(lineback, HIDESTATE_SHOWN);
1003 if (data3)
1004 data3->SetLineHideState(lineback, HIDESTATE_SHOWN);
1005 lineback--;
1008 else if ((hideState == HIDESTATE_HIDDEN)&&(lastHideState != HIDESTATE_MARKER))
1010 // go forward and show the next 'contextLines' lines to "SHOWN"
1011 int lineforward = i + 1;
1012 int stopline = lineforward + (int)(DWORD)contextLines;
1013 while ((lineforward < data1->GetCount())&&(lineforward < stopline))
1015 data1->SetLineHideState(lineforward, HIDESTATE_SHOWN);
1016 if (data2)
1017 data2->SetLineHideState(lineforward, HIDESTATE_SHOWN);
1018 if (data3)
1019 data3->SetLineHideState(lineforward, HIDESTATE_SHOWN);
1020 lineforward++;
1022 if ((lineforward < data1->GetCount())&&(data1->GetHideState(lineforward) == HIDESTATE_HIDDEN))
1024 data1->SetLineHideState(lineforward, HIDESTATE_MARKER);
1025 if (data2)
1026 data2->SetLineHideState(lineforward, HIDESTATE_MARKER);
1027 if (data3)
1028 data3->SetLineHideState(lineforward, HIDESTATE_MARKER);
1032 lastHideState = hideState;
1037 void CDiffData::SetCommentTokens( const CString& sLineStart, const CString& sBlockStart, const CString& sBlockEnd )
1039 m_CommentLineStart = sLineStart;
1040 m_CommentBlockStart = sBlockStart;
1041 m_CommentBlockEnd = sBlockEnd;
1044 void CDiffData::SetRegexTokens( const std::wregex& rx, const std::wstring& replacement )
1046 m_rx = rx;
1047 m_replacement = replacement;