1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
4 // Copyright (C) 2003-2011, 2013-2014 - 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.
21 #include "TortoiseProc.h"
22 #include "PathUtils.h"
24 #include "StringUtils.h"
25 #include "MessageBox.h"
29 #include "UnicodeUtils.h"
30 #include "ExportDlg.h"
31 #include "ProgressDlg.h"
32 #include "GitAdminDir.h"
33 #include "ProgressDlg.h"
34 #include "BrowseFolder.h"
35 #include "DirFileEnum.h"
36 #include "MessageBox.h"
37 #include "GitStatus.h"
38 #include "CreateBranchTagDlg.h"
39 #include "GitSwitchDlg.h"
41 #include "DeleteConflictDlg.h"
42 #include "ChangedDlg.h"
43 #include "SendMailDlg.h"
44 #include "GitProgressDlg.h"
46 #include "CommitDlg.h"
48 #include "MergeAbortDlg.h"
50 #include "..\Settings\Settings.h"
52 #include "SVNDCommitDlg.h"
53 #include "requestpulldlg.h"
54 #include "PullFetchDlg.h"
55 #include "FileDiffDlg.h"
56 #include "RebaseDlg.h"
58 #include "StashSave.h"
59 #include "IgnoreDlg.h"
60 #include "FormatMessageWrapper.h"
61 #include "SmartHandle.h"
62 #include "BisectStartDlg.h"
63 #include "SysProgressDlg.h"
64 #include "UserPassword.h"
67 #include "ProgressCommands/ResetProgressCommand.h"
68 #include "ProgressCommands/FetchProgressCommand.h"
69 #include "ProgressCommands/SendMailProgressCommand.h"
70 #include "CertificateValidationHelper.h"
71 #include "CheckCertificateDlg.h"
73 static struct last_accepted_cert
{
86 boolean
cmp(git_cert_x509
* cert
)
88 return len
> 0 && len
== cert
->len
&& memcmp(data
, cert
->data
, len
) == 0;
90 void set(git_cert_x509
* cert
)
100 memcpy(data
, cert
->data
, len
);
102 } last_accepted_cert
;
104 static bool DoFetch(const CString
& url
, const bool fetchAllRemotes
, const bool loadPuttyAgent
, const int prune
, const bool bDepth
, const int nDepth
, const int fetchTags
, const CString
& remoteBranch
, const boolean runRebase
);
106 CAppUtils::CAppUtils(void)
110 CAppUtils::~CAppUtils(void)
114 bool CAppUtils::StashSave(const CString
& msg
)
117 dlg
.m_sMessage
= msg
;
118 if (dlg
.DoModal() == IDOK
)
121 cmd
= _T("git.exe stash save");
123 if (CAppUtils::GetMsysgitVersion() >= 0x01070700)
125 if (dlg
.m_bIncludeUntracked
)
126 cmd
+= _T(" --include-untracked");
131 if (!dlg
.m_sMessage
.IsEmpty())
133 CString message
= dlg
.m_sMessage
;
134 message
.Replace(_T("\""), _T("\"\""));
135 cmd
+= _T(" -- \"") + message
+ _T("\"");
138 CProgressDlg progress
;
139 progress
.m_GitCmd
= cmd
;
140 return (progress
.DoModal() == IDOK
);
145 bool CAppUtils::StashApply(CString ref
, bool showChanges
/* true */)
148 cmd
= _T("git.exe stash apply ");
149 if (ref
.Find(_T("refs/")) == 0)
151 if (ref
.Find(_T("stash{")) == 0)
152 ref
= _T("stash@") + ref
.Mid(5);
155 CSysProgressDlg sysProgressDlg
;
156 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
157 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
158 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
159 sysProgressDlg
.SetShowProgressBar(false);
160 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
161 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
163 int ret
= g_Git
.Run(cmd
, &out
, CP_UTF8
);
165 sysProgressDlg
.Stop();
167 bool hasConflicts
= (out
.Find(_T("CONFLICT")) >= 0);
168 if (ret
&& !(ret
== 1 && hasConflicts
))
170 CMessageBox::Show(NULL
, CString(MAKEINTRESOURCE(IDS_PROC_STASHAPPLYFAILED
)) + _T("\n") + out
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
175 message
.LoadString(IDS_PROC_STASHAPPLYSUCCESS
);
177 message
.LoadString(IDS_PROC_STASHAPPLYFAILEDCONFLICTS
);
180 if(CMessageBox::Show(NULL
,message
+ _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES
))
181 ,_T("TortoiseGit"),MB_YESNO
|MB_ICONINFORMATION
) == IDYES
)
184 dlg
.m_pathList
.AddPath(CTGitPath());
191 CMessageBox::Show(NULL
, message
,_T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
198 bool CAppUtils::StashPop(bool showChanges
/* true */)
201 cmd
=_T("git.exe stash pop ");
203 CSysProgressDlg sysProgressDlg
;
204 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
205 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
206 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
207 sysProgressDlg
.SetShowProgressBar(false);
208 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
209 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
211 int ret
= g_Git
.Run(cmd
, &out
, CP_UTF8
);
213 sysProgressDlg
.Stop();
215 bool hasConflicts
= (out
.Find(_T("CONFLICT")) >= 0);
216 if (ret
&& !(ret
== 1 && hasConflicts
))
218 CMessageBox::Show(NULL
,CString(MAKEINTRESOURCE(IDS_PROC_STASHPOPFAILED
)) + _T("\n") + out
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
223 message
.LoadString(IDS_PROC_STASHPOPSUCCESS
);
225 message
.LoadString(IDS_PROC_STASHPOPFAILEDCONFLICTS
);
228 if(CMessageBox::Show(NULL
,CString(message
+ _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES
)))
229 ,_T("TortoiseGit"),MB_YESNO
|MB_ICONINFORMATION
) == IDYES
)
232 dlg
.m_pathList
.AddPath(CTGitPath());
239 CMessageBox::Show(NULL
, message
,_T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
246 BOOL
CAppUtils::StartExtMerge(
247 const CTGitPath
& basefile
, const CTGitPath
& theirfile
, const CTGitPath
& yourfile
, const CTGitPath
& mergedfile
,
248 const CString
& basename
, const CString
& theirname
, const CString
& yourname
, const CString
& mergedname
, bool bReadOnly
,
252 CRegString regCom
= CRegString(_T("Software\\TortoiseGit\\Merge"));
253 CString ext
= mergedfile
.GetFileExtension();
254 CString com
= regCom
;
255 bool bInternal
= false;
259 // is there an extension specific merge tool?
260 CRegString
mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext
.MakeLower());
261 if (!CString(mergetool
).IsEmpty())
266 // is there a filename specific merge tool?
267 CRegString
mergetool(_T("Software\\TortoiseGit\\MergeTools\\.") + mergedfile
.GetFilename().MakeLower());
268 if (!CString(mergetool
).IsEmpty())
273 if (com
.IsEmpty()||(com
.Left(1).Compare(_T("#"))==0))
275 // Maybe we should use TortoiseIDiff?
276 if ((ext
== _T(".jpg")) || (ext
== _T(".jpeg")) ||
277 (ext
== _T(".bmp")) || (ext
== _T(".gif")) ||
278 (ext
== _T(".png")) || (ext
== _T(".ico")) ||
279 (ext
== _T(".tif")) || (ext
== _T(".tiff")) ||
280 (ext
== _T(".dib")) || (ext
== _T(".emf")) ||
283 com
= CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe");
284 com
= _T("\"") + com
+ _T("\"");
285 com
= com
+ _T(" /base:%base /theirs:%theirs /mine:%mine /result:%merged");
286 com
= com
+ _T(" /basetitle:%bname /theirstitle:%tname /minetitle:%yname");
290 // use TortoiseGitMerge
292 com
= CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe");
293 com
= _T("\"") + com
+ _T("\"");
294 com
= com
+ _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
295 com
= com
+ _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
296 com
+= _T(" /saverequired");
300 s
.Format(L
" /resolvemsghwnd:%I64d", (__int64
)resolveMsgHwnd
);
304 if (!g_sGroupingUUID
.IsEmpty())
306 com
+= L
" /groupuuid:\"";
307 com
+= g_sGroupingUUID
;
311 // check if the params are set. If not, just add the files to the command line
312 if ((com
.Find(_T("%merged"))<0)&&(com
.Find(_T("%base"))<0)&&(com
.Find(_T("%theirs"))<0)&&(com
.Find(_T("%mine"))<0))
314 com
+= _T(" \"")+basefile
.GetWinPathString()+_T("\"");
315 com
+= _T(" \"")+theirfile
.GetWinPathString()+_T("\"");
316 com
+= _T(" \"")+yourfile
.GetWinPathString()+_T("\"");
317 com
+= _T(" \"")+mergedfile
.GetWinPathString()+_T("\"");
319 if (basefile
.IsEmpty())
321 com
.Replace(_T("/base:%base"), _T(""));
322 com
.Replace(_T("%base"), _T(""));
325 com
.Replace(_T("%base"), _T("\"") + basefile
.GetWinPathString() + _T("\""));
326 if (theirfile
.IsEmpty())
328 com
.Replace(_T("/theirs:%theirs"), _T(""));
329 com
.Replace(_T("%theirs"), _T(""));
332 com
.Replace(_T("%theirs"), _T("\"") + theirfile
.GetWinPathString() + _T("\""));
333 if (yourfile
.IsEmpty())
335 com
.Replace(_T("/mine:%mine"), _T(""));
336 com
.Replace(_T("%mine"), _T(""));
339 com
.Replace(_T("%mine"), _T("\"") + yourfile
.GetWinPathString() + _T("\""));
340 if (mergedfile
.IsEmpty())
342 com
.Replace(_T("/merged:%merged"), _T(""));
343 com
.Replace(_T("%merged"), _T(""));
346 com
.Replace(_T("%merged"), _T("\"") + mergedfile
.GetWinPathString() + _T("\""));
347 if (basename
.IsEmpty())
349 if (basefile
.IsEmpty())
351 com
.Replace(_T("/basename:%bname"), _T(""));
352 com
.Replace(_T("%bname"), _T(""));
356 com
.Replace(_T("%bname"), _T("\"") + basefile
.GetUIFileOrDirectoryName() + _T("\""));
360 com
.Replace(_T("%bname"), _T("\"") + basename
+ _T("\""));
361 if (theirname
.IsEmpty())
363 if (theirfile
.IsEmpty())
365 com
.Replace(_T("/theirsname:%tname"), _T(""));
366 com
.Replace(_T("%tname"), _T(""));
370 com
.Replace(_T("%tname"), _T("\"") + theirfile
.GetUIFileOrDirectoryName() + _T("\""));
374 com
.Replace(_T("%tname"), _T("\"") + theirname
+ _T("\""));
375 if (yourname
.IsEmpty())
377 if (yourfile
.IsEmpty())
379 com
.Replace(_T("/minename:%yname"), _T(""));
380 com
.Replace(_T("%yname"), _T(""));
384 com
.Replace(_T("%yname"), _T("\"") + yourfile
.GetUIFileOrDirectoryName() + _T("\""));
388 com
.Replace(_T("%yname"), _T("\"") + yourname
+ _T("\""));
389 if (mergedname
.IsEmpty())
391 if (mergedfile
.IsEmpty())
393 com
.Replace(_T("/mergedname:%mname"), _T(""));
394 com
.Replace(_T("%mname"), _T(""));
398 com
.Replace(_T("%mname"), _T("\"") + mergedfile
.GetUIFileOrDirectoryName() + _T("\""));
402 com
.Replace(_T("%mname"), _T("\"") + mergedname
+ _T("\""));
404 if ((bReadOnly
)&&(bInternal
))
405 com
+= _T(" /readonly");
407 if(!LaunchApplication(com
, IDS_ERR_EXTMERGESTART
, false))
415 BOOL
CAppUtils::StartExtPatch(const CTGitPath
& patchfile
, const CTGitPath
& dir
, const CString
& sOriginalDescription
, const CString
& sPatchedDescription
, BOOL bReversed
, BOOL bWait
)
418 // use TortoiseGitMerge
419 viewer
= CPathUtils::GetAppDirectory();
420 viewer
+= _T("TortoiseGitMerge.exe");
422 viewer
= _T("\"") + viewer
+ _T("\"");
423 viewer
= viewer
+ _T(" /diff:\"") + patchfile
.GetWinPathString() + _T("\"");
424 viewer
= viewer
+ _T(" /patchpath:\"") + dir
.GetWinPathString() + _T("\"");
426 viewer
+= _T(" /reversedpatch");
427 if (!sOriginalDescription
.IsEmpty())
428 viewer
= viewer
+ _T(" /patchoriginal:\"") + sOriginalDescription
+ _T("\"");
429 if (!sPatchedDescription
.IsEmpty())
430 viewer
= viewer
+ _T(" /patchpatched:\"") + sPatchedDescription
+ _T("\"");
431 if (!g_sGroupingUUID
.IsEmpty())
433 viewer
+= L
" /groupuuid:\"";
434 viewer
+= g_sGroupingUUID
;
437 if(!LaunchApplication(viewer
, IDS_ERR_DIFFVIEWSTART
, !!bWait
))
444 CString
CAppUtils::PickDiffTool(const CTGitPath
& file1
, const CTGitPath
& file2
)
446 CString difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file2
.GetFilename().MakeLower());
447 if (!difftool
.IsEmpty())
449 difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file1
.GetFilename().MakeLower());
450 if (!difftool
.IsEmpty())
453 // Is there an extension specific diff tool?
454 CString ext
= file2
.GetFileExtension().MakeLower();
457 difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext
);
458 if (!difftool
.IsEmpty())
460 // Maybe we should use TortoiseIDiff?
461 if ((ext
== _T(".jpg")) || (ext
== _T(".jpeg")) ||
462 (ext
== _T(".bmp")) || (ext
== _T(".gif")) ||
463 (ext
== _T(".png")) || (ext
== _T(".ico")) ||
464 (ext
== _T(".tif")) || (ext
== _T(".tiff")) ||
465 (ext
== _T(".dib")) || (ext
== _T(".emf")) ||
469 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe") + _T("\"") +
470 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname") +
471 L
" /groupuuid:\"" + g_sGroupingUUID
+ L
"\"";
475 // Finally, pick a generic external diff tool
476 difftool
= CRegString(_T("Software\\TortoiseGit\\Diff"));
480 bool CAppUtils::StartExtDiff(
481 const CString
& file1
, const CString
& file2
,
482 const CString
& sName1
, const CString
& sName2
,
483 const CString
& originalFile1
, const CString
& originalFile2
,
484 const git_revnum_t
& hash1
, const git_revnum_t
& hash2
,
485 const DiffFlags
& flags
, int jumpToLine
)
489 CRegDWORD
blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE
);
490 if (!flags
.bBlame
|| !(DWORD
)blamediff
)
492 viewer
= PickDiffTool(file1
, file2
);
493 // If registry entry for a diff program is commented out, use TortoiseGitMerge.
494 bool bCommentedOut
= viewer
.Left(1) == _T("#");
495 if (flags
.bAlternativeTool
)
497 // Invert external vs. internal diff tool selection.
499 viewer
.Delete(0); // uncomment
503 else if (bCommentedOut
)
507 bool bInternal
= viewer
.IsEmpty();
511 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe") + _T("\"") +
512 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname") +
513 _T(" /basereflectedname:%bpath /minereflectedname:%ypath");
514 if (!g_sGroupingUUID
.IsEmpty())
516 viewer
+= L
" /groupuuid:\"";
517 viewer
+= g_sGroupingUUID
;
521 viewer
+= _T(" /blame");
523 // check if the params are set. If not, just add the files to the command line
524 if ((viewer
.Find(_T("%base"))<0)&&(viewer
.Find(_T("%mine"))<0))
526 viewer
+= _T(" \"")+file1
+_T("\"");
527 viewer
+= _T(" \"")+file2
+_T("\"");
529 if (viewer
.Find(_T("%base")) >= 0)
531 viewer
.Replace(_T("%base"), _T("\"")+file1
+_T("\""));
533 if (viewer
.Find(_T("%mine")) >= 0)
535 viewer
.Replace(_T("%mine"), _T("\"")+file2
+_T("\""));
538 if (sName1
.IsEmpty())
539 viewer
.Replace(_T("%bname"), _T("\"") + file1
+ _T("\""));
541 viewer
.Replace(_T("%bname"), _T("\"") + sName1
+ _T("\""));
543 if (sName2
.IsEmpty())
544 viewer
.Replace(_T("%yname"), _T("\"") + file2
+ _T("\""));
546 viewer
.Replace(_T("%yname"), _T("\"") + sName2
+ _T("\""));
548 viewer
.Replace(_T("%bpath"), _T("\"") + originalFile1
+ _T("\""));
549 viewer
.Replace(_T("%ypath"), _T("\"") + originalFile2
+ _T("\""));
551 viewer
.Replace(_T("%brev"), _T("\"") + hash1
+ _T("\""));
552 viewer
.Replace(_T("%yrev"), _T("\"") + hash2
+ _T("\""));
554 if (flags
.bReadOnly
&& bInternal
)
555 viewer
+= _T(" /readonly");
560 temp
.Format(_T(" /line:%d"), jumpToLine
);
564 return LaunchApplication(viewer
, IDS_ERR_EXTDIFFSTART
, flags
.bWait
);
567 BOOL
CAppUtils::StartUnifiedDiffViewer(const CString
& patchfile
, const CString
& title
, BOOL bWait
)
570 CRegString v
= CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
572 if (viewer
.IsEmpty() || (viewer
.Left(1).Compare(_T("#"))==0))
574 // use TortoiseGitUDiff
575 viewer
= CPathUtils::GetAppDirectory();
576 viewer
+= _T("TortoiseGitUDiff.exe");
577 // enquote the path to TortoiseGitUDiff
578 viewer
= _T("\"") + viewer
+ _T("\"");
580 viewer
= viewer
+ _T(" /patchfile:%1 /title:\"%title\"");
581 if (!g_sGroupingUUID
.IsEmpty())
583 viewer
+= L
" /groupuuid:\"";
584 viewer
+= g_sGroupingUUID
;
588 if (viewer
.Find(_T("%1"))>=0)
590 if (viewer
.Find(_T("\"%1\"")) >= 0)
591 viewer
.Replace(_T("%1"), patchfile
);
593 viewer
.Replace(_T("%1"), _T("\"") + patchfile
+ _T("\""));
596 viewer
+= _T(" \"") + patchfile
+ _T("\"");
597 if (viewer
.Find(_T("%title")) >= 0)
599 viewer
.Replace(_T("%title"), title
);
602 if(!LaunchApplication(viewer
, IDS_ERR_DIFFVIEWSTART
, !!bWait
))
609 BOOL
CAppUtils::StartTextViewer(CString file
)
612 CRegString txt
= CRegString(_T(".txt\\"), _T(""), FALSE
, HKEY_CLASSES_ROOT
);
614 viewer
= viewer
+ _T("\\Shell\\Open\\Command\\");
615 CRegString txtexe
= CRegString(viewer
, _T(""), FALSE
, HKEY_CLASSES_ROOT
);
618 DWORD len
= ExpandEnvironmentStrings(viewer
, NULL
, 0);
619 std::unique_ptr
<TCHAR
[]> buf(new TCHAR
[len
+ 1]);
620 ExpandEnvironmentStrings(viewer
, buf
.get(), len
);
622 len
= ExpandEnvironmentStrings(file
, NULL
, 0);
623 std::unique_ptr
<TCHAR
[]> buf2(new TCHAR
[len
+ 1]);
624 ExpandEnvironmentStrings(file
, buf2
.get(), len
);
626 file
= _T("\"")+file
+_T("\"");
627 if (viewer
.IsEmpty())
629 viewer
= _T("RUNDLL32 Shell32,OpenAs_RunDLL");
631 if (viewer
.Find(_T("\"%1\"")) >= 0)
633 viewer
.Replace(_T("\"%1\""), file
);
635 else if (viewer
.Find(_T("%1")) >= 0)
637 viewer
.Replace(_T("%1"), file
);
645 if(!LaunchApplication(viewer
, IDS_ERR_TEXTVIEWSTART
, false))
652 BOOL
CAppUtils::CheckForEmptyDiff(const CTGitPath
& sDiffPath
)
655 CAutoFile hFile
= ::CreateFile(sDiffPath
.GetWinPath(), GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
658 length
= ::GetFileSize(hFile
, NULL
);
665 void CAppUtils::CreateFontForLogs(CFont
& fontToCreate
)
668 HDC hScreenDC
= ::GetDC(NULL
);
669 logFont
.lfHeight
= -MulDiv((DWORD
)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC
, LOGPIXELSY
), 72);
670 ::ReleaseDC(NULL
, hScreenDC
);
672 logFont
.lfEscapement
= 0;
673 logFont
.lfOrientation
= 0;
674 logFont
.lfWeight
= FW_NORMAL
;
675 logFont
.lfItalic
= 0;
676 logFont
.lfUnderline
= 0;
677 logFont
.lfStrikeOut
= 0;
678 logFont
.lfCharSet
= DEFAULT_CHARSET
;
679 logFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
680 logFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
681 logFont
.lfQuality
= DRAFT_QUALITY
;
682 logFont
.lfPitchAndFamily
= FF_DONTCARE
| FIXED_PITCH
;
683 _tcscpy_s(logFont
.lfFaceName
, 32, (LPCTSTR
)(CString
)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
684 VERIFY(fontToCreate
.CreateFontIndirect(&logFont
));
687 bool CAppUtils::LaunchPAgent(const CString
* keyfile
, const CString
* pRemote
)
701 cmd
.Format(_T("remote.%s.puttykeyfile"),remote
);
702 key
= g_Git
.GetConfigValue(cmd
);
710 CString proc
=CPathUtils::GetAppDirectory();
711 proc
+= _T("pageant.exe \"");
715 CString tempfile
= GetTempFile();
716 ::DeleteFile(tempfile
);
718 proc
+= _T(" -c \"");
719 proc
+= CPathUtils::GetAppDirectory();
720 proc
+= _T("tgittouch.exe\"");
725 CString appDir
= CPathUtils::GetAppDirectory();
726 bool b
= LaunchApplication(proc
, IDS_ERR_PAGEANT
, true, &appDir
);
731 while(!::PathFileExists(tempfile
))
736 break; //timeout 5 minutes
741 CMessageBox::Show(NULL
, IDS_ERR_PAEGENTTIMEOUT
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
743 ::DeleteFile(tempfile
);
746 bool CAppUtils::LaunchAlternativeEditor(const CString
& filename
, bool uac
)
748 CString editTool
= CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
749 if (editTool
.IsEmpty() || (editTool
.Left(1).Compare(_T("#"))==0)) {
750 editTool
= CPathUtils::GetAppDirectory() + _T("notepad2.exe");
754 sCmd
.Format(_T("\"%s\" \"%s\""), editTool
, filename
);
756 LaunchApplication(sCmd
, NULL
, false, NULL
, uac
);
759 bool CAppUtils::LaunchRemoteSetting()
761 CTGitPath
path(g_Git
.m_CurrentDir
);
762 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
, &path
);
763 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
764 dlg
.SetTreeWidth(220);
765 dlg
.m_DefaultPage
= _T("gitremote");
772 * Launch the external blame viewer
774 bool CAppUtils::LaunchTortoiseBlame(const CString
& sBlameFile
,CString Rev
,const CString
& sParams
)
776 CString viewer
= _T("\"") + CPathUtils::GetAppDirectory();
777 viewer
+= _T("TortoiseGitBlame.exe");
778 viewer
+= _T("\" \"") + sBlameFile
+ _T("\"");
779 //viewer += _T(" \"") + sLogFile + _T("\"");
780 //viewer += _T(" \"") + sOriginalFile + _T("\"");
781 if(!Rev
.IsEmpty() && Rev
!= GIT_REV_ZERO
)
782 viewer
+= CString(_T(" /rev:"))+Rev
;
783 if (!g_sGroupingUUID
.IsEmpty())
785 viewer
+= L
" /groupuuid:\"";
786 viewer
+= g_sGroupingUUID
;
789 viewer
+= _T(" ")+sParams
;
791 return LaunchApplication(viewer
, IDS_ERR_TGITBLAME
, false);
794 bool CAppUtils::FormatTextInRichEditControl(CWnd
* pWnd
)
799 bool bStyled
= false;
800 pWnd
->GetWindowText(sText
);
801 // the rich edit control doesn't count the CR char!
802 // to be exact: CRLF is treated as one char.
803 sText
.Remove(_T('\r'));
805 // style each line separately
810 nNewlinePos
= sText
.Find('\n', offset
);
811 CString sLine
= nNewlinePos
>= 0 ? sText
.Mid(offset
, nNewlinePos
- offset
) : sText
.Mid(offset
);
815 while (FindStyleChars(sLine
, '*', start
, end
))
817 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
818 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
819 SetCharFormat(pWnd
, CFM_BOLD
, CFE_BOLD
);
825 while (FindStyleChars(sLine
, '^', start
, end
))
827 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
828 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
829 SetCharFormat(pWnd
, CFM_ITALIC
, CFE_ITALIC
);
835 while (FindStyleChars(sLine
, '_', start
, end
))
837 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
838 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
839 SetCharFormat(pWnd
, CFM_UNDERLINE
, CFE_UNDERLINE
);
843 offset
= nNewlinePos
+1;
844 } while(nNewlinePos
>=0);
848 bool CAppUtils::FindStyleChars(const CString
& sText
, TCHAR stylechar
, int& start
, int& end
)
851 int last
= sText
.GetLength() - 1;
852 bool bFoundMarker
= false;
853 TCHAR c
= i
== 0 ? _T('\0') : sText
[i
- 1];
854 TCHAR nextChar
= i
>= last
? _T('\0') : sText
[i
+ 1];
856 // find a starting marker
861 nextChar
= sText
[i
+ 1];
863 // IsCharAlphaNumeric can be somewhat expensive.
864 // Long lines of "*****" or "----" will be pre-empted efficiently
865 // by the (c != nextChar) condition.
867 if ((c
== stylechar
) && (c
!= nextChar
))
869 if (IsCharAlphaNumeric(nextChar
) && !IsCharAlphaNumeric(prevChar
))
881 // find ending marker
884 bFoundMarker
= false;
891 if ((i
== last
) || (!IsCharAlphaNumeric(sText
[i
+ 1]) && IsCharAlphaNumeric(prevChar
)))
904 bool CAppUtils::StartShowUnifiedDiff(HWND hWnd
, const CTGitPath
& url1
, const git_revnum_t
& rev1
,
905 const CTGitPath
& /*url2*/, const git_revnum_t
& rev2
,
906 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
907 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */,
908 bool /* blame = false */,
913 if (GetMsysgitVersion() > 0x01080100)
914 diffContext
= g_Git
.GetConfigValueInt32(_T("diff.context"), -1);
915 CString tempfile
=GetTempFile();
916 if (g_Git
.GetUnifiedDiff(url1
, rev1
, rev2
, tempfile
, bMerge
, bCombine
, diffContext
))
918 CMessageBox::Show(hWnd
, g_Git
.GetGitLastErr(_T("Could not get unified diff."), CGit::GIT_CMD_DIFF
), _T("TortoiseGit"), MB_OK
);
921 CAppUtils::StartUnifiedDiffViewer(tempfile
, rev1
+ _T(":") + rev2
);
925 sCmd
.Format(_T("%s /command:showcompare /unified"),
926 (LPCTSTR
)(CPathUtils::GetAppDirectory()+_T("TortoiseGitProc.exe")));
927 sCmd
+= _T(" /url1:\"") + url1
.GetGitPathString() + _T("\"");
929 sCmd
+= _T(" /revision1:") + rev1
.ToString();
930 sCmd
+= _T(" /url2:\"") + url2
.GetGitPathString() + _T("\"");
932 sCmd
+= _T(" /revision2:") + rev2
.ToString();
934 sCmd
+= _T(" /pegrevision:") + peg
.ToString();
935 if (headpeg
.IsValid())
936 sCmd
+= _T(" /headpegrevision:") + headpeg
.ToString();
939 sCmd
+= _T(" /alternatediff");
942 sCmd
+= _T(" /ignoreancestry");
946 sCmd
+= _T(" /hwnd:");
948 _stprintf_s(buf
, 30, _T("%p"), (void*)hWnd
);
952 return CAppUtils::LaunchApplication(sCmd
, NULL
, false);
957 bool CAppUtils::SetupDiffScripts(bool force
, const CString
& type
)
959 CString scriptsdir
= CPathUtils::GetAppParentDirectory();
960 scriptsdir
+= _T("Diff-Scripts");
961 CSimpleFileFind
files(scriptsdir
);
962 while (files
.FindNextFileNoDirectories())
964 CString file
= files
.GetFilePath();
965 CString filename
= files
.GetFileName();
966 CString ext
= file
.Mid(file
.ReverseFind('-') + 1);
967 ext
= _T(".") + ext
.Left(ext
.ReverseFind('.'));
968 std::set
<CString
> extensions
;
969 extensions
.insert(ext
);
971 if (file
.Right(3).CompareNoCase(_T("vbs"))==0)
973 kind
= _T(" //E:vbscript");
975 if (file
.Right(2).CompareNoCase(_T("js"))==0)
977 kind
= _T(" //E:javascript");
979 // open the file, read the first line and find possible extensions
980 // this script can handle
983 CStdioFile
f(file
, CFile::modeRead
| CFile::shareDenyNone
);
985 if (f
.ReadString(extline
))
987 if ((extline
.GetLength() > 15 ) &&
988 ((extline
.Left(15).Compare(_T("// extensions: ")) == 0) ||
989 (extline
.Left(14).Compare(_T("' extensions: ")) == 0)))
991 if (extline
[0] == '/')
992 extline
= extline
.Mid(15);
994 extline
= extline
.Mid(14);
997 sToken
= extline
.Tokenize(_T(";"), curPos
);
998 while (!sToken
.IsEmpty())
1000 if (!sToken
.IsEmpty())
1002 if (sToken
[0] != '.')
1003 sToken
= _T(".") + sToken
;
1004 extensions
.insert(sToken
);
1006 sToken
= extline
.Tokenize(_T(";"), curPos
);
1012 catch (CFileException
* e
)
1017 for (std::set
<CString
>::const_iterator it
= extensions
.begin(); it
!= extensions
.end(); ++it
)
1019 if (type
.IsEmpty() || (type
.Compare(_T("Diff")) == 0))
1021 if (filename
.Left(5).CompareNoCase(_T("diff-")) == 0)
1023 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + *it
);
1024 CString diffregstring
= diffreg
;
1025 if (force
|| (diffregstring
.IsEmpty()) || (diffregstring
.Find(filename
) >= 0))
1026 diffreg
= _T("wscript.exe \"") + file
+ _T("\" %base %mine") + kind
;
1029 if (type
.IsEmpty() || (type
.Compare(_T("Merge"))==0))
1031 if (filename
.Left(6).CompareNoCase(_T("merge-"))==0)
1033 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\MergeTools\\") + *it
);
1034 CString diffregstring
= diffreg
;
1035 if (force
|| (diffregstring
.IsEmpty()) || (diffregstring
.Find(filename
) >= 0))
1036 diffreg
= _T("wscript.exe \"") + file
+ _T("\" %merged %theirs %mine %base") + kind
;
1045 bool CAppUtils::Export(CString
*BashHash
, const CTGitPath
*orgPath
)
1047 // ask from where the export has to be done
1050 dlg
.m_initialRefName
=*BashHash
;
1053 if (PathIsRelative(orgPath
->GetWinPath()))
1054 dlg
.m_orgPath
= g_Git
.CombinePath(orgPath
);
1056 dlg
.m_orgPath
= *orgPath
;
1059 if (dlg
.DoModal() == IDOK
)
1062 cmd
.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s --"),
1063 dlg
.m_strFile
, g_Git
.FixBranchName(dlg
.m_VersionName
));
1067 pro
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
1071 postCmdList
.push_back(PostCmd(IDI_EXPLORER
, IDS_STATUSLIST_CONTEXT_EXPLORE
, [&]{ CAppUtils::ExploreTo(hWndExplorer
, dlg
.m_strFile
); }));
1075 if (!dlg
.m_bWholeProject
&& !dlg
.m_orgPath
.IsEmpty() && PathIsDirectory(dlg
.m_orgPath
.GetWinPathString()))
1077 git
.m_CurrentDir
= dlg
.m_orgPath
.GetWinPathString();
1080 return (pro
.DoModal() == IDOK
);
1085 bool CAppUtils::CreateBranchTag(bool IsTag
,CString
*CommitHash
, bool switch_new_brach
)
1087 CCreateBranchTagDlg dlg
;
1089 dlg
.m_bSwitch
=switch_new_brach
;
1092 dlg
.m_initialRefName
= *CommitHash
;
1094 if(dlg
.DoModal()==IDOK
)
1099 if(dlg
.m_bTrack
== TRUE
)
1100 track
=_T(" --track ");
1101 else if(dlg
.m_bTrack
== FALSE
)
1102 track
=_T(" --no-track");
1113 cmd
.Format(_T("git.exe tag %s %s %s %s"),
1116 dlg
.m_BranchTagName
,
1117 g_Git
.FixBranchName(dlg
.m_VersionName
)
1120 if(!dlg
.m_Message
.Trim().IsEmpty())
1122 CString tempfile
= ::GetTempFile();
1123 if (CAppUtils::SaveCommitUnicodeFile(tempfile
, dlg
.m_Message
))
1125 CMessageBox::Show(nullptr, _T("Could not save tag message"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
1128 cmd
+= _T(" -F ")+tempfile
;
1133 cmd
.Format(_T("git.exe branch %s %s %s %s"),
1136 dlg
.m_BranchTagName
,
1137 g_Git
.FixBranchName(dlg
.m_VersionName
)
1141 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1143 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1146 if( !IsTag
&& dlg
.m_bSwitch
)
1148 // it is a new branch and the user has requested to switch to it
1149 PerformSwitch(dlg
.m_BranchTagName
);
1157 bool CAppUtils::Switch(CString initialRefName
)
1160 if(!initialRefName
.IsEmpty())
1161 dlg
.m_initialRefName
= initialRefName
;
1163 if (dlg
.DoModal() == IDOK
)
1167 branch
= dlg
.m_NewBranch
;
1169 // if refs/heads/ is not stripped, checkout will detach HEAD
1170 // checkout prefers branches on name clashes (with tags)
1171 if (dlg
.m_VersionName
.Left(11) ==_T("refs/heads/") && dlg
.m_bBranchOverride
!= TRUE
)
1172 dlg
.m_VersionName
= dlg
.m_VersionName
.Mid(11);
1174 return PerformSwitch(dlg
.m_VersionName
, dlg
.m_bForce
== TRUE
, branch
, dlg
.m_bBranchOverride
== TRUE
, dlg
.m_bTrack
, dlg
.m_bMerge
== TRUE
);
1179 bool CAppUtils::PerformSwitch(CString ref
, bool bForce
/* false */, CString sNewBranch
/* CString() */, bool bBranchOverride
/* false */, BOOL bTrack
/* 2 */, bool bMerge
/* false */)
1187 if(!sNewBranch
.IsEmpty()){
1188 if (bBranchOverride
)
1190 branch
.Format(_T("-B %s"), sNewBranch
);
1194 branch
.Format(_T("-b %s"), sNewBranch
);
1197 track
= _T("--track");
1198 else if (bTrack
== FALSE
)
1199 track
= _T("--no-track");
1204 merge
= _T("--merge");
1206 cmd
.Format(_T("git.exe checkout %s %s %s %s %s --"),
1211 g_Git
.FixBranchName(ref
));
1213 CProgressDlg progress
;
1214 progress
.m_GitCmd
= cmd
;
1216 CString currentBranch
;
1217 bool hasBranch
= CGit::GetCurrentBranchFromFile(g_Git
.m_CurrentDir
, currentBranch
) == 0;
1218 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
1222 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
1223 if (gitPath
.HasSubmodules())
1225 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, [&]
1228 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
1229 RunTortoiseGitProc(sCmd
);
1233 postCmdList
.push_back(PostCmd(IDI_MERGE
, IDS_MENUMERGE
, [&]{ Merge(¤tBranch
); }));
1237 if (!CGit::GetCurrentBranchFromFile(g_Git
.m_CurrentDir
, newBranch
))
1238 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUPULL
, [&]{ Pull(); }));
1240 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_MENUCOMMIT
, []{
1241 CTGitPathList pathlist
;
1242 CTGitPathList selectedlist
;
1243 bool bSelectFilesForCommit
= !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE
));
1245 Commit(CString(), false, str
, pathlist
, selectedlist
, bSelectFilesForCommit
);
1250 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ PerformSwitch(ref
, bForce
, sNewBranch
, bBranchOverride
, bTrack
, bMerge
); }));
1252 postCmdList
.push_back(PostCmd(IDI_SWITCH
, IDS_SWITCH_WITH_MERGE
, [&]{ PerformSwitch(ref
, bForce
, sNewBranch
, bBranchOverride
, bTrack
, true); }));
1256 INT_PTR ret
= progress
.DoModal();
1261 class CIgnoreFile
: public CStdioFile
1264 STRING_VECTOR m_Items
;
1267 virtual BOOL
ReadString(CString
& rString
)
1269 if (GetPosition() == 0)
1271 unsigned char utf8bom
[] = { 0xEF, 0xBB, 0xBF };
1272 char buf
[3] = { 0, 0, 0 };
1274 if (memcpy(buf
, utf8bom
, sizeof(utf8bom
)))
1281 char lastChar
= '\0';
1282 for (char c
= '\0'; Read(&c
, 1) == 1; lastChar
= c
)
1288 m_eol
= lastChar
== '\r' ? _T("\r\n") : _T("\n");
1296 rString
= CUnicodeUtils::GetUnicode(strA
);
1307 bool CAppUtils::OpenIgnoreFile(CIgnoreFile
&file
, const CString
& filename
)
1310 if (!file
.Open(filename
, CFile::modeCreate
| CFile::modeReadWrite
| CFile::modeNoTruncate
| CFile::typeBinary
))
1312 CMessageBox::Show(NULL
, filename
+ _T(" Open Failure"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
1316 if (file
.GetLength() > 0)
1319 while (file
.ReadString(fileText
))
1320 file
.m_Items
.push_back(fileText
);
1321 file
.Seek(file
.GetLength() - 1, 0);
1322 char lastchar
[1] = { 0 };
1323 file
.Read(lastchar
, 1);
1325 if (lastchar
[0] != '\n')
1327 CStringA eol
= CStringA(file
.m_eol
.IsEmpty() ? _T("\n") : file
.m_eol
);
1328 file
.Write(eol
, eol
.GetLength());
1337 bool CAppUtils::IgnoreFile(CTGitPathList
&path
,bool IsMask
)
1339 CIgnoreDlg ignoreDlg
;
1340 if (ignoreDlg
.DoModal() == IDOK
)
1343 ignorefile
= g_Git
.m_CurrentDir
+ _T("\\");
1345 switch (ignoreDlg
.m_IgnoreFile
)
1348 ignorefile
+= _T(".gitignore");
1351 g_GitAdminDir
.GetAdminDirPath(g_Git
.m_CurrentDir
, ignorefile
);
1352 ignorefile
+= _T("info/exclude");
1359 if (ignoreDlg
.m_IgnoreFile
!= 1 && !OpenIgnoreFile(file
, ignorefile
))
1362 for (int i
= 0; i
< path
.GetCount(); ++i
)
1364 if (ignoreDlg
.m_IgnoreFile
== 1)
1366 ignorefile
= g_Git
.CombinePath(path
[i
].GetContainingDirectory()) + _T("\\.gitignore");
1367 if (!OpenIgnoreFile(file
, ignorefile
))
1371 CString ignorePattern
;
1372 if (ignoreDlg
.m_IgnoreType
== 0)
1374 if (ignoreDlg
.m_IgnoreFile
!= 1 && !path
[i
].GetContainingDirectory().GetGitPathString().IsEmpty())
1375 ignorePattern
+= _T("/") + path
[i
].GetContainingDirectory().GetGitPathString();
1377 ignorePattern
+= _T("/");
1381 ignorePattern
+= _T("*") + path
[i
].GetFileExtension();
1385 ignorePattern
+= path
[i
].GetFileOrDirectoryName();
1388 // escape [ and ] so that files get ignored correctly
1389 ignorePattern
.Replace(_T("["), _T("\\["));
1390 ignorePattern
.Replace(_T("]"), _T("\\]"));
1393 for (size_t j
= 0; j
< file
.m_Items
.size(); ++j
)
1395 if (file
.m_Items
[j
] == ignorePattern
)
1403 file
.m_Items
.push_back(ignorePattern
);
1404 ignorePattern
+= file
.m_eol
.IsEmpty() ? _T("\n") : file
.m_eol
;
1405 CStringA ignorePatternA
= CUnicodeUtils::GetUTF8(ignorePattern
);
1406 file
.Write(ignorePatternA
, ignorePatternA
.GetLength());
1409 if (ignoreDlg
.m_IgnoreFile
== 1)
1413 if (ignoreDlg
.m_IgnoreFile
!= 1)
1427 static bool Reset(const CString
& resetTo
, int resetType
)
1434 type
= _T("--soft");
1437 type
= _T("--mixed");
1440 type
= _T("--hard");
1444 type
= _T("--mixed");
1447 cmd
.Format(_T("git.exe reset %s %s --"), type
, resetTo
);
1449 CProgressDlg progress
;
1450 progress
.m_GitCmd
= cmd
;
1452 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
1456 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ Reset(resetTo
, resetType
); }));
1460 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
1461 if (gitPath
.HasSubmodules() && resetType
== 2)
1463 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, [&]
1466 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
1467 CAppUtils::RunTortoiseGitProc(sCmd
);
1473 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_RESET
))
1475 CGitProgressDlg gitdlg
;
1476 ResetProgressCommand resetProgressCommand
;
1477 gitdlg
.SetCommand(&resetProgressCommand
);
1478 resetProgressCommand
.m_PostCmdCallback
= progress
.m_PostCmdCallback
;
1479 resetProgressCommand
.SetRevision(resetTo
);
1480 resetProgressCommand
.SetResetType(resetType
);
1481 ret
= gitdlg
.DoModal();
1484 ret
= progress
.DoModal();
1489 bool CAppUtils::GitReset(CString
*CommitHash
,int type
)
1492 dlg
.m_ResetType
=type
;
1493 dlg
.m_ResetToVersion
=*CommitHash
;
1494 dlg
.m_initialRefName
= *CommitHash
;
1495 if (dlg
.DoModal() == IDOK
)
1496 return Reset(dlg
.m_ResetToVersion
, dlg
.m_ResetType
);
1501 void CAppUtils::DescribeConflictFile(bool mode
, bool base
,CString
&descript
)
1505 descript
= CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE
));
1510 descript
= CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED
));
1513 descript
= CString(MAKEINTRESOURCE(IDS_PROC_CREATED
));
1517 void CAppUtils::RemoveTempMergeFile(CTGitPath
&path
)
1519 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("LOCAL"), path
));
1520 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("REMOTE"), path
));
1521 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("BASE"), path
));
1523 CString
CAppUtils::GetMergeTempFile(CString type
,CTGitPath
&merge
)
1526 file
= g_Git
.CombinePath(merge
.GetWinPathString() + _T(".") + type
+ merge
.GetFileExtension());
1531 bool ParseHashesFromLsFile(BYTE_VECTOR
&out
, CString
&hash1
, CString
&hash2
, CString
&hash3
)
1533 unsigned int pos
= 0;
1537 while (pos
>= 0 && pos
< out
.size())
1541 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1543 one
.Tokenize(_T("\t"), tabstart
);
1546 part
= one
.Tokenize(_T(" "), tabstart
); //Tag
1547 part
= one
.Tokenize(_T(" "), tabstart
); //Mode
1548 part
= one
.Tokenize(_T(" "), tabstart
); //Hash
1549 CString hash
= part
;
1550 part
= one
.Tokenize(_T("\t"), tabstart
); //Stage
1551 int stage
= _ttol(part
);
1554 else if (stage
== 2)
1556 else if (stage
== 3)
1562 pos
= out
.findNextString(pos
);
1568 bool CAppUtils::ConflictEdit(CTGitPath
& path
, bool /*bAlternativeTool = false*/, bool revertTheirMy
/*= false*/, HWND resolveMsgHwnd
/*= nullptr*/)
1572 CTGitPath merge
=path
;
1573 CTGitPath directory
= merge
.GetDirectory();
1575 // we have the conflicted file (%merged)
1576 // now look for the other required files
1578 //stat.GetStatus(merge);
1579 //if (stat.status == NULL)
1585 cmd
.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge
.GetGitPathString());
1587 if (g_Git
.Run(cmd
, &vector
))
1592 if (merge
.IsDirectory())
1594 CString baseHash
, localHash
, remoteHash
;
1595 if (!ParseHashesFromLsFile(vector
, baseHash
, localHash
, remoteHash
))
1599 msg
.Format(_T("BASE: %s\nLOCAL: %s\nREMOTE: %s"), baseHash
, localHash
, remoteHash
);
1600 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
);
1605 if (list
.ParserFromLsFile(vector
))
1607 CMessageBox::Show(NULL
, _T("Parse ls-files failed!"), _T("TortoiseGit"), MB_OK
);
1620 mine
.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge
));
1621 theirs
.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge
));
1625 mine
.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge
));
1626 theirs
.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge
));
1628 base
.SetFromGit(GetMergeTempFile(_T("BASE"),merge
));
1632 //format=_T("git.exe cat-file blob \":%d:%s\"");
1633 format
= _T("git.exe checkout-index --temp --stage=%d -- \"%s\"");
1635 //create a empty file, incase stage is not three
1636 tempfile
.Open(mine
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1638 tempfile
.Open(theirs
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1640 tempfile
.Open(base
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1643 bool b_base
=false, b_local
=false, b_remote
=false;
1645 for (int i
= 0; i
< list
.GetCount(); ++i
)
1652 if( list
[i
].m_Stage
== 1)
1654 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1656 outfile
= base
.GetWinPathString();
1659 if( list
[i
].m_Stage
== 2 )
1661 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1663 outfile
= mine
.GetWinPathString();
1666 if( list
[i
].m_Stage
== 3 )
1668 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1670 outfile
= theirs
.GetWinPathString();
1672 CString output
, err
;
1673 if(!outfile
.IsEmpty())
1674 if (!g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
1678 file
= output
.Tokenize(_T("\t"), start
);
1679 ::MoveFileEx(file
,outfile
,MOVEFILE_REPLACE_EXISTING
|MOVEFILE_COPY_ALLOWED
);
1683 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
1687 if(b_local
&& b_remote
)
1689 merge
.SetFromWin(g_Git
.CombinePath(merge
));
1691 bRet
= !!CAppUtils::StartExtMerge(base
, mine
, theirs
, merge
, _T("BASE"), _T("REMOTE"), _T("LOCAL"), CString(), false, resolveMsgHwnd
);
1693 bRet
= !!CAppUtils::StartExtMerge(base
, theirs
, mine
, merge
, _T("BASE"), _T("REMOTE"), _T("LOCAL"), CString(), false, resolveMsgHwnd
);
1698 ::DeleteFile(mine
.GetWinPathString());
1699 ::DeleteFile(theirs
.GetWinPathString());
1700 ::DeleteFile(base
.GetWinPathString());
1702 CDeleteConflictDlg dlg
;
1703 DescribeConflictFile(b_local
, b_base
,dlg
.m_LocalStatus
);
1704 DescribeConflictFile(b_remote
,b_base
,dlg
.m_RemoteStatus
);
1705 CGitHash localHash
, remoteHash
;
1706 if (!g_Git
.GetHash(localHash
, _T("HEAD")))
1707 dlg
.m_LocalHash
= localHash
.ToString();
1708 if (!g_Git
.GetHash(remoteHash
, _T("MERGE_HEAD")))
1709 dlg
.m_RemoteHash
= remoteHash
.ToString();
1710 else if (!g_Git
.GetHash(remoteHash
, _T("rebase-apply/original-commit")))
1711 dlg
.m_RemoteHash
= remoteHash
.ToString();
1712 else if (!g_Git
.GetHash(remoteHash
, _T("CHERRY_PICK_HEAD")))
1713 dlg
.m_RemoteHash
= remoteHash
.ToString();
1714 else if (!g_Git
.GetHash(remoteHash
, _T("REVERT_HEAD")))
1715 dlg
.m_RemoteHash
= remoteHash
.ToString();
1716 dlg
.m_bShowModifiedButton
=b_base
;
1717 dlg
.m_File
=merge
.GetGitPathString();
1718 if(dlg
.DoModal() == IDOK
)
1723 cmd
.Format(_T("git.exe rm -- \"%s\""),merge
.GetGitPathString());
1726 cmd
.Format(_T("git.exe add -- \"%s\""),merge
.GetGitPathString());
1728 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
1730 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1740 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1741 base
, theirs
, mine
, merge
);
1744 if (stat
.status
->text_status
== svn_wc_status_conflicted
)
1746 // we have a text conflict, use our merge tool to resolve the conflict
1748 CTSVNPath
theirs(directory
);
1749 CTSVNPath
mine(directory
);
1750 CTSVNPath
base(directory
);
1751 bool bConflictData
= false;
1753 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_new
))
1755 theirs
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_new
));
1756 bConflictData
= true;
1758 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_old
))
1760 base
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_old
));
1761 bConflictData
= true;
1763 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_wrk
))
1765 mine
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_wrk
));
1766 bConflictData
= true;
1773 bRet
= !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1774 base
, theirs
, mine
, merge
);
1777 if (stat
.status
->prop_status
== svn_wc_status_conflicted
)
1779 // we have a property conflict
1780 CTSVNPath
prej(directory
);
1781 if ((stat
.status
->entry
)&&(stat
.status
->entry
->prejfile
))
1783 prej
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->prejfile
));
1784 // there's a problem: the prej file contains a _description_ of the conflict, and
1785 // that description string might be translated. That means we have no way of parsing
1786 // the file to find out the conflicting values.
1787 // The only thing we can do: show a dialog with the conflict description, then
1788 // let the user either accept the existing property or open the property edit dialog
1789 // to manually change the properties and values. And a button to mark the conflict as
1791 CEditPropConflictDlg dlg
;
1792 dlg
.SetPrejFile(prej
);
1793 dlg
.SetConflictedItem(merge
);
1794 bRet
= (dlg
.DoModal() != IDCANCEL
);
1798 if (stat
.status
->tree_conflict
)
1800 // we have a tree conflict
1802 const SVNInfoData
* pInfoData
= info
.GetFirstFileInfo(merge
, SVNRev(), SVNRev());
1805 if (pInfoData
->treeconflict_kind
== svn_wc_conflict_kind_text
)
1807 CTSVNPath
theirs(directory
);
1808 CTSVNPath
mine(directory
);
1809 CTSVNPath
base(directory
);
1810 bool bConflictData
= false;
1812 if (pInfoData
->treeconflict_theirfile
)
1814 theirs
.AppendPathString(pInfoData
->treeconflict_theirfile
);
1815 bConflictData
= true;
1817 if (pInfoData
->treeconflict_basefile
)
1819 base
.AppendPathString(pInfoData
->treeconflict_basefile
);
1820 bConflictData
= true;
1822 if (pInfoData
->treeconflict_myfile
)
1824 mine
.AppendPathString(pInfoData
->treeconflict_myfile
);
1825 bConflictData
= true;
1832 bRet
= !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1833 base
, theirs
, mine
, merge
);
1835 else if (pInfoData
->treeconflict_kind
== svn_wc_conflict_kind_tree
)
1837 CString sConflictAction
;
1838 CString sConflictReason
;
1839 CString sResolveTheirs
;
1840 CString sResolveMine
;
1841 CTSVNPath treeConflictPath
= CTSVNPath(pInfoData
->treeconflict_path
);
1842 CString sItemName
= treeConflictPath
.GetUIFileOrDirectoryName();
1844 if (pInfoData
->treeconflict_nodekind
== svn_node_file
)
1846 switch (pInfoData
->treeconflict_operation
)
1848 case svn_wc_operation_update
:
1849 switch (pInfoData
->treeconflict_action
)
1851 case svn_wc_conflict_action_edit
:
1852 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEEDIT
, (LPCTSTR
)sItemName
);
1853 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1855 case svn_wc_conflict_action_add
:
1856 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEADD
, (LPCTSTR
)sItemName
);
1857 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1859 case svn_wc_conflict_action_delete
:
1860 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEDELETE
, (LPCTSTR
)sItemName
);
1861 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1865 case svn_wc_operation_switch
:
1866 switch (pInfoData
->treeconflict_action
)
1868 case svn_wc_conflict_action_edit
:
1869 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHEDIT
, (LPCTSTR
)sItemName
);
1870 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1872 case svn_wc_conflict_action_add
:
1873 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHADD
, (LPCTSTR
)sItemName
);
1874 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1876 case svn_wc_conflict_action_delete
:
1877 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHDELETE
, (LPCTSTR
)sItemName
);
1878 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1882 case svn_wc_operation_merge
:
1883 switch (pInfoData
->treeconflict_action
)
1885 case svn_wc_conflict_action_edit
:
1886 sConflictAction
.Format(IDS_TREECONFLICT_FILEMERGEEDIT
, (LPCTSTR
)sItemName
);
1887 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1889 case svn_wc_conflict_action_add
:
1890 sResolveTheirs
.Format(IDS_TREECONFLICT_FILEMERGEADD
, (LPCTSTR
)sItemName
);
1891 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1893 case svn_wc_conflict_action_delete
:
1894 sConflictAction
.Format(IDS_TREECONFLICT_FILEMERGEDELETE
, (LPCTSTR
)sItemName
);
1895 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1901 else if (pInfoData
->treeconflict_nodekind
== svn_node_dir
)
1903 switch (pInfoData
->treeconflict_operation
)
1905 case svn_wc_operation_update
:
1906 switch (pInfoData
->treeconflict_action
)
1908 case svn_wc_conflict_action_edit
:
1909 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEEDIT
, (LPCTSTR
)sItemName
);
1910 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1912 case svn_wc_conflict_action_add
:
1913 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEADD
, (LPCTSTR
)sItemName
);
1914 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1916 case svn_wc_conflict_action_delete
:
1917 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEDELETE
, (LPCTSTR
)sItemName
);
1918 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1922 case svn_wc_operation_switch
:
1923 switch (pInfoData
->treeconflict_action
)
1925 case svn_wc_conflict_action_edit
:
1926 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHEDIT
, (LPCTSTR
)sItemName
);
1927 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1929 case svn_wc_conflict_action_add
:
1930 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHADD
, (LPCTSTR
)sItemName
);
1931 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1933 case svn_wc_conflict_action_delete
:
1934 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHDELETE
, (LPCTSTR
)sItemName
);
1935 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1939 case svn_wc_operation_merge
:
1940 switch (pInfoData
->treeconflict_action
)
1942 case svn_wc_conflict_action_edit
:
1943 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEEDIT
, (LPCTSTR
)sItemName
);
1944 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1946 case svn_wc_conflict_action_add
:
1947 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEADD
, (LPCTSTR
)sItemName
);
1948 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1950 case svn_wc_conflict_action_delete
:
1951 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEDELETE
, (LPCTSTR
)sItemName
);
1952 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1960 switch (pInfoData
->treeconflict_reason
)
1962 case svn_wc_conflict_reason_edited
:
1963 uReasonID
= IDS_TREECONFLICT_REASON_EDITED
;
1964 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1966 case svn_wc_conflict_reason_obstructed
:
1967 uReasonID
= IDS_TREECONFLICT_REASON_OBSTRUCTED
;
1968 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1970 case svn_wc_conflict_reason_deleted
:
1971 uReasonID
= IDS_TREECONFLICT_REASON_DELETED
;
1972 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_REMOVEDIR
: IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1974 case svn_wc_conflict_reason_added
:
1975 uReasonID
= IDS_TREECONFLICT_REASON_ADDED
;
1976 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1978 case svn_wc_conflict_reason_missing
:
1979 uReasonID
= IDS_TREECONFLICT_REASON_MISSING
;
1980 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_REMOVEDIR
: IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1982 case svn_wc_conflict_reason_unversioned
:
1983 uReasonID
= IDS_TREECONFLICT_REASON_UNVERSIONED
;
1984 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1987 sConflictReason
.Format(uReasonID
, (LPCTSTR
)sConflictAction
);
1989 CTreeConflictEditorDlg dlg
;
1990 dlg
.SetConflictInfoText(sConflictReason
);
1991 dlg
.SetResolveTexts(sResolveTheirs
, sResolveMine
);
1992 dlg
.SetPath(treeConflictPath
);
1993 INT_PTR dlgRet
= dlg
.DoModal();
1994 bRet
= (dlgRet
!= IDCANCEL
);
2002 bool CAppUtils::IsSSHPutty()
2004 CString sshclient
=g_Git
.m_Environment
.GetEnv(_T("GIT_SSH"));
2005 sshclient
=sshclient
.MakeLower();
2006 if(sshclient
.Find(_T("plink.exe"),0)>=0)
2013 CString
CAppUtils::GetClipboardLink(const CString
&skipGitPrefix
, int paramsCount
)
2015 if (!OpenClipboard(NULL
))
2018 CString sClipboardText
;
2019 HGLOBAL hglb
= GetClipboardData(CF_TEXT
);
2022 LPCSTR lpstr
= (LPCSTR
)GlobalLock(hglb
);
2023 sClipboardText
= CString(lpstr
);
2026 hglb
= GetClipboardData(CF_UNICODETEXT
);
2029 LPCTSTR lpstr
= (LPCTSTR
)GlobalLock(hglb
);
2030 sClipboardText
= lpstr
;
2035 if(!sClipboardText
.IsEmpty())
2037 if(sClipboardText
[0] == _T('\"') && sClipboardText
[sClipboardText
.GetLength()-1] == _T('\"'))
2038 sClipboardText
=sClipboardText
.Mid(1,sClipboardText
.GetLength()-2);
2040 if(sClipboardText
.Find( _T("http://")) == 0)
2041 return sClipboardText
;
2043 if(sClipboardText
.Find( _T("https://")) == 0)
2044 return sClipboardText
;
2046 if(sClipboardText
.Find( _T("git://")) == 0)
2047 return sClipboardText
;
2049 if(sClipboardText
.Find( _T("ssh://")) == 0)
2050 return sClipboardText
;
2052 if (sClipboardText
.Find(_T("git@")) == 0)
2053 return sClipboardText
;
2055 if(sClipboardText
.GetLength()>=2)
2056 if( sClipboardText
[1] == _T(':') )
2057 if( (sClipboardText
[0] >= 'A' && sClipboardText
[0] <= 'Z')
2058 || (sClipboardText
[0] >= 'a' && sClipboardText
[0] <= 'z') )
2059 return sClipboardText
;
2061 // trim prefixes like "git clone "
2062 if (!skipGitPrefix
.IsEmpty() && sClipboardText
.Find(skipGitPrefix
) == 0)
2064 sClipboardText
= sClipboardText
.Mid(skipGitPrefix
.GetLength()).Trim();
2066 while (paramsCount
>= 0)
2069 spacePos
= sClipboardText
.Find(_T(' '), spacePos
+ 1);
2073 if (spacePos
> 0 && paramsCount
< 0)
2074 sClipboardText
= sClipboardText
.Left(spacePos
);
2075 return sClipboardText
;
2079 return CString(_T(""));
2082 CString
CAppUtils::ChooseRepository(CString
*path
)
2084 CBrowseFolder browseFolder
;
2085 CRegString regLastResopitory
= CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2087 browseFolder
.m_style
= BIF_EDITBOX
| BIF_NEWDIALOGSTYLE
| BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
;
2088 CString strCloneDirectory
;
2090 strCloneDirectory
=*path
;
2093 strCloneDirectory
= regLastResopitory
;
2097 title
.LoadString(IDS_CHOOSE_REPOSITORY
);
2099 browseFolder
.SetInfo(title
);
2101 if (browseFolder
.Show(NULL
, strCloneDirectory
) == CBrowseFolder::OK
)
2103 regLastResopitory
= strCloneDirectory
;
2104 return strCloneDirectory
;
2112 bool CAppUtils::SendPatchMail(CTGitPathList
& list
, bool bIsMainWnd
)
2116 dlg
.m_PathList
= list
;
2118 if(dlg
.DoModal()==IDOK
)
2120 if (dlg
.m_PathList
.IsEmpty())
2123 CGitProgressDlg progDlg
;
2125 theApp
.m_pMainWnd
= &progDlg
;
2126 SendMailProgressCommand sendMailProgressCommand
;
2127 progDlg
.SetCommand(&sendMailProgressCommand
);
2129 sendMailProgressCommand
.SetPathList(dlg
.m_PathList
);
2130 progDlg
.SetItemCount(dlg
.m_PathList
.GetCount());
2132 CSendMailPatch
sendMailPatch(dlg
.m_To
, dlg
.m_CC
, dlg
.m_Subject
, !!dlg
.m_bAttachment
, !!dlg
.m_bCombine
);
2133 sendMailProgressCommand
.SetSendMailOption(&sendMailPatch
);
2142 bool CAppUtils::SendPatchMail(CString
&cmd
, CString
&formatpatchoutput
, bool bIsMainWnd
)
2145 CString log
=formatpatchoutput
;
2146 int start
=log
.Find(cmd
);
2148 CString one
=log
.Tokenize(_T("\n"),start
);
2154 CString one
=log
.Tokenize(_T("\n"),start
);
2156 if(one
.IsEmpty() || one
.Find(_T("Success")) == 0)
2158 one
.Replace(_T('/'),_T('\\'));
2160 path
.SetFromWin(one
);
2163 if (!list
.IsEmpty())
2165 return SendPatchMail(list
, bIsMainWnd
);
2169 CMessageBox::Show(NULL
, IDS_ERR_NOPATCHES
, IDS_APPNAME
, MB_ICONINFORMATION
);
2175 int CAppUtils::GetLogOutputEncode(CGit
*pGit
)
2178 output
= pGit
->GetConfigValue(_T("i18n.logOutputEncoding"));
2179 if(output
.IsEmpty())
2180 return CUnicodeUtils::GetCPCode(pGit
->GetConfigValue(_T("i18n.commitencoding")));
2183 return CUnicodeUtils::GetCPCode(output
);
2186 int CAppUtils::SaveCommitUnicodeFile(CString
&filename
, CString
&message
)
2190 CFile
file(filename
, CFile::modeReadWrite
| CFile::modeCreate
);
2191 int cp
= CUnicodeUtils::GetCPCode(g_Git
.GetConfigValue(_T("i18n.commitencoding")));
2193 bool stripComments
= (CRegDWORD(_T("Software\\TortoiseGit\\StripCommentedLines"), FALSE
) == TRUE
);
2195 if (CRegDWORD(_T("Software\\TortoiseGit\\SanitizeCommitMsg"), TRUE
) == TRUE
)
2196 message
.TrimRight(L
" \r\n");
2198 int len
= message
.GetLength();
2200 while (start
>= 0 && start
< len
)
2202 int oldStart
= start
;
2203 start
= message
.Find(L
"\n", oldStart
);
2204 CString line
= message
.Mid(oldStart
);
2207 line
= line
.Left(start
- oldStart
);
2208 ++start
; // move forward so we don't find the same char again
2210 if (stripComments
&& (line
.GetLength() >= 1 && line
.GetAt(0) == '#') || (start
< 0 && line
.IsEmpty()))
2212 line
.TrimRight(L
" \r");
2213 CStringA lineA
= CUnicodeUtils::GetMulti(line
+ L
"\n", cp
);
2214 file
.Write(lineA
.GetBuffer(), lineA
.GetLength());
2219 catch (CFileException
*e
)
2226 bool CAppUtils::Pull(bool showPush
)
2229 dlg
.m_IsPull
= TRUE
;
2230 if (dlg
.DoModal() == IDOK
)
2232 // "git.exe pull --rebase" is not supported, never and ever. So, adapting it to Fetch & Rebase.
2234 return DoFetch(dlg
.m_RemoteURL
,
2235 FALSE
, // Fetch all remotes
2236 dlg
.m_bAutoLoad
== BST_CHECKED
,
2238 dlg
.m_bDepth
== BST_CHECKED
,
2241 dlg
.m_RemoteBranchName
,
2242 TRUE
); // Rebase after fetching
2244 CString url
= dlg
.m_RemoteURL
;
2246 if (dlg
.m_bAutoLoad
)
2248 CAppUtils::LaunchPAgent(NULL
, &dlg
.m_RemoteURL
);
2253 if (g_Git
.GetHash(hashOld
, _T("HEAD")))
2255 MessageBox(NULL
, g_Git
.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR
);
2268 if (!dlg
.m_bFetchTags
)
2269 notags
= _T("--no-tags ");
2271 if (dlg
.m_bFetchTags
== TRUE
)
2272 notags
= _T("--tags ");
2275 noff
=_T("--no-ff ");
2278 ffonly
= _T("--ff-only ");
2281 squash
= _T("--squash ");
2283 if (dlg
.m_bNoCommit
)
2284 nocommit
= _T("--no-commit ");
2287 depth
.Format(_T("--depth %d "), dlg
.m_nDepth
);
2289 int ver
= CAppUtils::GetMsysgitVersion();
2291 if (dlg
.m_bPrune
== TRUE
)
2292 prune
= _T("--prune ");
2293 else if (dlg
.m_bPrune
== FALSE
&& ver
>= 0x01080500)
2294 prune
= _T("--no-prune ");
2296 if(ver
>= 0x01070203) //above 1.7.0.2
2297 cmdRebase
+= _T("--progress ");
2299 cmd
.Format(_T("git.exe pull -v %s%s%s%s%s%s%s%s\"%s\" %s"), cmdRebase
, noff
, ffonly
, squash
, nocommit
, depth
, notags
, prune
, url
, dlg
.m_RemoteBranchName
);
2300 CProgressDlg progress
;
2301 progress
.m_GitCmd
= cmd
;
2303 CGitHash hashNew
; // declare outside lambda, because it is captured by reference
2304 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2308 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUPULL
, [&]{ Pull(); }));
2312 if (g_Git
.GetHash(hashNew
, _T("HEAD")))
2313 MessageBox(nullptr, g_Git
.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR
);
2316 postCmdList
.push_back(PostCmd(IDI_DIFF
, IDS_PROC_PULL_DIFFS
, [&]
2319 dlg
.SetDiff(NULL
, hashNew
.ToString(), hashOld
.ToString());
2322 postCmdList
.push_back(PostCmd(IDI_LOG
, IDS_PROC_PULL_LOG
, [&]
2325 dlg
.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld
.ToString() + _T("..") + hashNew
.ToString(), 0);
2331 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, []{ Push(_T("")); }));
2333 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
2334 if (gitPath
.HasSubmodules())
2336 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, []
2339 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
2340 CAppUtils::RunTortoiseGitProc(sCmd
);
2345 INT_PTR ret
= progress
.DoModal();
2347 if (ret
== IDOK
&& progress
.m_GitStatus
== 1 && progress
.m_LogText
.Find(_T("CONFLICT")) >= 0 && CMessageBox::Show(NULL
, IDS_SEECHANGES
, IDS_APPNAME
, MB_YESNO
| MB_ICONINFORMATION
) == IDYES
)
2350 dlg
.m_pathList
.AddPath(CTGitPath());
2362 bool CAppUtils::RebaseAfterFetch(const CString
& upstream
)
2367 if (!upstream
.IsEmpty())
2368 dlg
.m_Upstream
= upstream
;
2369 dlg
.m_PostButtonTexts
.Add(CString(MAKEINTRESOURCE(IDS_MENULOG
)));
2370 dlg
.m_PostButtonTexts
.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL
)));
2371 dlg
.m_PostButtonTexts
.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE
)));
2372 INT_PTR response
= dlg
.DoModal();
2373 if (response
== IDOK
)
2377 else if (response
== IDC_REBASE_POST_BUTTON
)
2379 CString cmd
= _T("/command:log");
2380 cmd
+= _T(" /path:\"") + g_Git
.m_CurrentDir
+ _T("\"");
2381 CAppUtils::RunTortoiseGitProc(cmd
);
2384 else if (response
== IDC_REBASE_POST_BUTTON
+ 1)
2386 CString cmd
, out
, err
;
2387 cmd
.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2389 g_Git
.FixBranchName(dlg
.m_Upstream
),
2390 g_Git
.FixBranchName(dlg
.m_Branch
));
2391 if (g_Git
.Run(cmd
, &out
, &err
, CP_UTF8
))
2393 CMessageBox::Show(NULL
, out
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2396 CAppUtils::SendPatchMail(cmd
, out
);
2399 else if (response
== IDC_REBASE_POST_BUTTON
+ 2)
2401 else if (response
== IDCANCEL
)
2407 static bool DoFetch(const CString
& url
, const bool fetchAllRemotes
, const bool loadPuttyAgent
, const int prune
, const bool bDepth
, const int nDepth
, const int fetchTags
, const CString
& remoteBranch
, const boolean runRebase
)
2411 if (fetchAllRemotes
)
2414 g_Git
.GetRemoteList(list
);
2416 STRING_VECTOR::const_iterator it
= list
.begin();
2417 while (it
!= list
.end())
2419 CString
remote(*it
);
2420 CAppUtils::LaunchPAgent(NULL
, &remote
);
2425 CAppUtils::LaunchPAgent(NULL
, &url
);
2429 int ver
= CAppUtils::GetMsysgitVersion();
2430 if (ver
>= 0x01070203) //above 1.7.0.2
2431 arg
+= _T(" --progress");
2434 arg
.AppendFormat(_T(" --depth %d"), nDepth
);
2437 arg
+= _T(" --prune");
2438 else if (prune
== FALSE
&& ver
>= 0x01080500)
2439 arg
+= _T(" --no-prune");
2442 arg
+= _T(" --tags");
2443 else if (fetchTags
== 0)
2444 arg
+= _T(" --no-tags");
2446 if (fetchAllRemotes
)
2447 cmd
.Format(_T("git.exe fetch --all -v%s"), arg
);
2449 cmd
.Format(_T("git.exe fetch -v%s \"%s\" %s"), arg
, url
, remoteBranch
);
2451 CProgressDlg progress
;
2452 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2456 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ DoFetch(url
, fetchAllRemotes
, loadPuttyAgent
, prune
, bDepth
, nDepth
, fetchTags
, remoteBranch
, runRebase
); }));
2460 postCmdList
.push_back(PostCmd(IDI_LOG
, IDS_MENULOG
, []
2462 CString cmd
= _T("/command:log");
2463 cmd
+= _T(" /path:\"") + g_Git
.m_CurrentDir
+ _T("\"");
2464 CAppUtils::RunTortoiseGitProc(cmd
);
2467 postCmdList
.push_back(PostCmd(IDI_REVERT
, IDS_PROC_RESET
, []
2469 CString pullRemote
, pullBranch
;
2470 g_Git
.GetRemoteTrackedBranchForHEAD(pullRemote
, pullBranch
);
2471 CString defaultUpstream
;
2472 if (!pullRemote
.IsEmpty() && !pullBranch
.IsEmpty())
2473 defaultUpstream
.Format(_T("remotes/%s/%s"), pullRemote
, pullBranch
);
2474 CAppUtils::GitReset(&defaultUpstream
, 2);
2477 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUFETCH
, []{ CAppUtils::Fetch(); }));
2479 if (!runRebase
&& !g_GitAdminDir
.IsBareRepo(g_Git
.m_CurrentDir
))
2480 postCmdList
.push_back(PostCmd(IDI_REBASE
, IDS_MENUREBASE
, []{ CAppUtils::RebaseAfterFetch(); }));
2483 progress
.m_GitCmd
= cmd
;
2484 INT_PTR userResponse
;
2486 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_FETCH
))
2488 CGitProgressDlg gitdlg
;
2489 FetchProgressCommand fetchProgressCommand
;
2490 if (!fetchAllRemotes
)
2491 fetchProgressCommand
.SetUrl(url
);
2492 gitdlg
.SetCommand(&fetchProgressCommand
);
2493 fetchProgressCommand
.m_PostCmdCallback
= progress
.m_PostCmdCallback
;
2494 fetchProgressCommand
.SetAutoTag(fetchTags
== 1 ? GIT_REMOTE_DOWNLOAD_TAGS_ALL
: fetchTags
== 2 ? GIT_REMOTE_DOWNLOAD_TAGS_AUTO
: GIT_REMOTE_DOWNLOAD_TAGS_NONE
);
2495 if (!fetchAllRemotes
)
2496 fetchProgressCommand
.SetRefSpec(remoteBranch
);
2497 userResponse
= gitdlg
.DoModal();
2498 return userResponse
== IDOK
;
2501 userResponse
= progress
.DoModal();
2502 if (!progress
.m_GitStatus
)
2505 return CAppUtils::RebaseAfterFetch();
2508 return userResponse
== IDOK
;
2511 bool CAppUtils::Fetch(CString remoteName
, bool allowRebase
, bool allRemotes
)
2514 dlg
.m_PreSelectRemote
= remoteName
;
2515 dlg
.m_bAllowRebase
= allowRebase
;
2517 dlg
.m_bAllRemotes
= allRemotes
;
2519 if(dlg
.DoModal()==IDOK
)
2520 return DoFetch(dlg
.m_RemoteURL
, dlg
.m_bAllRemotes
== BST_CHECKED
, dlg
.m_bAutoLoad
== BST_CHECKED
, dlg
.m_bPrune
, dlg
.m_bDepth
== BST_CHECKED
, dlg
.m_nDepth
, dlg
.m_bFetchTags
, dlg
.m_RemoteBranchName
, dlg
.m_bRebase
== BST_CHECKED
);
2525 bool CAppUtils::Push(CString selectLocalBranch
)
2528 dlg
.m_BranchSourceName
= selectLocalBranch
;
2530 if (dlg
.DoModal() == IDOK
)
2533 DWORD exitcode
= 0xFFFFFFFF;
2534 if (CHooks::Instance().PrePush(g_Git
.m_CurrentDir
, exitcode
, error
))
2539 temp
.Format(IDS_ERR_HOOKFAILED
, (LPCTSTR
)error
);
2540 CMessageBox::Show(nullptr, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2548 arg
+= _T("--thin ");
2549 if(dlg
.m_bTags
&& !dlg
.m_bPushAllBranches
)
2550 arg
+= _T("--tags ");
2552 arg
+= _T("--force ");
2553 if (dlg
.m_bForceWithLease
)
2554 arg
+= _T("--force-with-lease ");
2555 if (dlg
.m_bSetUpstream
)
2556 arg
+= _T("--set-upstream ");
2557 if (dlg
.m_RecurseSubmodules
== 1)
2558 arg
+= _T("--recurse-submodules=check ");
2559 if (dlg
.m_RecurseSubmodules
== 2)
2560 arg
+= _T("--recurse-submodules=on-demand ");
2562 int ver
= CAppUtils::GetMsysgitVersion();
2564 if(ver
>= 0x01070203) //above 1.7.0.2
2565 arg
+= _T("--progress ");
2567 CProgressDlg progress
;
2569 STRING_VECTOR remotesList
;
2570 if (dlg
.m_bPushAllRemotes
)
2571 g_Git
.GetRemoteList(remotesList
);
2573 remotesList
.push_back(dlg
.m_URL
);
2575 for (unsigned int i
= 0; i
< remotesList
.size(); ++i
)
2577 if (dlg
.m_bAutoLoad
)
2578 CAppUtils::LaunchPAgent(NULL
, &remotesList
[i
]);
2581 if (dlg
.m_bPushAllBranches
)
2583 cmd
.Format(_T("git.exe push --all %s \"%s\""),
2589 progress
.m_GitCmdList
.push_back(cmd
);
2590 cmd
.Format(_T("git.exe push --tags %s \"%s\""), arg
, remotesList
[i
]);
2595 cmd
.Format(_T("git.exe push %s \"%s\" %s"),
2598 dlg
.m_BranchSourceName
);
2599 if (!dlg
.m_BranchRemoteName
.IsEmpty())
2601 cmd
+= _T(":") + dlg
.m_BranchRemoteName
;
2604 progress
.m_GitCmdList
.push_back(cmd
);
2607 CString superprojectRoot
;
2608 g_GitAdminDir
.HasAdminDir(g_Git
.m_CurrentDir
, false, &superprojectRoot
);
2609 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2611 // need to execute hooks as those might be needed by post action commands
2612 DWORD exitcode
= 0xFFFFFFFF;
2614 if (CHooks::Instance().PostPush(g_Git
.m_CurrentDir
, exitcode
, error
))
2619 temp
.Format(IDS_ERR_HOOKFAILED
, (LPCTSTR
)error
);
2620 MessageBox(nullptr, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2626 bool rejected
= progress
.GetLogText().Find(_T("! [rejected]")) > 0;
2629 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUPULL
, []{ Pull(true); }));
2630 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUFETCH
, [&]{ Fetch(dlg
.m_bPushAllRemotes
? _T("") : dlg
.m_URL
, true, !!dlg
.m_bPushAllRemotes
); }));
2632 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, [&]{ Push(selectLocalBranch
); }));
2636 postCmdList
.push_back(PostCmd(IDS_PROC_REQUESTPULL
, [&]{ RequestPull(dlg
.m_BranchRemoteName
); }));
2637 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, [&]{ Push(selectLocalBranch
); }));
2638 postCmdList
.push_back(PostCmd(IDI_SWITCH
, IDS_MENUSWITCH
, [&]{ Switch(); }));
2639 if (!superprojectRoot
.IsEmpty())
2641 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_PROC_COMMIT_SUPERPROJECT
, [&]
2644 sCmd
.Format(_T("/command:commit /path:\"%s\""), superprojectRoot
);
2645 RunTortoiseGitProc(sCmd
);
2650 INT_PTR ret
= progress
.DoModal();
2656 bool CAppUtils::RequestPull(CString endrevision
, CString repositoryUrl
, bool bIsMainWnd
)
2658 CRequestPullDlg dlg
;
2659 dlg
.m_RepositoryURL
= repositoryUrl
;
2660 dlg
.m_EndRevision
= endrevision
;
2661 if (dlg
.DoModal()==IDOK
)
2664 cmd
.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg
.m_StartRevision
, dlg
.m_RepositoryURL
, dlg
.m_EndRevision
);
2666 CSysProgressDlg sysProgressDlg
;
2667 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2668 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST
)));
2669 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
2670 sysProgressDlg
.SetShowProgressBar(false);
2671 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
2673 CString tempFileName
= GetTempFile();
2675 DeleteFile(tempFileName
);
2676 CreateDirectory(tempFileName
, NULL
);
2677 tempFileName
+= _T("\\pullrequest.txt");
2678 if (g_Git
.RunLogFile(cmd
, tempFileName
, &err
))
2681 msg
.LoadString(IDS_ERR_PULLREUQESTFAILED
);
2682 CMessageBox::Show(NULL
, msg
+ _T("\n") + err
, _T("TortoiseGit"), MB_OK
);
2686 if (sysProgressDlg
.HasUserCancelled())
2688 CMessageBox::Show(NULL
, IDS_SVN_USERCANCELLED
, IDS_APPNAME
, MB_OK
);
2689 ::DeleteFile(tempFileName
);
2693 sysProgressDlg
.Stop();
2695 if (dlg
.m_bSendMail
)
2698 dlg
.m_PathList
= CTGitPathList(CTGitPath(tempFileName
));
2699 dlg
.m_bCustomSubject
= true;
2701 if (dlg
.DoModal() == IDOK
)
2703 if (dlg
.m_PathList
.IsEmpty())
2706 CGitProgressDlg progDlg
;
2708 theApp
.m_pMainWnd
= &progDlg
;
2709 SendMailProgressCommand sendMailProgressCommand
;
2710 progDlg
.SetCommand(&sendMailProgressCommand
);
2712 sendMailProgressCommand
.SetPathList(dlg
.m_PathList
);
2713 progDlg
.SetItemCount(dlg
.m_PathList
.GetCount());
2715 CSendMailCombineable
sendMailCombineable(dlg
.m_To
, dlg
.m_CC
, dlg
.m_Subject
, !!dlg
.m_bAttachment
, !!dlg
.m_bCombine
);
2716 sendMailProgressCommand
.SetSendMailOption(&sendMailCombineable
);
2725 CAppUtils::LaunchAlternativeEditor(tempFileName
);
2730 bool CAppUtils::CreateMultipleDirectory(const CString
& szPath
)
2732 CString
strDir(szPath
);
2733 if (strDir
.GetAt(strDir
.GetLength()-1)!=_T('\\'))
2735 strDir
.AppendChar(_T('\\'));
2737 std::vector
<CString
> vPath
;
2739 bool bSuccess
= false;
2741 for (int i
=0;i
<strDir
.GetLength();++i
)
2743 if (strDir
.GetAt(i
) != _T('\\'))
2745 strTemp
.AppendChar(strDir
.GetAt(i
));
2749 vPath
.push_back(strTemp
);
2750 strTemp
.AppendChar(_T('\\'));
2754 for (auto vIter
= vPath
.begin(); vIter
!= vPath
.end(); ++vIter
)
2756 bSuccess
= CreateDirectory(*vIter
, NULL
) ? true : false;
2762 void CAppUtils::RemoveTrailSlash(CString
&path
)
2767 // For URL, do not trim the slash just after the host name component.
2768 int index
= path
.Find(_T("://"));
2772 index
= path
.Find(_T('/'), index
);
2773 if (index
== path
.GetLength() - 1)
2777 while(path
[path
.GetLength()-1] == _T('\\') || path
[path
.GetLength()-1] == _T('/' ) )
2779 path
=path
.Left(path
.GetLength()-1);
2785 bool CAppUtils::CheckUserData()
2787 while(g_Git
.GetUserName().IsEmpty() || g_Git
.GetUserEmail().IsEmpty())
2789 if(CMessageBox::Show(NULL
, IDS_PROC_NOUSERDATA
, IDS_APPNAME
, MB_YESNO
| MB_ICONERROR
) == IDYES
)
2791 CTGitPath
path(g_Git
.m_CurrentDir
);
2792 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
,&path
);
2793 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
2794 dlg
.SetTreeWidth(220);
2795 dlg
.m_DefaultPage
= _T("gitconfig");
2798 dlg
.HandleRestart();
2808 BOOL
CAppUtils::Commit(CString bugid
,BOOL bWholeProject
,CString
&sLogMsg
,
2809 CTGitPathList
&pathList
,
2810 CTGitPathList
&selectedList
,
2811 bool bSelectFilesForCommit
)
2813 bool bFailed
= true;
2815 if (!CheckUserData())
2822 dlg
.m_sBugID
= bugid
;
2824 dlg
.m_bWholeProject
= bWholeProject
;
2826 dlg
.m_sLogMessage
= sLogMsg
;
2827 dlg
.m_pathList
= pathList
;
2828 dlg
.m_checkedPathList
= selectedList
;
2829 dlg
.m_bSelectFilesForCommit
= bSelectFilesForCommit
;
2830 if (dlg
.DoModal() == IDOK
)
2832 if (dlg
.m_pathList
.IsEmpty())
2834 // if the user hasn't changed the list of selected items
2835 // we don't use that list. Because if we would use the list
2836 // of pre-checked items, the dialog would show different
2837 // checked items on the next startup: it would only try
2838 // to check the parent folder (which might not even show)
2839 // instead, we simply use an empty list and let the
2840 // default checking do its job.
2841 if (!dlg
.m_pathList
.IsEqual(pathList
))
2842 selectedList
= dlg
.m_pathList
;
2843 pathList
= dlg
.m_updatedPathList
;
2844 sLogMsg
= dlg
.m_sLogMessage
;
2845 bSelectFilesForCommit
= true;
2847 switch (dlg
.m_PostCmd
)
2849 case GIT_POSTCOMMIT_CMD_DCOMMIT
:
2850 CAppUtils::SVNDCommit();
2852 case GIT_POSTCOMMIT_CMD_PUSH
:
2855 case GIT_POSTCOMMIT_CMD_CREATETAG
:
2856 CAppUtils::CreateBranchTag(TRUE
);
2858 case GIT_POSTCOMMIT_CMD_PULL
:
2859 CAppUtils::Pull(true);
2865 // CGitProgressDlg progDlg;
2866 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2867 // if (parser.HasVal(_T("closeonend")))
2868 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2869 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2870 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2871 // progDlg.SetPathList(dlg.m_pathList);
2872 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2873 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2874 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2875 // progDlg.SetItemCount(dlg.m_itemsCount);
2876 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2877 // progDlg.DoModal();
2878 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2879 // err = (DWORD)progDlg.DidErrorsOccur();
2880 // bFailed = progDlg.DidErrorsOccur();
2881 // bRet = progDlg.DidErrorsOccur();
2882 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2883 // if (DWORD(bFailRepeat)==0)
2884 // bFailed = false; // do not repeat if the user chose not to in the settings.
2891 BOOL
CAppUtils::SVNDCommit()
2893 CSVNDCommitDlg dcommitdlg
;
2894 CString gitSetting
= g_Git
.GetConfigValue(_T("svn.rmdir"));
2895 if (gitSetting
== _T("")) {
2896 if (dcommitdlg
.DoModal() != IDOK
)
2902 if (dcommitdlg
.m_remember
)
2904 if (dcommitdlg
.m_rmdir
)
2906 gitSetting
= _T("true");
2910 gitSetting
= _T("false");
2912 if(g_Git
.SetConfigValue(_T("svn.rmdir"),gitSetting
))
2915 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, _T("svn.rmdir"), gitSetting
);
2916 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2922 BOOL IsStash
= false;
2923 if(!g_Git
.CheckCleanWorkTree())
2925 if (CMessageBox::Show(NULL
, IDS_ERROR_NOCLEAN_STASH
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_STASHBUTTON
, IDS_ABORTBUTTON
) == 1)
2927 CSysProgressDlg sysProgressDlg
;
2928 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2929 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
2930 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
2931 sysProgressDlg
.SetShowProgressBar(false);
2932 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
2933 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
2936 cmd
=_T("git.exe stash");
2937 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
2939 sysProgressDlg
.Stop();
2940 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
2943 sysProgressDlg
.Stop();
2953 CProgressDlg progress
;
2954 if (dcommitdlg
.m_rmdir
)
2956 progress
.m_GitCmd
=_T("git.exe svn dcommit --rmdir");
2960 progress
.m_GitCmd
=_T("git.exe svn dcommit");
2962 if(progress
.DoModal()==IDOK
&& progress
.m_GitStatus
== 0)
2964 ::DeleteFile(g_Git
.m_CurrentDir
+ _T("\\sys$command"));
2967 if(CMessageBox::Show(NULL
,IDS_DCOMMIT_STASH_POP
,IDS_APPNAME
,MB_YESNO
|MB_ICONINFORMATION
)==IDYES
)
2969 CSysProgressDlg sysProgressDlg
;
2970 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2971 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
2972 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
2973 sysProgressDlg
.SetShowProgressBar(false);
2974 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
2975 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
2978 cmd
=_T("git.exe stash pop");
2979 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
2981 sysProgressDlg
.Stop();
2982 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
2985 sysProgressDlg
.Stop();
2997 BOOL
CAppUtils::Merge(CString
*commit
)
2999 if (!CheckUserData())
3004 dlg
.m_initialRefName
= *commit
;
3006 if(dlg
.DoModal()==IDOK
)
3012 args
+= _T(" --no-ff");
3015 args
+= _T(" --squash");
3018 args
+= _T(" --no-commit");
3023 fmt
.Format(_T(" --log=%d"), dlg
.m_nLog
);
3027 if (!dlg
.m_MergeStrategy
.IsEmpty())
3029 args
+= _T(" --strategy=") + dlg
.m_MergeStrategy
;
3030 if (!dlg
.m_StrategyOption
.IsEmpty())
3032 args
+= _T(" --strategy-option=") + dlg
.m_StrategyOption
;
3033 if (!dlg
.m_StrategyParam
.IsEmpty())
3034 args
+= _T("=") + dlg
.m_StrategyParam
;
3038 if(!dlg
.m_strLogMesage
.IsEmpty())
3040 CString logmsg
= dlg
.m_strLogMesage
;
3041 logmsg
.Replace(_T("\""), _T("\\\""));
3042 args
+= _T(" -m \"") + logmsg
+ _T("\"");
3044 cmd
.Format(_T("git.exe merge %s %s"), args
, g_Git
.FixBranchName(dlg
.m_VersionName
));
3046 CProgressDlg Prodlg
;
3047 Prodlg
.m_GitCmd
= cmd
;
3049 Prodlg
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
3054 if (!g_Git
.ListConflictFile(list
) && !list
.IsEmpty())
3056 // there are conflict files
3058 postCmdList
.push_back(PostCmd(IDI_RESOLVE
, IDS_PROGRS_CMD_RESOLVE
, []
3061 sCmd
.Format(_T("/command:commit /path:\"%s\""), g_Git
.m_CurrentDir
);
3062 CAppUtils::RunTortoiseGitProc(sCmd
);
3068 if (dlg
.m_bNoCommit
)
3070 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_MENUCOMMIT
, []
3073 sCmd
.Format(_T("/command:commit /path:\"%s\""), g_Git
.m_CurrentDir
);
3074 CAppUtils::RunTortoiseGitProc(sCmd
);
3079 if (dlg
.m_bIsBranch
&& dlg
.m_VersionName
.Find(L
"remotes/") == -1) // do not ask to remove remote branches
3081 postCmdList
.push_back(PostCmd(IDI_DELETE
, IDS_PROC_REMOVEBRANCH
, [&]
3084 msg
.Format(IDS_PROC_DELETEBRANCHTAG
, dlg
.m_VersionName
);
3085 if (CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), 2, IDI_QUESTION
, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON
)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON
))) == 1)
3088 cmd
.Format(_T("git.exe branch -D -- %s"), dlg
.m_VersionName
);
3089 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
3090 MessageBox(nullptr, out
, _T("TortoiseGit"), MB_OK
);
3094 if (dlg
.m_bIsBranch
)
3095 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, []{ Push(); }));
3097 BOOL hasGitSVN
= CTGitPath(g_Git
.m_CurrentDir
).GetAdminDirMask() & ITEMIS_GITSVN
;
3099 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_MENUSVNDCOMMIT
, []{ SVNDCommit(); }));
3103 return !Prodlg
.m_GitStatus
;
3108 BOOL
CAppUtils::MergeAbort()
3111 if (dlg
.DoModal() == IDOK
)
3112 return Reset(_T("HEAD"), dlg
.m_ResetType
+ 1);
3117 void CAppUtils::EditNote(GitRev
*rev
)
3119 if (!CheckUserData())
3123 dlg
.m_sHintText
= CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES
));
3124 dlg
.m_sInputText
= rev
->m_Notes
;
3125 dlg
.m_sTitle
= CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES
));
3126 //dlg.m_pProjectProperties = &m_ProjectProperties;
3127 dlg
.m_bUseLogWidth
= true;
3128 if(dlg
.DoModal() == IDOK
)
3131 cmd
=_T("notes add -f -F \"");
3133 CString tempfile
=::GetTempFile();
3134 if (CAppUtils::SaveCommitUnicodeFile(tempfile
, dlg
.m_sInputText
))
3136 CMessageBox::Show(nullptr, IDS_PROC_FAILEDSAVINGNOTES
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
3141 cmd
+= rev
->m_CommitHash
.ToString();
3145 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd
, CP_UTF8
).GetBuffer()))
3147 CMessageBox::Show(NULL
, IDS_PROC_FAILEDSAVINGNOTES
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
3152 rev
->m_Notes
= dlg
.m_sInputText
;
3156 CMessageBox::Show(NULL
, IDS_PROC_FAILEDSAVINGNOTES
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
3158 ::DeleteFile(tempfile
);
3163 int CAppUtils::GetMsysgitVersion()
3165 if (g_Git
.ms_LastMsysGitVersion
)
3166 return g_Git
.ms_LastMsysGitVersion
;
3169 CString versiondebug
;
3172 CRegDWORD regTime
= CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3173 CRegDWORD regVersion
= CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3175 CString gitpath
= CGit::ms_LastMsysGitDir
+_T("\\git.exe");
3178 if (!g_Git
.GetFileModifyTime(gitpath
, &time
))
3180 if((DWORD
)time
== regTime
)
3182 g_Git
.ms_LastMsysGitVersion
= regVersion
;
3188 cmd
= _T("git.exe --version");
3189 if (g_Git
.Run(cmd
, &version
, &err
, CP_UTF8
))
3191 CMessageBox::Show(NULL
, _T("git.exe not correctly set up (") + err
+ _T(")\nCheck TortoiseGit settings and consult help file for \"Git.exe Path\"."), _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
3198 versiondebug
= version
;
3202 CString str
=version
.Tokenize(_T("."), start
);
3203 int space
= str
.ReverseFind(_T(' '));
3204 str
= str
.Mid(space
+1,start
);
3208 version
= version
.Mid(start
);
3211 str
= version
.Tokenize(_T("."), start
);
3213 ver
|= (_ttol(str
) & 0xFF) << 16;
3215 str
= version
.Tokenize(_T("."), start
);
3216 ver
|= (_ttol(str
) & 0xFF) << 8;
3218 str
= version
.Tokenize(_T("."), start
);
3219 ver
|= (_ttol(str
) & 0xFF);
3225 CMessageBox::Show(NULL
, _T("Could not parse git.exe version number: \"") + versiondebug
+ _T("\""), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
3230 regTime
= time
&0xFFFFFFFF;
3232 g_Git
.ms_LastMsysGitVersion
= ver
;
3237 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd
)
3239 typedef HRESULT (WINAPI
*SHGPSFW
) (HWND hwnd
,REFIID riid
,void** ppv
);
3241 CAutoLibrary hShell
= AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3243 if (hShell
.IsValid()) {
3244 SHGPSFW pfnSHGPSFW
= (SHGPSFW
)::GetProcAddress(hShell
, "SHGetPropertyStoreForWindow");
3246 IPropertyStore
*pps
;
3247 HRESULT hr
= pfnSHGPSFW(hWnd
, IID_PPV_ARGS(&pps
));
3248 if (SUCCEEDED(hr
)) {
3251 var
.boolVal
= VARIANT_TRUE
;
3252 hr
= pps
->SetValue(PKEY_AppUserModel_PreventPinning
, var
);
3259 void CAppUtils::SetWindowTitle(HWND hWnd
, const CString
& urlorpath
, const CString
& dialogname
)
3261 ASSERT(dialogname
.GetLength() < 70);
3262 ASSERT(urlorpath
.GetLength() < MAX_PATH
);
3263 WCHAR pathbuf
[MAX_PATH
] = {0};
3265 PathCompactPathEx(pathbuf
, urlorpath
, 70 - dialogname
.GetLength(), 0);
3267 wcscat_s(pathbuf
, L
" - ");
3268 wcscat_s(pathbuf
, dialogname
);
3269 wcscat_s(pathbuf
, L
" - ");
3270 wcscat_s(pathbuf
, CString(MAKEINTRESOURCE(IDS_APPNAME
)));
3271 SetWindowText(hWnd
, pathbuf
);
3274 bool CAppUtils::BisectStart(CString lastGood
, CString firstBad
, bool bIsMainWnd
)
3276 if (!g_Git
.CheckCleanWorkTree())
3278 if (CMessageBox::Show(NULL
, IDS_ERROR_NOCLEAN_STASH
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_STASHBUTTON
, IDS_ABORTBUTTON
) == 1)
3280 CSysProgressDlg sysProgressDlg
;
3281 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
3282 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
3283 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
3284 sysProgressDlg
.SetShowProgressBar(false);
3285 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
3286 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
3289 cmd
= _T("git.exe stash");
3290 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
3292 sysProgressDlg
.Stop();
3293 CMessageBox::Show(NULL
, out
, _T("TortoiseGit"), MB_OK
);
3296 sysProgressDlg
.Stop();
3302 CBisectStartDlg bisectStartDlg
;
3304 if (!lastGood
.IsEmpty())
3305 bisectStartDlg
.m_sLastGood
= lastGood
;
3306 if (!firstBad
.IsEmpty())
3307 bisectStartDlg
.m_sFirstBad
= firstBad
;
3309 if (bisectStartDlg
.DoModal() == IDOK
)
3311 CProgressDlg progress
;
3313 theApp
.m_pMainWnd
= &progress
;
3314 progress
.m_GitCmdList
.push_back(_T("git.exe bisect start"));
3315 progress
.m_GitCmdList
.push_back(_T("git.exe bisect good ") + bisectStartDlg
.m_LastGoodRevision
);
3316 progress
.m_GitCmdList
.push_back(_T("git.exe bisect bad ") + bisectStartDlg
.m_FirstBadRevision
);
3318 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
3323 CTGitPath
path(g_Git
.m_CurrentDir
);
3324 if (path
.HasSubmodules())
3326 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, []
3329 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
3330 CAppUtils::RunTortoiseGitProc(sCmd
);
3337 INT_PTR ret
= progress
.DoModal();
3344 int CAppUtils::Git2GetUserPassword(git_cred
**out
, const char *url
, const char *username_from_url
, unsigned int /*allowed_types*/, void * /*payload*/)
3347 dlg
.m_URL
= CUnicodeUtils::GetUnicode(url
, CP_UTF8
);
3348 if (username_from_url
)
3349 dlg
.m_UserName
= CUnicodeUtils::GetUnicode(username_from_url
, CP_UTF8
);
3351 CStringA username
, password
;
3352 if (dlg
.DoModal() == IDOK
)
3354 username
= CUnicodeUtils::GetMulti(dlg
.m_UserName
, CP_UTF8
);
3355 password
= CUnicodeUtils::GetMulti(dlg
.m_Password
, CP_UTF8
);
3356 return git_cred_userpass_plaintext_new(out
, username
, password
);
3358 giterr_set_str(GITERR_NONE
, "User cancelled.");
3362 int CAppUtils::Git2CertificateCheck(git_cert
* base_cert
, int /*valid*/, const char* host
, void* /*payload*/)
3364 if (base_cert
->cert_type
== GIT_CERT_X509
)
3366 git_cert_x509
* cert
= (git_cert_x509
*)base_cert
;
3368 if (last_accepted_cert
.cmp(cert
))
3371 PCCERT_CONTEXT pServerCert
= CertCreateCertificateContext(X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
, (BYTE
*)cert
->data
, (DWORD
)cert
->len
);
3373 DWORD verificationError
= VerifyServerCertificate(pServerCert
, CUnicodeUtils::GetUnicode(host
).GetBuffer(), 0);
3374 if (!verificationError
)
3376 last_accepted_cert
.set(cert
);
3377 CertFreeCertificateContext(pServerCert
);
3381 CString servernameInCert
;
3382 CertGetNameString(pServerCert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0, nullptr, servernameInCert
.GetBuffer(128), 128);
3383 servernameInCert
.ReleaseBuffer();
3386 CertGetNameString(pServerCert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, CERT_NAME_ISSUER_FLAG
, nullptr, issuer
.GetBuffer(128), 128);
3387 issuer
.ReleaseBuffer();
3389 CertFreeCertificateContext(pServerCert
);
3391 CCheckCertificateDlg dlg
;
3393 dlg
.m_sCertificateCN
= servernameInCert
;
3394 dlg
.m_sCertificateIssuer
= issuer
;
3395 dlg
.m_sHostname
= CUnicodeUtils::GetUnicode(host
);
3396 dlg
.m_sError
= CFormatMessageWrapper(verificationError
);
3397 if (dlg
.DoModal() == IDOK
)
3399 last_accepted_cert
.set(cert
);
3403 return GIT_ECERTIFICATE
;
3406 void CAppUtils::ExploreTo(HWND hwnd
, CString path
)
3408 if (PathFileExists(path
))
3410 ITEMIDLIST __unaligned
* pidl
= ILCreateFromPath(path
);
3413 SHOpenFolderAndSelectItems(pidl
, 0, 0, 0);
3418 // if filepath does not exist any more, navigate to closest matching folder
3421 int pos
= path
.ReverseFind(_T('\\'));
3424 path
= path
.Left(pos
);
3425 } while (!PathFileExists(path
));
3426 ShellExecute(hwnd
, _T("explore"), path
, nullptr, nullptr, SW_SHOW
);