Fixed issue #2222: 'Prune' in the pull dialog does not work
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob6c45fb8be7092281ab6922c52955ea3e4c033599
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
4 // Copyright (C) 2003-2011, 2013 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseProc.h"
22 #include "PathUtils.h"
23 #include "AppUtils.h"
24 #include "StringUtils.h"
25 #include "MessageBox.h"
26 #include "registry.h"
27 #include "TGitPath.h"
28 #include "Git.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"
40 #include "ResetDlg.h"
41 #include "DeleteConflictDlg.h"
42 #include "ChangedDlg.h"
43 #include "SendMailDlg.h"
44 #include "GitProgressDlg.h"
45 #include "PushDlg.h"
46 #include "CommitDlg.h"
47 #include "MergeDlg.h"
48 #include "MergeAbortDlg.h"
49 #include "Hooks.h"
50 #include "..\Settings\Settings.h"
51 #include "InputDlg.h"
52 #include "SVNDCommitDlg.h"
53 #include "requestpulldlg.h"
54 #include "PullFetchDlg.h"
55 #include "FileDiffDlg.h"
56 #include "RebaseDlg.h"
57 #include "PropKey.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"
65 #include "Patch.h"
66 #include "Globals.h"
68 CAppUtils::CAppUtils(void)
72 CAppUtils::~CAppUtils(void)
76 bool CAppUtils::StashSave()
78 CStashSaveDlg dlg;
80 if (dlg.DoModal() == IDOK)
82 CString cmd;
83 cmd = _T("git.exe stash save");
85 if (CAppUtils::GetMsysgitVersion() >= 0x01070700)
87 if (dlg.m_bIncludeUntracked)
88 cmd += _T(" --include-untracked");
89 else if (dlg.m_bAll)
90 cmd += _T(" --all");
93 if (!dlg.m_sMessage.IsEmpty())
95 CString message = dlg.m_sMessage;
96 message.Replace(_T("\""), _T("\"\""));
97 cmd += _T(" -- \"") + message + _T("\"");
100 CProgressDlg progress;
101 progress.m_GitCmd = cmd;
102 return (progress.DoModal() == IDOK);
104 return false;
107 bool CAppUtils::StashApply(CString ref, bool showChanges /* true */)
109 CString cmd,out;
110 cmd = _T("git.exe stash apply ");
111 if (ref.Find(_T("refs/")) == 0)
112 ref = ref.Mid(5);
113 if (ref.Find(_T("stash{")) == 0)
114 ref = _T("stash@") + ref.Mid(5);
115 cmd += ref;
117 CSysProgressDlg sysProgressDlg;
118 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
119 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
120 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
121 sysProgressDlg.SetShowProgressBar(false);
122 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
123 sysProgressDlg.ShowModeless((HWND)NULL, true);
125 int ret = g_Git.Run(cmd, &out, CP_UTF8);
127 sysProgressDlg.Stop();
129 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
130 if (ret && !(ret == 1 && hasConflicts))
132 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHAPPLYFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
134 else
136 CString message;
137 message.LoadString(IDS_PROC_STASHAPPLYSUCCESS);
138 if (hasConflicts)
139 message.LoadString(IDS_PROC_STASHAPPLYFAILEDCONFLICTS);
140 if (showChanges)
142 if(CMessageBox::Show(NULL,message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES))
143 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
145 CChangedDlg dlg;
146 dlg.m_pathList.AddPath(CTGitPath());
147 dlg.DoModal();
149 return true;
151 else
153 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
154 return true;
157 return false;
160 bool CAppUtils::StashPop(bool showChanges /* true */)
162 CString cmd,out;
163 cmd=_T("git.exe stash pop ");
165 CSysProgressDlg sysProgressDlg;
166 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
167 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
168 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
169 sysProgressDlg.SetShowProgressBar(false);
170 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
171 sysProgressDlg.ShowModeless((HWND)NULL, true);
173 int ret = g_Git.Run(cmd, &out, CP_UTF8);
175 sysProgressDlg.Stop();
177 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
178 if (ret && !(ret == 1 && hasConflicts))
180 CMessageBox::Show(NULL,CString(MAKEINTRESOURCE(IDS_PROC_STASHPOPFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
182 else
184 CString message;
185 message.LoadString(IDS_PROC_STASHPOPSUCCESS);
186 if (hasConflicts)
187 message.LoadString(IDS_PROC_STASHPOPFAILEDCONFLICTS);
188 if (showChanges)
190 if(CMessageBox::Show(NULL,CString(message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES)))
191 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
193 CChangedDlg dlg;
194 dlg.m_pathList.AddPath(CTGitPath());
195 dlg.DoModal();
197 return true;
199 else
201 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
202 return true;
205 return false;
208 BOOL CAppUtils::StartExtMerge(
209 const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,
210 const CString& basename, const CString& theirname, const CString& yourname, const CString& mergedname, bool bReadOnly,
211 HWND resolveMsgHwnd)
214 CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));
215 CString ext = mergedfile.GetFileExtension();
216 CString com = regCom;
217 bool bInternal = false;
219 if (!ext.IsEmpty())
221 // is there an extension specific merge tool?
222 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());
223 if (!CString(mergetool).IsEmpty())
225 com = mergetool;
228 // is there a filename specific merge tool?
229 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\.") + mergedfile.GetFilename().MakeLower());
230 if (!CString(mergetool).IsEmpty())
232 com = mergetool;
235 if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))
237 // Maybe we should use TortoiseIDiff?
238 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
239 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
240 (ext == _T(".png")) || (ext == _T(".ico")) ||
241 (ext == _T(".tif")) || (ext == _T(".tiff")) ||
242 (ext == _T(".dib")) || (ext == _T(".emf")))
244 com = CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe");
245 com = _T("\"") + com + _T("\"");
246 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /result:%merged");
247 com = com + _T(" /basetitle:%bname /theirstitle:%tname /minetitle:%yname");
249 else
251 // use TortoiseGitMerge
252 bInternal = true;
253 com = CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe");
254 com = _T("\"") + com + _T("\"");
255 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
256 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
257 com += _T(" /saverequired");
258 if (resolveMsgHwnd)
260 CString s;
261 s.Format(L" /resolvemsghwnd:%I64d", (__int64)resolveMsgHwnd);
262 com += s;
265 if (!g_sGroupingUUID.IsEmpty())
267 com += L" /groupuuid:\"";
268 com += g_sGroupingUUID;
269 com += L"\"";
272 // check if the params are set. If not, just add the files to the command line
273 if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))
275 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");
276 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");
277 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");
278 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");
280 if (basefile.IsEmpty())
282 com.Replace(_T("/base:%base"), _T(""));
283 com.Replace(_T("%base"), _T(""));
285 else
286 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));
287 if (theirfile.IsEmpty())
289 com.Replace(_T("/theirs:%theirs"), _T(""));
290 com.Replace(_T("%theirs"), _T(""));
292 else
293 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));
294 if (yourfile.IsEmpty())
296 com.Replace(_T("/mine:%mine"), _T(""));
297 com.Replace(_T("%mine"), _T(""));
299 else
300 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));
301 if (mergedfile.IsEmpty())
303 com.Replace(_T("/merged:%merged"), _T(""));
304 com.Replace(_T("%merged"), _T(""));
306 else
307 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));
308 if (basename.IsEmpty())
310 if (basefile.IsEmpty())
312 com.Replace(_T("/basename:%bname"), _T(""));
313 com.Replace(_T("%bname"), _T(""));
315 else
317 com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));
320 else
321 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));
322 if (theirname.IsEmpty())
324 if (theirfile.IsEmpty())
326 com.Replace(_T("/theirsname:%tname"), _T(""));
327 com.Replace(_T("%tname"), _T(""));
329 else
331 com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));
334 else
335 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));
336 if (yourname.IsEmpty())
338 if (yourfile.IsEmpty())
340 com.Replace(_T("/minename:%yname"), _T(""));
341 com.Replace(_T("%yname"), _T(""));
343 else
345 com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));
348 else
349 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));
350 if (mergedname.IsEmpty())
352 if (mergedfile.IsEmpty())
354 com.Replace(_T("/mergedname:%mname"), _T(""));
355 com.Replace(_T("%mname"), _T(""));
357 else
359 com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));
362 else
363 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));
365 if ((bReadOnly)&&(bInternal))
366 com += _T(" /readonly");
368 if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))
370 return FALSE;
373 return TRUE;
376 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)
378 CString viewer;
379 // use TortoiseGitMerge
380 viewer = CPathUtils::GetAppDirectory();
381 viewer += _T("TortoiseGitMerge.exe");
383 viewer = _T("\"") + viewer + _T("\"");
384 viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");
385 viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");
386 if (bReversed)
387 viewer += _T(" /reversedpatch");
388 if (!sOriginalDescription.IsEmpty())
389 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");
390 if (!sPatchedDescription.IsEmpty())
391 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");
392 if (!g_sGroupingUUID.IsEmpty())
394 viewer += L" /groupuuid:\"";
395 viewer += g_sGroupingUUID;
396 viewer += L"\"";
398 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
400 return FALSE;
402 return TRUE;
405 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)
407 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file2.GetFilename().MakeLower());
408 if (!difftool.IsEmpty())
409 return difftool;
410 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file1.GetFilename().MakeLower());
411 if (!difftool.IsEmpty())
412 return difftool;
414 // Is there an extension specific diff tool?
415 CString ext = file2.GetFileExtension().MakeLower();
416 if (!ext.IsEmpty())
418 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);
419 if (!difftool.IsEmpty())
420 return difftool;
421 // Maybe we should use TortoiseIDiff?
422 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
423 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
424 (ext == _T(".png")) || (ext == _T(".ico")) ||
425 (ext == _T(".dib")) || (ext == _T(".emf")))
427 return
428 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe") + _T("\"") +
429 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname") +
430 L" /groupuuid:\"" + g_sGroupingUUID + L"\"";
434 // Finally, pick a generic external diff tool
435 difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));
436 return difftool;
439 bool CAppUtils::StartExtDiff(
440 const CString& file1, const CString& file2,
441 const CString& sName1, const CString& sName2,
442 const CString& originalFile1, const CString& originalFile2,
443 const git_revnum_t& hash1, const git_revnum_t& hash2,
444 const DiffFlags& flags, int jumpToLine)
446 CString viewer;
448 CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);
449 if (!flags.bBlame || !(DWORD)blamediff)
451 viewer = PickDiffTool(file1, file2);
452 // If registry entry for a diff program is commented out, use TortoiseGitMerge.
453 bool bCommentedOut = viewer.Left(1) == _T("#");
454 if (flags.bAlternativeTool)
456 // Invert external vs. internal diff tool selection.
457 if (bCommentedOut)
458 viewer.Delete(0); // uncomment
459 else
460 viewer = "";
462 else if (bCommentedOut)
463 viewer = "";
466 bool bInternal = viewer.IsEmpty();
467 if (bInternal)
469 viewer =
470 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe") + _T("\"") +
471 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
472 if (!g_sGroupingUUID.IsEmpty())
474 viewer += L" /groupuuid:\"";
475 viewer += g_sGroupingUUID;
476 viewer += L"\"";
478 if (flags.bBlame)
479 viewer += _T(" /blame");
481 // check if the params are set. If not, just add the files to the command line
482 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
484 viewer += _T(" \"")+file1+_T("\"");
485 viewer += _T(" \"")+file2+_T("\"");
487 if (viewer.Find(_T("%base")) >= 0)
489 viewer.Replace(_T("%base"), _T("\"")+file1+_T("\""));
491 if (viewer.Find(_T("%mine")) >= 0)
493 viewer.Replace(_T("%mine"), _T("\"")+file2+_T("\""));
496 if (sName1.IsEmpty())
497 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));
498 else
499 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
501 if (sName2.IsEmpty())
502 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));
503 else
504 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
506 viewer.Replace(_T("%bpath"), _T("\"") + originalFile1 + _T("\""));
507 viewer.Replace(_T("%ypath"), _T("\"") + originalFile2 + _T("\""));
509 viewer.Replace(_T("%brev"), _T("\"") + hash1 + _T("\""));
510 viewer.Replace(_T("%yrev"), _T("\"") + hash2 + _T("\""));
512 if (flags.bReadOnly && bInternal)
513 viewer += _T(" /readonly");
515 if (jumpToLine > 0)
517 CString temp;
518 temp.Format(_T(" /line:%d"), jumpToLine);
519 viewer += temp;
522 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);
525 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)
527 CString viewer;
528 CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
529 viewer = v;
530 if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))
532 // use TortoiseGitUDiff
533 viewer = CPathUtils::GetAppDirectory();
534 viewer += _T("TortoiseGitUDiff.exe");
535 // enquote the path to TortoiseGitUDiff
536 viewer = _T("\"") + viewer + _T("\"");
537 // add the params
538 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");
539 if (!g_sGroupingUUID.IsEmpty())
541 viewer += L" /groupuuid:\"";
542 viewer += g_sGroupingUUID;
543 viewer += L"\"";
546 if (viewer.Find(_T("%1"))>=0)
548 if (viewer.Find(_T("\"%1\"")) >= 0)
549 viewer.Replace(_T("%1"), patchfile);
550 else
551 viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));
553 else
554 viewer += _T(" \"") + patchfile + _T("\"");
555 if (viewer.Find(_T("%title")) >= 0)
557 viewer.Replace(_T("%title"), title);
560 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
562 return FALSE;
564 return TRUE;
567 BOOL CAppUtils::StartTextViewer(CString file)
569 CString viewer;
570 CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);
571 viewer = txt;
572 viewer = viewer + _T("\\Shell\\Open\\Command\\");
573 CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);
574 viewer = txtexe;
576 DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
577 std::unique_ptr<TCHAR[]> buf(new TCHAR[len + 1]);
578 ExpandEnvironmentStrings(viewer, buf.get(), len);
579 viewer = buf.get();
580 len = ExpandEnvironmentStrings(file, NULL, 0);
581 std::unique_ptr<TCHAR[]> buf2(new TCHAR[len + 1]);
582 ExpandEnvironmentStrings(file, buf2.get(), len);
583 file = buf2.get();
584 file = _T("\"")+file+_T("\"");
585 if (viewer.IsEmpty())
587 viewer = _T("RUNDLL32 Shell32,OpenAs_RunDLL");
589 if (viewer.Find(_T("\"%1\"")) >= 0)
591 viewer.Replace(_T("\"%1\""), file);
593 else if (viewer.Find(_T("%1")) >= 0)
595 viewer.Replace(_T("%1"), file);
597 else
599 viewer += _T(" ");
600 viewer += file;
603 if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))
605 return FALSE;
607 return TRUE;
610 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)
612 DWORD length = 0;
613 CAutoFile hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
614 if (!hFile)
615 return TRUE;
616 length = ::GetFileSize(hFile, NULL);
617 if (length < 4)
618 return TRUE;
619 return FALSE;
623 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)
625 LOGFONT logFont;
626 HDC hScreenDC = ::GetDC(NULL);
627 logFont.lfHeight = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);
628 ::ReleaseDC(NULL, hScreenDC);
629 logFont.lfWidth = 0;
630 logFont.lfEscapement = 0;
631 logFont.lfOrientation = 0;
632 logFont.lfWeight = FW_NORMAL;
633 logFont.lfItalic = 0;
634 logFont.lfUnderline = 0;
635 logFont.lfStrikeOut = 0;
636 logFont.lfCharSet = DEFAULT_CHARSET;
637 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
638 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
639 logFont.lfQuality = DRAFT_QUALITY;
640 logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;
641 _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
642 VERIFY(fontToCreate.CreateFontIndirect(&logFont));
645 bool CAppUtils::LaunchPAgent(CString *keyfile,CString * pRemote)
647 CString key,remote;
648 CString cmd,out;
649 if( pRemote == NULL)
651 remote=_T("origin");
653 else
655 remote=*pRemote;
657 if(keyfile == NULL)
659 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
660 key = g_Git.GetConfigValue(cmd);
662 else
663 key=*keyfile;
665 if(key.IsEmpty())
666 return false;
668 CString proc=CPathUtils::GetAppDirectory();
669 proc += _T("pageant.exe \"");
670 proc += key;
671 proc += _T("\"");
673 CString tempfile = GetTempFile();
674 ::DeleteFile(tempfile);
676 proc += _T(" -c \"");
677 proc += CPathUtils::GetAppDirectory();
678 proc += _T("tgittouch.exe\"");
679 proc += _T(" \"");
680 proc += tempfile;
681 proc += _T("\"");
683 CString appDir = CPathUtils::GetAppDirectory();
684 bool b = LaunchApplication(proc, IDS_ERR_PAGEANT, true, &appDir);
685 if(!b)
686 return b;
688 int i=0;
689 while(!::PathFileExists(tempfile))
691 Sleep(100);
692 ++i;
693 if(i>10*60*5)
694 break; //timeout 5 minutes
697 if( i== 10*60*5)
699 CMessageBox::Show(NULL, IDS_ERR_PAEGENTTIMEOUT, IDS_APPNAME, MB_OK | MB_ICONERROR);
701 ::DeleteFile(tempfile);
702 return true;
704 bool CAppUtils::LaunchAlternativeEditor(const CString& filename, bool uac)
706 CString editTool = CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
707 if (editTool.IsEmpty() || (editTool.Left(1).Compare(_T("#"))==0)) {
708 editTool = CPathUtils::GetAppDirectory() + _T("notepad2.exe");
711 CString sCmd;
712 sCmd.Format(_T("\"%s\" \"%s\""), editTool, filename);
714 LaunchApplication(sCmd, NULL, false, NULL, uac);
715 return true;
717 bool CAppUtils::LaunchRemoteSetting()
719 CTGitPath path(g_Git.m_CurrentDir);
720 CSettings dlg(IDS_PROC_SETTINGS_TITLE, &path);
721 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
722 dlg.SetTreeWidth(220);
723 dlg.m_DefaultPage = _T("gitremote");
725 dlg.DoModal();
726 dlg.HandleRestart();
727 return true;
730 * Launch the external blame viewer
732 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)
734 CString viewer = _T("\"") + CPathUtils::GetAppDirectory();
735 viewer += _T("TortoiseGitBlame.exe");
736 viewer += _T("\" \"") + sBlameFile + _T("\"");
737 //viewer += _T(" \"") + sLogFile + _T("\"");
738 //viewer += _T(" \"") + sOriginalFile + _T("\"");
739 if(!Rev.IsEmpty() && Rev != GIT_REV_ZERO)
740 viewer += CString(_T(" /rev:"))+Rev;
741 if (!g_sGroupingUUID.IsEmpty())
743 viewer += L" /groupuuid:\"";
744 viewer += g_sGroupingUUID;
745 viewer += L"\"";
747 viewer += _T(" ")+sParams;
749 return LaunchApplication(viewer, IDS_ERR_TGITBLAME, false);
752 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)
754 CString sText;
755 if (pWnd == NULL)
756 return false;
757 bool bStyled = false;
758 pWnd->GetWindowText(sText);
759 // the rich edit control doesn't count the CR char!
760 // to be exact: CRLF is treated as one char.
761 sText.Remove(_T('\r'));
763 // style each line separately
764 int offset = 0;
765 int nNewlinePos;
768 nNewlinePos = sText.Find('\n', offset);
769 CString sLine = nNewlinePos >= 0 ? sText.Mid(offset, nNewlinePos - offset) : sText.Mid(offset);
771 int start = 0;
772 int end = 0;
773 while (FindStyleChars(sLine, '*', start, end))
775 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
776 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
777 SetCharFormat(pWnd, CFM_BOLD, CFE_BOLD);
778 bStyled = true;
779 start = end;
781 start = 0;
782 end = 0;
783 while (FindStyleChars(sLine, '^', start, end))
785 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
786 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
787 SetCharFormat(pWnd, CFM_ITALIC, CFE_ITALIC);
788 bStyled = true;
789 start = end;
791 start = 0;
792 end = 0;
793 while (FindStyleChars(sLine, '_', start, end))
795 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
796 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
797 SetCharFormat(pWnd, CFM_UNDERLINE, CFE_UNDERLINE);
798 bStyled = true;
799 start = end;
801 offset = nNewlinePos+1;
802 } while(nNewlinePos>=0);
803 return bStyled;
806 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)
808 int i=start;
809 int last = sText.GetLength() - 1;
810 bool bFoundMarker = false;
811 TCHAR c = i == 0 ? _T('\0') : sText[i - 1];
812 TCHAR nextChar = i >= last ? _T('\0') : sText[i + 1];
814 // find a starting marker
815 while (i < last)
817 TCHAR prevChar = c;
818 c = nextChar;
819 nextChar = sText[i + 1];
821 // IsCharAlphaNumeric can be somewhat expensive.
822 // Long lines of "*****" or "----" will be pre-empted efficiently
823 // by the (c != nextChar) condition.
825 if ((c == stylechar) && (c != nextChar))
827 if (IsCharAlphaNumeric(nextChar) && !IsCharAlphaNumeric(prevChar))
829 start = ++i;
830 bFoundMarker = true;
831 break;
834 ++i;
836 if (!bFoundMarker)
837 return false;
839 // find ending marker
840 // c == sText[i - 1]
842 bFoundMarker = false;
843 while (i <= last)
845 TCHAR prevChar = c;
846 c = sText[i];
847 if (c == stylechar)
849 if ((i == last) || (!IsCharAlphaNumeric(sText[i + 1]) && IsCharAlphaNumeric(prevChar)))
851 end = i;
852 ++i;
853 bFoundMarker = true;
854 break;
857 ++i;
859 return bFoundMarker;
862 bool CAppUtils::StartShowUnifiedDiff(HWND hWnd, const CTGitPath& url1, const git_revnum_t& rev1,
863 const CTGitPath& /*url2*/, const git_revnum_t& rev2,
864 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
865 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */,
866 bool /* blame = false */,
867 bool bMerge,
868 bool bCombine)
870 int diffContext = 0;
871 if (GetMsysgitVersion() > 0x01080100)
872 diffContext = _ttoi(g_Git.GetConfigValue(_T("diff.context")));
873 CString tempfile=GetTempFile();
874 if (g_Git.GetUnifiedDiff(url1, rev1, rev2, tempfile, bMerge, bCombine, diffContext))
876 CMessageBox::Show(hWnd, g_Git.GetGitLastErr(_T("Could not get unified diff."), CGit::GIT_CMD_DIFF), _T("TortoiseGit"), MB_OK);
877 return false;
879 CAppUtils::StartUnifiedDiffViewer(tempfile, rev1 + _T(":") + rev2);
881 #if 0
882 CString sCmd;
883 sCmd.Format(_T("%s /command:showcompare /unified"),
884 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseGitProc.exe")));
885 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
886 if (rev1.IsValid())
887 sCmd += _T(" /revision1:") + rev1.ToString();
888 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
889 if (rev2.IsValid())
890 sCmd += _T(" /revision2:") + rev2.ToString();
891 if (peg.IsValid())
892 sCmd += _T(" /pegrevision:") + peg.ToString();
893 if (headpeg.IsValid())
894 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
896 if (bAlternateDiff)
897 sCmd += _T(" /alternatediff");
899 if (bIgnoreAncestry)
900 sCmd += _T(" /ignoreancestry");
902 if (hWnd)
904 sCmd += _T(" /hwnd:");
905 TCHAR buf[30];
906 _stprintf_s(buf, 30, _T("%p"), (void*)hWnd);
907 sCmd += buf;
910 return CAppUtils::LaunchApplication(sCmd, NULL, false);
911 #endif
912 return TRUE;
915 bool CAppUtils::SetupDiffScripts(bool force, const CString& type)
917 CString scriptsdir = CPathUtils::GetAppParentDirectory();
918 scriptsdir += _T("Diff-Scripts");
919 CSimpleFileFind files(scriptsdir);
920 while (files.FindNextFileNoDirectories())
922 CString file = files.GetFilePath();
923 CString filename = files.GetFileName();
924 CString ext = file.Mid(file.ReverseFind('-') + 1);
925 ext = _T(".") + ext.Left(ext.ReverseFind('.'));
926 std::set<CString> extensions;
927 extensions.insert(ext);
928 CString kind;
929 if (file.Right(3).CompareNoCase(_T("vbs"))==0)
931 kind = _T(" //E:vbscript");
933 if (file.Right(2).CompareNoCase(_T("js"))==0)
935 kind = _T(" //E:javascript");
937 // open the file, read the first line and find possible extensions
938 // this script can handle
941 CStdioFile f(file, CFile::modeRead | CFile::shareDenyNone);
942 CString extline;
943 if (f.ReadString(extline))
945 if ((extline.GetLength() > 15 ) &&
946 ((extline.Left(15).Compare(_T("// extensions: ")) == 0) ||
947 (extline.Left(14).Compare(_T("' extensions: ")) == 0)))
949 if (extline[0] == '/')
950 extline = extline.Mid(15);
951 else
952 extline = extline.Mid(14);
953 CString sToken;
954 int curPos = 0;
955 sToken = extline.Tokenize(_T(";"), curPos);
956 while (!sToken.IsEmpty())
958 if (!sToken.IsEmpty())
960 if (sToken[0] != '.')
961 sToken = _T(".") + sToken;
962 extensions.insert(sToken);
964 sToken = extline.Tokenize(_T(";"), curPos);
968 f.Close();
970 catch (CFileException* e)
972 e->Delete();
975 for (std::set<CString>::const_iterator it = extensions.begin(); it != extensions.end(); ++it)
977 if (type.IsEmpty() || (type.Compare(_T("Diff")) == 0))
979 if (filename.Left(5).CompareNoCase(_T("diff-")) == 0)
981 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + *it);
982 CString diffregstring = diffreg;
983 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
984 diffreg = _T("wscript.exe \"") + file + _T("\" %base %mine") + kind;
987 if (type.IsEmpty() || (type.Compare(_T("Merge"))==0))
989 if (filename.Left(6).CompareNoCase(_T("merge-"))==0)
991 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\MergeTools\\") + *it);
992 CString diffregstring = diffreg;
993 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
994 diffreg = _T("wscript.exe \"") + file + _T("\" %merged %theirs %mine %base") + kind;
1000 return true;
1003 bool CAppUtils::Export(CString *BashHash, const CTGitPath *orgPath)
1005 // ask from where the export has to be done
1006 CExportDlg dlg;
1007 if(BashHash)
1008 dlg.m_Revision=*BashHash;
1009 if (orgPath)
1011 if (PathIsRelative(orgPath->GetWinPath()))
1012 dlg.m_orgPath = g_Git.CombinePath(orgPath);
1013 else
1014 dlg.m_orgPath = *orgPath;
1017 if (dlg.DoModal() == IDOK)
1019 CString cmd;
1020 cmd.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s --"),
1021 dlg.m_strFile, g_Git.FixBranchName(dlg.m_VersionName));
1023 CProgressDlg pro;
1024 pro.m_GitCmd=cmd;
1025 CGit git;
1026 if (!dlg.m_bWholeProject && !dlg.m_orgPath.IsEmpty() && PathIsDirectory(dlg.m_orgPath.GetWinPathString()))
1028 git.m_CurrentDir = dlg.m_orgPath.GetWinPathString();
1029 pro.m_Git = &git;
1031 return (pro.DoModal() == IDOK);
1033 return false;
1036 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash, bool switch_new_brach)
1038 CCreateBranchTagDlg dlg;
1039 dlg.m_bIsTag=IsTag;
1040 dlg.m_bSwitch=switch_new_brach;
1042 if(CommitHash)
1043 dlg.m_initialRefName = *CommitHash;
1045 if(dlg.DoModal()==IDOK)
1047 CString cmd;
1048 CString force;
1049 CString track;
1050 if(dlg.m_bTrack == TRUE)
1051 track=_T(" --track ");
1052 else if(dlg.m_bTrack == FALSE)
1053 track=_T(" --no-track");
1055 if(dlg.m_bForce)
1056 force=_T(" -f ");
1058 if(IsTag)
1060 CString sign;
1061 if(dlg.m_bSign)
1062 sign=_T("-s");
1064 cmd.Format(_T("git.exe tag %s %s %s %s"),
1065 force,
1066 sign,
1067 dlg.m_BranchTagName,
1068 g_Git.FixBranchName(dlg.m_VersionName)
1071 CString tempfile=::GetTempFile();
1072 if(!dlg.m_Message.Trim().IsEmpty())
1074 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_Message);
1075 cmd += _T(" -F ")+tempfile;
1078 else
1080 cmd.Format(_T("git.exe branch %s %s %s %s"),
1081 track,
1082 force,
1083 dlg.m_BranchTagName,
1084 g_Git.FixBranchName(dlg.m_VersionName)
1087 CString out;
1088 if(g_Git.Run(cmd,&out,CP_UTF8))
1090 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1091 return FALSE;
1093 if( !IsTag && dlg.m_bSwitch )
1095 // it is a new branch and the user has requested to switch to it
1096 PerformSwitch(dlg.m_BranchTagName);
1099 return TRUE;
1101 return FALSE;
1104 bool CAppUtils::Switch(CString initialRefName)
1106 CGitSwitchDlg dlg;
1107 if(!initialRefName.IsEmpty())
1108 dlg.m_initialRefName = initialRefName;
1110 if (dlg.DoModal() == IDOK)
1112 CString branch;
1113 if (dlg.m_bBranch)
1114 branch = dlg.m_NewBranch;
1116 // if refs/heads/ is not stripped, checkout will detach HEAD
1117 // checkout prefers branches on name clashes (with tags)
1118 if (dlg.m_VersionName.Left(11) ==_T("refs/heads/") && dlg.m_bBranchOverride != TRUE)
1119 dlg.m_VersionName = dlg.m_VersionName.Mid(11);
1121 return PerformSwitch(dlg.m_VersionName, dlg.m_bForce == TRUE , branch, dlg.m_bBranchOverride == TRUE, dlg.m_bTrack, dlg.m_bMerge == TRUE);
1123 return FALSE;
1126 bool CAppUtils::PerformSwitch(CString ref, bool bForce /* false */, CString sNewBranch /* CString() */, bool bBranchOverride /* false */, BOOL bTrack /* 2 */, bool bMerge /* false */)
1128 CString cmd;
1129 CString track;
1130 CString force;
1131 CString branch;
1132 CString merge;
1134 if(!sNewBranch.IsEmpty()){
1135 if (bBranchOverride)
1137 branch.Format(_T("-B %s"), sNewBranch);
1139 else
1141 branch.Format(_T("-b %s"), sNewBranch);
1143 if (bTrack == TRUE)
1144 track = _T("--track");
1145 else if (bTrack == FALSE)
1146 track = _T("--no-track");
1148 if (bForce)
1149 force = _T("-f");
1150 if (bMerge)
1151 merge = _T("--merge");
1153 cmd.Format(_T("git.exe checkout %s %s %s %s %s --"),
1154 force,
1155 track,
1156 merge,
1157 branch,
1158 g_Git.FixBranchName(ref));
1160 while (true)
1162 CProgressDlg progress;
1163 progress.m_GitCmd = cmd;
1165 INT_PTR idPull = -1;
1166 INT_PTR idSubmoduleUpdate = -1;
1167 INT_PTR idMerge = -1;
1169 CTGitPath gitPath = g_Git.m_CurrentDir;
1170 if (gitPath.HasSubmodules())
1171 idSubmoduleUpdate = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1172 CString currentBranch;
1173 bool hasBranch = CGit::GetCurrentBranchFromFile(g_Git.m_CurrentDir, currentBranch) == 0;
1174 if (hasBranch)
1175 idMerge = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUMERGE)));
1177 progress.m_PostCmdCallback = [&] ()
1179 if (!progress.m_GitStatus)
1181 CString newBranch;
1182 if (!CGit::GetCurrentBranchFromFile(g_Git.m_CurrentDir, newBranch))
1183 idPull = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
1185 else
1187 progress.m_PostCmdList.RemoveAll();
1188 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
1189 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_SWITCH_WITH_MERGE)));
1193 INT_PTR ret = progress.DoModal();
1194 if (progress.m_GitStatus == 0)
1196 if (idSubmoduleUpdate >= 0 && ret == IDC_PROGRESS_BUTTON1 + idSubmoduleUpdate)
1198 CString sCmd;
1199 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1201 RunTortoiseGitProc(sCmd);
1202 return TRUE;
1204 else if (ret == IDC_PROGRESS_BUTTON1 + idPull)
1206 Pull();
1207 return TRUE;
1209 else if (ret == IDC_PROGRESS_BUTTON1 + idMerge)
1211 Merge(&currentBranch);
1212 return TRUE;
1214 else if (ret == IDOK)
1215 return TRUE;
1217 else if (ret == IDC_PROGRESS_BUTTON1)
1218 continue; // retry
1219 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
1221 merge = _T("--merge");
1222 cmd.Format(_T("git.exe checkout %s %s %s %s %s --"),
1223 force, track, merge, branch, g_Git.FixBranchName(ref));
1224 continue; // retry
1227 return FALSE;
1231 class CIgnoreFile : public CStdioFile
1233 public:
1234 STRING_VECTOR m_Items;
1235 CString m_eol;
1237 virtual BOOL ReadString(CString& rString)
1239 if (GetPosition() == 0)
1241 unsigned char utf8bom[] = { 0xEF, 0xBB, 0xBF };
1242 char buf[3] = { 0, 0, 0 };
1243 Read(buf, 3);
1244 if (memcpy(buf, utf8bom, sizeof(utf8bom)))
1246 SeekToBegin();
1250 CStringA strA;
1251 char lastChar = '\0';
1252 for (char c = '\0'; Read(&c, 1) == 1; lastChar = c)
1254 if (c == '\r')
1255 continue;
1256 if (c == '\n')
1258 m_eol = lastChar == '\r' ? _T("\r\n") : _T("\n");
1259 break;
1261 strA.AppendChar(c);
1263 if (strA.IsEmpty())
1264 return FALSE;
1266 rString = CUnicodeUtils::GetUnicode(strA);
1267 return TRUE;
1270 void ResetState()
1272 m_Items.clear();
1273 m_eol = _T("");
1277 bool CAppUtils::OpenIgnoreFile(CIgnoreFile &file, const CString& filename)
1279 file.ResetState();
1280 if (!file.Open(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate | CFile::typeBinary))
1282 CMessageBox::Show(NULL, filename + _T(" Open Failure"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1283 return false;
1286 if (file.GetLength() > 0)
1288 CString fileText;
1289 while (file.ReadString(fileText))
1290 file.m_Items.push_back(fileText);
1291 file.Seek(file.GetLength() - 1, 0);
1292 char lastchar[1] = { 0 };
1293 file.Read(lastchar, 1);
1294 file.SeekToEnd();
1295 if (lastchar[0] != '\n')
1297 CStringA eol = CStringA(file.m_eol.IsEmpty() ? _T("\n") : file.m_eol);
1298 file.Write(eol, eol.GetLength());
1301 else
1302 file.SeekToEnd();
1304 return true;
1307 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1309 CIgnoreDlg ignoreDlg;
1310 if (ignoreDlg.DoModal() == IDOK)
1312 CString ignorefile;
1313 ignorefile = g_Git.m_CurrentDir + _T("\\");
1315 switch (ignoreDlg.m_IgnoreFile)
1317 case 0:
1318 ignorefile += _T(".gitignore");
1319 break;
1320 case 2:
1321 g_GitAdminDir.GetAdminDirPath(g_Git.m_CurrentDir, ignorefile);
1322 ignorefile += _T("info/exclude");
1323 break;
1326 CIgnoreFile file;
1329 if (ignoreDlg.m_IgnoreFile != 1 && !OpenIgnoreFile(file, ignorefile))
1330 return false;
1332 for (int i = 0; i < path.GetCount(); ++i)
1334 if (ignoreDlg.m_IgnoreFile == 1)
1336 ignorefile = g_Git.CombinePath(path[i].GetContainingDirectory()) + _T("\\.gitignore");
1337 if (!OpenIgnoreFile(file, ignorefile))
1338 return false;
1341 CString ignorePattern;
1342 if (ignoreDlg.m_IgnoreType == 0)
1344 if (ignoreDlg.m_IgnoreFile != 1 && !path[i].GetContainingDirectory().GetGitPathString().IsEmpty())
1345 ignorePattern += _T("/") + path[i].GetContainingDirectory().GetGitPathString();
1347 ignorePattern += _T("/");
1349 if (IsMask)
1351 ignorePattern += _T("*") + path[i].GetFileExtension();
1353 else
1355 ignorePattern += path[i].GetFileOrDirectoryName();
1358 // escape [ and ] so that files get ignored correctly
1359 ignorePattern.Replace(_T("["), _T("\\["));
1360 ignorePattern.Replace(_T("]"), _T("\\]"));
1362 bool found = false;
1363 for (size_t j = 0; j < file.m_Items.size(); ++j)
1365 if (file.m_Items[j] == ignorePattern)
1367 found = true;
1368 break;
1371 if (!found)
1373 file.m_Items.push_back(ignorePattern);
1374 ignorePattern += file.m_eol.IsEmpty() ? _T("\n") : file.m_eol;
1375 CStringA ignorePatternA = CUnicodeUtils::GetUTF8(ignorePattern);
1376 file.Write(ignorePatternA, ignorePatternA.GetLength());
1379 if (ignoreDlg.m_IgnoreFile == 1)
1380 file.Close();
1383 if (ignoreDlg.m_IgnoreFile != 1)
1384 file.Close();
1386 catch(...)
1388 file.Abort();
1389 return false;
1392 return true;
1394 return false;
1398 bool CAppUtils::GitReset(CString *CommitHash,int type)
1400 CResetDlg dlg;
1401 dlg.m_ResetType=type;
1402 dlg.m_ResetToVersion=*CommitHash;
1403 dlg.m_initialRefName = *CommitHash;
1404 if (dlg.DoModal() == IDOK)
1406 CString cmd;
1407 CString type;
1408 switch(dlg.m_ResetType)
1410 case 0:
1411 type=_T("--soft");
1412 break;
1413 case 1:
1414 type=_T("--mixed");
1415 break;
1416 case 2:
1417 type=_T("--hard");
1418 break;
1419 default:
1420 dlg.m_ResetType = 1;
1421 type=_T("--mixed");
1422 break;
1424 cmd.Format(_T("git.exe reset %s %s --"),type, dlg.m_ResetToVersion);
1426 while (true)
1428 CProgressDlg progress;
1429 progress.m_GitCmd=cmd;
1431 CTGitPath gitPath = g_Git.m_CurrentDir;
1432 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2)
1433 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1435 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
1437 INT_PTR ret;
1438 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
1440 CGitProgressDlg gitdlg;
1441 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
1442 gitdlg.SetRevision(dlg.m_ResetToVersion);
1443 gitdlg.SetResetType(dlg.m_ResetType);
1444 ret = gitdlg.DoModal();
1446 else
1447 ret = progress.DoModal();
1449 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 2 && ret == IDC_PROGRESS_BUTTON1)
1451 CString sCmd;
1452 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1454 RunTortoiseGitProc(sCmd);
1455 return TRUE;
1457 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
1458 continue; // retry
1459 else if (ret == IDOK)
1460 return TRUE;
1461 else
1462 break;
1465 return FALSE;
1468 void CAppUtils::DescribeConflictFile(bool mode, bool base,CString &descript)
1470 if(mode == FALSE)
1472 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE));
1473 return;
1475 if(base)
1477 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED));
1478 return;
1480 descript = CString(MAKEINTRESOURCE(IDS_PROC_CREATED));
1481 return;
1484 void CAppUtils::RemoveTempMergeFile(CTGitPath &path)
1486 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("LOCAL"), path));
1487 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("REMOTE"), path));
1488 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("BASE"), path));
1490 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1492 CString file;
1493 file=g_Git.m_CurrentDir+_T("\\") + merge.GetWinPathString()+_T(".")+type+merge.GetFileExtension();
1495 return file;
1498 bool ParseHashesFromLsFile(BYTE_VECTOR &out, CString &hash1, CString &hash2, CString &hash3)
1500 unsigned int pos = 0;
1501 CString one;
1502 CString part;
1504 while (pos >= 0 && pos < out.size())
1506 one.Empty();
1508 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1509 int tabstart = 0;
1510 one.Tokenize(_T("\t"), tabstart);
1512 tabstart = 0;
1513 part = one.Tokenize(_T(" "), tabstart); //Tag
1514 part = one.Tokenize(_T(" "), tabstart); //Mode
1515 part = one.Tokenize(_T(" "), tabstart); //Hash
1516 CString hash = part;
1517 part = one.Tokenize(_T("\t"), tabstart); //Stage
1518 int stage = _ttol(part);
1519 if (stage == 1)
1520 hash1 = hash;
1521 else if (stage == 2)
1522 hash2 = hash;
1523 else if (stage == 3)
1525 hash3 = hash;
1526 return true;
1529 pos = out.findNextString(pos);
1532 return false;
1535 bool CAppUtils::ConflictEdit(CTGitPath& path, bool /*bAlternativeTool = false*/, bool revertTheirMy /*= false*/, HWND resolveMsgHwnd /*= nullptr*/)
1537 bool bRet = false;
1539 CTGitPath merge=path;
1540 CTGitPath directory = merge.GetDirectory();
1542 // we have the conflicted file (%merged)
1543 // now look for the other required files
1544 //GitStatus stat;
1545 //stat.GetStatus(merge);
1546 //if (stat.status == NULL)
1547 // return false;
1549 BYTE_VECTOR vector;
1551 CString cmd;
1552 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1554 if (g_Git.Run(cmd, &vector))
1556 return FALSE;
1559 if (merge.IsDirectory())
1561 CString baseHash, localHash, remoteHash;
1562 if (!ParseHashesFromLsFile(vector, baseHash, localHash, remoteHash))
1563 return FALSE;
1565 CString msg;
1566 msg.Format(_T("BASE: %s\nLOCAL: %s\nREMOTE: %s"), baseHash, localHash, remoteHash);
1567 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK);
1568 return TRUE;
1571 CTGitPathList list;
1572 if (list.ParserFromLsFile(vector))
1574 CMessageBox::Show(NULL, _T("Parse ls-files failed!"), _T("TortoiseGit"), MB_OK);
1575 return FALSE;
1578 if (list.IsEmpty())
1579 return FALSE;
1581 CTGitPath theirs;
1582 CTGitPath mine;
1583 CTGitPath base;
1585 if (revertTheirMy)
1587 mine.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1588 theirs.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1590 else
1592 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1593 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1595 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1597 CString format;
1599 //format=_T("git.exe cat-file blob \":%d:%s\"");
1600 format = _T("git.exe checkout-index --temp --stage=%d -- \"%s\"");
1601 CFile tempfile;
1602 //create a empty file, incase stage is not three
1603 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1604 tempfile.Close();
1605 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1606 tempfile.Close();
1607 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1608 tempfile.Close();
1610 bool b_base=false, b_local=false, b_remote=false;
1612 for (int i = 0; i< list.GetCount(); ++i)
1614 CString cmd;
1615 CString outfile;
1616 cmd.Empty();
1617 outfile.Empty();
1619 if( list[i].m_Stage == 1)
1621 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1622 b_base = true;
1623 outfile = base.GetWinPathString();
1626 if( list[i].m_Stage == 2 )
1628 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1629 b_local = true;
1630 outfile = mine.GetWinPathString();
1633 if( list[i].m_Stage == 3 )
1635 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1636 b_remote = true;
1637 outfile = theirs.GetWinPathString();
1639 CString output, err;
1640 if(!outfile.IsEmpty())
1641 if (!g_Git.Run(cmd, &output, &err, CP_UTF8))
1643 CString file;
1644 int start =0 ;
1645 file = output.Tokenize(_T("\t"), start);
1646 ::MoveFileEx(file,outfile,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
1648 else
1650 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1654 if(b_local && b_remote )
1656 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1657 if( revertTheirMy )
1658 bRet = !!CAppUtils::StartExtMerge(base, mine, theirs, merge, _T("BASE"), _T("REMOTE"), _T("LOCAL"), CString(), false, resolveMsgHwnd);
1659 else
1660 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge, _T("BASE"), _T("REMOTE"), _T("LOCAL"), CString(), false, resolveMsgHwnd);
1663 else
1665 ::DeleteFile(mine.GetWinPathString());
1666 ::DeleteFile(theirs.GetWinPathString());
1667 ::DeleteFile(base.GetWinPathString());
1669 CDeleteConflictDlg dlg;
1670 DescribeConflictFile(b_local, b_base,dlg.m_LocalStatus);
1671 DescribeConflictFile(b_remote,b_base,dlg.m_RemoteStatus);
1672 CGitHash localHash, remoteHash;
1673 if (!g_Git.GetHash(localHash, _T("HEAD")))
1674 dlg.m_LocalHash = localHash.ToString();
1675 if (!g_Git.GetHash(remoteHash, _T("MERGE_HEAD")))
1676 dlg.m_RemoteHash = remoteHash.ToString();
1677 else if (!g_Git.GetHash(remoteHash, _T("rebase-apply/original-commit")))
1678 dlg.m_RemoteHash = remoteHash.ToString();
1679 else if (!g_Git.GetHash(remoteHash, _T("CHERRY_PICK_HEAD")))
1680 dlg.m_RemoteHash = remoteHash.ToString();
1681 else if (!g_Git.GetHash(remoteHash, _T("REVERT_HEAD")))
1682 dlg.m_RemoteHash = remoteHash.ToString();
1683 dlg.m_bShowModifiedButton=b_base;
1684 dlg.m_File=merge.GetGitPathString();
1685 if(dlg.DoModal() == IDOK)
1687 CString cmd,out;
1688 if(dlg.m_bIsDelete)
1690 cmd.Format(_T("git.exe rm -- \"%s\""),merge.GetGitPathString());
1692 else
1693 cmd.Format(_T("git.exe add -- \"%s\""),merge.GetGitPathString());
1695 if (g_Git.Run(cmd, &out, CP_UTF8))
1697 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1698 return FALSE;
1700 return TRUE;
1702 else
1703 return FALSE;
1706 #if 0
1707 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1708 base, theirs, mine, merge);
1709 #endif
1710 #if 0
1711 if (stat.status->text_status == svn_wc_status_conflicted)
1713 // we have a text conflict, use our merge tool to resolve the conflict
1715 CTSVNPath theirs(directory);
1716 CTSVNPath mine(directory);
1717 CTSVNPath base(directory);
1718 bool bConflictData = false;
1720 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1722 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1723 bConflictData = true;
1725 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1727 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1728 bConflictData = true;
1730 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1732 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1733 bConflictData = true;
1735 else
1737 mine = merge;
1739 if (bConflictData)
1740 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1741 base, theirs, mine, merge);
1744 if (stat.status->prop_status == svn_wc_status_conflicted)
1746 // we have a property conflict
1747 CTSVNPath prej(directory);
1748 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1750 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1751 // there's a problem: the prej file contains a _description_ of the conflict, and
1752 // that description string might be translated. That means we have no way of parsing
1753 // the file to find out the conflicting values.
1754 // The only thing we can do: show a dialog with the conflict description, then
1755 // let the user either accept the existing property or open the property edit dialog
1756 // to manually change the properties and values. And a button to mark the conflict as
1757 // resolved.
1758 CEditPropConflictDlg dlg;
1759 dlg.SetPrejFile(prej);
1760 dlg.SetConflictedItem(merge);
1761 bRet = (dlg.DoModal() != IDCANCEL);
1765 if (stat.status->tree_conflict)
1767 // we have a tree conflict
1768 SVNInfo info;
1769 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1770 if (pInfoData)
1772 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1774 CTSVNPath theirs(directory);
1775 CTSVNPath mine(directory);
1776 CTSVNPath base(directory);
1777 bool bConflictData = false;
1779 if (pInfoData->treeconflict_theirfile)
1781 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1782 bConflictData = true;
1784 if (pInfoData->treeconflict_basefile)
1786 base.AppendPathString(pInfoData->treeconflict_basefile);
1787 bConflictData = true;
1789 if (pInfoData->treeconflict_myfile)
1791 mine.AppendPathString(pInfoData->treeconflict_myfile);
1792 bConflictData = true;
1794 else
1796 mine = merge;
1798 if (bConflictData)
1799 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1800 base, theirs, mine, merge);
1802 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1804 CString sConflictAction;
1805 CString sConflictReason;
1806 CString sResolveTheirs;
1807 CString sResolveMine;
1808 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1809 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1811 if (pInfoData->treeconflict_nodekind == svn_node_file)
1813 switch (pInfoData->treeconflict_operation)
1815 case svn_wc_operation_update:
1816 switch (pInfoData->treeconflict_action)
1818 case svn_wc_conflict_action_edit:
1819 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1820 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1821 break;
1822 case svn_wc_conflict_action_add:
1823 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1824 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1825 break;
1826 case svn_wc_conflict_action_delete:
1827 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1828 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1829 break;
1831 break;
1832 case svn_wc_operation_switch:
1833 switch (pInfoData->treeconflict_action)
1835 case svn_wc_conflict_action_edit:
1836 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1837 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1838 break;
1839 case svn_wc_conflict_action_add:
1840 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1841 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1842 break;
1843 case svn_wc_conflict_action_delete:
1844 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1845 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1846 break;
1848 break;
1849 case svn_wc_operation_merge:
1850 switch (pInfoData->treeconflict_action)
1852 case svn_wc_conflict_action_edit:
1853 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1854 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1855 break;
1856 case svn_wc_conflict_action_add:
1857 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1858 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1859 break;
1860 case svn_wc_conflict_action_delete:
1861 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1862 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1863 break;
1865 break;
1868 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1870 switch (pInfoData->treeconflict_operation)
1872 case svn_wc_operation_update:
1873 switch (pInfoData->treeconflict_action)
1875 case svn_wc_conflict_action_edit:
1876 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1877 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1878 break;
1879 case svn_wc_conflict_action_add:
1880 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1881 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1882 break;
1883 case svn_wc_conflict_action_delete:
1884 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1885 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1886 break;
1888 break;
1889 case svn_wc_operation_switch:
1890 switch (pInfoData->treeconflict_action)
1892 case svn_wc_conflict_action_edit:
1893 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1894 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1895 break;
1896 case svn_wc_conflict_action_add:
1897 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1898 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1899 break;
1900 case svn_wc_conflict_action_delete:
1901 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1902 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1903 break;
1905 break;
1906 case svn_wc_operation_merge:
1907 switch (pInfoData->treeconflict_action)
1909 case svn_wc_conflict_action_edit:
1910 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1911 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1912 break;
1913 case svn_wc_conflict_action_add:
1914 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1915 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1916 break;
1917 case svn_wc_conflict_action_delete:
1918 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1919 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1920 break;
1922 break;
1926 UINT uReasonID = 0;
1927 switch (pInfoData->treeconflict_reason)
1929 case svn_wc_conflict_reason_edited:
1930 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1931 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1932 break;
1933 case svn_wc_conflict_reason_obstructed:
1934 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1935 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1936 break;
1937 case svn_wc_conflict_reason_deleted:
1938 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1939 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1940 break;
1941 case svn_wc_conflict_reason_added:
1942 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1943 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1944 break;
1945 case svn_wc_conflict_reason_missing:
1946 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1947 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1948 break;
1949 case svn_wc_conflict_reason_unversioned:
1950 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1951 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1952 break;
1954 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1956 CTreeConflictEditorDlg dlg;
1957 dlg.SetConflictInfoText(sConflictReason);
1958 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1959 dlg.SetPath(treeConflictPath);
1960 INT_PTR dlgRet = dlg.DoModal();
1961 bRet = (dlgRet != IDCANCEL);
1965 #endif
1966 return bRet;
1969 bool CAppUtils::IsSSHPutty()
1971 CString sshclient=g_Git.m_Environment.GetEnv(_T("GIT_SSH"));
1972 sshclient=sshclient.MakeLower();
1973 if(sshclient.Find(_T("plink.exe"),0)>=0)
1975 return true;
1977 return false;
1980 CString CAppUtils::GetClipboardLink(const CString &skipGitPrefix, int paramsCount)
1982 if (!OpenClipboard(NULL))
1983 return CString();
1985 CString sClipboardText;
1986 HGLOBAL hglb = GetClipboardData(CF_TEXT);
1987 if (hglb)
1989 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1990 sClipboardText = CString(lpstr);
1991 GlobalUnlock(hglb);
1993 hglb = GetClipboardData(CF_UNICODETEXT);
1994 if (hglb)
1996 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
1997 sClipboardText = lpstr;
1998 GlobalUnlock(hglb);
2000 CloseClipboard();
2002 if(!sClipboardText.IsEmpty())
2004 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
2005 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
2007 if(sClipboardText.Find( _T("http://")) == 0)
2008 return sClipboardText;
2010 if(sClipboardText.Find( _T("https://")) == 0)
2011 return sClipboardText;
2013 if(sClipboardText.Find( _T("git://")) == 0)
2014 return sClipboardText;
2016 if(sClipboardText.Find( _T("ssh://")) == 0)
2017 return sClipboardText;
2019 if(sClipboardText.GetLength()>=2)
2020 if( sClipboardText[1] == _T(':') )
2021 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
2022 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
2023 return sClipboardText;
2025 // trim prefixes like "git clone "
2026 if (!skipGitPrefix.IsEmpty() && sClipboardText.Find(skipGitPrefix) == 0)
2028 sClipboardText = sClipboardText.Mid(skipGitPrefix.GetLength()).Trim();
2029 int spacePos = -1;
2030 while (paramsCount >= 0)
2032 --paramsCount;
2033 spacePos = sClipboardText.Find(_T(' '), spacePos + 1);
2034 if (spacePos == -1)
2035 break;
2037 if (spacePos > 0 && paramsCount < 0)
2038 sClipboardText = sClipboardText.Left(spacePos);
2039 return sClipboardText;
2043 return CString(_T(""));
2046 CString CAppUtils::ChooseRepository(CString *path)
2048 CBrowseFolder browseFolder;
2049 CRegString regLastResopitory = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2051 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
2052 CString strCloneDirectory;
2053 if(path)
2054 strCloneDirectory=*path;
2055 else
2057 strCloneDirectory = regLastResopitory;
2060 CString title;
2061 title.LoadString(IDS_CHOOSE_REPOSITORY);
2063 browseFolder.SetInfo(title);
2065 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
2067 regLastResopitory = strCloneDirectory;
2068 return strCloneDirectory;
2070 else
2072 return CString();
2076 bool CAppUtils::SendPatchMail(CTGitPathList& list)
2078 CSendMailDlg dlg;
2080 dlg.m_PathList = list;
2082 if(dlg.DoModal()==IDOK)
2084 if (dlg.m_PathList.IsEmpty())
2085 return FALSE;
2087 CGitProgressDlg progDlg;
2089 theApp.m_pMainWnd = &progDlg;
2090 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2092 progDlg.SetPathList(dlg.m_PathList);
2093 //ProjectProperties props;
2094 //props.ReadPropsPathList(dlg.m_pathList);
2095 //progDlg.SetProjectProperties(props);
2096 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2098 CSendMailPatch sendMailPatch(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2099 progDlg.SetSendMailOption(&sendMailPatch);
2101 progDlg.DoModal();
2103 return true;
2105 return false;
2108 bool CAppUtils::SendPatchMail(CString &cmd,CString &formatpatchoutput)
2110 CTGitPathList list;
2111 CString log=formatpatchoutput;
2112 int start=log.Find(cmd);
2113 if(start >=0)
2114 CString one=log.Tokenize(_T("\n"),start);
2115 else
2116 start = 0;
2118 while(start>=0)
2120 CString one=log.Tokenize(_T("\n"),start);
2121 one=one.Trim();
2122 if(one.IsEmpty() || one.Find(_T("Success")) == 0)
2123 continue;
2124 one.Replace(_T('/'),_T('\\'));
2125 CTGitPath path;
2126 path.SetFromWin(one);
2127 list.AddPath(path);
2129 if (!list.IsEmpty())
2131 return SendPatchMail(list);
2133 else
2135 CMessageBox::Show(NULL, IDS_ERR_NOPATCHES, IDS_APPNAME, MB_ICONINFORMATION);
2136 return true;
2141 int CAppUtils::GetLogOutputEncode(CGit *pGit)
2143 CString output;
2144 output = pGit->GetConfigValue(_T("i18n.logOutputEncoding"));
2145 if(output.IsEmpty())
2146 return CUnicodeUtils::GetCPCode(pGit->GetConfigValue(_T("i18n.commitencoding")));
2147 else
2149 return CUnicodeUtils::GetCPCode(output);
2152 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
2154 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
2155 int cp = CUnicodeUtils::GetCPCode(g_Git.GetConfigValue(_T("i18n.commitencoding")));
2157 bool stripComments = (CRegDWORD(_T("Software\\TortoiseGit\\StripCommentedLines"), FALSE) == TRUE);
2159 if (CRegDWORD(_T("Software\\TortoiseGit\\SanitizeCommitMsg"), TRUE) == TRUE)
2160 message.TrimRight(L" \r\n");
2162 int len = message.GetLength();
2163 int start = 0;
2164 while (start >= 0 && start < len)
2166 int oldStart = start;
2167 start = message.Find(L"\n", oldStart);
2168 CString line = message.Mid(oldStart);
2169 if (start != -1)
2171 line = line.Left(start - oldStart);
2172 ++start; // move forward so we don't find the same char again
2174 if (stripComments && (line.GetLength() >= 1 && line.GetAt(0) == '#') || (start < 0 && line.IsEmpty()))
2175 continue;
2176 line.TrimRight(L" \r");
2177 CStringA lineA = CUnicodeUtils::GetMulti(line + L"\n", cp);
2178 file.Write(lineA.GetBuffer(), lineA.GetLength());
2180 file.Close();
2181 return 0;
2184 bool CAppUtils::Pull(bool showPush)
2186 CPullFetchDlg dlg;
2187 dlg.m_IsPull = TRUE;
2188 if (dlg.DoModal() == IDOK)
2190 CString url = dlg.m_RemoteURL;
2192 if (dlg.m_bAutoLoad)
2194 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2197 CString cmd;
2198 CGitHash hashOld;
2199 if (g_Git.GetHash(hashOld, _T("HEAD")))
2201 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
2202 return false;
2205 CString cmdRebase;
2206 CString noff;
2207 CString ffonly;
2208 CString squash;
2209 CString nocommit;
2210 CString notags;
2211 CString prune;
2213 if (dlg.m_bRebase)
2214 cmdRebase = "--rebase ";
2216 if (!dlg.m_bFetchTags)
2217 notags = _T("--no-tags");
2219 if (dlg.m_bNoFF)
2220 noff=_T("--no-ff");
2222 if (dlg.m_bFFonly)
2223 ffonly = _T("--ff-only");
2225 if (dlg.m_bSquash)
2226 squash = _T("--squash");
2228 if (dlg.m_bNoCommit)
2229 nocommit = _T("--no-commit");
2231 int ver = CAppUtils::GetMsysgitVersion();
2233 if (dlg.m_bPrune == TRUE)
2234 prune = _T("--prune ");
2235 else if (dlg.m_bPrune == FALSE && ver >= 0x01080500)
2236 prune = _T("--no-prune ");
2238 if(ver >= 0x01070203) //above 1.7.0.2
2239 cmdRebase += _T("--progress ");
2241 cmd.Format(_T("git.exe pull -v %s %s %s %s %s %s %s \"%s\" %s"), cmdRebase, noff, ffonly, squash, nocommit, notags, prune, url, dlg.m_RemoteBranchName);
2242 CProgressDlg progress;
2243 progress.m_GitCmd = cmd;
2244 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_DIFFS)));
2245 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_LOG)));
2246 INT_PTR pushButton = showPush ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH))) + IDC_PROGRESS_BUTTON1 : -1;
2248 CTGitPath gitPath = g_Git.m_CurrentDir;
2249 INT_PTR smUpdateButton = gitPath.HasSubmodules() ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE))) + IDC_PROGRESS_BUTTON1 : -1;
2251 INT_PTR ret = progress.DoModal();
2253 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)
2255 CChangedDlg dlg;
2256 dlg.m_pathList.AddPath(CTGitPath());
2257 dlg.DoModal();
2259 return true;
2262 CGitHash hashNew;
2263 if (g_Git.GetHash(hashNew, _T("HEAD")))
2265 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR);
2266 return FALSE;
2269 if( ret == IDC_PROGRESS_BUTTON1)
2271 if(hashOld == hashNew)
2273 if(progress.m_GitStatus == 0)
2274 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2275 return TRUE;
2278 CFileDiffDlg dlg;
2279 dlg.SetDiff(NULL, hashNew.ToString(), hashOld.ToString());
2280 dlg.DoModal();
2282 return true;
2284 else if ( ret == IDC_PROGRESS_BUTTON1 +1 )
2286 if(hashOld == hashNew)
2288 if(progress.m_GitStatus == 0)
2289 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2290 return true;
2293 CLogDlg dlg;
2294 dlg.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld.ToString() + _T("..") + hashNew.ToString(), 0);
2295 dlg.DoModal();
2297 else if (ret == pushButton)
2299 Push(_T(""));
2300 return true;
2302 else if (ret == smUpdateButton)
2304 CString sCmd;
2305 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
2307 CAppUtils::RunTortoiseGitProc(sCmd);
2308 return true;
2312 return true;
2315 bool CAppUtils::Fetch(CString remoteName, bool allowRebase, bool allRemotes)
2317 CPullFetchDlg dlg;
2318 dlg.m_PreSelectRemote = remoteName;
2319 dlg.m_bAllowRebase = allowRebase;
2320 dlg.m_IsPull=FALSE;
2321 dlg.m_bAllRemotes = allRemotes;
2323 if(dlg.DoModal()==IDOK)
2325 if(dlg.m_bAutoLoad)
2327 if (dlg.m_bAllRemotes)
2329 STRING_VECTOR list;
2330 g_Git.GetRemoteList(list);
2332 STRING_VECTOR::const_iterator it = list.begin();
2333 while (it != list.end())
2335 CString remote(*it);
2336 CAppUtils::LaunchPAgent(NULL, &remote);
2337 ++it;
2340 else
2341 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2344 CString url;
2345 url=dlg.m_RemoteURL;
2346 CString cmd;
2347 CString arg;
2349 int ver = CAppUtils::GetMsysgitVersion();
2351 if(ver >= 0x01070203) //above 1.7.0.2
2352 arg = _T("--progress ");
2354 if (dlg.m_bPrune == TRUE)
2355 arg += _T("--prune ");
2356 else if (dlg.m_bPrune == FALSE && ver >= 0x01080500)
2357 arg += _T("--no-prune ");
2359 if (dlg.m_bFetchTags == 1)
2361 arg += _T("--tags ");
2363 else if (dlg.m_bFetchTags == 0)
2365 arg += _T("--no-tags ");
2368 if (dlg.m_bAllRemotes)
2369 cmd.Format(_T("git.exe fetch --all -v %s"), arg);
2370 else
2371 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"), arg, url, dlg.m_RemoteBranchName);
2373 CProgressDlg progress;
2375 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2376 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_RESET)));
2378 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
2380 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2383 progress.m_GitCmd=cmd;
2384 INT_PTR userResponse;
2386 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
2388 CGitProgressDlg gitdlg;
2389 if (!dlg.m_bAllRemotes)
2390 gitdlg.SetUrl(url);
2391 gitdlg.SetCommand(CGitProgressList::GitProgress_Fetch);
2392 gitdlg.SetAutoTag(dlg.m_bFetchTags == 1 ? GIT_REMOTE_DOWNLOAD_TAGS_ALL : dlg.m_bFetchTags == 2 ? GIT_REMOTE_DOWNLOAD_TAGS_AUTO : GIT_REMOTE_DOWNLOAD_TAGS_NONE);
2393 if (!dlg.m_bAllRemotes)
2394 gitdlg.SetRefSpec(dlg.m_RemoteBranchName);
2395 userResponse = gitdlg.DoModal();
2398 else
2399 userResponse = progress.DoModal();
2401 if (userResponse == IDC_PROGRESS_BUTTON1)
2403 CString cmd = _T("/command:log");
2404 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2405 RunTortoiseGitProc(cmd);
2406 return TRUE;
2408 else if (userResponse == IDC_PROGRESS_BUTTON1 + 1)
2410 CString pullRemote, pullBranch;
2411 g_Git.GetRemoteTrackedBranchForHEAD(pullRemote, pullBranch);
2413 CString defaultUpstream;
2414 if (!pullRemote.IsEmpty() && !pullBranch.IsEmpty())
2415 defaultUpstream.Format(_T("remotes/%s/%s"), pullRemote, pullBranch);
2416 GitReset(&defaultUpstream, 2);
2417 return TRUE;
2419 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 2) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
2421 while(1)
2423 CRebaseDlg dlg;
2424 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2425 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
2426 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2427 INT_PTR response = dlg.DoModal();
2428 if(response == IDOK)
2430 return TRUE;
2432 else if (response == IDC_REBASE_POST_BUTTON)
2434 CString cmd = _T("/command:log");
2435 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2436 RunTortoiseGitProc(cmd);
2437 return TRUE;
2439 else if (response == IDC_REBASE_POST_BUTTON + 1)
2441 CString cmd, out, err;
2442 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2443 g_Git.m_CurrentDir,
2444 g_Git.FixBranchName(dlg.m_Upstream),
2445 g_Git.FixBranchName(dlg.m_Branch));
2446 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
2448 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2449 return FALSE;
2452 CAppUtils::SendPatchMail(cmd,out);
2453 return TRUE;
2455 else if (response == IDC_REBASE_POST_BUTTON + 2)
2456 continue;
2457 else if(response == IDCANCEL)
2458 return FALSE;
2460 return TRUE;
2462 else if (userResponse != IDCANCEL)
2463 return TRUE;
2465 return FALSE;
2468 bool CAppUtils::Push(CString selectLocalBranch)
2470 CPushDlg dlg;
2471 dlg.m_BranchSourceName = selectLocalBranch;
2473 if (dlg.DoModal() == IDOK)
2475 CString error;
2476 DWORD exitcode = 0xFFFFFFFF;
2477 if (CHooks::Instance().PrePush(g_Git.m_CurrentDir, exitcode, error))
2479 if (exitcode)
2481 CString temp;
2482 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2483 CMessageBox::Show(nullptr, temp, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2484 return false;
2488 CString arg;
2490 if(dlg.m_bPack)
2491 arg += _T("--thin ");
2492 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
2493 arg += _T("--tags ");
2494 if(dlg.m_bForce)
2495 arg += _T("--force ");
2496 if (dlg.m_bSetUpstream)
2497 arg += _T("--set-upstream ");
2498 if (dlg.m_RecurseSubmodules == 1)
2499 arg += _T("--recurse-submodules=check ");
2500 if (dlg.m_RecurseSubmodules == 2)
2501 arg += _T("--recurse-submodules=on-demand ");
2503 int ver = CAppUtils::GetMsysgitVersion();
2505 if(ver >= 0x01070203) //above 1.7.0.2
2506 arg += _T("--progress ");
2508 CProgressDlg progress;
2510 STRING_VECTOR remotesList;
2511 if (dlg.m_bPushAllRemotes)
2512 g_Git.GetRemoteList(remotesList);
2513 else
2514 remotesList.push_back(dlg.m_URL);
2516 for (unsigned int i = 0; i < remotesList.size(); ++i)
2518 if (dlg.m_bAutoLoad)
2519 CAppUtils::LaunchPAgent(NULL, &remotesList[i]);
2521 CString cmd;
2522 if (dlg.m_bPushAllBranches)
2524 cmd.Format(_T("git.exe push --all %s \"%s\""),
2525 arg,
2526 remotesList[i]);
2528 if (dlg.m_bTags)
2530 progress.m_GitCmdList.push_back(cmd);
2531 cmd.Format(_T("git.exe push --tags %s \"%s\""), arg, remotesList[i]);
2534 else
2536 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2537 arg,
2538 remotesList[i],
2539 dlg.m_BranchSourceName);
2540 if (!dlg.m_BranchRemoteName.IsEmpty())
2542 cmd += _T(":") + dlg.m_BranchRemoteName;
2545 progress.m_GitCmdList.push_back(cmd);
2548 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2549 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2550 bool rejected = false;
2551 progress.m_PostCmdCallback = [&] ()
2553 if (progress.m_GitStatus)
2555 progress.m_PostCmdList.RemoveAll();
2556 rejected = progress.GetLogText().Find(_T("! [rejected]")) > 0;
2557 if (rejected)
2559 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
2560 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUFETCH)));
2562 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2566 INT_PTR ret = progress.DoModal();
2568 exitcode = 0xFFFFFFFF;
2569 error.Empty();
2570 if (CHooks::Instance().PostPush(g_Git.m_CurrentDir, exitcode, error))
2572 if (exitcode)
2574 CString temp;
2575 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2576 CMessageBox::Show(nullptr, temp, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2577 return false;
2581 if (!progress.m_GitStatus)
2583 if(ret == IDC_PROGRESS_BUTTON1)
2585 RequestPull(dlg.m_BranchRemoteName);
2587 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2589 Push(selectLocalBranch);
2591 return TRUE;
2593 else
2595 if (rejected)
2597 // failed, pull first
2598 if (ret == IDC_PROGRESS_BUTTON1)
2600 Pull(true);
2602 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
2604 Fetch(dlg.m_bPushAllRemotes ? _T("") : dlg.m_URL, true, !!dlg.m_bPushAllRemotes);
2606 else if (ret == IDC_PROGRESS_BUTTON1 + 2)
2608 Push(selectLocalBranch);
2611 else
2613 if (ret == IDC_PROGRESS_BUTTON1)
2615 Push(selectLocalBranch);
2620 return FALSE;
2623 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2625 CRequestPullDlg dlg;
2626 dlg.m_RepositoryURL = repositoryUrl;
2627 dlg.m_EndRevision = endrevision;
2628 if (dlg.DoModal()==IDOK)
2630 CString cmd;
2631 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2633 CSysProgressDlg sysProgressDlg;
2634 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2635 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST)));
2636 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2637 sysProgressDlg.SetShowProgressBar(false);
2638 sysProgressDlg.ShowModeless((HWND)NULL, true);
2640 CString tempFileName = GetTempFile();
2641 CString err;
2642 DeleteFile(tempFileName);
2643 CreateDirectory(tempFileName, NULL);
2644 tempFileName += _T("\\pullrequest.txt");
2645 if (g_Git.RunLogFile(cmd, tempFileName, &err))
2647 CString msg;
2648 msg.LoadString(IDS_ERR_PULLREUQESTFAILED);
2649 CMessageBox::Show(NULL, msg + _T("\n") + err, _T("TortoiseGit"), MB_OK);
2650 return false;
2653 if (sysProgressDlg.HasUserCancelled())
2655 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
2656 ::DeleteFile(tempFileName);
2657 return false;
2660 sysProgressDlg.Stop();
2662 if (dlg.m_bSendMail)
2664 CSendMailDlg dlg;
2665 dlg.m_PathList = CTGitPathList(CTGitPath(tempFileName));
2666 dlg.m_bCustomSubject = true;
2668 if (dlg.DoModal() == IDOK)
2670 if (dlg.m_PathList.IsEmpty())
2671 return FALSE;
2673 CGitProgressDlg progDlg;
2675 theApp.m_pMainWnd = &progDlg;
2676 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2678 progDlg.SetPathList(dlg.m_PathList);
2679 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2681 CSendMailCombineable sendMailCombineable(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2682 progDlg.SetSendMailOption(&sendMailCombineable);
2684 progDlg.DoModal();
2686 return true;
2688 return false;
2691 CAppUtils::LaunchAlternativeEditor(tempFileName);
2693 return true;
2696 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2698 CString strDir(szPath);
2699 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2701 strDir.AppendChar(_T('\\'));
2703 std::vector<CString> vPath;
2704 CString strTemp;
2705 bool bSuccess = false;
2707 for (int i=0;i<strDir.GetLength();++i)
2709 if (strDir.GetAt(i) != _T('\\'))
2711 strTemp.AppendChar(strDir.GetAt(i));
2713 else
2715 vPath.push_back(strTemp);
2716 strTemp.AppendChar(_T('\\'));
2720 for (auto vIter = vPath.begin(); vIter != vPath.end(); ++vIter)
2722 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2725 return bSuccess;
2728 void CAppUtils::RemoveTrailSlash(CString &path)
2730 if(path.IsEmpty())
2731 return ;
2733 // For URL, do not trim the slash just after the host name component.
2734 int index = path.Find(_T("://"));
2735 if (index >= 0)
2737 index += 4;
2738 index = path.Find(_T('/'), index);
2739 if (index == path.GetLength() - 1)
2740 return;
2743 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2745 path=path.Left(path.GetLength()-1);
2746 if(path.IsEmpty())
2747 return;
2751 bool CAppUtils::CheckUserData()
2753 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2755 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2757 CTGitPath path(g_Git.m_CurrentDir);
2758 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2759 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2760 dlg.SetTreeWidth(220);
2761 dlg.m_DefaultPage = _T("gitconfig");
2763 dlg.DoModal();
2764 dlg.HandleRestart();
2767 else
2768 return false;
2771 return true;
2774 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2775 CTGitPathList &pathList,
2776 CTGitPathList &selectedList,
2777 bool bSelectFilesForCommit)
2779 bool bFailed = true;
2781 if (!CheckUserData())
2782 return false;
2784 while (bFailed)
2786 bFailed = false;
2787 CCommitDlg dlg;
2788 dlg.m_sBugID = bugid;
2790 dlg.m_bWholeProject = bWholeProject;
2792 dlg.m_sLogMessage = sLogMsg;
2793 dlg.m_pathList = pathList;
2794 dlg.m_checkedPathList = selectedList;
2795 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2796 if (dlg.DoModal() == IDOK)
2798 if (dlg.m_pathList.IsEmpty())
2799 return false;
2800 // if the user hasn't changed the list of selected items
2801 // we don't use that list. Because if we would use the list
2802 // of pre-checked items, the dialog would show different
2803 // checked items on the next startup: it would only try
2804 // to check the parent folder (which might not even show)
2805 // instead, we simply use an empty list and let the
2806 // default checking do its job.
2807 if (!dlg.m_pathList.IsEqual(pathList))
2808 selectedList = dlg.m_pathList;
2809 pathList = dlg.m_updatedPathList;
2810 sLogMsg = dlg.m_sLogMessage;
2811 bSelectFilesForCommit = true;
2813 switch (dlg.m_PostCmd)
2815 case GIT_POSTCOMMIT_CMD_DCOMMIT:
2816 CAppUtils::SVNDCommit();
2817 break;
2818 case GIT_POSTCOMMIT_CMD_PUSH:
2819 CAppUtils::Push();
2820 break;
2821 case GIT_POSTCOMMIT_CMD_CREATETAG:
2822 CAppUtils::CreateBranchTag(TRUE);
2823 break;
2824 case GIT_POSTCOMMIT_CMD_PULL:
2825 CAppUtils::Pull(true);
2826 break;
2827 default:
2828 break;
2831 // CGitProgressDlg progDlg;
2832 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2833 // if (parser.HasVal(_T("closeonend")))
2834 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2835 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2836 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2837 // progDlg.SetPathList(dlg.m_pathList);
2838 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2839 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2840 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2841 // progDlg.SetItemCount(dlg.m_itemsCount);
2842 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2843 // progDlg.DoModal();
2844 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2845 // err = (DWORD)progDlg.DidErrorsOccur();
2846 // bFailed = progDlg.DidErrorsOccur();
2847 // bRet = progDlg.DidErrorsOccur();
2848 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2849 // if (DWORD(bFailRepeat)==0)
2850 // bFailed = false; // do not repeat if the user chose not to in the settings.
2853 return true;
2857 BOOL CAppUtils::SVNDCommit()
2859 CSVNDCommitDlg dcommitdlg;
2860 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2861 if (gitSetting == _T("")) {
2862 if (dcommitdlg.DoModal() != IDOK)
2864 return false;
2866 else
2868 if (dcommitdlg.m_remember)
2870 if (dcommitdlg.m_rmdir)
2872 gitSetting = _T("true");
2874 else
2876 gitSetting = _T("false");
2878 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2880 CString msg;
2881 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2882 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2888 BOOL IsStash = false;
2889 if(!g_Git.CheckCleanWorkTree())
2891 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2893 CSysProgressDlg sysProgressDlg;
2894 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2895 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2896 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2897 sysProgressDlg.SetShowProgressBar(false);
2898 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2899 sysProgressDlg.ShowModeless((HWND)NULL, true);
2901 CString cmd,out;
2902 cmd=_T("git.exe stash");
2903 if (g_Git.Run(cmd, &out, CP_UTF8))
2905 sysProgressDlg.Stop();
2906 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2907 return false;
2909 sysProgressDlg.Stop();
2911 IsStash =true;
2913 else
2915 return false;
2919 CProgressDlg progress;
2920 if (dcommitdlg.m_rmdir)
2922 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2924 else
2926 progress.m_GitCmd=_T("git.exe svn dcommit");
2928 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2930 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
2931 if( IsStash)
2933 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2935 CSysProgressDlg sysProgressDlg;
2936 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2937 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2938 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2939 sysProgressDlg.SetShowProgressBar(false);
2940 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2941 sysProgressDlg.ShowModeless((HWND)NULL, true);
2943 CString cmd,out;
2944 cmd=_T("git.exe stash pop");
2945 if (g_Git.Run(cmd, &out, CP_UTF8))
2947 sysProgressDlg.Stop();
2948 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2949 return false;
2951 sysProgressDlg.Stop();
2953 else
2955 return false;
2958 return TRUE;
2960 return FALSE;
2963 BOOL CAppUtils::Merge(CString *commit)
2965 if (!CheckUserData())
2966 return FALSE;
2968 CMergeDlg dlg;
2969 if(commit)
2970 dlg.m_initialRefName = *commit;
2972 if(dlg.DoModal()==IDOK)
2974 CString cmd;
2975 CString args;
2977 if(dlg.m_bNoFF)
2978 args += _T(" --no-ff");
2980 if(dlg.m_bSquash)
2981 args += _T(" --squash");
2983 if(dlg.m_bNoCommit)
2984 args += _T(" --no-commit");
2986 if (dlg.m_bLog)
2988 CString fmt;
2989 fmt.Format(_T(" --log=%d"), dlg.m_nLog);
2990 args += fmt;
2993 if (!dlg.m_MergeStrategy.IsEmpty())
2995 args += _T(" --strategy=") + dlg.m_MergeStrategy;
2996 if (!dlg.m_StrategyOption.IsEmpty())
2998 args += _T(" --strategy-option=") + dlg.m_StrategyOption;
2999 if (!dlg.m_StrategyParam.IsEmpty())
3000 args += _T("=") + dlg.m_StrategyParam;
3004 if(!dlg.m_strLogMesage.IsEmpty())
3006 CString logmsg = dlg.m_strLogMesage;
3007 logmsg.Replace(_T("\""), _T("\\\""));
3008 args += _T(" -m \"") + logmsg + _T("\"");
3010 cmd.Format(_T("git.exe merge %s %s"), args, g_Git.FixBranchName(dlg.m_VersionName));
3012 CProgressDlg Prodlg;
3013 Prodlg.m_GitCmd = cmd;
3015 BOOL hasGitSVN = CTGitPath(g_Git.m_CurrentDir).GetAdminDirMask() & ITEMIS_GITSVN;
3016 if (dlg.m_bNoCommit)
3017 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
3018 else
3020 if (dlg.m_bIsBranch)
3021 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
3023 if (hasGitSVN)
3024 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUSVNDCOMMIT)));
3027 Prodlg.m_PostCmdCallback = [&] ()
3029 if (Prodlg.m_GitStatus)
3031 Prodlg.m_PostCmdList.RemoveAll();
3033 CTGitPathList list;
3034 if (!g_Git.ListConflictFile(list) && !list.IsEmpty())
3036 // there are conflict files
3037 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROGRS_CMD_RESOLVE)));
3042 INT_PTR ret = Prodlg.DoModal();
3043 if (Prodlg.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3045 CTGitPathList pathlist;
3046 CTGitPathList selectedlist;
3047 pathlist.AddPath(g_Git.m_CurrentDir);
3048 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
3049 CString str;
3050 CAppUtils::Commit(CString(), false, str, pathlist, selectedlist, bSelectFilesForCommit);
3052 else if (ret == IDC_PROGRESS_BUTTON1)
3054 if (dlg.m_bNoCommit)
3056 CString sLogMsg;
3057 CTGitPathList pathList;
3058 CTGitPathList selectedList;
3059 return Commit(_T(""), TRUE, sLogMsg, pathList, selectedList, true);
3061 else if (dlg.m_bIsBranch)
3063 CString msg;
3064 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
3065 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
3067 CString cmd, out;
3068 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
3069 if (g_Git.Run(cmd, &out, CP_UTF8))
3070 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
3073 else if (hasGitSVN)
3074 CAppUtils::SVNDCommit();
3076 else if (ret == IDC_PROGRESS_BUTTON1 + 1 && hasGitSVN)
3077 CAppUtils::SVNDCommit();
3079 return !Prodlg.m_GitStatus;
3081 return false;
3084 BOOL CAppUtils::MergeAbort()
3086 CMergeAbortDlg dlg;
3087 if (dlg.DoModal() == IDOK)
3089 CString cmd;
3090 CString type;
3091 switch (dlg.m_ResetType)
3093 case 0:
3094 type = _T("--mixed");
3095 break;
3096 case 1:
3097 type = _T("--hard");
3098 break;
3099 default:
3100 dlg.m_ResetType = 0;
3101 type = _T("--mixed");
3102 break;
3104 cmd.Format(_T("git.exe reset %s HEAD --"), type);
3106 while (true)
3108 CProgressDlg progress;
3109 progress.m_GitCmd = cmd;
3111 CTGitPath gitPath = g_Git.m_CurrentDir;
3112 if (gitPath.HasSubmodules() && dlg.m_ResetType == 1)
3113 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3115 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
3117 INT_PTR ret;
3118 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
3120 CGitProgressDlg gitdlg;
3121 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
3122 gitdlg.SetRevision(_T("HEAD"));
3123 gitdlg.SetResetType(dlg.m_ResetType + 1);
3124 ret = gitdlg.DoModal();
3126 else
3127 ret = progress.DoModal();
3129 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 1 && ret == IDC_PROGRESS_BUTTON1)
3131 CString sCmd;
3132 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3134 CCommonAppUtils::RunTortoiseGitProc(sCmd);
3135 return TRUE;
3137 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3138 continue; // retry
3139 else if (ret == IDOK)
3140 return TRUE;
3141 else
3142 break;
3145 return FALSE;
3148 void CAppUtils::EditNote(GitRev *rev)
3150 if (!CheckUserData())
3151 return;
3153 CInputDlg dlg;
3154 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3155 dlg.m_sInputText = rev->m_Notes;
3156 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3157 //dlg.m_pProjectProperties = &m_ProjectProperties;
3158 dlg.m_bUseLogWidth = true;
3159 if(dlg.DoModal() == IDOK)
3161 CString cmd,output;
3162 cmd=_T("notes add -f -F \"");
3164 CString tempfile=::GetTempFile();
3165 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
3166 cmd += tempfile;
3167 cmd += _T("\" ");
3168 cmd += rev->m_CommitHash.ToString();
3172 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
3174 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3177 else
3179 rev->m_Notes = dlg.m_sInputText;
3181 }catch(...)
3183 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3185 ::DeleteFile(tempfile);
3190 int CAppUtils::GetMsysgitVersion()
3192 if (g_Git.ms_LastMsysGitVersion)
3193 return g_Git.ms_LastMsysGitVersion;
3195 CString cmd;
3196 CString versiondebug;
3197 CString version;
3199 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3200 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3202 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
3204 __int64 time=0;
3205 if (!g_Git.GetFileModifyTime(gitpath, &time))
3207 if((DWORD)time == regTime)
3209 g_Git.ms_LastMsysGitVersion = regVersion;
3210 return regVersion;
3214 CString err;
3215 cmd = _T("git.exe --version");
3216 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
3218 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);
3219 return -1;
3222 int start=0;
3223 int ver = 0;
3225 versiondebug = version;
3229 CString str=version.Tokenize(_T("."), start);
3230 int space = str.ReverseFind(_T(' '));
3231 str = str.Mid(space+1,start);
3232 ver = _ttol(str);
3233 ver <<=24;
3235 version = version.Mid(start);
3236 start = 0;
3238 str = version.Tokenize(_T("."), start);
3240 ver |= (_ttol(str) & 0xFF) << 16;
3242 str = version.Tokenize(_T("."), start);
3243 ver |= (_ttol(str) & 0xFF) << 8;
3245 str = version.Tokenize(_T("."), start);
3246 ver |= (_ttol(str) & 0xFF);
3248 catch(...)
3250 if (!ver)
3252 CMessageBox::Show(NULL, _T("Could not parse git.exe version number: \"") + versiondebug + _T("\""), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
3253 return -1;
3257 regTime = time&0xFFFFFFFF;
3258 regVersion = ver;
3259 g_Git.ms_LastMsysGitVersion = ver;
3261 return ver;
3264 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
3266 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
3268 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3270 if (hShell.IsValid()) {
3271 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
3272 if (pfnSHGPSFW) {
3273 IPropertyStore *pps;
3274 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
3275 if (SUCCEEDED(hr)) {
3276 PROPVARIANT var;
3277 var.vt = VT_BOOL;
3278 var.boolVal = VARIANT_TRUE;
3279 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
3280 pps->Release();
3286 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
3288 ASSERT(dialogname.GetLength() < 70);
3289 ASSERT(urlorpath.GetLength() < MAX_PATH);
3290 WCHAR pathbuf[MAX_PATH] = {0};
3292 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
3294 wcscat_s(pathbuf, L" - ");
3295 wcscat_s(pathbuf, dialogname);
3296 wcscat_s(pathbuf, L" - ");
3297 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
3298 SetWindowText(hWnd, pathbuf);
3301 bool CAppUtils::BisectStart(CString lastGood, CString firstBad)
3303 if (!g_Git.CheckCleanWorkTree())
3305 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
3307 CSysProgressDlg sysProgressDlg;
3308 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
3309 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
3310 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
3311 sysProgressDlg.SetShowProgressBar(false);
3312 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
3313 sysProgressDlg.ShowModeless((HWND)NULL, true);
3315 CString cmd, out;
3316 cmd = _T("git.exe stash");
3317 if (g_Git.Run(cmd, &out, CP_UTF8))
3319 sysProgressDlg.Stop();
3320 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK);
3321 return false;
3323 sysProgressDlg.Stop();
3325 else
3326 return false;
3329 CBisectStartDlg bisectStartDlg;
3331 if (!lastGood.IsEmpty())
3332 bisectStartDlg.m_sLastGood = lastGood;
3333 if (!firstBad.IsEmpty())
3334 bisectStartDlg.m_sFirstBad = firstBad;
3336 if (bisectStartDlg.DoModal() == IDOK)
3338 CProgressDlg progress;
3339 theApp.m_pMainWnd = &progress;
3340 progress.m_GitCmdList.push_back(_T("git.exe bisect start"));
3341 progress.m_GitCmdList.push_back(_T("git.exe bisect good ") + bisectStartDlg.m_LastGoodRevision);
3342 progress.m_GitCmdList.push_back(_T("git.exe bisect bad ") + bisectStartDlg.m_FirstBadRevision);
3344 CTGitPath path(g_Git.m_CurrentDir);
3346 if (path.HasSubmodules())
3347 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3349 INT_PTR ret = progress.DoModal();
3350 if (path.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
3352 CString sCmd;
3353 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3355 CAppUtils::RunTortoiseGitProc(sCmd);
3356 return true;
3358 else if (ret == IDOK)
3359 return true;
3362 return false;
3365 int CAppUtils::Git2GetUserPassword(git_cred **out, const char *url, const char *username_from_url, unsigned int /*allowed_types*/, void * /*payload*/)
3367 CUserPassword dlg;
3368 dlg.m_URL = CUnicodeUtils::GetUnicode(url, CP_UTF8);
3369 if (username_from_url)
3370 dlg.m_UserName = CUnicodeUtils::GetUnicode(username_from_url, CP_UTF8);
3372 CStringA username, password;
3373 if (dlg.DoModal() == IDOK)
3375 username = CUnicodeUtils::GetMulti(dlg.m_UserName, CP_UTF8);
3376 password = CUnicodeUtils::GetMulti(dlg.m_Password, CP_UTF8);
3377 return git_cred_userpass_plaintext_new(out, username, password);
3379 return -1;