Fix typos
[TortoiseGit.git] / src / TortoiseMerge / Patch.h
blobab508b9532f59c19845668287ae1f64d86f4ccc3
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2008, 2014 - TortoiseSVN
4 // Copyright (C) 2012-2013, 2018-2019, 2021-2023 - 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();
38 ~CPatch();
40 BOOL OpenUnifiedDiffFile(const CString& filename);
41 int PatchFile(const int strip, const int nIndex, const CString& sPath, const CString& sSavePath = L"", const CString& sBaseFile = L"", const bool force = false);
42 int GetNumberOfFiles() const { return static_cast<int>(m_arFileDiffs.size()); }
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 protected:
52 void FreeMemory();
53 BOOL HasExpandedKeyWords(const CString& line) const;
54 int CountMatches(const CString& path);
55 int CountDirMatches(const CString& path);
56 CString RemoveUnicodeBOM(const CString& str) const;
57 bool HasUnicodeBOM(const CString& str) const;
59 BOOL ParsePatchFile(CFileTextLines &PatchLines);
61 /**
62 * Strips the filename by removing m_nStrip prefixes.
64 CString Strip(const CString& filename) const;
65 struct Chunk
67 LONG lRemoveStart = 0;
68 LONG lRemoveLength = 0;
69 LONG lAddStart = 0;
70 LONG lAddLength = 0;
71 CStringArray arLines;
72 CStdDWORDArray arLinesStates;
73 std::vector<EOL> arEOLs;
76 struct Chunks
78 CString sFilePath;
79 CString sRevision;
80 CString sFilePath2;
81 CString sRevision2;
82 std::vector<std::unique_ptr<Chunk>> chunks;
83 int oldHasBom = -1;
84 int newHasBom = -1;
87 std::vector<std::unique_ptr<Chunks>> m_arFileDiffs;
88 CString m_sErrorMessage;
89 CFileTextLines::UnicodeType m_UnicodeType = CFileTextLines::UnicodeType::AUTOTYPE;
91 /**
92 * Defines how many prefixes are removed from the paths in the
93 * patch file. This allows applying patches which contain absolute
94 * paths or a prefix which differs in the patch and the working copy.
95 * Example: A filename like "/home/ts/my-working-copy/dir/file.txt"
96 * stripped by 4 prefixes is interpreted as "dir/file.txt"
98 int m_nStrip = 0;
100 #ifdef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
101 public:
102 const auto& GetChunks(int index) const { return m_arFileDiffs[index]->chunks; };
103 #endif