optimize TGitCache for CLI operations
[TortoiseGit.git] / src / TortoiseMerge / Patch.h
blob91f4101e82ff8c1505257a3bfb6d5fff9a88698c
1 // TortoiseMerge - a Diff/Patch program
3 // Copyright (C) 2006-2008 - 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 #pragma once
20 #include "FileTextLines.h"
23 #define PATCHSTATE_REMOVED 0
24 #define PATCHSTATE_ADDED 1
25 #define PATCHSTATE_CONTEXT 2
27 /**
28 * \ingroup TortoiseMerge
30 * Handles unified diff files, parses them and also is able to
31 * apply those diff files.
33 class CPatch
35 public:
36 CPatch(void);
37 ~CPatch(void);
39 BOOL OpenUnifiedDiffFile(const CString& filename);
40 BOOL PatchFile(const CString& sPath, const CString& sSavePath = _T(""), const CString& sBaseFile = _T(""));
41 int GetNumberOfFiles() const {return m_arFileDiffs.GetCount();}
42 CString GetFilename(int nIndex);
43 CString GetRevision(int nIndex);
44 CString GetFilename2(int nIndex);
45 CString GetRevision2(int nIndex);
46 CString GetErrorMessage() const {return m_sErrorMessage;}
47 CString CheckPatchPath(const CString& path);
49 /**
50 * Returns TRUE if stripping prefixes from the paths in the patch file
51 * allows the patch file to being applied. The variable m_nStrip is then set appropriately.
52 * Returns FALSE if stripping prefixes doesn't help. The variable m_nStrip is set to 0.
54 BOOL StripPrefixes(const CString& path);
55 protected:
56 void FreeMemory();
57 BOOL HasExpandedKeyWords(const CString& line);
58 int CountMatches(const CString& path);
59 int CountDirMatches(const CString& path);
60 CString RemoveUnicodeBOM(const CString& str);
62 BOOL ParserGitPatch(CFileTextLines &PatchLines,int nIndex);
64 /**
65 * Strips the filename by removing m_nStrip prefixes.
67 CString Strip(const CString& filename);
68 struct Chunk
70 LONG lRemoveStart;
71 LONG lRemoveLength;
72 LONG lAddStart;
73 LONG lAddLength;
74 CStdCStringArray arLines;
75 CStdDWORDArray arLinesStates;
76 std::vector<EOL> arEOLs;
79 struct Chunks
81 CString sFilePath;
82 CString sRevision;
83 CString sFilePath2;
84 CString sRevision2;
85 CStdArray<Chunk*> chunks;
88 CStdArray<Chunks*> m_arFileDiffs;
89 CString m_sErrorMessage;
90 CFileTextLines::UnicodeType m_UnicodeType;
92 /**
93 * Defines how many prefixes are removed from the paths in the
94 * patch file. This allows applying patches which contain absolute
95 * paths or a prefix which differs in the patch and the working copy.
96 * Example: A filename like "/home/ts/my-working-copy/dir/file.txt"
97 * stripped by 4 prefixes is interpreted as "dir/file.txt"
99 int m_nStrip;
100 public:
101 bool m_IsGitPatch;