Unbreak /logmsg and /logmsgfile params of commit command
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob06436f3052cf7cd6f3c2220f0729cff01254cf5e
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;
2212 if (dlg.m_bRebase)
2213 cmdRebase = "--rebase ";
2215 if (!dlg.m_bFetchTags)
2216 notags = _T("--no-tags");
2218 if (dlg.m_bNoFF)
2219 noff=_T("--no-ff");
2221 if (dlg.m_bFFonly)
2222 ffonly = _T("--ff-only");
2224 if (dlg.m_bSquash)
2225 squash = _T("--squash");
2227 if (dlg.m_bNoCommit)
2228 nocommit = _T("--no-commit");
2230 int ver = CAppUtils::GetMsysgitVersion();
2232 if(ver >= 0x01070203) //above 1.7.0.2
2233 cmdRebase += _T("--progress ");
2235 cmd.Format(_T("git.exe pull -v %s %s %s %s %s %s \"%s\" %s"), cmdRebase, noff, ffonly, squash, nocommit, notags, url, dlg.m_RemoteBranchName);
2236 CProgressDlg progress;
2237 progress.m_GitCmd = cmd;
2238 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_DIFFS)));
2239 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_LOG)));
2240 INT_PTR pushButton = showPush ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH))) + IDC_PROGRESS_BUTTON1 : -1;
2242 CTGitPath gitPath = g_Git.m_CurrentDir;
2243 INT_PTR smUpdateButton = gitPath.HasSubmodules() ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE))) + IDC_PROGRESS_BUTTON1 : -1;
2245 INT_PTR ret = progress.DoModal();
2247 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)
2249 CChangedDlg dlg;
2250 dlg.m_pathList.AddPath(CTGitPath());
2251 dlg.DoModal();
2253 return true;
2256 CGitHash hashNew;
2257 if (g_Git.GetHash(hashNew, _T("HEAD")))
2259 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR);
2260 return FALSE;
2263 if( ret == IDC_PROGRESS_BUTTON1)
2265 if(hashOld == hashNew)
2267 if(progress.m_GitStatus == 0)
2268 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2269 return TRUE;
2272 CFileDiffDlg dlg;
2273 dlg.SetDiff(NULL, hashNew.ToString(), hashOld.ToString());
2274 dlg.DoModal();
2276 return true;
2278 else if ( ret == IDC_PROGRESS_BUTTON1 +1 )
2280 if(hashOld == hashNew)
2282 if(progress.m_GitStatus == 0)
2283 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2284 return true;
2287 CLogDlg dlg;
2288 dlg.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld.ToString() + _T("..") + hashNew.ToString(), 0);
2289 dlg.DoModal();
2291 else if (ret == pushButton)
2293 Push(_T(""));
2294 return true;
2296 else if (ret == smUpdateButton)
2298 CString sCmd;
2299 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
2301 CAppUtils::RunTortoiseGitProc(sCmd);
2302 return true;
2306 return true;
2309 bool CAppUtils::Fetch(CString remoteName, bool allowRebase, bool allRemotes)
2311 CPullFetchDlg dlg;
2312 dlg.m_PreSelectRemote = remoteName;
2313 dlg.m_bAllowRebase = allowRebase;
2314 dlg.m_IsPull=FALSE;
2315 dlg.m_bAllRemotes = allRemotes;
2317 if(dlg.DoModal()==IDOK)
2319 if(dlg.m_bAutoLoad)
2321 if (dlg.m_bAllRemotes)
2323 STRING_VECTOR list;
2324 g_Git.GetRemoteList(list);
2326 STRING_VECTOR::const_iterator it = list.begin();
2327 while (it != list.end())
2329 CString remote(*it);
2330 CAppUtils::LaunchPAgent(NULL, &remote);
2331 ++it;
2334 else
2335 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2338 CString url;
2339 url=dlg.m_RemoteURL;
2340 CString cmd;
2341 CString arg;
2343 int ver = CAppUtils::GetMsysgitVersion();
2345 if(ver >= 0x01070203) //above 1.7.0.2
2346 arg = _T("--progress ");
2348 if (dlg.m_bPrune == TRUE)
2349 arg += _T("--prune ");
2350 else if (dlg.m_bPrune == FALSE && ver >= 0x01080500)
2351 arg += _T("--no-prune ");
2353 if (dlg.m_bFetchTags == 1)
2355 arg += _T("--tags ");
2357 else if (dlg.m_bFetchTags == 0)
2359 arg += _T("--no-tags ");
2362 if (dlg.m_bAllRemotes)
2363 cmd.Format(_T("git.exe fetch --all -v %s"), arg);
2364 else
2365 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"), arg, url, dlg.m_RemoteBranchName);
2367 CProgressDlg progress;
2369 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2370 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_RESET)));
2372 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
2374 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2377 progress.m_GitCmd=cmd;
2378 INT_PTR userResponse;
2380 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
2382 CGitProgressDlg gitdlg;
2383 if (!dlg.m_bAllRemotes)
2384 gitdlg.SetUrl(url);
2385 gitdlg.SetCommand(CGitProgressList::GitProgress_Fetch);
2386 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);
2387 if (!dlg.m_bAllRemotes)
2388 gitdlg.SetRefSpec(dlg.m_RemoteBranchName);
2389 userResponse = gitdlg.DoModal();
2392 else
2393 userResponse = progress.DoModal();
2395 if (userResponse == IDC_PROGRESS_BUTTON1)
2397 CString cmd = _T("/command:log");
2398 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2399 RunTortoiseGitProc(cmd);
2400 return TRUE;
2402 else if (userResponse == IDC_PROGRESS_BUTTON1 + 1)
2404 CString pullRemote, pullBranch;
2405 g_Git.GetRemoteTrackedBranchForHEAD(pullRemote, pullBranch);
2407 CString defaultUpstream;
2408 if (!pullRemote.IsEmpty() && !pullBranch.IsEmpty())
2409 defaultUpstream.Format(_T("remotes/%s/%s"), pullRemote, pullBranch);
2410 GitReset(&defaultUpstream, 2);
2411 return TRUE;
2413 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 2) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
2415 while(1)
2417 CRebaseDlg dlg;
2418 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2419 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
2420 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2421 INT_PTR response = dlg.DoModal();
2422 if(response == IDOK)
2424 return TRUE;
2426 else if (response == IDC_REBASE_POST_BUTTON)
2428 CString cmd = _T("/command:log");
2429 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2430 RunTortoiseGitProc(cmd);
2431 return TRUE;
2433 else if (response == IDC_REBASE_POST_BUTTON + 1)
2435 CString cmd, out, err;
2436 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2437 g_Git.m_CurrentDir,
2438 g_Git.FixBranchName(dlg.m_Upstream),
2439 g_Git.FixBranchName(dlg.m_Branch));
2440 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
2442 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2443 return FALSE;
2446 CAppUtils::SendPatchMail(cmd,out);
2447 return TRUE;
2449 else if (response == IDC_REBASE_POST_BUTTON + 2)
2450 continue;
2451 else if(response == IDCANCEL)
2452 return FALSE;
2454 return TRUE;
2456 else if (userResponse != IDCANCEL)
2457 return TRUE;
2459 return FALSE;
2462 bool CAppUtils::Push(CString selectLocalBranch)
2464 CPushDlg dlg;
2465 dlg.m_BranchSourceName = selectLocalBranch;
2466 CString error;
2467 DWORD exitcode = 0xFFFFFFFF;
2468 CTGitPathList list;
2469 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
2470 if (CHooks::Instance().PrePush(list,exitcode, error))
2472 if (exitcode)
2474 CString temp;
2475 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2476 //ReportError(temp);
2477 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2478 return false;
2482 if(dlg.DoModal()==IDOK)
2484 CString arg;
2486 if(dlg.m_bPack)
2487 arg += _T("--thin ");
2488 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
2489 arg += _T("--tags ");
2490 if(dlg.m_bForce)
2491 arg += _T("--force ");
2492 if (dlg.m_bSetUpstream)
2493 arg += _T("--set-upstream ");
2494 if (dlg.m_RecurseSubmodules == 1)
2495 arg += _T("--recurse-submodules=check ");
2496 if (dlg.m_RecurseSubmodules == 2)
2497 arg += _T("--recurse-submodules=on-demand ");
2499 int ver = CAppUtils::GetMsysgitVersion();
2501 if(ver >= 0x01070203) //above 1.7.0.2
2502 arg += _T("--progress ");
2504 CProgressDlg progress;
2506 STRING_VECTOR remotesList;
2507 if (dlg.m_bPushAllRemotes)
2508 g_Git.GetRemoteList(remotesList);
2509 else
2510 remotesList.push_back(dlg.m_URL);
2512 for (unsigned int i = 0; i < remotesList.size(); ++i)
2514 if (dlg.m_bAutoLoad)
2515 CAppUtils::LaunchPAgent(NULL, &remotesList[i]);
2517 CString cmd;
2518 if (dlg.m_bPushAllBranches)
2520 cmd.Format(_T("git.exe push --all %s \"%s\""),
2521 arg,
2522 remotesList[i]);
2524 if (dlg.m_bTags)
2526 progress.m_GitCmdList.push_back(cmd);
2527 cmd.Format(_T("git.exe push --tags %s \"%s\""), arg, remotesList[i]);
2530 else
2532 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2533 arg,
2534 remotesList[i],
2535 dlg.m_BranchSourceName);
2536 if (!dlg.m_BranchRemoteName.IsEmpty())
2538 cmd += _T(":") + dlg.m_BranchRemoteName;
2541 progress.m_GitCmdList.push_back(cmd);
2544 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2545 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2546 bool rejected = false;
2547 progress.m_PostCmdCallback = [&] ()
2549 if (progress.m_GitStatus)
2551 progress.m_PostCmdList.RemoveAll();
2552 rejected = progress.GetLogText().Find(_T("! [rejected]")) > 0;
2553 if (rejected)
2555 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
2556 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUFETCH)));
2558 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2562 INT_PTR ret = progress.DoModal();
2564 if(!progress.m_GitStatus)
2566 if (CHooks::Instance().PostPush(list,exitcode, error))
2568 if (exitcode)
2570 CString temp;
2571 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2572 //ReportError(temp);
2573 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2574 return false;
2577 if(ret == IDC_PROGRESS_BUTTON1)
2579 RequestPull(dlg.m_BranchRemoteName);
2581 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2583 Push(selectLocalBranch);
2585 return TRUE;
2587 else
2589 if (rejected)
2591 // failed, pull first
2592 if (ret == IDC_PROGRESS_BUTTON1)
2594 Pull(true);
2596 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
2598 Fetch(dlg.m_bPushAllRemotes ? _T("") : dlg.m_URL, true, !!dlg.m_bPushAllRemotes);
2600 else if (ret == IDC_PROGRESS_BUTTON1 + 2)
2602 Push(selectLocalBranch);
2605 else
2607 if (ret == IDC_PROGRESS_BUTTON1)
2609 Push(selectLocalBranch);
2614 return FALSE;
2617 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2619 CRequestPullDlg dlg;
2620 dlg.m_RepositoryURL = repositoryUrl;
2621 dlg.m_EndRevision = endrevision;
2622 if (dlg.DoModal()==IDOK)
2624 CString cmd;
2625 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2627 CSysProgressDlg sysProgressDlg;
2628 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2629 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST)));
2630 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2631 sysProgressDlg.SetShowProgressBar(false);
2632 sysProgressDlg.ShowModeless((HWND)NULL, true);
2634 CString tempFileName = GetTempFile();
2635 CString err;
2636 DeleteFile(tempFileName);
2637 CreateDirectory(tempFileName, NULL);
2638 tempFileName += _T("\\pullrequest.txt");
2639 if (g_Git.RunLogFile(cmd, tempFileName, &err))
2641 CString msg;
2642 msg.LoadString(IDS_ERR_PULLREUQESTFAILED);
2643 CMessageBox::Show(NULL, msg + _T("\n") + err, _T("TortoiseGit"), MB_OK);
2644 return false;
2647 if (sysProgressDlg.HasUserCancelled())
2649 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
2650 ::DeleteFile(tempFileName);
2651 return false;
2654 sysProgressDlg.Stop();
2656 if (dlg.m_bSendMail)
2658 CSendMailDlg dlg;
2659 dlg.m_PathList = CTGitPathList(CTGitPath(tempFileName));
2660 dlg.m_bCustomSubject = true;
2662 if (dlg.DoModal() == IDOK)
2664 if (dlg.m_PathList.IsEmpty())
2665 return FALSE;
2667 CGitProgressDlg progDlg;
2669 theApp.m_pMainWnd = &progDlg;
2670 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2672 progDlg.SetPathList(dlg.m_PathList);
2673 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2675 CSendMailCombineable sendMailCombineable(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2676 progDlg.SetSendMailOption(&sendMailCombineable);
2678 progDlg.DoModal();
2680 return true;
2682 return false;
2685 CAppUtils::LaunchAlternativeEditor(tempFileName);
2687 return true;
2690 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2692 CString strDir(szPath);
2693 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2695 strDir.AppendChar(_T('\\'));
2697 std::vector<CString> vPath;
2698 CString strTemp;
2699 bool bSuccess = false;
2701 for (int i=0;i<strDir.GetLength();++i)
2703 if (strDir.GetAt(i) != _T('\\'))
2705 strTemp.AppendChar(strDir.GetAt(i));
2707 else
2709 vPath.push_back(strTemp);
2710 strTemp.AppendChar(_T('\\'));
2714 for (auto vIter = vPath.begin(); vIter != vPath.end(); ++vIter)
2716 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2719 return bSuccess;
2722 void CAppUtils::RemoveTrailSlash(CString &path)
2724 if(path.IsEmpty())
2725 return ;
2727 // For URL, do not trim the slash just after the host name component.
2728 int index = path.Find(_T("://"));
2729 if (index >= 0)
2731 index += 4;
2732 index = path.Find(_T('/'), index);
2733 if (index == path.GetLength() - 1)
2734 return;
2737 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2739 path=path.Left(path.GetLength()-1);
2740 if(path.IsEmpty())
2741 return;
2745 bool CAppUtils::CheckUserData()
2747 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2749 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2751 CTGitPath path(g_Git.m_CurrentDir);
2752 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2753 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2754 dlg.SetTreeWidth(220);
2755 dlg.m_DefaultPage = _T("gitconfig");
2757 dlg.DoModal();
2758 dlg.HandleRestart();
2761 else
2762 return false;
2765 return true;
2768 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2769 CTGitPathList &pathList,
2770 CTGitPathList &selectedList,
2771 bool bSelectFilesForCommit)
2773 bool bFailed = true;
2775 if (!CheckUserData())
2776 return false;
2778 while (bFailed)
2780 bFailed = false;
2781 CCommitDlg dlg;
2782 dlg.m_sBugID = bugid;
2784 dlg.m_bWholeProject = bWholeProject;
2786 dlg.m_sLogMessage = sLogMsg;
2787 dlg.m_pathList = pathList;
2788 dlg.m_checkedPathList = selectedList;
2789 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2790 if (dlg.DoModal() == IDOK)
2792 if (dlg.m_pathList.IsEmpty())
2793 return false;
2794 // if the user hasn't changed the list of selected items
2795 // we don't use that list. Because if we would use the list
2796 // of pre-checked items, the dialog would show different
2797 // checked items on the next startup: it would only try
2798 // to check the parent folder (which might not even show)
2799 // instead, we simply use an empty list and let the
2800 // default checking do its job.
2801 if (!dlg.m_pathList.IsEqual(pathList))
2802 selectedList = dlg.m_pathList;
2803 pathList = dlg.m_updatedPathList;
2804 sLogMsg = dlg.m_sLogMessage;
2805 bSelectFilesForCommit = true;
2807 switch (dlg.m_PostCmd)
2809 case GIT_POSTCOMMIT_CMD_DCOMMIT:
2810 CAppUtils::SVNDCommit();
2811 break;
2812 case GIT_POSTCOMMIT_CMD_PUSH:
2813 CAppUtils::Push();
2814 break;
2815 case GIT_POSTCOMMIT_CMD_CREATETAG:
2816 CAppUtils::CreateBranchTag(TRUE);
2817 break;
2818 case GIT_POSTCOMMIT_CMD_PULL:
2819 CAppUtils::Pull(true);
2820 break;
2821 default:
2822 break;
2825 // CGitProgressDlg progDlg;
2826 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2827 // if (parser.HasVal(_T("closeonend")))
2828 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2829 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2830 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2831 // progDlg.SetPathList(dlg.m_pathList);
2832 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2833 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2834 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2835 // progDlg.SetItemCount(dlg.m_itemsCount);
2836 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2837 // progDlg.DoModal();
2838 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2839 // err = (DWORD)progDlg.DidErrorsOccur();
2840 // bFailed = progDlg.DidErrorsOccur();
2841 // bRet = progDlg.DidErrorsOccur();
2842 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2843 // if (DWORD(bFailRepeat)==0)
2844 // bFailed = false; // do not repeat if the user chose not to in the settings.
2847 return true;
2851 BOOL CAppUtils::SVNDCommit()
2853 CSVNDCommitDlg dcommitdlg;
2854 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2855 if (gitSetting == _T("")) {
2856 if (dcommitdlg.DoModal() != IDOK)
2858 return false;
2860 else
2862 if (dcommitdlg.m_remember)
2864 if (dcommitdlg.m_rmdir)
2866 gitSetting = _T("true");
2868 else
2870 gitSetting = _T("false");
2872 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2874 CString msg;
2875 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2876 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2882 BOOL IsStash = false;
2883 if(!g_Git.CheckCleanWorkTree())
2885 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2887 CSysProgressDlg sysProgressDlg;
2888 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2889 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2890 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2891 sysProgressDlg.SetShowProgressBar(false);
2892 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2893 sysProgressDlg.ShowModeless((HWND)NULL, true);
2895 CString cmd,out;
2896 cmd=_T("git.exe stash");
2897 if (g_Git.Run(cmd, &out, CP_UTF8))
2899 sysProgressDlg.Stop();
2900 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2901 return false;
2903 sysProgressDlg.Stop();
2905 IsStash =true;
2907 else
2909 return false;
2913 CProgressDlg progress;
2914 if (dcommitdlg.m_rmdir)
2916 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2918 else
2920 progress.m_GitCmd=_T("git.exe svn dcommit");
2922 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2924 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
2925 if( IsStash)
2927 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2929 CSysProgressDlg sysProgressDlg;
2930 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2931 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2932 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2933 sysProgressDlg.SetShowProgressBar(false);
2934 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2935 sysProgressDlg.ShowModeless((HWND)NULL, true);
2937 CString cmd,out;
2938 cmd=_T("git.exe stash pop");
2939 if (g_Git.Run(cmd, &out, CP_UTF8))
2941 sysProgressDlg.Stop();
2942 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2943 return false;
2945 sysProgressDlg.Stop();
2947 else
2949 return false;
2952 return TRUE;
2954 return FALSE;
2957 BOOL CAppUtils::Merge(CString *commit)
2959 if (!CheckUserData())
2960 return FALSE;
2962 CMergeDlg dlg;
2963 if(commit)
2964 dlg.m_initialRefName = *commit;
2966 if(dlg.DoModal()==IDOK)
2968 CString cmd;
2969 CString args;
2971 if(dlg.m_bNoFF)
2972 args += _T(" --no-ff");
2974 if(dlg.m_bSquash)
2975 args += _T(" --squash");
2977 if(dlg.m_bNoCommit)
2978 args += _T(" --no-commit");
2980 if (dlg.m_bLog)
2982 CString fmt;
2983 fmt.Format(_T(" --log=%d"), dlg.m_nLog);
2984 args += fmt;
2987 if (!dlg.m_MergeStrategy.IsEmpty())
2989 args += _T(" --strategy=") + dlg.m_MergeStrategy;
2990 if (!dlg.m_StrategyOption.IsEmpty())
2992 args += _T(" --strategy-option=") + dlg.m_StrategyOption;
2993 if (!dlg.m_StrategyParam.IsEmpty())
2994 args += _T("=") + dlg.m_StrategyParam;
2998 if(!dlg.m_strLogMesage.IsEmpty())
3000 CString logmsg = dlg.m_strLogMesage;
3001 logmsg.Replace(_T("\""), _T("\\\""));
3002 args += _T(" -m \"") + logmsg + _T("\"");
3004 cmd.Format(_T("git.exe merge %s %s"), args, g_Git.FixBranchName(dlg.m_VersionName));
3006 CProgressDlg Prodlg;
3007 Prodlg.m_GitCmd = cmd;
3009 BOOL hasGitSVN = CTGitPath(g_Git.m_CurrentDir).GetAdminDirMask() & ITEMIS_GITSVN;
3010 if (dlg.m_bNoCommit)
3011 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
3012 else
3014 if (dlg.m_bIsBranch)
3015 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
3017 if (hasGitSVN)
3018 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUSVNDCOMMIT)));
3021 Prodlg.m_PostCmdCallback = [&] ()
3023 if (Prodlg.m_GitStatus)
3025 Prodlg.m_PostCmdList.RemoveAll();
3027 CTGitPathList list;
3028 if (!g_Git.ListConflictFile(list) && !list.IsEmpty())
3030 // there are conflict files
3031 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROGRS_CMD_RESOLVE)));
3036 INT_PTR ret = Prodlg.DoModal();
3037 if (Prodlg.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3039 CTGitPathList pathlist;
3040 CTGitPathList selectedlist;
3041 pathlist.AddPath(g_Git.m_CurrentDir);
3042 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
3043 CString str;
3044 CAppUtils::Commit(CString(), false, str, pathlist, selectedlist, bSelectFilesForCommit);
3046 else if (ret == IDC_PROGRESS_BUTTON1)
3048 if (dlg.m_bNoCommit)
3050 CString sLogMsg;
3051 CTGitPathList pathList;
3052 CTGitPathList selectedList;
3053 return Commit(_T(""), TRUE, sLogMsg, pathList, selectedList, true);
3055 else if (dlg.m_bIsBranch)
3057 CString msg;
3058 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
3059 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
3061 CString cmd, out;
3062 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
3063 if (g_Git.Run(cmd, &out, CP_UTF8))
3064 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
3067 else if (hasGitSVN)
3068 CAppUtils::SVNDCommit();
3070 else if (ret == IDC_PROGRESS_BUTTON1 + 1 && hasGitSVN)
3071 CAppUtils::SVNDCommit();
3073 return !Prodlg.m_GitStatus;
3075 return false;
3078 BOOL CAppUtils::MergeAbort()
3080 CMergeAbortDlg dlg;
3081 if (dlg.DoModal() == IDOK)
3083 CString cmd;
3084 CString type;
3085 switch (dlg.m_ResetType)
3087 case 0:
3088 type = _T("--mixed");
3089 break;
3090 case 1:
3091 type = _T("--hard");
3092 break;
3093 default:
3094 dlg.m_ResetType = 0;
3095 type = _T("--mixed");
3096 break;
3098 cmd.Format(_T("git.exe reset %s HEAD --"), type);
3100 while (true)
3102 CProgressDlg progress;
3103 progress.m_GitCmd = cmd;
3105 CTGitPath gitPath = g_Git.m_CurrentDir;
3106 if (gitPath.HasSubmodules() && dlg.m_ResetType == 1)
3107 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3109 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
3111 INT_PTR ret;
3112 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
3114 CGitProgressDlg gitdlg;
3115 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
3116 gitdlg.SetRevision(_T("HEAD"));
3117 gitdlg.SetResetType(dlg.m_ResetType + 1);
3118 ret = gitdlg.DoModal();
3120 else
3121 ret = progress.DoModal();
3123 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 1 && ret == IDC_PROGRESS_BUTTON1)
3125 CString sCmd;
3126 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3128 CCommonAppUtils::RunTortoiseGitProc(sCmd);
3129 return TRUE;
3131 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3132 continue; // retry
3133 else if (ret == IDOK)
3134 return TRUE;
3135 else
3136 break;
3139 return FALSE;
3142 void CAppUtils::EditNote(GitRev *rev)
3144 if (!CheckUserData())
3145 return;
3147 CInputDlg dlg;
3148 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3149 dlg.m_sInputText = rev->m_Notes;
3150 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3151 //dlg.m_pProjectProperties = &m_ProjectProperties;
3152 dlg.m_bUseLogWidth = true;
3153 if(dlg.DoModal() == IDOK)
3155 CString cmd,output;
3156 cmd=_T("notes add -f -F \"");
3158 CString tempfile=::GetTempFile();
3159 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
3160 cmd += tempfile;
3161 cmd += _T("\" ");
3162 cmd += rev->m_CommitHash.ToString();
3166 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
3168 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3171 else
3173 rev->m_Notes = dlg.m_sInputText;
3175 }catch(...)
3177 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3179 ::DeleteFile(tempfile);
3184 int CAppUtils::GetMsysgitVersion()
3186 if (g_Git.ms_LastMsysGitVersion)
3187 return g_Git.ms_LastMsysGitVersion;
3189 CString cmd;
3190 CString versiondebug;
3191 CString version;
3193 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3194 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3196 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
3198 __int64 time=0;
3199 if (!g_Git.GetFileModifyTime(gitpath, &time))
3201 if((DWORD)time == regTime)
3203 g_Git.ms_LastMsysGitVersion = regVersion;
3204 return regVersion;
3208 CString err;
3209 cmd = _T("git.exe --version");
3210 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
3212 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);
3213 return -1;
3216 int start=0;
3217 int ver = 0;
3219 versiondebug = version;
3223 CString str=version.Tokenize(_T("."), start);
3224 int space = str.ReverseFind(_T(' '));
3225 str = str.Mid(space+1,start);
3226 ver = _ttol(str);
3227 ver <<=24;
3229 version = version.Mid(start);
3230 start = 0;
3232 str = version.Tokenize(_T("."), start);
3234 ver |= (_ttol(str) & 0xFF) << 16;
3236 str = version.Tokenize(_T("."), start);
3237 ver |= (_ttol(str) & 0xFF) << 8;
3239 str = version.Tokenize(_T("."), start);
3240 ver |= (_ttol(str) & 0xFF);
3242 catch(...)
3244 if (!ver)
3246 CMessageBox::Show(NULL, _T("Could not parse git.exe version number: \"") + versiondebug + _T("\""), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
3247 return -1;
3251 regTime = time&0xFFFFFFFF;
3252 regVersion = ver;
3253 g_Git.ms_LastMsysGitVersion = ver;
3255 return ver;
3258 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
3260 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
3262 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3264 if (hShell.IsValid()) {
3265 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
3266 if (pfnSHGPSFW) {
3267 IPropertyStore *pps;
3268 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
3269 if (SUCCEEDED(hr)) {
3270 PROPVARIANT var;
3271 var.vt = VT_BOOL;
3272 var.boolVal = VARIANT_TRUE;
3273 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
3274 pps->Release();
3280 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
3282 ASSERT(dialogname.GetLength() < 70);
3283 ASSERT(urlorpath.GetLength() < MAX_PATH);
3284 WCHAR pathbuf[MAX_PATH] = {0};
3286 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
3288 wcscat_s(pathbuf, L" - ");
3289 wcscat_s(pathbuf, dialogname);
3290 wcscat_s(pathbuf, L" - ");
3291 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
3292 SetWindowText(hWnd, pathbuf);
3295 bool CAppUtils::BisectStart(CString lastGood, CString firstBad)
3297 if (!g_Git.CheckCleanWorkTree())
3299 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
3301 CSysProgressDlg sysProgressDlg;
3302 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
3303 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
3304 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
3305 sysProgressDlg.SetShowProgressBar(false);
3306 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
3307 sysProgressDlg.ShowModeless((HWND)NULL, true);
3309 CString cmd, out;
3310 cmd = _T("git.exe stash");
3311 if (g_Git.Run(cmd, &out, CP_UTF8))
3313 sysProgressDlg.Stop();
3314 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK);
3315 return false;
3317 sysProgressDlg.Stop();
3319 else
3320 return false;
3323 CBisectStartDlg bisectStartDlg;
3325 if (!lastGood.IsEmpty())
3326 bisectStartDlg.m_sLastGood = lastGood;
3327 if (!firstBad.IsEmpty())
3328 bisectStartDlg.m_sFirstBad = firstBad;
3330 if (bisectStartDlg.DoModal() == IDOK)
3332 CProgressDlg progress;
3333 theApp.m_pMainWnd = &progress;
3334 progress.m_GitCmdList.push_back(_T("git.exe bisect start"));
3335 progress.m_GitCmdList.push_back(_T("git.exe bisect good ") + bisectStartDlg.m_LastGoodRevision);
3336 progress.m_GitCmdList.push_back(_T("git.exe bisect bad ") + bisectStartDlg.m_FirstBadRevision);
3338 CTGitPath path(g_Git.m_CurrentDir);
3340 if (path.HasSubmodules())
3341 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3343 INT_PTR ret = progress.DoModal();
3344 if (path.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
3346 CString sCmd;
3347 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3349 CAppUtils::RunTortoiseGitProc(sCmd);
3350 return true;
3352 else if (ret == IDOK)
3353 return true;
3356 return false;
3359 int CAppUtils::Git2GetUserPassword(git_cred **out, const char *url, const char *username_from_url, unsigned int /*allowed_types*/, void * /*payload*/)
3361 CUserPassword dlg;
3362 dlg.m_URL = CUnicodeUtils::GetUnicode(url, CP_UTF8);
3363 if (username_from_url)
3364 dlg.m_UserName = CUnicodeUtils::GetUnicode(username_from_url, CP_UTF8);
3366 CStringA username, password;
3367 if (dlg.DoModal() == IDOK)
3369 username = CUnicodeUtils::GetMulti(dlg.m_UserName, CP_UTF8);
3370 password = CUnicodeUtils::GetMulti(dlg.m_Password, CP_UTF8);
3371 return git_cred_userpass_plaintext_new(out, username, password);
3373 return -1;