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"
71 CAppUtils::CAppUtils(void)
75 CAppUtils::~CAppUtils(void)
79 bool CAppUtils::StashSave(const CString
& msg
)
83 if (dlg
.DoModal() == IDOK
)
86 cmd
= _T("git.exe stash save");
88 if (CAppUtils::GetMsysgitVersion() >= 0x01070700)
90 if (dlg
.m_bIncludeUntracked
)
91 cmd
+= _T(" --include-untracked");
96 if (!dlg
.m_sMessage
.IsEmpty())
98 CString message
= dlg
.m_sMessage
;
99 message
.Replace(_T("\""), _T("\"\""));
100 cmd
+= _T(" -- \"") + message
+ _T("\"");
103 CProgressDlg progress
;
104 progress
.m_GitCmd
= cmd
;
105 return (progress
.DoModal() == IDOK
);
110 bool CAppUtils::StashApply(CString ref
, bool showChanges
/* true */)
113 cmd
= _T("git.exe stash apply ");
114 if (ref
.Find(_T("refs/")) == 0)
116 if (ref
.Find(_T("stash{")) == 0)
117 ref
= _T("stash@") + ref
.Mid(5);
120 CSysProgressDlg sysProgressDlg
;
121 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
122 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
123 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
124 sysProgressDlg
.SetShowProgressBar(false);
125 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
126 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
128 int ret
= g_Git
.Run(cmd
, &out
, CP_UTF8
);
130 sysProgressDlg
.Stop();
132 bool hasConflicts
= (out
.Find(_T("CONFLICT")) >= 0);
133 if (ret
&& !(ret
== 1 && hasConflicts
))
135 CMessageBox::Show(NULL
, CString(MAKEINTRESOURCE(IDS_PROC_STASHAPPLYFAILED
)) + _T("\n") + out
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
140 message
.LoadString(IDS_PROC_STASHAPPLYSUCCESS
);
142 message
.LoadString(IDS_PROC_STASHAPPLYFAILEDCONFLICTS
);
145 if(CMessageBox::Show(NULL
,message
+ _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES
))
146 ,_T("TortoiseGit"),MB_YESNO
|MB_ICONINFORMATION
) == IDYES
)
149 dlg
.m_pathList
.AddPath(CTGitPath());
156 CMessageBox::Show(NULL
, message
,_T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
163 bool CAppUtils::StashPop(bool showChanges
/* true */)
166 cmd
=_T("git.exe stash pop ");
168 CSysProgressDlg sysProgressDlg
;
169 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
170 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
171 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
172 sysProgressDlg
.SetShowProgressBar(false);
173 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
174 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
176 int ret
= g_Git
.Run(cmd
, &out
, CP_UTF8
);
178 sysProgressDlg
.Stop();
180 bool hasConflicts
= (out
.Find(_T("CONFLICT")) >= 0);
181 if (ret
&& !(ret
== 1 && hasConflicts
))
183 CMessageBox::Show(NULL
,CString(MAKEINTRESOURCE(IDS_PROC_STASHPOPFAILED
)) + _T("\n") + out
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
188 message
.LoadString(IDS_PROC_STASHPOPSUCCESS
);
190 message
.LoadString(IDS_PROC_STASHPOPFAILEDCONFLICTS
);
193 if(CMessageBox::Show(NULL
,CString(message
+ _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES
)))
194 ,_T("TortoiseGit"),MB_YESNO
|MB_ICONINFORMATION
) == IDYES
)
197 dlg
.m_pathList
.AddPath(CTGitPath());
204 CMessageBox::Show(NULL
, message
,_T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
211 BOOL
CAppUtils::StartExtMerge(
212 const CTGitPath
& basefile
, const CTGitPath
& theirfile
, const CTGitPath
& yourfile
, const CTGitPath
& mergedfile
,
213 const CString
& basename
, const CString
& theirname
, const CString
& yourname
, const CString
& mergedname
, bool bReadOnly
,
217 CRegString regCom
= CRegString(_T("Software\\TortoiseGit\\Merge"));
218 CString ext
= mergedfile
.GetFileExtension();
219 CString com
= regCom
;
220 bool bInternal
= false;
224 // is there an extension specific merge tool?
225 CRegString
mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext
.MakeLower());
226 if (!CString(mergetool
).IsEmpty())
231 // is there a filename specific merge tool?
232 CRegString
mergetool(_T("Software\\TortoiseGit\\MergeTools\\.") + mergedfile
.GetFilename().MakeLower());
233 if (!CString(mergetool
).IsEmpty())
238 if (com
.IsEmpty()||(com
.Left(1).Compare(_T("#"))==0))
240 // Maybe we should use TortoiseIDiff?
241 if ((ext
== _T(".jpg")) || (ext
== _T(".jpeg")) ||
242 (ext
== _T(".bmp")) || (ext
== _T(".gif")) ||
243 (ext
== _T(".png")) || (ext
== _T(".ico")) ||
244 (ext
== _T(".tif")) || (ext
== _T(".tiff")) ||
245 (ext
== _T(".dib")) || (ext
== _T(".emf")) ||
248 com
= CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe");
249 com
= _T("\"") + com
+ _T("\"");
250 com
= com
+ _T(" /base:%base /theirs:%theirs /mine:%mine /result:%merged");
251 com
= com
+ _T(" /basetitle:%bname /theirstitle:%tname /minetitle:%yname");
255 // use TortoiseGitMerge
257 com
= CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe");
258 com
= _T("\"") + com
+ _T("\"");
259 com
= com
+ _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
260 com
= com
+ _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
261 com
+= _T(" /saverequired");
265 s
.Format(L
" /resolvemsghwnd:%I64d", (__int64
)resolveMsgHwnd
);
269 if (!g_sGroupingUUID
.IsEmpty())
271 com
+= L
" /groupuuid:\"";
272 com
+= g_sGroupingUUID
;
276 // check if the params are set. If not, just add the files to the command line
277 if ((com
.Find(_T("%merged"))<0)&&(com
.Find(_T("%base"))<0)&&(com
.Find(_T("%theirs"))<0)&&(com
.Find(_T("%mine"))<0))
279 com
+= _T(" \"")+basefile
.GetWinPathString()+_T("\"");
280 com
+= _T(" \"")+theirfile
.GetWinPathString()+_T("\"");
281 com
+= _T(" \"")+yourfile
.GetWinPathString()+_T("\"");
282 com
+= _T(" \"")+mergedfile
.GetWinPathString()+_T("\"");
284 if (basefile
.IsEmpty())
286 com
.Replace(_T("/base:%base"), _T(""));
287 com
.Replace(_T("%base"), _T(""));
290 com
.Replace(_T("%base"), _T("\"") + basefile
.GetWinPathString() + _T("\""));
291 if (theirfile
.IsEmpty())
293 com
.Replace(_T("/theirs:%theirs"), _T(""));
294 com
.Replace(_T("%theirs"), _T(""));
297 com
.Replace(_T("%theirs"), _T("\"") + theirfile
.GetWinPathString() + _T("\""));
298 if (yourfile
.IsEmpty())
300 com
.Replace(_T("/mine:%mine"), _T(""));
301 com
.Replace(_T("%mine"), _T(""));
304 com
.Replace(_T("%mine"), _T("\"") + yourfile
.GetWinPathString() + _T("\""));
305 if (mergedfile
.IsEmpty())
307 com
.Replace(_T("/merged:%merged"), _T(""));
308 com
.Replace(_T("%merged"), _T(""));
311 com
.Replace(_T("%merged"), _T("\"") + mergedfile
.GetWinPathString() + _T("\""));
312 if (basename
.IsEmpty())
314 if (basefile
.IsEmpty())
316 com
.Replace(_T("/basename:%bname"), _T(""));
317 com
.Replace(_T("%bname"), _T(""));
321 com
.Replace(_T("%bname"), _T("\"") + basefile
.GetUIFileOrDirectoryName() + _T("\""));
325 com
.Replace(_T("%bname"), _T("\"") + basename
+ _T("\""));
326 if (theirname
.IsEmpty())
328 if (theirfile
.IsEmpty())
330 com
.Replace(_T("/theirsname:%tname"), _T(""));
331 com
.Replace(_T("%tname"), _T(""));
335 com
.Replace(_T("%tname"), _T("\"") + theirfile
.GetUIFileOrDirectoryName() + _T("\""));
339 com
.Replace(_T("%tname"), _T("\"") + theirname
+ _T("\""));
340 if (yourname
.IsEmpty())
342 if (yourfile
.IsEmpty())
344 com
.Replace(_T("/minename:%yname"), _T(""));
345 com
.Replace(_T("%yname"), _T(""));
349 com
.Replace(_T("%yname"), _T("\"") + yourfile
.GetUIFileOrDirectoryName() + _T("\""));
353 com
.Replace(_T("%yname"), _T("\"") + yourname
+ _T("\""));
354 if (mergedname
.IsEmpty())
356 if (mergedfile
.IsEmpty())
358 com
.Replace(_T("/mergedname:%mname"), _T(""));
359 com
.Replace(_T("%mname"), _T(""));
363 com
.Replace(_T("%mname"), _T("\"") + mergedfile
.GetUIFileOrDirectoryName() + _T("\""));
367 com
.Replace(_T("%mname"), _T("\"") + mergedname
+ _T("\""));
369 if ((bReadOnly
)&&(bInternal
))
370 com
+= _T(" /readonly");
372 if(!LaunchApplication(com
, IDS_ERR_EXTMERGESTART
, false))
380 BOOL
CAppUtils::StartExtPatch(const CTGitPath
& patchfile
, const CTGitPath
& dir
, const CString
& sOriginalDescription
, const CString
& sPatchedDescription
, BOOL bReversed
, BOOL bWait
)
383 // use TortoiseGitMerge
384 viewer
= CPathUtils::GetAppDirectory();
385 viewer
+= _T("TortoiseGitMerge.exe");
387 viewer
= _T("\"") + viewer
+ _T("\"");
388 viewer
= viewer
+ _T(" /diff:\"") + patchfile
.GetWinPathString() + _T("\"");
389 viewer
= viewer
+ _T(" /patchpath:\"") + dir
.GetWinPathString() + _T("\"");
391 viewer
+= _T(" /reversedpatch");
392 if (!sOriginalDescription
.IsEmpty())
393 viewer
= viewer
+ _T(" /patchoriginal:\"") + sOriginalDescription
+ _T("\"");
394 if (!sPatchedDescription
.IsEmpty())
395 viewer
= viewer
+ _T(" /patchpatched:\"") + sPatchedDescription
+ _T("\"");
396 if (!g_sGroupingUUID
.IsEmpty())
398 viewer
+= L
" /groupuuid:\"";
399 viewer
+= g_sGroupingUUID
;
402 if(!LaunchApplication(viewer
, IDS_ERR_DIFFVIEWSTART
, !!bWait
))
409 CString
CAppUtils::PickDiffTool(const CTGitPath
& file1
, const CTGitPath
& file2
)
411 CString difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file2
.GetFilename().MakeLower());
412 if (!difftool
.IsEmpty())
414 difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file1
.GetFilename().MakeLower());
415 if (!difftool
.IsEmpty())
418 // Is there an extension specific diff tool?
419 CString ext
= file2
.GetFileExtension().MakeLower();
422 difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext
);
423 if (!difftool
.IsEmpty())
425 // Maybe we should use TortoiseIDiff?
426 if ((ext
== _T(".jpg")) || (ext
== _T(".jpeg")) ||
427 (ext
== _T(".bmp")) || (ext
== _T(".gif")) ||
428 (ext
== _T(".png")) || (ext
== _T(".ico")) ||
429 (ext
== _T(".tif")) || (ext
== _T(".tiff")) ||
430 (ext
== _T(".dib")) || (ext
== _T(".emf")) ||
434 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe") + _T("\"") +
435 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname") +
436 L
" /groupuuid:\"" + g_sGroupingUUID
+ L
"\"";
440 // Finally, pick a generic external diff tool
441 difftool
= CRegString(_T("Software\\TortoiseGit\\Diff"));
445 bool CAppUtils::StartExtDiff(
446 const CString
& file1
, const CString
& file2
,
447 const CString
& sName1
, const CString
& sName2
,
448 const CString
& originalFile1
, const CString
& originalFile2
,
449 const git_revnum_t
& hash1
, const git_revnum_t
& hash2
,
450 const DiffFlags
& flags
, int jumpToLine
)
454 CRegDWORD
blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE
);
455 if (!flags
.bBlame
|| !(DWORD
)blamediff
)
457 viewer
= PickDiffTool(file1
, file2
);
458 // If registry entry for a diff program is commented out, use TortoiseGitMerge.
459 bool bCommentedOut
= viewer
.Left(1) == _T("#");
460 if (flags
.bAlternativeTool
)
462 // Invert external vs. internal diff tool selection.
464 viewer
.Delete(0); // uncomment
468 else if (bCommentedOut
)
472 bool bInternal
= viewer
.IsEmpty();
476 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe") + _T("\"") +
477 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname") +
478 _T(" /basereflectedname:%bpath /minereflectedname:%ypath");
479 if (!g_sGroupingUUID
.IsEmpty())
481 viewer
+= L
" /groupuuid:\"";
482 viewer
+= g_sGroupingUUID
;
486 viewer
+= _T(" /blame");
488 // check if the params are set. If not, just add the files to the command line
489 if ((viewer
.Find(_T("%base"))<0)&&(viewer
.Find(_T("%mine"))<0))
491 viewer
+= _T(" \"")+file1
+_T("\"");
492 viewer
+= _T(" \"")+file2
+_T("\"");
494 if (viewer
.Find(_T("%base")) >= 0)
496 viewer
.Replace(_T("%base"), _T("\"")+file1
+_T("\""));
498 if (viewer
.Find(_T("%mine")) >= 0)
500 viewer
.Replace(_T("%mine"), _T("\"")+file2
+_T("\""));
503 if (sName1
.IsEmpty())
504 viewer
.Replace(_T("%bname"), _T("\"") + file1
+ _T("\""));
506 viewer
.Replace(_T("%bname"), _T("\"") + sName1
+ _T("\""));
508 if (sName2
.IsEmpty())
509 viewer
.Replace(_T("%yname"), _T("\"") + file2
+ _T("\""));
511 viewer
.Replace(_T("%yname"), _T("\"") + sName2
+ _T("\""));
513 viewer
.Replace(_T("%bpath"), _T("\"") + originalFile1
+ _T("\""));
514 viewer
.Replace(_T("%ypath"), _T("\"") + originalFile2
+ _T("\""));
516 viewer
.Replace(_T("%brev"), _T("\"") + hash1
+ _T("\""));
517 viewer
.Replace(_T("%yrev"), _T("\"") + hash2
+ _T("\""));
519 if (flags
.bReadOnly
&& bInternal
)
520 viewer
+= _T(" /readonly");
525 temp
.Format(_T(" /line:%d"), jumpToLine
);
529 return LaunchApplication(viewer
, IDS_ERR_EXTDIFFSTART
, flags
.bWait
);
532 BOOL
CAppUtils::StartUnifiedDiffViewer(const CString
& patchfile
, const CString
& title
, BOOL bWait
)
535 CRegString v
= CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
537 if (viewer
.IsEmpty() || (viewer
.Left(1).Compare(_T("#"))==0))
539 // use TortoiseGitUDiff
540 viewer
= CPathUtils::GetAppDirectory();
541 viewer
+= _T("TortoiseGitUDiff.exe");
542 // enquote the path to TortoiseGitUDiff
543 viewer
= _T("\"") + viewer
+ _T("\"");
545 viewer
= viewer
+ _T(" /patchfile:%1 /title:\"%title\"");
546 if (!g_sGroupingUUID
.IsEmpty())
548 viewer
+= L
" /groupuuid:\"";
549 viewer
+= g_sGroupingUUID
;
553 if (viewer
.Find(_T("%1"))>=0)
555 if (viewer
.Find(_T("\"%1\"")) >= 0)
556 viewer
.Replace(_T("%1"), patchfile
);
558 viewer
.Replace(_T("%1"), _T("\"") + patchfile
+ _T("\""));
561 viewer
+= _T(" \"") + patchfile
+ _T("\"");
562 if (viewer
.Find(_T("%title")) >= 0)
564 viewer
.Replace(_T("%title"), title
);
567 if(!LaunchApplication(viewer
, IDS_ERR_DIFFVIEWSTART
, !!bWait
))
574 BOOL
CAppUtils::StartTextViewer(CString file
)
577 CRegString txt
= CRegString(_T(".txt\\"), _T(""), FALSE
, HKEY_CLASSES_ROOT
);
579 viewer
= viewer
+ _T("\\Shell\\Open\\Command\\");
580 CRegString txtexe
= CRegString(viewer
, _T(""), FALSE
, HKEY_CLASSES_ROOT
);
583 DWORD len
= ExpandEnvironmentStrings(viewer
, NULL
, 0);
584 std::unique_ptr
<TCHAR
[]> buf(new TCHAR
[len
+ 1]);
585 ExpandEnvironmentStrings(viewer
, buf
.get(), len
);
587 len
= ExpandEnvironmentStrings(file
, NULL
, 0);
588 std::unique_ptr
<TCHAR
[]> buf2(new TCHAR
[len
+ 1]);
589 ExpandEnvironmentStrings(file
, buf2
.get(), len
);
591 file
= _T("\"")+file
+_T("\"");
592 if (viewer
.IsEmpty())
594 viewer
= _T("RUNDLL32 Shell32,OpenAs_RunDLL");
596 if (viewer
.Find(_T("\"%1\"")) >= 0)
598 viewer
.Replace(_T("\"%1\""), file
);
600 else if (viewer
.Find(_T("%1")) >= 0)
602 viewer
.Replace(_T("%1"), file
);
610 if(!LaunchApplication(viewer
, IDS_ERR_TEXTVIEWSTART
, false))
617 BOOL
CAppUtils::CheckForEmptyDiff(const CTGitPath
& sDiffPath
)
620 CAutoFile hFile
= ::CreateFile(sDiffPath
.GetWinPath(), GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
623 length
= ::GetFileSize(hFile
, NULL
);
630 void CAppUtils::CreateFontForLogs(CFont
& fontToCreate
)
633 HDC hScreenDC
= ::GetDC(NULL
);
634 logFont
.lfHeight
= -MulDiv((DWORD
)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC
, LOGPIXELSY
), 72);
635 ::ReleaseDC(NULL
, hScreenDC
);
637 logFont
.lfEscapement
= 0;
638 logFont
.lfOrientation
= 0;
639 logFont
.lfWeight
= FW_NORMAL
;
640 logFont
.lfItalic
= 0;
641 logFont
.lfUnderline
= 0;
642 logFont
.lfStrikeOut
= 0;
643 logFont
.lfCharSet
= DEFAULT_CHARSET
;
644 logFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
645 logFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
646 logFont
.lfQuality
= DRAFT_QUALITY
;
647 logFont
.lfPitchAndFamily
= FF_DONTCARE
| FIXED_PITCH
;
648 _tcscpy_s(logFont
.lfFaceName
, 32, (LPCTSTR
)(CString
)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
649 VERIFY(fontToCreate
.CreateFontIndirect(&logFont
));
652 bool CAppUtils::LaunchPAgent(const CString
* keyfile
, const CString
* pRemote
)
666 cmd
.Format(_T("remote.%s.puttykeyfile"),remote
);
667 key
= g_Git
.GetConfigValue(cmd
);
675 CString proc
=CPathUtils::GetAppDirectory();
676 proc
+= _T("pageant.exe \"");
680 CString tempfile
= GetTempFile();
681 ::DeleteFile(tempfile
);
683 proc
+= _T(" -c \"");
684 proc
+= CPathUtils::GetAppDirectory();
685 proc
+= _T("tgittouch.exe\"");
690 CString appDir
= CPathUtils::GetAppDirectory();
691 bool b
= LaunchApplication(proc
, IDS_ERR_PAGEANT
, true, &appDir
);
696 while(!::PathFileExists(tempfile
))
701 break; //timeout 5 minutes
706 CMessageBox::Show(NULL
, IDS_ERR_PAEGENTTIMEOUT
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
708 ::DeleteFile(tempfile
);
711 bool CAppUtils::LaunchAlternativeEditor(const CString
& filename
, bool uac
)
713 CString editTool
= CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
714 if (editTool
.IsEmpty() || (editTool
.Left(1).Compare(_T("#"))==0)) {
715 editTool
= CPathUtils::GetAppDirectory() + _T("notepad2.exe");
719 sCmd
.Format(_T("\"%s\" \"%s\""), editTool
, filename
);
721 LaunchApplication(sCmd
, NULL
, false, NULL
, uac
);
724 bool CAppUtils::LaunchRemoteSetting()
726 CTGitPath
path(g_Git
.m_CurrentDir
);
727 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
, &path
);
728 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
729 dlg
.SetTreeWidth(220);
730 dlg
.m_DefaultPage
= _T("gitremote");
737 * Launch the external blame viewer
739 bool CAppUtils::LaunchTortoiseBlame(const CString
& sBlameFile
,CString Rev
,const CString
& sParams
)
741 CString viewer
= _T("\"") + CPathUtils::GetAppDirectory();
742 viewer
+= _T("TortoiseGitBlame.exe");
743 viewer
+= _T("\" \"") + sBlameFile
+ _T("\"");
744 //viewer += _T(" \"") + sLogFile + _T("\"");
745 //viewer += _T(" \"") + sOriginalFile + _T("\"");
746 if(!Rev
.IsEmpty() && Rev
!= GIT_REV_ZERO
)
747 viewer
+= CString(_T(" /rev:"))+Rev
;
748 if (!g_sGroupingUUID
.IsEmpty())
750 viewer
+= L
" /groupuuid:\"";
751 viewer
+= g_sGroupingUUID
;
754 viewer
+= _T(" ")+sParams
;
756 return LaunchApplication(viewer
, IDS_ERR_TGITBLAME
, false);
759 bool CAppUtils::FormatTextInRichEditControl(CWnd
* pWnd
)
764 bool bStyled
= false;
765 pWnd
->GetWindowText(sText
);
766 // the rich edit control doesn't count the CR char!
767 // to be exact: CRLF is treated as one char.
768 sText
.Remove(_T('\r'));
770 // style each line separately
775 nNewlinePos
= sText
.Find('\n', offset
);
776 CString sLine
= nNewlinePos
>= 0 ? sText
.Mid(offset
, nNewlinePos
- offset
) : sText
.Mid(offset
);
780 while (FindStyleChars(sLine
, '*', start
, end
))
782 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
783 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
784 SetCharFormat(pWnd
, CFM_BOLD
, CFE_BOLD
);
790 while (FindStyleChars(sLine
, '^', start
, end
))
792 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
793 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
794 SetCharFormat(pWnd
, CFM_ITALIC
, CFE_ITALIC
);
800 while (FindStyleChars(sLine
, '_', start
, end
))
802 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
803 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
804 SetCharFormat(pWnd
, CFM_UNDERLINE
, CFE_UNDERLINE
);
808 offset
= nNewlinePos
+1;
809 } while(nNewlinePos
>=0);
813 bool CAppUtils::FindStyleChars(const CString
& sText
, TCHAR stylechar
, int& start
, int& end
)
816 int last
= sText
.GetLength() - 1;
817 bool bFoundMarker
= false;
818 TCHAR c
= i
== 0 ? _T('\0') : sText
[i
- 1];
819 TCHAR nextChar
= i
>= last
? _T('\0') : sText
[i
+ 1];
821 // find a starting marker
826 nextChar
= sText
[i
+ 1];
828 // IsCharAlphaNumeric can be somewhat expensive.
829 // Long lines of "*****" or "----" will be pre-empted efficiently
830 // by the (c != nextChar) condition.
832 if ((c
== stylechar
) && (c
!= nextChar
))
834 if (IsCharAlphaNumeric(nextChar
) && !IsCharAlphaNumeric(prevChar
))
846 // find ending marker
849 bFoundMarker
= false;
856 if ((i
== last
) || (!IsCharAlphaNumeric(sText
[i
+ 1]) && IsCharAlphaNumeric(prevChar
)))
869 bool CAppUtils::StartShowUnifiedDiff(HWND hWnd
, const CTGitPath
& url1
, const git_revnum_t
& rev1
,
870 const CTGitPath
& /*url2*/, const git_revnum_t
& rev2
,
871 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
872 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */,
873 bool /* blame = false */,
878 if (GetMsysgitVersion() > 0x01080100)
879 diffContext
= g_Git
.GetConfigValueInt32(_T("diff.context"), -1);
880 CString tempfile
=GetTempFile();
881 if (g_Git
.GetUnifiedDiff(url1
, rev1
, rev2
, tempfile
, bMerge
, bCombine
, diffContext
))
883 CMessageBox::Show(hWnd
, g_Git
.GetGitLastErr(_T("Could not get unified diff."), CGit::GIT_CMD_DIFF
), _T("TortoiseGit"), MB_OK
);
886 CAppUtils::StartUnifiedDiffViewer(tempfile
, rev1
+ _T(":") + rev2
);
890 sCmd
.Format(_T("%s /command:showcompare /unified"),
891 (LPCTSTR
)(CPathUtils::GetAppDirectory()+_T("TortoiseGitProc.exe")));
892 sCmd
+= _T(" /url1:\"") + url1
.GetGitPathString() + _T("\"");
894 sCmd
+= _T(" /revision1:") + rev1
.ToString();
895 sCmd
+= _T(" /url2:\"") + url2
.GetGitPathString() + _T("\"");
897 sCmd
+= _T(" /revision2:") + rev2
.ToString();
899 sCmd
+= _T(" /pegrevision:") + peg
.ToString();
900 if (headpeg
.IsValid())
901 sCmd
+= _T(" /headpegrevision:") + headpeg
.ToString();
904 sCmd
+= _T(" /alternatediff");
907 sCmd
+= _T(" /ignoreancestry");
911 sCmd
+= _T(" /hwnd:");
913 _stprintf_s(buf
, 30, _T("%p"), (void*)hWnd
);
917 return CAppUtils::LaunchApplication(sCmd
, NULL
, false);
922 bool CAppUtils::SetupDiffScripts(bool force
, const CString
& type
)
924 CString scriptsdir
= CPathUtils::GetAppParentDirectory();
925 scriptsdir
+= _T("Diff-Scripts");
926 CSimpleFileFind
files(scriptsdir
);
927 while (files
.FindNextFileNoDirectories())
929 CString file
= files
.GetFilePath();
930 CString filename
= files
.GetFileName();
931 CString ext
= file
.Mid(file
.ReverseFind('-') + 1);
932 ext
= _T(".") + ext
.Left(ext
.ReverseFind('.'));
933 std::set
<CString
> extensions
;
934 extensions
.insert(ext
);
936 if (file
.Right(3).CompareNoCase(_T("vbs"))==0)
938 kind
= _T(" //E:vbscript");
940 if (file
.Right(2).CompareNoCase(_T("js"))==0)
942 kind
= _T(" //E:javascript");
944 // open the file, read the first line and find possible extensions
945 // this script can handle
948 CStdioFile
f(file
, CFile::modeRead
| CFile::shareDenyNone
);
950 if (f
.ReadString(extline
))
952 if ((extline
.GetLength() > 15 ) &&
953 ((extline
.Left(15).Compare(_T("// extensions: ")) == 0) ||
954 (extline
.Left(14).Compare(_T("' extensions: ")) == 0)))
956 if (extline
[0] == '/')
957 extline
= extline
.Mid(15);
959 extline
= extline
.Mid(14);
962 sToken
= extline
.Tokenize(_T(";"), curPos
);
963 while (!sToken
.IsEmpty())
965 if (!sToken
.IsEmpty())
967 if (sToken
[0] != '.')
968 sToken
= _T(".") + sToken
;
969 extensions
.insert(sToken
);
971 sToken
= extline
.Tokenize(_T(";"), curPos
);
977 catch (CFileException
* e
)
982 for (std::set
<CString
>::const_iterator it
= extensions
.begin(); it
!= extensions
.end(); ++it
)
984 if (type
.IsEmpty() || (type
.Compare(_T("Diff")) == 0))
986 if (filename
.Left(5).CompareNoCase(_T("diff-")) == 0)
988 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + *it
);
989 CString diffregstring
= diffreg
;
990 if (force
|| (diffregstring
.IsEmpty()) || (diffregstring
.Find(filename
) >= 0))
991 diffreg
= _T("wscript.exe \"") + file
+ _T("\" %base %mine") + kind
;
994 if (type
.IsEmpty() || (type
.Compare(_T("Merge"))==0))
996 if (filename
.Left(6).CompareNoCase(_T("merge-"))==0)
998 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\MergeTools\\") + *it
);
999 CString diffregstring
= diffreg
;
1000 if (force
|| (diffregstring
.IsEmpty()) || (diffregstring
.Find(filename
) >= 0))
1001 diffreg
= _T("wscript.exe \"") + file
+ _T("\" %merged %theirs %mine %base") + kind
;
1010 bool CAppUtils::Export(CString
*BashHash
, const CTGitPath
*orgPath
)
1012 // ask from where the export has to be done
1015 dlg
.m_initialRefName
=*BashHash
;
1018 if (PathIsRelative(orgPath
->GetWinPath()))
1019 dlg
.m_orgPath
= g_Git
.CombinePath(orgPath
);
1021 dlg
.m_orgPath
= *orgPath
;
1024 if (dlg
.DoModal() == IDOK
)
1027 cmd
.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s --"),
1028 dlg
.m_strFile
, g_Git
.FixBranchName(dlg
.m_VersionName
));
1033 if (!dlg
.m_bWholeProject
&& !dlg
.m_orgPath
.IsEmpty() && PathIsDirectory(dlg
.m_orgPath
.GetWinPathString()))
1035 git
.m_CurrentDir
= dlg
.m_orgPath
.GetWinPathString();
1038 return (pro
.DoModal() == IDOK
);
1043 bool CAppUtils::CreateBranchTag(bool IsTag
,CString
*CommitHash
, bool switch_new_brach
)
1045 CCreateBranchTagDlg dlg
;
1047 dlg
.m_bSwitch
=switch_new_brach
;
1050 dlg
.m_initialRefName
= *CommitHash
;
1052 if(dlg
.DoModal()==IDOK
)
1057 if(dlg
.m_bTrack
== TRUE
)
1058 track
=_T(" --track ");
1059 else if(dlg
.m_bTrack
== FALSE
)
1060 track
=_T(" --no-track");
1071 cmd
.Format(_T("git.exe tag %s %s %s %s"),
1074 dlg
.m_BranchTagName
,
1075 g_Git
.FixBranchName(dlg
.m_VersionName
)
1078 if(!dlg
.m_Message
.Trim().IsEmpty())
1080 CString tempfile
= ::GetTempFile();
1081 if (CAppUtils::SaveCommitUnicodeFile(tempfile
, dlg
.m_Message
))
1083 CMessageBox::Show(nullptr, _T("Could not save tag message"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
1086 cmd
+= _T(" -F ")+tempfile
;
1091 cmd
.Format(_T("git.exe branch %s %s %s %s"),
1094 dlg
.m_BranchTagName
,
1095 g_Git
.FixBranchName(dlg
.m_VersionName
)
1099 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1101 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1104 if( !IsTag
&& dlg
.m_bSwitch
)
1106 // it is a new branch and the user has requested to switch to it
1107 PerformSwitch(dlg
.m_BranchTagName
);
1115 bool CAppUtils::Switch(CString initialRefName
)
1118 if(!initialRefName
.IsEmpty())
1119 dlg
.m_initialRefName
= initialRefName
;
1121 if (dlg
.DoModal() == IDOK
)
1125 branch
= dlg
.m_NewBranch
;
1127 // if refs/heads/ is not stripped, checkout will detach HEAD
1128 // checkout prefers branches on name clashes (with tags)
1129 if (dlg
.m_VersionName
.Left(11) ==_T("refs/heads/") && dlg
.m_bBranchOverride
!= TRUE
)
1130 dlg
.m_VersionName
= dlg
.m_VersionName
.Mid(11);
1132 return PerformSwitch(dlg
.m_VersionName
, dlg
.m_bForce
== TRUE
, branch
, dlg
.m_bBranchOverride
== TRUE
, dlg
.m_bTrack
, dlg
.m_bMerge
== TRUE
);
1137 bool CAppUtils::PerformSwitch(CString ref
, bool bForce
/* false */, CString sNewBranch
/* CString() */, bool bBranchOverride
/* false */, BOOL bTrack
/* 2 */, bool bMerge
/* false */)
1145 if(!sNewBranch
.IsEmpty()){
1146 if (bBranchOverride
)
1148 branch
.Format(_T("-B %s"), sNewBranch
);
1152 branch
.Format(_T("-b %s"), sNewBranch
);
1155 track
= _T("--track");
1156 else if (bTrack
== FALSE
)
1157 track
= _T("--no-track");
1162 merge
= _T("--merge");
1164 cmd
.Format(_T("git.exe checkout %s %s %s %s %s --"),
1169 g_Git
.FixBranchName(ref
));
1171 CProgressDlg progress
;
1172 progress
.m_GitCmd
= cmd
;
1174 CString currentBranch
;
1175 bool hasBranch
= CGit::GetCurrentBranchFromFile(g_Git
.m_CurrentDir
, currentBranch
) == 0;
1176 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
1180 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
1181 if (gitPath
.HasSubmodules())
1183 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, [&]
1186 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
1187 RunTortoiseGitProc(sCmd
);
1191 postCmdList
.push_back(PostCmd(IDI_MERGE
, IDS_MENUMERGE
, [&]{ Merge(¤tBranch
); }));
1195 if (!CGit::GetCurrentBranchFromFile(g_Git
.m_CurrentDir
, newBranch
))
1196 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUPULL
, [&]{ Pull(); }));
1200 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ PerformSwitch(ref
, bForce
, sNewBranch
, bBranchOverride
, bTrack
, bMerge
); }));
1202 postCmdList
.push_back(PostCmd(IDI_SWITCH
, IDS_SWITCH_WITH_MERGE
, [&]{ PerformSwitch(ref
, bForce
, sNewBranch
, bBranchOverride
, bTrack
, true); }));
1206 INT_PTR ret
= progress
.DoModal();
1211 class CIgnoreFile
: public CStdioFile
1214 STRING_VECTOR m_Items
;
1217 virtual BOOL
ReadString(CString
& rString
)
1219 if (GetPosition() == 0)
1221 unsigned char utf8bom
[] = { 0xEF, 0xBB, 0xBF };
1222 char buf
[3] = { 0, 0, 0 };
1224 if (memcpy(buf
, utf8bom
, sizeof(utf8bom
)))
1231 char lastChar
= '\0';
1232 for (char c
= '\0'; Read(&c
, 1) == 1; lastChar
= c
)
1238 m_eol
= lastChar
== '\r' ? _T("\r\n") : _T("\n");
1246 rString
= CUnicodeUtils::GetUnicode(strA
);
1257 bool CAppUtils::OpenIgnoreFile(CIgnoreFile
&file
, const CString
& filename
)
1260 if (!file
.Open(filename
, CFile::modeCreate
| CFile::modeReadWrite
| CFile::modeNoTruncate
| CFile::typeBinary
))
1262 CMessageBox::Show(NULL
, filename
+ _T(" Open Failure"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
1266 if (file
.GetLength() > 0)
1269 while (file
.ReadString(fileText
))
1270 file
.m_Items
.push_back(fileText
);
1271 file
.Seek(file
.GetLength() - 1, 0);
1272 char lastchar
[1] = { 0 };
1273 file
.Read(lastchar
, 1);
1275 if (lastchar
[0] != '\n')
1277 CStringA eol
= CStringA(file
.m_eol
.IsEmpty() ? _T("\n") : file
.m_eol
);
1278 file
.Write(eol
, eol
.GetLength());
1287 bool CAppUtils::IgnoreFile(CTGitPathList
&path
,bool IsMask
)
1289 CIgnoreDlg ignoreDlg
;
1290 if (ignoreDlg
.DoModal() == IDOK
)
1293 ignorefile
= g_Git
.m_CurrentDir
+ _T("\\");
1295 switch (ignoreDlg
.m_IgnoreFile
)
1298 ignorefile
+= _T(".gitignore");
1301 g_GitAdminDir
.GetAdminDirPath(g_Git
.m_CurrentDir
, ignorefile
);
1302 ignorefile
+= _T("info/exclude");
1309 if (ignoreDlg
.m_IgnoreFile
!= 1 && !OpenIgnoreFile(file
, ignorefile
))
1312 for (int i
= 0; i
< path
.GetCount(); ++i
)
1314 if (ignoreDlg
.m_IgnoreFile
== 1)
1316 ignorefile
= g_Git
.CombinePath(path
[i
].GetContainingDirectory()) + _T("\\.gitignore");
1317 if (!OpenIgnoreFile(file
, ignorefile
))
1321 CString ignorePattern
;
1322 if (ignoreDlg
.m_IgnoreType
== 0)
1324 if (ignoreDlg
.m_IgnoreFile
!= 1 && !path
[i
].GetContainingDirectory().GetGitPathString().IsEmpty())
1325 ignorePattern
+= _T("/") + path
[i
].GetContainingDirectory().GetGitPathString();
1327 ignorePattern
+= _T("/");
1331 ignorePattern
+= _T("*") + path
[i
].GetFileExtension();
1335 ignorePattern
+= path
[i
].GetFileOrDirectoryName();
1338 // escape [ and ] so that files get ignored correctly
1339 ignorePattern
.Replace(_T("["), _T("\\["));
1340 ignorePattern
.Replace(_T("]"), _T("\\]"));
1343 for (size_t j
= 0; j
< file
.m_Items
.size(); ++j
)
1345 if (file
.m_Items
[j
] == ignorePattern
)
1353 file
.m_Items
.push_back(ignorePattern
);
1354 ignorePattern
+= file
.m_eol
.IsEmpty() ? _T("\n") : file
.m_eol
;
1355 CStringA ignorePatternA
= CUnicodeUtils::GetUTF8(ignorePattern
);
1356 file
.Write(ignorePatternA
, ignorePatternA
.GetLength());
1359 if (ignoreDlg
.m_IgnoreFile
== 1)
1363 if (ignoreDlg
.m_IgnoreFile
!= 1)
1377 static bool Reset(const CString
& resetTo
, int resetType
)
1384 type
= _T("--soft");
1387 type
= _T("--mixed");
1390 type
= _T("--hard");
1394 type
= _T("--mixed");
1397 cmd
.Format(_T("git.exe reset %s %s --"), type
, resetTo
);
1399 CProgressDlg progress
;
1400 progress
.m_GitCmd
= cmd
;
1402 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
1406 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ Reset(resetTo
, resetType
); }));
1410 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
1411 if (gitPath
.HasSubmodules() && resetType
== 2)
1413 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, [&]
1416 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
1417 CAppUtils::RunTortoiseGitProc(sCmd
);
1423 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_RESET
))
1425 CGitProgressDlg gitdlg
;
1426 ResetProgressCommand resetProgressCommand
;
1427 gitdlg
.SetCommand(&resetProgressCommand
);
1428 resetProgressCommand
.m_PostCmdCallback
= progress
.m_PostCmdCallback
;
1429 resetProgressCommand
.SetRevision(resetTo
);
1430 resetProgressCommand
.SetResetType(resetType
);
1431 ret
= gitdlg
.DoModal();
1434 ret
= progress
.DoModal();
1439 bool CAppUtils::GitReset(CString
*CommitHash
,int type
)
1442 dlg
.m_ResetType
=type
;
1443 dlg
.m_ResetToVersion
=*CommitHash
;
1444 dlg
.m_initialRefName
= *CommitHash
;
1445 if (dlg
.DoModal() == IDOK
)
1446 return Reset(dlg
.m_ResetToVersion
, dlg
.m_ResetType
);
1451 void CAppUtils::DescribeConflictFile(bool mode
, bool base
,CString
&descript
)
1455 descript
= CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE
));
1460 descript
= CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED
));
1463 descript
= CString(MAKEINTRESOURCE(IDS_PROC_CREATED
));
1467 void CAppUtils::RemoveTempMergeFile(CTGitPath
&path
)
1469 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("LOCAL"), path
));
1470 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("REMOTE"), path
));
1471 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("BASE"), path
));
1473 CString
CAppUtils::GetMergeTempFile(CString type
,CTGitPath
&merge
)
1476 file
= g_Git
.CombinePath(merge
.GetWinPathString() + _T(".") + type
+ merge
.GetFileExtension());
1481 bool ParseHashesFromLsFile(BYTE_VECTOR
&out
, CString
&hash1
, CString
&hash2
, CString
&hash3
)
1483 unsigned int pos
= 0;
1487 while (pos
>= 0 && pos
< out
.size())
1491 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1493 one
.Tokenize(_T("\t"), tabstart
);
1496 part
= one
.Tokenize(_T(" "), tabstart
); //Tag
1497 part
= one
.Tokenize(_T(" "), tabstart
); //Mode
1498 part
= one
.Tokenize(_T(" "), tabstart
); //Hash
1499 CString hash
= part
;
1500 part
= one
.Tokenize(_T("\t"), tabstart
); //Stage
1501 int stage
= _ttol(part
);
1504 else if (stage
== 2)
1506 else if (stage
== 3)
1512 pos
= out
.findNextString(pos
);
1518 bool CAppUtils::ConflictEdit(CTGitPath
& path
, bool /*bAlternativeTool = false*/, bool revertTheirMy
/*= false*/, HWND resolveMsgHwnd
/*= nullptr*/)
1522 CTGitPath merge
=path
;
1523 CTGitPath directory
= merge
.GetDirectory();
1525 // we have the conflicted file (%merged)
1526 // now look for the other required files
1528 //stat.GetStatus(merge);
1529 //if (stat.status == NULL)
1535 cmd
.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge
.GetGitPathString());
1537 if (g_Git
.Run(cmd
, &vector
))
1542 if (merge
.IsDirectory())
1544 CString baseHash
, localHash
, remoteHash
;
1545 if (!ParseHashesFromLsFile(vector
, baseHash
, localHash
, remoteHash
))
1549 msg
.Format(_T("BASE: %s\nLOCAL: %s\nREMOTE: %s"), baseHash
, localHash
, remoteHash
);
1550 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
);
1555 if (list
.ParserFromLsFile(vector
))
1557 CMessageBox::Show(NULL
, _T("Parse ls-files failed!"), _T("TortoiseGit"), MB_OK
);
1570 mine
.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge
));
1571 theirs
.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge
));
1575 mine
.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge
));
1576 theirs
.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge
));
1578 base
.SetFromGit(GetMergeTempFile(_T("BASE"),merge
));
1582 //format=_T("git.exe cat-file blob \":%d:%s\"");
1583 format
= _T("git.exe checkout-index --temp --stage=%d -- \"%s\"");
1585 //create a empty file, incase stage is not three
1586 tempfile
.Open(mine
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1588 tempfile
.Open(theirs
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1590 tempfile
.Open(base
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1593 bool b_base
=false, b_local
=false, b_remote
=false;
1595 for (int i
= 0; i
< list
.GetCount(); ++i
)
1602 if( list
[i
].m_Stage
== 1)
1604 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1606 outfile
= base
.GetWinPathString();
1609 if( list
[i
].m_Stage
== 2 )
1611 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1613 outfile
= mine
.GetWinPathString();
1616 if( list
[i
].m_Stage
== 3 )
1618 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1620 outfile
= theirs
.GetWinPathString();
1622 CString output
, err
;
1623 if(!outfile
.IsEmpty())
1624 if (!g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
1628 file
= output
.Tokenize(_T("\t"), start
);
1629 ::MoveFileEx(file
,outfile
,MOVEFILE_REPLACE_EXISTING
|MOVEFILE_COPY_ALLOWED
);
1633 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
1637 if(b_local
&& b_remote
)
1639 merge
.SetFromWin(g_Git
.CombinePath(merge
));
1641 bRet
= !!CAppUtils::StartExtMerge(base
, mine
, theirs
, merge
, _T("BASE"), _T("REMOTE"), _T("LOCAL"), CString(), false, resolveMsgHwnd
);
1643 bRet
= !!CAppUtils::StartExtMerge(base
, theirs
, mine
, merge
, _T("BASE"), _T("REMOTE"), _T("LOCAL"), CString(), false, resolveMsgHwnd
);
1648 ::DeleteFile(mine
.GetWinPathString());
1649 ::DeleteFile(theirs
.GetWinPathString());
1650 ::DeleteFile(base
.GetWinPathString());
1652 CDeleteConflictDlg dlg
;
1653 DescribeConflictFile(b_local
, b_base
,dlg
.m_LocalStatus
);
1654 DescribeConflictFile(b_remote
,b_base
,dlg
.m_RemoteStatus
);
1655 CGitHash localHash
, remoteHash
;
1656 if (!g_Git
.GetHash(localHash
, _T("HEAD")))
1657 dlg
.m_LocalHash
= localHash
.ToString();
1658 if (!g_Git
.GetHash(remoteHash
, _T("MERGE_HEAD")))
1659 dlg
.m_RemoteHash
= remoteHash
.ToString();
1660 else if (!g_Git
.GetHash(remoteHash
, _T("rebase-apply/original-commit")))
1661 dlg
.m_RemoteHash
= remoteHash
.ToString();
1662 else if (!g_Git
.GetHash(remoteHash
, _T("CHERRY_PICK_HEAD")))
1663 dlg
.m_RemoteHash
= remoteHash
.ToString();
1664 else if (!g_Git
.GetHash(remoteHash
, _T("REVERT_HEAD")))
1665 dlg
.m_RemoteHash
= remoteHash
.ToString();
1666 dlg
.m_bShowModifiedButton
=b_base
;
1667 dlg
.m_File
=merge
.GetGitPathString();
1668 if(dlg
.DoModal() == IDOK
)
1673 cmd
.Format(_T("git.exe rm -- \"%s\""),merge
.GetGitPathString());
1676 cmd
.Format(_T("git.exe add -- \"%s\""),merge
.GetGitPathString());
1678 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
1680 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1690 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1691 base
, theirs
, mine
, merge
);
1694 if (stat
.status
->text_status
== svn_wc_status_conflicted
)
1696 // we have a text conflict, use our merge tool to resolve the conflict
1698 CTSVNPath
theirs(directory
);
1699 CTSVNPath
mine(directory
);
1700 CTSVNPath
base(directory
);
1701 bool bConflictData
= false;
1703 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_new
))
1705 theirs
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_new
));
1706 bConflictData
= true;
1708 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_old
))
1710 base
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_old
));
1711 bConflictData
= true;
1713 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_wrk
))
1715 mine
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_wrk
));
1716 bConflictData
= true;
1723 bRet
= !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1724 base
, theirs
, mine
, merge
);
1727 if (stat
.status
->prop_status
== svn_wc_status_conflicted
)
1729 // we have a property conflict
1730 CTSVNPath
prej(directory
);
1731 if ((stat
.status
->entry
)&&(stat
.status
->entry
->prejfile
))
1733 prej
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->prejfile
));
1734 // there's a problem: the prej file contains a _description_ of the conflict, and
1735 // that description string might be translated. That means we have no way of parsing
1736 // the file to find out the conflicting values.
1737 // The only thing we can do: show a dialog with the conflict description, then
1738 // let the user either accept the existing property or open the property edit dialog
1739 // to manually change the properties and values. And a button to mark the conflict as
1741 CEditPropConflictDlg dlg
;
1742 dlg
.SetPrejFile(prej
);
1743 dlg
.SetConflictedItem(merge
);
1744 bRet
= (dlg
.DoModal() != IDCANCEL
);
1748 if (stat
.status
->tree_conflict
)
1750 // we have a tree conflict
1752 const SVNInfoData
* pInfoData
= info
.GetFirstFileInfo(merge
, SVNRev(), SVNRev());
1755 if (pInfoData
->treeconflict_kind
== svn_wc_conflict_kind_text
)
1757 CTSVNPath
theirs(directory
);
1758 CTSVNPath
mine(directory
);
1759 CTSVNPath
base(directory
);
1760 bool bConflictData
= false;
1762 if (pInfoData
->treeconflict_theirfile
)
1764 theirs
.AppendPathString(pInfoData
->treeconflict_theirfile
);
1765 bConflictData
= true;
1767 if (pInfoData
->treeconflict_basefile
)
1769 base
.AppendPathString(pInfoData
->treeconflict_basefile
);
1770 bConflictData
= true;
1772 if (pInfoData
->treeconflict_myfile
)
1774 mine
.AppendPathString(pInfoData
->treeconflict_myfile
);
1775 bConflictData
= true;
1782 bRet
= !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1783 base
, theirs
, mine
, merge
);
1785 else if (pInfoData
->treeconflict_kind
== svn_wc_conflict_kind_tree
)
1787 CString sConflictAction
;
1788 CString sConflictReason
;
1789 CString sResolveTheirs
;
1790 CString sResolveMine
;
1791 CTSVNPath treeConflictPath
= CTSVNPath(pInfoData
->treeconflict_path
);
1792 CString sItemName
= treeConflictPath
.GetUIFileOrDirectoryName();
1794 if (pInfoData
->treeconflict_nodekind
== svn_node_file
)
1796 switch (pInfoData
->treeconflict_operation
)
1798 case svn_wc_operation_update
:
1799 switch (pInfoData
->treeconflict_action
)
1801 case svn_wc_conflict_action_edit
:
1802 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEEDIT
, (LPCTSTR
)sItemName
);
1803 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1805 case svn_wc_conflict_action_add
:
1806 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEADD
, (LPCTSTR
)sItemName
);
1807 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1809 case svn_wc_conflict_action_delete
:
1810 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEDELETE
, (LPCTSTR
)sItemName
);
1811 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1815 case svn_wc_operation_switch
:
1816 switch (pInfoData
->treeconflict_action
)
1818 case svn_wc_conflict_action_edit
:
1819 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHEDIT
, (LPCTSTR
)sItemName
);
1820 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1822 case svn_wc_conflict_action_add
:
1823 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHADD
, (LPCTSTR
)sItemName
);
1824 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1826 case svn_wc_conflict_action_delete
:
1827 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHDELETE
, (LPCTSTR
)sItemName
);
1828 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1832 case svn_wc_operation_merge
:
1833 switch (pInfoData
->treeconflict_action
)
1835 case svn_wc_conflict_action_edit
:
1836 sConflictAction
.Format(IDS_TREECONFLICT_FILEMERGEEDIT
, (LPCTSTR
)sItemName
);
1837 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1839 case svn_wc_conflict_action_add
:
1840 sResolveTheirs
.Format(IDS_TREECONFLICT_FILEMERGEADD
, (LPCTSTR
)sItemName
);
1841 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1843 case svn_wc_conflict_action_delete
:
1844 sConflictAction
.Format(IDS_TREECONFLICT_FILEMERGEDELETE
, (LPCTSTR
)sItemName
);
1845 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1851 else if (pInfoData
->treeconflict_nodekind
== svn_node_dir
)
1853 switch (pInfoData
->treeconflict_operation
)
1855 case svn_wc_operation_update
:
1856 switch (pInfoData
->treeconflict_action
)
1858 case svn_wc_conflict_action_edit
:
1859 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEEDIT
, (LPCTSTR
)sItemName
);
1860 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1862 case svn_wc_conflict_action_add
:
1863 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEADD
, (LPCTSTR
)sItemName
);
1864 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1866 case svn_wc_conflict_action_delete
:
1867 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEDELETE
, (LPCTSTR
)sItemName
);
1868 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1872 case svn_wc_operation_switch
:
1873 switch (pInfoData
->treeconflict_action
)
1875 case svn_wc_conflict_action_edit
:
1876 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHEDIT
, (LPCTSTR
)sItemName
);
1877 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1879 case svn_wc_conflict_action_add
:
1880 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHADD
, (LPCTSTR
)sItemName
);
1881 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1883 case svn_wc_conflict_action_delete
:
1884 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHDELETE
, (LPCTSTR
)sItemName
);
1885 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1889 case svn_wc_operation_merge
:
1890 switch (pInfoData
->treeconflict_action
)
1892 case svn_wc_conflict_action_edit
:
1893 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEEDIT
, (LPCTSTR
)sItemName
);
1894 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1896 case svn_wc_conflict_action_add
:
1897 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEADD
, (LPCTSTR
)sItemName
);
1898 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1900 case svn_wc_conflict_action_delete
:
1901 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEDELETE
, (LPCTSTR
)sItemName
);
1902 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1910 switch (pInfoData
->treeconflict_reason
)
1912 case svn_wc_conflict_reason_edited
:
1913 uReasonID
= IDS_TREECONFLICT_REASON_EDITED
;
1914 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1916 case svn_wc_conflict_reason_obstructed
:
1917 uReasonID
= IDS_TREECONFLICT_REASON_OBSTRUCTED
;
1918 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1920 case svn_wc_conflict_reason_deleted
:
1921 uReasonID
= IDS_TREECONFLICT_REASON_DELETED
;
1922 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_REMOVEDIR
: IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1924 case svn_wc_conflict_reason_added
:
1925 uReasonID
= IDS_TREECONFLICT_REASON_ADDED
;
1926 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1928 case svn_wc_conflict_reason_missing
:
1929 uReasonID
= IDS_TREECONFLICT_REASON_MISSING
;
1930 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_REMOVEDIR
: IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1932 case svn_wc_conflict_reason_unversioned
:
1933 uReasonID
= IDS_TREECONFLICT_REASON_UNVERSIONED
;
1934 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1937 sConflictReason
.Format(uReasonID
, (LPCTSTR
)sConflictAction
);
1939 CTreeConflictEditorDlg dlg
;
1940 dlg
.SetConflictInfoText(sConflictReason
);
1941 dlg
.SetResolveTexts(sResolveTheirs
, sResolveMine
);
1942 dlg
.SetPath(treeConflictPath
);
1943 INT_PTR dlgRet
= dlg
.DoModal();
1944 bRet
= (dlgRet
!= IDCANCEL
);
1952 bool CAppUtils::IsSSHPutty()
1954 CString sshclient
=g_Git
.m_Environment
.GetEnv(_T("GIT_SSH"));
1955 sshclient
=sshclient
.MakeLower();
1956 if(sshclient
.Find(_T("plink.exe"),0)>=0)
1963 CString
CAppUtils::GetClipboardLink(const CString
&skipGitPrefix
, int paramsCount
)
1965 if (!OpenClipboard(NULL
))
1968 CString sClipboardText
;
1969 HGLOBAL hglb
= GetClipboardData(CF_TEXT
);
1972 LPCSTR lpstr
= (LPCSTR
)GlobalLock(hglb
);
1973 sClipboardText
= CString(lpstr
);
1976 hglb
= GetClipboardData(CF_UNICODETEXT
);
1979 LPCTSTR lpstr
= (LPCTSTR
)GlobalLock(hglb
);
1980 sClipboardText
= lpstr
;
1985 if(!sClipboardText
.IsEmpty())
1987 if(sClipboardText
[0] == _T('\"') && sClipboardText
[sClipboardText
.GetLength()-1] == _T('\"'))
1988 sClipboardText
=sClipboardText
.Mid(1,sClipboardText
.GetLength()-2);
1990 if(sClipboardText
.Find( _T("http://")) == 0)
1991 return sClipboardText
;
1993 if(sClipboardText
.Find( _T("https://")) == 0)
1994 return sClipboardText
;
1996 if(sClipboardText
.Find( _T("git://")) == 0)
1997 return sClipboardText
;
1999 if(sClipboardText
.Find( _T("ssh://")) == 0)
2000 return sClipboardText
;
2002 if (sClipboardText
.Find(_T("git@")) == 0)
2003 return sClipboardText
;
2005 if(sClipboardText
.GetLength()>=2)
2006 if( sClipboardText
[1] == _T(':') )
2007 if( (sClipboardText
[0] >= 'A' && sClipboardText
[0] <= 'Z')
2008 || (sClipboardText
[0] >= 'a' && sClipboardText
[0] <= 'z') )
2009 return sClipboardText
;
2011 // trim prefixes like "git clone "
2012 if (!skipGitPrefix
.IsEmpty() && sClipboardText
.Find(skipGitPrefix
) == 0)
2014 sClipboardText
= sClipboardText
.Mid(skipGitPrefix
.GetLength()).Trim();
2016 while (paramsCount
>= 0)
2019 spacePos
= sClipboardText
.Find(_T(' '), spacePos
+ 1);
2023 if (spacePos
> 0 && paramsCount
< 0)
2024 sClipboardText
= sClipboardText
.Left(spacePos
);
2025 return sClipboardText
;
2029 return CString(_T(""));
2032 CString
CAppUtils::ChooseRepository(CString
*path
)
2034 CBrowseFolder browseFolder
;
2035 CRegString regLastResopitory
= CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2037 browseFolder
.m_style
= BIF_EDITBOX
| BIF_NEWDIALOGSTYLE
| BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
;
2038 CString strCloneDirectory
;
2040 strCloneDirectory
=*path
;
2043 strCloneDirectory
= regLastResopitory
;
2047 title
.LoadString(IDS_CHOOSE_REPOSITORY
);
2049 browseFolder
.SetInfo(title
);
2051 if (browseFolder
.Show(NULL
, strCloneDirectory
) == CBrowseFolder::OK
)
2053 regLastResopitory
= strCloneDirectory
;
2054 return strCloneDirectory
;
2062 bool CAppUtils::SendPatchMail(CTGitPathList
& list
, bool bIsMainWnd
)
2066 dlg
.m_PathList
= list
;
2068 if(dlg
.DoModal()==IDOK
)
2070 if (dlg
.m_PathList
.IsEmpty())
2073 CGitProgressDlg progDlg
;
2075 theApp
.m_pMainWnd
= &progDlg
;
2076 SendMailProgressCommand sendMailProgressCommand
;
2077 progDlg
.SetCommand(&sendMailProgressCommand
);
2079 sendMailProgressCommand
.SetPathList(dlg
.m_PathList
);
2080 progDlg
.SetItemCount(dlg
.m_PathList
.GetCount());
2082 CSendMailPatch
sendMailPatch(dlg
.m_To
, dlg
.m_CC
, dlg
.m_Subject
, !!dlg
.m_bAttachment
, !!dlg
.m_bCombine
);
2083 sendMailProgressCommand
.SetSendMailOption(&sendMailPatch
);
2092 bool CAppUtils::SendPatchMail(CString
&cmd
, CString
&formatpatchoutput
, bool bIsMainWnd
)
2095 CString log
=formatpatchoutput
;
2096 int start
=log
.Find(cmd
);
2098 CString one
=log
.Tokenize(_T("\n"),start
);
2104 CString one
=log
.Tokenize(_T("\n"),start
);
2106 if(one
.IsEmpty() || one
.Find(_T("Success")) == 0)
2108 one
.Replace(_T('/'),_T('\\'));
2110 path
.SetFromWin(one
);
2113 if (!list
.IsEmpty())
2115 return SendPatchMail(list
, bIsMainWnd
);
2119 CMessageBox::Show(NULL
, IDS_ERR_NOPATCHES
, IDS_APPNAME
, MB_ICONINFORMATION
);
2125 int CAppUtils::GetLogOutputEncode(CGit
*pGit
)
2128 output
= pGit
->GetConfigValue(_T("i18n.logOutputEncoding"));
2129 if(output
.IsEmpty())
2130 return CUnicodeUtils::GetCPCode(pGit
->GetConfigValue(_T("i18n.commitencoding")));
2133 return CUnicodeUtils::GetCPCode(output
);
2136 int CAppUtils::SaveCommitUnicodeFile(CString
&filename
, CString
&message
)
2140 CFile
file(filename
, CFile::modeReadWrite
| CFile::modeCreate
);
2141 int cp
= CUnicodeUtils::GetCPCode(g_Git
.GetConfigValue(_T("i18n.commitencoding")));
2143 bool stripComments
= (CRegDWORD(_T("Software\\TortoiseGit\\StripCommentedLines"), FALSE
) == TRUE
);
2145 if (CRegDWORD(_T("Software\\TortoiseGit\\SanitizeCommitMsg"), TRUE
) == TRUE
)
2146 message
.TrimRight(L
" \r\n");
2148 int len
= message
.GetLength();
2150 while (start
>= 0 && start
< len
)
2152 int oldStart
= start
;
2153 start
= message
.Find(L
"\n", oldStart
);
2154 CString line
= message
.Mid(oldStart
);
2157 line
= line
.Left(start
- oldStart
);
2158 ++start
; // move forward so we don't find the same char again
2160 if (stripComments
&& (line
.GetLength() >= 1 && line
.GetAt(0) == '#') || (start
< 0 && line
.IsEmpty()))
2162 line
.TrimRight(L
" \r");
2163 CStringA lineA
= CUnicodeUtils::GetMulti(line
+ L
"\n", cp
);
2164 file
.Write(lineA
.GetBuffer(), lineA
.GetLength());
2169 catch (CFileException
*e
)
2176 bool CAppUtils::Pull(bool showPush
)
2179 dlg
.m_IsPull
= TRUE
;
2180 if (dlg
.DoModal() == IDOK
)
2182 CString url
= dlg
.m_RemoteURL
;
2184 if (dlg
.m_bAutoLoad
)
2186 CAppUtils::LaunchPAgent(NULL
, &dlg
.m_RemoteURL
);
2191 if (g_Git
.GetHash(hashOld
, _T("HEAD")))
2193 MessageBox(NULL
, g_Git
.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR
);
2207 cmdRebase
= "--rebase ";
2209 if (!dlg
.m_bFetchTags
)
2210 notags
= _T("--no-tags");
2216 ffonly
= _T("--ff-only");
2219 squash
= _T("--squash");
2221 if (dlg
.m_bNoCommit
)
2222 nocommit
= _T("--no-commit");
2225 depth
.Format(_T("--depth %d "), dlg
.m_nDepth
);
2227 int ver
= CAppUtils::GetMsysgitVersion();
2229 if (dlg
.m_bPrune
== TRUE
)
2230 prune
= _T("--prune ");
2231 else if (dlg
.m_bPrune
== FALSE
&& ver
>= 0x01080500)
2232 prune
= _T("--no-prune ");
2234 if(ver
>= 0x01070203) //above 1.7.0.2
2235 cmdRebase
+= _T("--progress ");
2237 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
);
2238 CProgressDlg progress
;
2239 progress
.m_GitCmd
= cmd
;
2241 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2245 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUPULL
, [&]{ Pull(); }));
2250 if (g_Git
.GetHash(hashNew
, _T("HEAD")))
2251 MessageBox(nullptr, g_Git
.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR
);
2254 postCmdList
.push_back(PostCmd(IDI_DIFF
, IDS_PROC_PULL_DIFFS
, [&]
2257 dlg
.SetDiff(NULL
, hashNew
.ToString(), hashOld
.ToString());
2260 postCmdList
.push_back(PostCmd(IDI_LOG
, IDS_PROC_PULL_LOG
, [&]
2263 dlg
.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld
.ToString() + _T("..") + hashNew
.ToString(), 0);
2269 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, []{ Push(_T("")); }));
2271 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
2272 if (gitPath
.HasSubmodules())
2274 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, []
2277 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
2278 CAppUtils::RunTortoiseGitProc(sCmd
);
2283 INT_PTR ret
= progress
.DoModal();
2285 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
)
2288 dlg
.m_pathList
.AddPath(CTGitPath());
2300 static bool RebaseAfterFetch()
2305 dlg
.m_PostButtonTexts
.Add(CString(MAKEINTRESOURCE(IDS_MENULOG
)));
2306 dlg
.m_PostButtonTexts
.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL
)));
2307 dlg
.m_PostButtonTexts
.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE
)));
2308 INT_PTR response
= dlg
.DoModal();
2309 if (response
== IDOK
)
2313 else if (response
== IDC_REBASE_POST_BUTTON
)
2315 CString cmd
= _T("/command:log");
2316 cmd
+= _T(" /path:\"") + g_Git
.m_CurrentDir
+ _T("\"");
2317 CAppUtils::RunTortoiseGitProc(cmd
);
2320 else if (response
== IDC_REBASE_POST_BUTTON
+ 1)
2322 CString cmd
, out
, err
;
2323 cmd
.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2325 g_Git
.FixBranchName(dlg
.m_Upstream
),
2326 g_Git
.FixBranchName(dlg
.m_Branch
));
2327 if (g_Git
.Run(cmd
, &out
, &err
, CP_UTF8
))
2329 CMessageBox::Show(NULL
, out
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2332 CAppUtils::SendPatchMail(cmd
, out
);
2335 else if (response
== IDC_REBASE_POST_BUTTON
+ 2)
2337 else if (response
== IDCANCEL
)
2343 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
)
2347 if (fetchAllRemotes
)
2350 g_Git
.GetRemoteList(list
);
2352 STRING_VECTOR::const_iterator it
= list
.begin();
2353 while (it
!= list
.end())
2355 CString
remote(*it
);
2356 CAppUtils::LaunchPAgent(NULL
, &remote
);
2361 CAppUtils::LaunchPAgent(NULL
, &url
);
2365 int ver
= CAppUtils::GetMsysgitVersion();
2368 arg
.AppendFormat(_T(" --depth %d"), nDepth
);
2371 arg
+= _T(" --prune");
2372 else if (prune
== FALSE
&& ver
>= 0x01080500)
2373 arg
+= _T(" --no-prune");
2376 arg
+= _T(" --tags");
2377 else if (fetchTags
== 0)
2378 arg
+= _T(" --no-tags");
2380 if (fetchAllRemotes
)
2381 cmd
.Format(_T("git.exe fetch --all -v%s"), arg
);
2383 cmd
.Format(_T("git.exe fetch -v%s \"%s\" %s"), arg
, url
, remoteBranch
);
2385 CProgressDlg progress
;
2386 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2390 postCmdList
.push_back(PostCmd(IDI_REFRESH
, IDS_MSGBOX_RETRY
, [&]{ DoFetch(url
, fetchAllRemotes
, loadPuttyAgent
, prune
, bDepth
, nDepth
, fetchTags
, remoteBranch
, runRebase
); }));
2394 postCmdList
.push_back(PostCmd(IDI_LOG
, IDS_MENULOG
, []
2396 CString cmd
= _T("/command:log");
2397 cmd
+= _T(" /path:\"") + g_Git
.m_CurrentDir
+ _T("\"");
2398 CAppUtils::RunTortoiseGitProc(cmd
);
2401 postCmdList
.push_back(PostCmd(IDI_REVERT
, IDS_PROC_RESET
, []
2403 CString pullRemote
, pullBranch
;
2404 g_Git
.GetRemoteTrackedBranchForHEAD(pullRemote
, pullBranch
);
2405 CString defaultUpstream
;
2406 if (!pullRemote
.IsEmpty() && !pullBranch
.IsEmpty())
2407 defaultUpstream
.Format(_T("remotes/%s/%s"), pullRemote
, pullBranch
);
2408 CAppUtils::GitReset(&defaultUpstream
, 2);
2411 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUFETCH
, []{ CAppUtils::Fetch(); }));
2413 if (!runRebase
&& !g_GitAdminDir
.IsBareRepo(g_Git
.m_CurrentDir
))
2414 postCmdList
.push_back(PostCmd(IDI_REBASE
, IDS_MENUREBASE
, []{ RebaseAfterFetch(); }));
2417 progress
.m_GitCmd
= cmd
;
2418 INT_PTR userResponse
;
2420 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_FETCH
))
2422 CGitProgressDlg gitdlg
;
2423 FetchProgressCommand fetchProgressCommand
;
2424 if (!fetchAllRemotes
)
2425 fetchProgressCommand
.SetUrl(url
);
2426 gitdlg
.SetCommand(&fetchProgressCommand
);
2427 fetchProgressCommand
.m_PostCmdCallback
= progress
.m_PostCmdCallback
;
2428 fetchProgressCommand
.SetAutoTag(fetchTags
== 1 ? GIT_REMOTE_DOWNLOAD_TAGS_ALL
: fetchTags
== 2 ? GIT_REMOTE_DOWNLOAD_TAGS_AUTO
: GIT_REMOTE_DOWNLOAD_TAGS_NONE
);
2429 if (!fetchAllRemotes
)
2430 fetchProgressCommand
.SetRefSpec(remoteBranch
);
2431 userResponse
= gitdlg
.DoModal();
2432 return userResponse
== IDOK
;
2435 userResponse
= progress
.DoModal();
2436 if (!progress
.m_GitStatus
)
2439 return RebaseAfterFetch();
2442 return userResponse
== IDOK
;
2445 bool CAppUtils::Fetch(CString remoteName
, bool allowRebase
, bool allRemotes
)
2448 dlg
.m_PreSelectRemote
= remoteName
;
2449 dlg
.m_bAllowRebase
= allowRebase
;
2451 dlg
.m_bAllRemotes
= allRemotes
;
2453 if(dlg
.DoModal()==IDOK
)
2454 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
);
2459 bool CAppUtils::Push(CString selectLocalBranch
)
2462 dlg
.m_BranchSourceName
= selectLocalBranch
;
2464 if (dlg
.DoModal() == IDOK
)
2467 DWORD exitcode
= 0xFFFFFFFF;
2468 if (CHooks::Instance().PrePush(g_Git
.m_CurrentDir
, exitcode
, error
))
2473 temp
.Format(IDS_ERR_HOOKFAILED
, (LPCTSTR
)error
);
2474 CMessageBox::Show(nullptr, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2482 arg
+= _T("--thin ");
2483 if(dlg
.m_bTags
&& !dlg
.m_bPushAllBranches
)
2484 arg
+= _T("--tags ");
2486 arg
+= _T("--force ");
2487 if (dlg
.m_bForceWithLease
)
2488 arg
+= _T("--force-with-lease ");
2489 if (dlg
.m_bSetUpstream
)
2490 arg
+= _T("--set-upstream ");
2491 if (dlg
.m_RecurseSubmodules
== 1)
2492 arg
+= _T("--recurse-submodules=check ");
2493 if (dlg
.m_RecurseSubmodules
== 2)
2494 arg
+= _T("--recurse-submodules=on-demand ");
2496 int ver
= CAppUtils::GetMsysgitVersion();
2498 if(ver
>= 0x01070203) //above 1.7.0.2
2499 arg
+= _T("--progress ");
2501 CProgressDlg progress
;
2503 STRING_VECTOR remotesList
;
2504 if (dlg
.m_bPushAllRemotes
)
2505 g_Git
.GetRemoteList(remotesList
);
2507 remotesList
.push_back(dlg
.m_URL
);
2509 for (unsigned int i
= 0; i
< remotesList
.size(); ++i
)
2511 if (dlg
.m_bAutoLoad
)
2512 CAppUtils::LaunchPAgent(NULL
, &remotesList
[i
]);
2515 if (dlg
.m_bPushAllBranches
)
2517 cmd
.Format(_T("git.exe push --all %s \"%s\""),
2523 progress
.m_GitCmdList
.push_back(cmd
);
2524 cmd
.Format(_T("git.exe push --tags %s \"%s\""), arg
, remotesList
[i
]);
2529 cmd
.Format(_T("git.exe push %s \"%s\" %s"),
2532 dlg
.m_BranchSourceName
);
2533 if (!dlg
.m_BranchRemoteName
.IsEmpty())
2535 cmd
+= _T(":") + dlg
.m_BranchRemoteName
;
2538 progress
.m_GitCmdList
.push_back(cmd
);
2541 CString superprojectRoot
;
2542 g_GitAdminDir
.HasAdminDir(g_Git
.m_CurrentDir
, false, &superprojectRoot
);
2543 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2545 // need to execute hooks as those might be needed by post action commands
2546 DWORD exitcode
= 0xFFFFFFFF;
2548 if (CHooks::Instance().PostPush(g_Git
.m_CurrentDir
, exitcode
, error
))
2553 temp
.Format(IDS_ERR_HOOKFAILED
, (LPCTSTR
)error
);
2554 MessageBox(nullptr, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2560 bool rejected
= progress
.GetLogText().Find(_T("! [rejected]")) > 0;
2563 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUPULL
, []{ Pull(true); }));
2564 postCmdList
.push_back(PostCmd(IDI_PULL
, IDS_MENUFETCH
, [&]{ Fetch(dlg
.m_bPushAllRemotes
? _T("") : dlg
.m_URL
, true, !!dlg
.m_bPushAllRemotes
); }));
2566 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, [&]{ Push(selectLocalBranch
); }));
2570 postCmdList
.push_back(PostCmd(IDS_PROC_REQUESTPULL
, [&]{ RequestPull(dlg
.m_BranchRemoteName
); }));
2571 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, [&]{ Push(selectLocalBranch
); }));
2572 postCmdList
.push_back(PostCmd(IDI_SWITCH
, IDS_MENUSWITCH
, [&]{ Switch(); }));
2573 if (!superprojectRoot
.IsEmpty())
2575 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_PROC_COMMIT_SUPERPROJECT
, [&]
2578 sCmd
.Format(_T("/command:commit /path:\"%s\""), superprojectRoot
);
2579 RunTortoiseGitProc(sCmd
);
2584 INT_PTR ret
= progress
.DoModal();
2590 bool CAppUtils::RequestPull(CString endrevision
, CString repositoryUrl
, bool bIsMainWnd
)
2592 CRequestPullDlg dlg
;
2593 dlg
.m_RepositoryURL
= repositoryUrl
;
2594 dlg
.m_EndRevision
= endrevision
;
2595 if (dlg
.DoModal()==IDOK
)
2598 cmd
.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg
.m_StartRevision
, dlg
.m_RepositoryURL
, dlg
.m_EndRevision
);
2600 CSysProgressDlg sysProgressDlg
;
2601 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2602 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST
)));
2603 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
2604 sysProgressDlg
.SetShowProgressBar(false);
2605 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
2607 CString tempFileName
= GetTempFile();
2609 DeleteFile(tempFileName
);
2610 CreateDirectory(tempFileName
, NULL
);
2611 tempFileName
+= _T("\\pullrequest.txt");
2612 if (g_Git
.RunLogFile(cmd
, tempFileName
, &err
))
2615 msg
.LoadString(IDS_ERR_PULLREUQESTFAILED
);
2616 CMessageBox::Show(NULL
, msg
+ _T("\n") + err
, _T("TortoiseGit"), MB_OK
);
2620 if (sysProgressDlg
.HasUserCancelled())
2622 CMessageBox::Show(NULL
, IDS_SVN_USERCANCELLED
, IDS_APPNAME
, MB_OK
);
2623 ::DeleteFile(tempFileName
);
2627 sysProgressDlg
.Stop();
2629 if (dlg
.m_bSendMail
)
2632 dlg
.m_PathList
= CTGitPathList(CTGitPath(tempFileName
));
2633 dlg
.m_bCustomSubject
= true;
2635 if (dlg
.DoModal() == IDOK
)
2637 if (dlg
.m_PathList
.IsEmpty())
2640 CGitProgressDlg progDlg
;
2642 theApp
.m_pMainWnd
= &progDlg
;
2643 SendMailProgressCommand sendMailProgressCommand
;
2644 progDlg
.SetCommand(&sendMailProgressCommand
);
2646 sendMailProgressCommand
.SetPathList(dlg
.m_PathList
);
2647 progDlg
.SetItemCount(dlg
.m_PathList
.GetCount());
2649 CSendMailCombineable
sendMailCombineable(dlg
.m_To
, dlg
.m_CC
, dlg
.m_Subject
, !!dlg
.m_bAttachment
, !!dlg
.m_bCombine
);
2650 sendMailProgressCommand
.SetSendMailOption(&sendMailCombineable
);
2659 CAppUtils::LaunchAlternativeEditor(tempFileName
);
2664 bool CAppUtils::CreateMultipleDirectory(const CString
& szPath
)
2666 CString
strDir(szPath
);
2667 if (strDir
.GetAt(strDir
.GetLength()-1)!=_T('\\'))
2669 strDir
.AppendChar(_T('\\'));
2671 std::vector
<CString
> vPath
;
2673 bool bSuccess
= false;
2675 for (int i
=0;i
<strDir
.GetLength();++i
)
2677 if (strDir
.GetAt(i
) != _T('\\'))
2679 strTemp
.AppendChar(strDir
.GetAt(i
));
2683 vPath
.push_back(strTemp
);
2684 strTemp
.AppendChar(_T('\\'));
2688 for (auto vIter
= vPath
.begin(); vIter
!= vPath
.end(); ++vIter
)
2690 bSuccess
= CreateDirectory(*vIter
, NULL
) ? true : false;
2696 void CAppUtils::RemoveTrailSlash(CString
&path
)
2701 // For URL, do not trim the slash just after the host name component.
2702 int index
= path
.Find(_T("://"));
2706 index
= path
.Find(_T('/'), index
);
2707 if (index
== path
.GetLength() - 1)
2711 while(path
[path
.GetLength()-1] == _T('\\') || path
[path
.GetLength()-1] == _T('/' ) )
2713 path
=path
.Left(path
.GetLength()-1);
2719 bool CAppUtils::CheckUserData()
2721 while(g_Git
.GetUserName().IsEmpty() || g_Git
.GetUserEmail().IsEmpty())
2723 if(CMessageBox::Show(NULL
, IDS_PROC_NOUSERDATA
, IDS_APPNAME
, MB_YESNO
| MB_ICONERROR
) == IDYES
)
2725 CTGitPath
path(g_Git
.m_CurrentDir
);
2726 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
,&path
);
2727 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
2728 dlg
.SetTreeWidth(220);
2729 dlg
.m_DefaultPage
= _T("gitconfig");
2732 dlg
.HandleRestart();
2742 BOOL
CAppUtils::Commit(CString bugid
,BOOL bWholeProject
,CString
&sLogMsg
,
2743 CTGitPathList
&pathList
,
2744 CTGitPathList
&selectedList
,
2745 bool bSelectFilesForCommit
)
2747 bool bFailed
= true;
2749 if (!CheckUserData())
2756 dlg
.m_sBugID
= bugid
;
2758 dlg
.m_bWholeProject
= bWholeProject
;
2760 dlg
.m_sLogMessage
= sLogMsg
;
2761 dlg
.m_pathList
= pathList
;
2762 dlg
.m_checkedPathList
= selectedList
;
2763 dlg
.m_bSelectFilesForCommit
= bSelectFilesForCommit
;
2764 if (dlg
.DoModal() == IDOK
)
2766 if (dlg
.m_pathList
.IsEmpty())
2768 // if the user hasn't changed the list of selected items
2769 // we don't use that list. Because if we would use the list
2770 // of pre-checked items, the dialog would show different
2771 // checked items on the next startup: it would only try
2772 // to check the parent folder (which might not even show)
2773 // instead, we simply use an empty list and let the
2774 // default checking do its job.
2775 if (!dlg
.m_pathList
.IsEqual(pathList
))
2776 selectedList
= dlg
.m_pathList
;
2777 pathList
= dlg
.m_updatedPathList
;
2778 sLogMsg
= dlg
.m_sLogMessage
;
2779 bSelectFilesForCommit
= true;
2781 switch (dlg
.m_PostCmd
)
2783 case GIT_POSTCOMMIT_CMD_DCOMMIT
:
2784 CAppUtils::SVNDCommit();
2786 case GIT_POSTCOMMIT_CMD_PUSH
:
2789 case GIT_POSTCOMMIT_CMD_CREATETAG
:
2790 CAppUtils::CreateBranchTag(TRUE
);
2792 case GIT_POSTCOMMIT_CMD_PULL
:
2793 CAppUtils::Pull(true);
2799 // CGitProgressDlg progDlg;
2800 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2801 // if (parser.HasVal(_T("closeonend")))
2802 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2803 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2804 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2805 // progDlg.SetPathList(dlg.m_pathList);
2806 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2807 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2808 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2809 // progDlg.SetItemCount(dlg.m_itemsCount);
2810 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2811 // progDlg.DoModal();
2812 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2813 // err = (DWORD)progDlg.DidErrorsOccur();
2814 // bFailed = progDlg.DidErrorsOccur();
2815 // bRet = progDlg.DidErrorsOccur();
2816 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2817 // if (DWORD(bFailRepeat)==0)
2818 // bFailed = false; // do not repeat if the user chose not to in the settings.
2825 BOOL
CAppUtils::SVNDCommit()
2827 CSVNDCommitDlg dcommitdlg
;
2828 CString gitSetting
= g_Git
.GetConfigValue(_T("svn.rmdir"));
2829 if (gitSetting
== _T("")) {
2830 if (dcommitdlg
.DoModal() != IDOK
)
2836 if (dcommitdlg
.m_remember
)
2838 if (dcommitdlg
.m_rmdir
)
2840 gitSetting
= _T("true");
2844 gitSetting
= _T("false");
2846 if(g_Git
.SetConfigValue(_T("svn.rmdir"),gitSetting
))
2849 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, _T("svn.rmdir"), gitSetting
);
2850 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
2856 BOOL IsStash
= false;
2857 if(!g_Git
.CheckCleanWorkTree())
2859 if (CMessageBox::Show(NULL
, IDS_ERROR_NOCLEAN_STASH
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_STASHBUTTON
, IDS_ABORTBUTTON
) == 1)
2861 CSysProgressDlg sysProgressDlg
;
2862 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2863 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
2864 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
2865 sysProgressDlg
.SetShowProgressBar(false);
2866 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
2867 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
2870 cmd
=_T("git.exe stash");
2871 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
2873 sysProgressDlg
.Stop();
2874 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
2877 sysProgressDlg
.Stop();
2887 CProgressDlg progress
;
2888 if (dcommitdlg
.m_rmdir
)
2890 progress
.m_GitCmd
=_T("git.exe svn dcommit --rmdir");
2894 progress
.m_GitCmd
=_T("git.exe svn dcommit");
2896 if(progress
.DoModal()==IDOK
&& progress
.m_GitStatus
== 0)
2898 ::DeleteFile(g_Git
.m_CurrentDir
+ _T("\\sys$command"));
2901 if(CMessageBox::Show(NULL
,IDS_DCOMMIT_STASH_POP
,IDS_APPNAME
,MB_YESNO
|MB_ICONINFORMATION
)==IDYES
)
2903 CSysProgressDlg sysProgressDlg
;
2904 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2905 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
2906 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
2907 sysProgressDlg
.SetShowProgressBar(false);
2908 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
2909 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
2912 cmd
=_T("git.exe stash pop");
2913 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
2915 sysProgressDlg
.Stop();
2916 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
2919 sysProgressDlg
.Stop();
2931 BOOL
CAppUtils::Merge(CString
*commit
)
2933 if (!CheckUserData())
2938 dlg
.m_initialRefName
= *commit
;
2940 if(dlg
.DoModal()==IDOK
)
2946 args
+= _T(" --no-ff");
2949 args
+= _T(" --squash");
2952 args
+= _T(" --no-commit");
2957 fmt
.Format(_T(" --log=%d"), dlg
.m_nLog
);
2961 if (!dlg
.m_MergeStrategy
.IsEmpty())
2963 args
+= _T(" --strategy=") + dlg
.m_MergeStrategy
;
2964 if (!dlg
.m_StrategyOption
.IsEmpty())
2966 args
+= _T(" --strategy-option=") + dlg
.m_StrategyOption
;
2967 if (!dlg
.m_StrategyParam
.IsEmpty())
2968 args
+= _T("=") + dlg
.m_StrategyParam
;
2972 if(!dlg
.m_strLogMesage
.IsEmpty())
2974 CString logmsg
= dlg
.m_strLogMesage
;
2975 logmsg
.Replace(_T("\""), _T("\\\""));
2976 args
+= _T(" -m \"") + logmsg
+ _T("\"");
2978 cmd
.Format(_T("git.exe merge %s %s"), args
, g_Git
.FixBranchName(dlg
.m_VersionName
));
2980 CProgressDlg Prodlg
;
2981 Prodlg
.m_GitCmd
= cmd
;
2983 Prodlg
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
2988 if (!g_Git
.ListConflictFile(list
) && !list
.IsEmpty())
2990 // there are conflict files
2992 postCmdList
.push_back(PostCmd(IDI_RESOLVE
, IDS_PROGRS_CMD_RESOLVE
, []
2995 sCmd
.Format(_T("/command:commit /path:\"%s\""), g_Git
.m_CurrentDir
);
2996 CAppUtils::RunTortoiseGitProc(sCmd
);
3002 if (dlg
.m_bNoCommit
)
3004 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_MENUCOMMIT
, []
3007 sCmd
.Format(_T("/command:commit /path:\"%s\""), g_Git
.m_CurrentDir
);
3008 CAppUtils::RunTortoiseGitProc(sCmd
);
3013 if (dlg
.m_bIsBranch
&& dlg
.m_VersionName
.Find(L
"remotes/") == -1) // do not ask to remove remote branches
3015 postCmdList
.push_back(PostCmd(IDI_DELETE
, IDS_PROC_REMOVEBRANCH
, [&]
3018 msg
.Format(IDS_PROC_DELETEBRANCHTAG
, dlg
.m_VersionName
);
3019 if (CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), 2, IDI_QUESTION
, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON
)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON
))) == 1)
3022 cmd
.Format(_T("git.exe branch -D -- %s"), dlg
.m_VersionName
);
3023 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
3024 MessageBox(nullptr, out
, _T("TortoiseGit"), MB_OK
);
3028 if (dlg
.m_bIsBranch
)
3029 postCmdList
.push_back(PostCmd(IDI_PUSH
, IDS_MENUPUSH
, []{ Push(); }));
3031 BOOL hasGitSVN
= CTGitPath(g_Git
.m_CurrentDir
).GetAdminDirMask() & ITEMIS_GITSVN
;
3033 postCmdList
.push_back(PostCmd(IDI_COMMIT
, IDS_MENUSVNDCOMMIT
, []{ SVNDCommit(); }));
3037 return !Prodlg
.m_GitStatus
;
3042 BOOL
CAppUtils::MergeAbort()
3045 if (dlg
.DoModal() == IDOK
)
3046 return Reset(_T("HEAD"), dlg
.m_ResetType
+ 1);
3051 void CAppUtils::EditNote(GitRev
*rev
)
3053 if (!CheckUserData())
3057 dlg
.m_sHintText
= CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES
));
3058 dlg
.m_sInputText
= rev
->m_Notes
;
3059 dlg
.m_sTitle
= CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES
));
3060 //dlg.m_pProjectProperties = &m_ProjectProperties;
3061 dlg
.m_bUseLogWidth
= true;
3062 if(dlg
.DoModal() == IDOK
)
3065 cmd
=_T("notes add -f -F \"");
3067 CString tempfile
=::GetTempFile();
3068 if (CAppUtils::SaveCommitUnicodeFile(tempfile
, dlg
.m_sInputText
))
3070 CMessageBox::Show(nullptr, IDS_PROC_FAILEDSAVINGNOTES
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
3075 cmd
+= rev
->m_CommitHash
.ToString();
3079 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd
, CP_UTF8
).GetBuffer()))
3081 CMessageBox::Show(NULL
, IDS_PROC_FAILEDSAVINGNOTES
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
3086 rev
->m_Notes
= dlg
.m_sInputText
;
3090 CMessageBox::Show(NULL
, IDS_PROC_FAILEDSAVINGNOTES
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
3092 ::DeleteFile(tempfile
);
3097 int CAppUtils::GetMsysgitVersion()
3099 if (g_Git
.ms_LastMsysGitVersion
)
3100 return g_Git
.ms_LastMsysGitVersion
;
3103 CString versiondebug
;
3106 CRegDWORD regTime
= CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3107 CRegDWORD regVersion
= CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3109 CString gitpath
= CGit::ms_LastMsysGitDir
+_T("\\git.exe");
3112 if (!g_Git
.GetFileModifyTime(gitpath
, &time
))
3114 if((DWORD
)time
== regTime
)
3116 g_Git
.ms_LastMsysGitVersion
= regVersion
;
3122 cmd
= _T("git.exe --version");
3123 if (g_Git
.Run(cmd
, &version
, &err
, CP_UTF8
))
3125 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
);
3132 versiondebug
= version
;
3136 CString str
=version
.Tokenize(_T("."), start
);
3137 int space
= str
.ReverseFind(_T(' '));
3138 str
= str
.Mid(space
+1,start
);
3142 version
= version
.Mid(start
);
3145 str
= version
.Tokenize(_T("."), start
);
3147 ver
|= (_ttol(str
) & 0xFF) << 16;
3149 str
= version
.Tokenize(_T("."), start
);
3150 ver
|= (_ttol(str
) & 0xFF) << 8;
3152 str
= version
.Tokenize(_T("."), start
);
3153 ver
|= (_ttol(str
) & 0xFF);
3159 CMessageBox::Show(NULL
, _T("Could not parse git.exe version number: \"") + versiondebug
+ _T("\""), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
3164 regTime
= time
&0xFFFFFFFF;
3166 g_Git
.ms_LastMsysGitVersion
= ver
;
3171 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd
)
3173 typedef HRESULT (WINAPI
*SHGPSFW
) (HWND hwnd
,REFIID riid
,void** ppv
);
3175 CAutoLibrary hShell
= AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3177 if (hShell
.IsValid()) {
3178 SHGPSFW pfnSHGPSFW
= (SHGPSFW
)::GetProcAddress(hShell
, "SHGetPropertyStoreForWindow");
3180 IPropertyStore
*pps
;
3181 HRESULT hr
= pfnSHGPSFW(hWnd
, IID_PPV_ARGS(&pps
));
3182 if (SUCCEEDED(hr
)) {
3185 var
.boolVal
= VARIANT_TRUE
;
3186 hr
= pps
->SetValue(PKEY_AppUserModel_PreventPinning
, var
);
3193 void CAppUtils::SetWindowTitle(HWND hWnd
, const CString
& urlorpath
, const CString
& dialogname
)
3195 ASSERT(dialogname
.GetLength() < 70);
3196 ASSERT(urlorpath
.GetLength() < MAX_PATH
);
3197 WCHAR pathbuf
[MAX_PATH
] = {0};
3199 PathCompactPathEx(pathbuf
, urlorpath
, 70 - dialogname
.GetLength(), 0);
3201 wcscat_s(pathbuf
, L
" - ");
3202 wcscat_s(pathbuf
, dialogname
);
3203 wcscat_s(pathbuf
, L
" - ");
3204 wcscat_s(pathbuf
, CString(MAKEINTRESOURCE(IDS_APPNAME
)));
3205 SetWindowText(hWnd
, pathbuf
);
3208 bool CAppUtils::BisectStart(CString lastGood
, CString firstBad
, bool bIsMainWnd
)
3210 if (!g_Git
.CheckCleanWorkTree())
3212 if (CMessageBox::Show(NULL
, IDS_ERROR_NOCLEAN_STASH
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_STASHBUTTON
, IDS_ABORTBUTTON
) == 1)
3214 CSysProgressDlg sysProgressDlg
;
3215 sysProgressDlg
.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME
)));
3216 sysProgressDlg
.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING
)));
3217 sysProgressDlg
.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT
)));
3218 sysProgressDlg
.SetShowProgressBar(false);
3219 sysProgressDlg
.SetCancelMsg(IDS_PROGRS_INFOFAILED
);
3220 sysProgressDlg
.ShowModeless((HWND
)NULL
, true);
3223 cmd
= _T("git.exe stash");
3224 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
3226 sysProgressDlg
.Stop();
3227 CMessageBox::Show(NULL
, out
, _T("TortoiseGit"), MB_OK
);
3230 sysProgressDlg
.Stop();
3236 CBisectStartDlg bisectStartDlg
;
3238 if (!lastGood
.IsEmpty())
3239 bisectStartDlg
.m_sLastGood
= lastGood
;
3240 if (!firstBad
.IsEmpty())
3241 bisectStartDlg
.m_sFirstBad
= firstBad
;
3243 if (bisectStartDlg
.DoModal() == IDOK
)
3245 CProgressDlg progress
;
3247 theApp
.m_pMainWnd
= &progress
;
3248 progress
.m_GitCmdList
.push_back(_T("git.exe bisect start"));
3249 progress
.m_GitCmdList
.push_back(_T("git.exe bisect good ") + bisectStartDlg
.m_LastGoodRevision
);
3250 progress
.m_GitCmdList
.push_back(_T("git.exe bisect bad ") + bisectStartDlg
.m_FirstBadRevision
);
3252 progress
.m_PostCmdCallback
= [&](DWORD status
, PostCmdList
& postCmdList
)
3257 CTGitPath
path(g_Git
.m_CurrentDir
);
3258 if (path
.HasSubmodules())
3260 postCmdList
.push_back(PostCmd(IDI_UPDATE
, IDS_PROC_SUBMODULESUPDATE
, []
3263 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
3264 CAppUtils::RunTortoiseGitProc(sCmd
);
3271 INT_PTR ret
= progress
.DoModal();
3278 int CAppUtils::Git2GetUserPassword(git_cred
**out
, const char *url
, const char *username_from_url
, unsigned int /*allowed_types*/, void * /*payload*/)
3281 dlg
.m_URL
= CUnicodeUtils::GetUnicode(url
, CP_UTF8
);
3282 if (username_from_url
)
3283 dlg
.m_UserName
= CUnicodeUtils::GetUnicode(username_from_url
, CP_UTF8
);
3285 CStringA username
, password
;
3286 if (dlg
.DoModal() == IDOK
)
3288 username
= CUnicodeUtils::GetMulti(dlg
.m_UserName
, CP_UTF8
);
3289 password
= CUnicodeUtils::GetMulti(dlg
.m_Password
, CP_UTF8
);
3290 return git_cred_userpass_plaintext_new(out
, username
, password
);
3295 void CAppUtils::ExploreTo(HWND hwnd
, CString path
)
3297 if (PathFileExists(path
))
3299 ITEMIDLIST __unaligned
* pidl
= ILCreateFromPath(path
);
3302 SHOpenFolderAndSelectItems(pidl
, 0, 0, 0);
3307 // if filepath does not exist any more, navigate to closest matching folder
3310 int pos
= path
.ReverseFind(_T('\\'));
3313 path
= path
.Left(pos
);
3314 } while (!PathFileExists(path
));
3315 ShellExecute(hwnd
, _T("explore"), path
, nullptr, nullptr, SW_SHOW
);