TortoiseMerge: correctly detect and allow to resolve "added file" exists on apply...
[TortoiseGit.git] / src / TortoiseMerge / Patch.h
blob669aec2143a2f6d6835a9d97e15b5e3783a91001
1 // TortoiseMerge - a Diff/Patch program
3 // Copyright (C) 2006-2008 - TortoiseSVN
4 // Copyright (C) 2012 - Sven Strickroth <email@cs-ware.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "FileTextLines.h"
24 #define PATCHSTATE_REMOVED 0
25 #define PATCHSTATE_ADDED 1
26 #define PATCHSTATE_CONTEXT 2
28 /**
29 * \ingroup TortoiseMerge
31 * Handles unified diff files, parses them and also is able to
32 * apply those diff files.
34 class CPatch
36 public:
37 CPatch(void);
38 ~CPatch(void);
40 BOOL OpenUnifiedDiffFile(const CString& filename);
41 BOOL PatchFile(const int nIndex, const CString& sPath, const CString& sSavePath = _T(""), const CString& sBaseFile = _T(""), const bool force = false);
42 int GetNumberOfFiles() const {return m_arFileDiffs.GetCount();}
43 CString GetFilename(int nIndex);
44 CString GetRevision(int nIndex);
45 CString GetFilename2(int nIndex);
46 CString GetRevision2(int nIndex);
47 CString GetFullPath(const CString& sPath, int nIndex, int fileno = 0);
48 CString GetErrorMessage() const {return m_sErrorMessage;}
49 CString CheckPatchPath(const CString& path);
51 /**
52 * Returns TRUE if stripping prefixes from the paths in the patch file
53 * allows the patch file to being applied. The variable m_nStrip is then set appropriately.
54 * Returns FALSE if stripping prefixes doesn't help. The variable m_nStrip is set to 0.
56 BOOL StripPrefixes(const CString& path);
57 protected:
58 void FreeMemory();
59 BOOL HasExpandedKeyWords(const CString& line);
60 int CountMatches(const CString& path);
61 int CountDirMatches(const CString& path);
62 CString RemoveUnicodeBOM(const CString& str);
64 BOOL ParserGitPatch(CFileTextLines &PatchLines,int nIndex);
66 /**
67 * Strips the filename by removing m_nStrip prefixes.
69 CString Strip(const CString& filename);
70 struct Chunk
72 LONG lRemoveStart;
73 LONG lRemoveLength;
74 LONG lAddStart;
75 LONG lAddLength;
76 CStdCStringArray arLines;
77 CStdDWORDArray arLinesStates;
78 std::vector<EOL> arEOLs;
81 struct Chunks
83 CString sFilePath;
84 CString sRevision;
85 CString sFilePath2;
86 CString sRevision2;
87 CStdArray<Chunk*> chunks;
90 CStdArray<Chunks*> m_arFileDiffs;
91 CString m_sErrorMessage;
92 CFileTextLines::UnicodeType m_UnicodeType;
94 /**
95 * Defines how many prefixes are removed from the paths in the
96 * patch file. This allows applying patches which contain absolute
97 * paths or a prefix which differs in the patch and the working copy.
98 * Example: A filename like "/home/ts/my-working-copy/dir/file.txt"
99 * stripped by 4 prefixes is interpreted as "dir/file.txt"
101 int m_nStrip;
102 public:
103 bool m_IsGitPatch;