Extend static functions in CAppUtils with a window handle parameter
[TortoiseGit.git] / src / TortoiseProc / AppUtils.h
bloba0ff6bfb707c5ebb5630ceeb9c744d834e239e9d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2018 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
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 "GitRevLoglist.h"
22 #include "CommonAppUtils.h"
24 class CTGitPath;
25 struct git_cred;
26 struct git_transfer_progress;
27 class CIgnoreFile;
28 class ProjectProperties;
30 constexpr static inline int ConvertVersionToInt(unsigned __int8 major, unsigned __int8 minor, unsigned __int8 patchlevel, unsigned __int8 build = 0)
32 return (major << 24) + (minor << 16) + (patchlevel << 8) + build;
35 /**
36 * \ingroup TortoiseProc
37 * An utility class with static functions.
39 class CAppUtils : public CCommonAppUtils
41 public:
42 /**
43 * Flags for StartExtDiff function.
45 struct DiffFlags
47 bool bWait;
48 bool bBlame;
49 bool bReadOnly;
50 bool bAlternativeTool; // If true, invert selection of TortoiseGitMerge vs. external diff tool
52 DiffFlags(): bWait(false), bBlame(false), bReadOnly(false), bAlternativeTool(false) {}
53 DiffFlags& Wait(bool b = true) { bWait = b; return *this; }
54 DiffFlags& Blame(bool b = true) { bBlame = b; return *this; }
55 DiffFlags& ReadOnly(bool b = true) { bReadOnly = b; return *this; }
56 DiffFlags& AlternativeTool(bool b = true) { bAlternativeTool = b; return *this; }
59 CAppUtils() = delete;
61 public:
62 /**
63 * Launches the external merge program if there is one.
64 * \return TRUE if the program could be started
66 static BOOL StartExtMerge(bool bAlternativeTool,
67 const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,
68 const CString& basename = CString(), const CString& theirname = CString(), const CString& yourname = CString(),
69 const CString& mergedname = CString(), bool bReadOnly = false, HWND resolveMsgHwnd = nullptr, bool bDeleteBaseTheirsMineOnClose = false);
71 /**
72 * Starts the external patch program (currently always TortoiseGitMerge)
74 static BOOL StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir,
75 const CString& sOriginalDescription = CString(), const CString& sPatchedDescription = CString(),
76 BOOL bReversed = FALSE, BOOL bWait = FALSE);
78 /**
79 * Starts the external unified diff viewer (the app associated with *.diff or *.patch files).
80 * If no app is associated with those file types, the default text editor is used.
82 static BOOL StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait = FALSE, bool bAlternativeTool = false);
84 /**
85 * Sets up all the default diff and merge scripts.
86 * \param force if true, overwrite all existing entries
87 * \param either "Diff", "Merge" or an empty string
89 static bool SetupDiffScripts(bool force, const CString& type);
91 /**
92 * Starts the external diff application
94 static bool StartExtDiff(
95 const CString& file1, const CString& file2,
96 const CString& sName1, const CString& sName2,
97 const CString& originalFile1, const CString& originalFile2,
98 const CString& hash1, const CString& hash2, const DiffFlags& flags, int jumpToLine = 0);
101 * Launches the standard text viewer/editor application which is associated
102 * with txt files.
103 * \return TRUE if the program could be started.
105 static BOOL StartTextViewer(CString file);
108 * Checks if the given file has a size of less than four, which means
109 * an 'empty' file or just newlines, i.e. an empty diff.
111 static BOOL CheckForEmptyDiff(const CTGitPath& sDiffPath);
114 * Returns font name which is used for log messages, etc.
116 static CString GetLogFontName();
119 * Returns font size which is used for log messages, etc.
121 static DWORD GetLogFontSize();
124 * Create a font which can is used for log messages, etc
126 static void CreateFontForLogs(CFont& fontToCreate);
129 * Launch the external blame viewer
131 static bool LaunchTortoiseBlame(
132 const CString& sBlameFile, const CString& Rev, const CString& sParams = CString());
135 * Launch alternative editor
137 static bool LaunchAlternativeEditor(const CString& filename, bool uac = false);
140 * Sets the title of a dialog
142 static void SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname);
145 * Formats text in a rich edit control (version 2).
146 * text in between * chars is formatted bold
147 * text in between ^ chars is formatted italic
148 * text in between _ chars is underlined
150 static bool FormatTextInRichEditControl(CWnd * pWnd);
151 static bool FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end);
153 * implements URL searching with the same logic as CSciEdit::StyleURLs
155 static std::vector<CHARRANGE> FindURLMatches(const CString& msg);
156 static BOOL StyleURLs(const CString& msg, CWnd* pWnd);
159 * Replacement for GitDiff::ShowUnifiedDiff(), but started as a separate process.
161 static bool StartShowUnifiedDiff(HWND hWnd, const CTGitPath& url1, const CString& rev1,
162 const CTGitPath& url2, const CString& rev2,
164 //const GitRev& peg = GitRev(), const GitRev& headpeg = GitRev(),
165 bool bAlternateDiff = false,
166 bool bIgnoreAncestry = false,
167 bool blame = false,
168 bool bMerge = false,
169 bool bCompact = false,
170 bool bNoPrefix = false);
172 static bool Export(HWND hWnd, const CString* BashHash = nullptr, const CTGitPath* orgPath = nullptr);
173 static bool UpdateBranchDescription(const CString& branch, CString description);
174 static bool CreateBranchTag(HWND hWnd, bool isTag = true, const CString* commitHash = nullptr, bool switchNewBranch = false, LPCTSTR name = nullptr);
175 static bool Switch(HWND hWnd, const CString& initialRefName = CString());
176 static bool PerformSwitch(HWND hWnd, const CString& ref, bool bForce = false, const CString& sNewBranch = CString(), bool bBranchOverride = false, BOOL bTrack = 2, bool bMerge = false);
178 static bool IgnoreFile(HWND hWnd, const CTGitPathList& filelist, bool IsMask);
179 static bool GitReset(HWND hWnd, const CString* CommitHash, int type = 1);
180 static bool ConflictEdit(HWND hWnd, CTGitPath& file, bool bAlternativeTool = false, bool revertTheirMy = false, HWND resolveMsgHwnd = nullptr);
181 static void GetConflictTitles(CString* baseText, CString& mineText, CString& theirsText, bool rebaseActive);
183 static CString GetMergeTempFile(const CString& str, const CTGitPath& merge);
184 static bool StashSave(HWND hWnd, const CString& msg = CString(), bool showPull = false, bool pullShowPush = false, bool showMerge = false, const CString& mergeRev = CString());
185 static bool StashApply(HWND hWnd, CString ref, bool showChanges = true);
186 /** Execute "stash pop"
187 * showChanges
188 * 0: only show info on error (and allow to diff on error)
189 * 1: allow to open diff dialog on error or success
190 * 2: only show error or success message
192 static bool StashPop(HWND hWnd, int showChanges = 1);
194 static bool IsSSHPutty();
196 static bool LaunchRemoteSetting();
198 static bool LaunchPAgent(HWND hWnd, const CString* keyfile = nullptr, const CString* pRemote = nullptr);
200 static bool ShellOpen(const CString& file, HWND hwnd = nullptr);
201 static bool ShowOpenWithDialog(const CString& file, HWND hwnd = nullptr);
203 static CString GetClipboardLink(const CString& skipGitPrefix = L"", int paramsCount = 0);
204 static CString ChooseRepository(HWND hWnd, const CString* path);
206 static bool SendPatchMail(HWND hWnd, CTGitPathList& pathlist, bool bIsMainWnd = false);
207 static bool SendPatchMail(HWND hWnd, const CString& cmd, const CString& formatpatchoutput, bool bIsMainWnd = false);
209 static int SaveCommitUnicodeFile(const CString& filename, CString& mesage);
210 static bool MessageContainsConflictHints(HWND hWnd, const CString& message);
212 static int GetLogOutputEncode(CGit *pGit=&g_Git);
214 static bool Pull(HWND hWnd, bool showPush = false, bool showStashPop = false);
215 // rebase = 1: ask user what to do, rebase = 2: run autorebase
216 static bool RebaseAfterFetch(HWND hWnd, const CString& upstream = L"", int rebase = 0, bool preserveMerges = false);
217 static bool Fetch(HWND hWnd, const CString& remoteName = L"", bool allRemotes = false);
218 static bool DoPush(HWND hWnd, bool autoloadKey, bool pack, bool tags, bool allRemotes, bool allBranches, bool force, bool forceWithLease, const CString& localBranch, const CString& remote, const CString& remoteBranch, bool setUpstream, int recurseSubmodules);
219 static bool Push(HWND hWnd, const CString& selectLocalBranch = CString());
220 static bool RequestPull(HWND hWnd, const CString& endrevision = L"", const CString& repositoryUrl = L"", bool bIsMainWnd = false);
222 static void RemoveTrailSlash(CString &path);
224 static bool CheckUserData(HWND hWnd);
226 static BOOL Commit(HWND hWnd, const CString& bugid, BOOL bWholeProject, CString& sLogMsg,
227 CTGitPathList &pathList,
228 bool bSelectFilesForCommit);
230 static BOOL SVNDCommit(HWND hWnd);
231 static BOOL Merge(HWND hWnd, const CString* commit = nullptr, bool showStashPop = false);
232 static BOOL MergeAbort(HWND hWnd);
233 static void RemoveTempMergeFile(const CTGitPath& path);
234 static void EditNote(HWND hWnd, GitRevLoglist* rev, ProjectProperties* projectProperties);
235 static int GetMsysgitVersion(HWND hWnd);
236 static bool IsGitVersionNewerOrEqual(HWND hWnd, unsigned __int8 major, unsigned __int8 minor, unsigned __int8 patchlevel = 0, unsigned __int8 build = 0);
237 static void MarkWindowAsUnpinnable(HWND hWnd);
239 static bool BisectStart(HWND hWnd, const CString& lastGood, const CString& firstBad, bool bIsMainWnd = false);
240 static bool BisectOperation(HWND hWnd, const CString& op, const CString& ref = L"", bool bIsMainWnd = false);
242 static int Git2GetUserPassword(git_cred **out, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload);
244 static int Git2CertificateCheck(git_cert *cert, int valid, const char* host, void *payload);
246 static int ExploreTo(HWND hwnd, CString path);
248 static bool DeleteRef(CWnd* parent, const CString& ref);
250 enum resolve_with {
251 RESOLVE_WITH_CURRENT,
252 RESOLVE_WITH_MINE,
253 RESOLVE_WITH_THEIRS,
256 static int ResolveConflict(HWND hWnd, CTGitPath& path, resolve_with resolveWith);
258 static bool IsTGitRebaseActive(HWND hWnd);
260 private:
261 static CString PickDiffTool(const CTGitPath& file1, const CTGitPath& file2);
263 static bool OpenIgnoreFile(HWND hWnd, CIgnoreFile& file, const CString& filename);
265 static void DescribeConflictFile(bool mode, bool base,CString &descript);