Allow stash message to start with a dash
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob0a0532a0cde211b620ee781ff32c4262dcaf541a
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)
213 CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));
214 CString ext = mergedfile.GetFileExtension();
215 CString com = regCom;
216 bool bInternal = false;
218 if (!ext.IsEmpty())
220 // is there an extension specific merge tool?
221 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());
222 if (!CString(mergetool).IsEmpty())
224 com = mergetool;
227 // is there a filename specific merge tool?
228 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\.") + mergedfile.GetFilename().MakeLower());
229 if (!CString(mergetool).IsEmpty())
231 com = mergetool;
234 if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))
236 // Maybe we should use TortoiseIDiff?
237 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
238 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
239 (ext == _T(".png")) || (ext == _T(".ico")) ||
240 (ext == _T(".tif")) || (ext == _T(".tiff")) ||
241 (ext == _T(".dib")) || (ext == _T(".emf")))
243 com = CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe");
244 com = _T("\"") + com + _T("\"");
245 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /result:%merged");
246 com = com + _T(" /basetitle:%bname /theirstitle:%tname /minetitle:%yname");
248 else
250 // use TortoiseGitMerge
251 bInternal = true;
252 CRegString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE);
253 com = tortoiseMergePath;
254 if (com.IsEmpty())
256 com = CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe");
258 com = _T("\"") + com + _T("\"");
259 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
260 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
261 com += _T(" /saverequired");
263 if (!g_sGroupingUUID.IsEmpty())
265 com += L" /groupuuid:\"";
266 com += g_sGroupingUUID;
267 com += L"\"";
270 // check if the params are set. If not, just add the files to the command line
271 if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))
273 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");
274 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");
275 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");
276 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");
278 if (basefile.IsEmpty())
280 com.Replace(_T("/base:%base"), _T(""));
281 com.Replace(_T("%base"), _T(""));
283 else
284 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));
285 if (theirfile.IsEmpty())
287 com.Replace(_T("/theirs:%theirs"), _T(""));
288 com.Replace(_T("%theirs"), _T(""));
290 else
291 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));
292 if (yourfile.IsEmpty())
294 com.Replace(_T("/mine:%mine"), _T(""));
295 com.Replace(_T("%mine"), _T(""));
297 else
298 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));
299 if (mergedfile.IsEmpty())
301 com.Replace(_T("/merged:%merged"), _T(""));
302 com.Replace(_T("%merged"), _T(""));
304 else
305 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));
306 if (basename.IsEmpty())
308 if (basefile.IsEmpty())
310 com.Replace(_T("/basename:%bname"), _T(""));
311 com.Replace(_T("%bname"), _T(""));
313 else
315 com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));
318 else
319 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));
320 if (theirname.IsEmpty())
322 if (theirfile.IsEmpty())
324 com.Replace(_T("/theirsname:%tname"), _T(""));
325 com.Replace(_T("%tname"), _T(""));
327 else
329 com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));
332 else
333 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));
334 if (yourname.IsEmpty())
336 if (yourfile.IsEmpty())
338 com.Replace(_T("/minename:%yname"), _T(""));
339 com.Replace(_T("%yname"), _T(""));
341 else
343 com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));
346 else
347 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));
348 if (mergedname.IsEmpty())
350 if (mergedfile.IsEmpty())
352 com.Replace(_T("/mergedname:%mname"), _T(""));
353 com.Replace(_T("%mname"), _T(""));
355 else
357 com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));
360 else
361 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));
363 if ((bReadOnly)&&(bInternal))
364 com += _T(" /readonly");
366 if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))
368 return FALSE;
371 return TRUE;
374 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)
376 CString viewer;
377 // use TortoiseGitMerge
378 viewer = CPathUtils::GetAppDirectory();
379 viewer += _T("TortoiseGitMerge.exe");
381 viewer = _T("\"") + viewer + _T("\"");
382 viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");
383 viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");
384 if (bReversed)
385 viewer += _T(" /reversedpatch");
386 if (!sOriginalDescription.IsEmpty())
387 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");
388 if (!sPatchedDescription.IsEmpty())
389 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");
390 if (!g_sGroupingUUID.IsEmpty())
392 viewer += L" /groupuuid:\"";
393 viewer += g_sGroupingUUID;
394 viewer += L"\"";
396 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
398 return FALSE;
400 return TRUE;
403 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)
405 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file2.GetFilename().MakeLower());
406 if (!difftool.IsEmpty())
407 return difftool;
408 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file1.GetFilename().MakeLower());
409 if (!difftool.IsEmpty())
410 return difftool;
412 // Is there an extension specific diff tool?
413 CString ext = file2.GetFileExtension().MakeLower();
414 if (!ext.IsEmpty())
416 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);
417 if (!difftool.IsEmpty())
418 return difftool;
419 // Maybe we should use TortoiseIDiff?
420 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
421 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
422 (ext == _T(".png")) || (ext == _T(".ico")) ||
423 (ext == _T(".dib")) || (ext == _T(".emf")))
425 return
426 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe") + _T("\"") +
427 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname") +
428 L" /groupuuid:\"" + g_sGroupingUUID + L"\"";
432 // Finally, pick a generic external diff tool
433 difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));
434 return difftool;
437 bool CAppUtils::StartExtDiff(
438 const CString& file1, const CString& file2,
439 const CString& sName1, const CString& sName2,
440 const CString& originalFile1, const CString& originalFile2,
441 const git_revnum_t& hash1, const git_revnum_t& hash2,
442 const DiffFlags& flags, int jumpToLine)
444 CString viewer;
446 CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);
447 if (!flags.bBlame || !(DWORD)blamediff)
449 viewer = PickDiffTool(file1, file2);
450 // If registry entry for a diff program is commented out, use TortoiseGitMerge.
451 bool bCommentedOut = viewer.Left(1) == _T("#");
452 if (flags.bAlternativeTool)
454 // Invert external vs. internal diff tool selection.
455 if (bCommentedOut)
456 viewer.Delete(0); // uncomment
457 else
458 viewer = "";
460 else if (bCommentedOut)
461 viewer = "";
464 bool bInternal = viewer.IsEmpty();
465 if (bInternal)
467 viewer =
468 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe") + _T("\"") +
469 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
470 if (!g_sGroupingUUID.IsEmpty())
472 viewer += L" /groupuuid:\"";
473 viewer += g_sGroupingUUID;
474 viewer += L"\"";
476 if (flags.bBlame)
477 viewer += _T(" /blame");
479 // check if the params are set. If not, just add the files to the command line
480 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
482 viewer += _T(" \"")+file1+_T("\"");
483 viewer += _T(" \"")+file2+_T("\"");
485 if (viewer.Find(_T("%base")) >= 0)
487 viewer.Replace(_T("%base"), _T("\"")+file1+_T("\""));
489 if (viewer.Find(_T("%mine")) >= 0)
491 viewer.Replace(_T("%mine"), _T("\"")+file2+_T("\""));
494 if (sName1.IsEmpty())
495 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));
496 else
497 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
499 if (sName2.IsEmpty())
500 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));
501 else
502 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
504 viewer.Replace(_T("%bpath"), _T("\"") + originalFile1 + _T("\""));
505 viewer.Replace(_T("%ypath"), _T("\"") + originalFile2 + _T("\""));
507 viewer.Replace(_T("%brev"), _T("\"") + hash1 + _T("\""));
508 viewer.Replace(_T("%yrev"), _T("\"") + hash2 + _T("\""));
510 if (flags.bReadOnly && bInternal)
511 viewer += _T(" /readonly");
513 if (jumpToLine > 0)
515 CString temp;
516 temp.Format(_T(" /line:%d"), jumpToLine);
517 viewer += temp;
520 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);
523 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)
525 CString viewer;
526 CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
527 viewer = v;
528 if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))
530 // use TortoiseGitUDiff
531 viewer = CPathUtils::GetAppDirectory();
532 viewer += _T("TortoiseGitUDiff.exe");
533 // enquote the path to TortoiseGitUDiff
534 viewer = _T("\"") + viewer + _T("\"");
535 // add the params
536 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");
537 if (!g_sGroupingUUID.IsEmpty())
539 viewer += L" /groupuuid:\"";
540 viewer += g_sGroupingUUID;
541 viewer += L"\"";
544 if (viewer.Find(_T("%1"))>=0)
546 if (viewer.Find(_T("\"%1\"")) >= 0)
547 viewer.Replace(_T("%1"), patchfile);
548 else
549 viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));
551 else
552 viewer += _T(" \"") + patchfile + _T("\"");
553 if (viewer.Find(_T("%title")) >= 0)
555 viewer.Replace(_T("%title"), title);
558 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
560 return FALSE;
562 return TRUE;
565 BOOL CAppUtils::StartTextViewer(CString file)
567 CString viewer;
568 CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);
569 viewer = txt;
570 viewer = viewer + _T("\\Shell\\Open\\Command\\");
571 CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);
572 viewer = txtexe;
574 DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
575 std::unique_ptr<TCHAR[]> buf(new TCHAR[len + 1]);
576 ExpandEnvironmentStrings(viewer, buf.get(), len);
577 viewer = buf.get();
578 len = ExpandEnvironmentStrings(file, NULL, 0);
579 std::unique_ptr<TCHAR[]> buf2(new TCHAR[len + 1]);
580 ExpandEnvironmentStrings(file, buf2.get(), len);
581 file = buf2.get();
582 file = _T("\"")+file+_T("\"");
583 if (viewer.IsEmpty())
585 viewer = _T("RUNDLL32 Shell32,OpenAs_RunDLL");
587 if (viewer.Find(_T("\"%1\"")) >= 0)
589 viewer.Replace(_T("\"%1\""), file);
591 else if (viewer.Find(_T("%1")) >= 0)
593 viewer.Replace(_T("%1"), file);
595 else
597 viewer += _T(" ");
598 viewer += file;
601 if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))
603 return FALSE;
605 return TRUE;
608 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)
610 DWORD length = 0;
611 CAutoFile hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
612 if (!hFile)
613 return TRUE;
614 length = ::GetFileSize(hFile, NULL);
615 if (length < 4)
616 return TRUE;
617 return FALSE;
621 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)
623 LOGFONT logFont;
624 HDC hScreenDC = ::GetDC(NULL);
625 logFont.lfHeight = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);
626 ::ReleaseDC(NULL, hScreenDC);
627 logFont.lfWidth = 0;
628 logFont.lfEscapement = 0;
629 logFont.lfOrientation = 0;
630 logFont.lfWeight = FW_NORMAL;
631 logFont.lfItalic = 0;
632 logFont.lfUnderline = 0;
633 logFont.lfStrikeOut = 0;
634 logFont.lfCharSet = DEFAULT_CHARSET;
635 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
636 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
637 logFont.lfQuality = DRAFT_QUALITY;
638 logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;
639 _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
640 VERIFY(fontToCreate.CreateFontIndirect(&logFont));
643 bool CAppUtils::LaunchPAgent(CString *keyfile,CString * pRemote)
645 CString key,remote;
646 CString cmd,out;
647 if( pRemote == NULL)
649 remote=_T("origin");
651 else
653 remote=*pRemote;
655 if(keyfile == NULL)
657 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
658 key = g_Git.GetConfigValue(cmd);
660 else
661 key=*keyfile;
663 if(key.IsEmpty())
664 return false;
666 CString proc=CPathUtils::GetAppDirectory();
667 proc += _T("pageant.exe \"");
668 proc += key;
669 proc += _T("\"");
671 CString tempfile = GetTempFile();
672 ::DeleteFile(tempfile);
674 proc += _T(" -c \"");
675 proc += CPathUtils::GetAppDirectory();
676 proc += _T("tgittouch.exe\"");
677 proc += _T(" \"");
678 proc += tempfile;
679 proc += _T("\"");
681 CString appDir = CPathUtils::GetAppDirectory();
682 bool b = LaunchApplication(proc, IDS_ERR_PAGEANT, true, &appDir);
683 if(!b)
684 return b;
686 int i=0;
687 while(!::PathFileExists(tempfile))
689 Sleep(100);
690 ++i;
691 if(i>10*60*5)
692 break; //timeout 5 minutes
695 if( i== 10*60*5)
697 CMessageBox::Show(NULL, IDS_ERR_PAEGENTTIMEOUT, IDS_APPNAME, MB_OK | MB_ICONERROR);
699 ::DeleteFile(tempfile);
700 return true;
702 bool CAppUtils::LaunchAlternativeEditor(const CString& filename, bool uac)
704 CString editTool = CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
705 if (editTool.IsEmpty() || (editTool.Left(1).Compare(_T("#"))==0)) {
706 editTool = CPathUtils::GetAppDirectory() + _T("notepad2.exe");
709 CString sCmd;
710 sCmd.Format(_T("\"%s\" \"%s\""), editTool, filename);
712 LaunchApplication(sCmd, NULL, false, NULL, uac);
713 return true;
715 bool CAppUtils::LaunchRemoteSetting()
717 CTGitPath path(g_Git.m_CurrentDir);
718 CSettings dlg(IDS_PROC_SETTINGS_TITLE, &path);
719 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
720 dlg.SetTreeWidth(220);
721 dlg.m_DefaultPage = _T("gitremote");
723 dlg.DoModal();
724 dlg.HandleRestart();
725 return true;
728 * Launch the external blame viewer
730 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)
732 CString viewer = _T("\"") + CPathUtils::GetAppDirectory();
733 viewer += _T("TortoiseGitBlame.exe");
734 viewer += _T("\" \"") + sBlameFile + _T("\"");
735 //viewer += _T(" \"") + sLogFile + _T("\"");
736 //viewer += _T(" \"") + sOriginalFile + _T("\"");
737 if(!Rev.IsEmpty() && Rev != GIT_REV_ZERO)
738 viewer += CString(_T(" /rev:"))+Rev;
739 if (!g_sGroupingUUID.IsEmpty())
741 viewer += L" /groupuuid:\"";
742 viewer += g_sGroupingUUID;
743 viewer += L"\"";
745 viewer += _T(" ")+sParams;
747 return LaunchApplication(viewer, IDS_ERR_TGITBLAME, false);
750 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)
752 CString sText;
753 if (pWnd == NULL)
754 return false;
755 bool bStyled = false;
756 pWnd->GetWindowText(sText);
757 // the rich edit control doesn't count the CR char!
758 // to be exact: CRLF is treated as one char.
759 sText.Remove(_T('\r'));
761 // style each line separately
762 int offset = 0;
763 int nNewlinePos;
766 nNewlinePos = sText.Find('\n', offset);
767 CString sLine = nNewlinePos >= 0 ? sText.Mid(offset, nNewlinePos - offset) : sText.Mid(offset);
769 int start = 0;
770 int end = 0;
771 while (FindStyleChars(sLine, '*', start, end))
773 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
774 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
775 SetCharFormat(pWnd, CFM_BOLD, CFE_BOLD);
776 bStyled = true;
777 start = end;
779 start = 0;
780 end = 0;
781 while (FindStyleChars(sLine, '^', start, end))
783 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
784 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
785 SetCharFormat(pWnd, CFM_ITALIC, CFE_ITALIC);
786 bStyled = true;
787 start = end;
789 start = 0;
790 end = 0;
791 while (FindStyleChars(sLine, '_', start, end))
793 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
794 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
795 SetCharFormat(pWnd, CFM_UNDERLINE, CFE_UNDERLINE);
796 bStyled = true;
797 start = end;
799 offset = nNewlinePos+1;
800 } while(nNewlinePos>=0);
801 return bStyled;
804 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)
806 int i=start;
807 int last = sText.GetLength() - 1;
808 bool bFoundMarker = false;
809 TCHAR c = i == 0 ? _T('\0') : sText[i - 1];
810 TCHAR nextChar = i >= last ? _T('\0') : sText[i + 1];
812 // find a starting marker
813 while (i < last)
815 TCHAR prevChar = c;
816 c = nextChar;
817 nextChar = sText[i + 1];
819 // IsCharAlphaNumeric can be somewhat expensive.
820 // Long lines of "*****" or "----" will be pre-empted efficiently
821 // by the (c != nextChar) condition.
823 if ((c == stylechar) && (c != nextChar))
825 if (IsCharAlphaNumeric(nextChar) && !IsCharAlphaNumeric(prevChar))
827 start = ++i;
828 bFoundMarker = true;
829 break;
832 ++i;
834 if (!bFoundMarker)
835 return false;
837 // find ending marker
838 // c == sText[i - 1]
840 bFoundMarker = false;
841 while (i <= last)
843 TCHAR prevChar = c;
844 c = sText[i];
845 if (c == stylechar)
847 if ((i == last) || (!IsCharAlphaNumeric(sText[i + 1]) && IsCharAlphaNumeric(prevChar)))
849 end = i;
850 ++i;
851 bFoundMarker = true;
852 break;
855 ++i;
857 return bFoundMarker;
860 bool CAppUtils::StartShowUnifiedDiff(HWND hWnd, const CTGitPath& url1, const git_revnum_t& rev1,
861 const CTGitPath& /*url2*/, const git_revnum_t& rev2,
862 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
863 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */,
864 bool /* blame = false */,
865 bool bMerge,
866 bool bCombine)
868 int diffContext = 0;
869 if (GetMsysgitVersion() > 0x01080100)
870 diffContext = _ttoi(g_Git.GetConfigValue(_T("diff.context")));
871 CString tempfile=GetTempFile();
872 if (g_Git.GetUnifiedDiff(url1, rev1, rev2, tempfile, bMerge, bCombine, diffContext))
874 CMessageBox::Show(hWnd, g_Git.GetGitLastErr(_T("Could not get unified diff."), CGit::GIT_CMD_DIFF), _T("TortoiseGit"), MB_OK);
875 return false;
877 CAppUtils::StartUnifiedDiffViewer(tempfile, rev1 + _T(":") + rev2);
879 #if 0
880 CString sCmd;
881 sCmd.Format(_T("%s /command:showcompare /unified"),
882 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseGitProc.exe")));
883 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
884 if (rev1.IsValid())
885 sCmd += _T(" /revision1:") + rev1.ToString();
886 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
887 if (rev2.IsValid())
888 sCmd += _T(" /revision2:") + rev2.ToString();
889 if (peg.IsValid())
890 sCmd += _T(" /pegrevision:") + peg.ToString();
891 if (headpeg.IsValid())
892 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
894 if (bAlternateDiff)
895 sCmd += _T(" /alternatediff");
897 if (bIgnoreAncestry)
898 sCmd += _T(" /ignoreancestry");
900 if (hWnd)
902 sCmd += _T(" /hwnd:");
903 TCHAR buf[30];
904 _stprintf_s(buf, 30, _T("%p"), (void*)hWnd);
905 sCmd += buf;
908 return CAppUtils::LaunchApplication(sCmd, NULL, false);
909 #endif
910 return TRUE;
913 bool CAppUtils::SetupDiffScripts(bool force, const CString& type)
915 CString scriptsdir = CPathUtils::GetAppParentDirectory();
916 scriptsdir += _T("Diff-Scripts");
917 CSimpleFileFind files(scriptsdir);
918 while (files.FindNextFileNoDirectories())
920 CString file = files.GetFilePath();
921 CString filename = files.GetFileName();
922 CString ext = file.Mid(file.ReverseFind('-') + 1);
923 ext = _T(".") + ext.Left(ext.ReverseFind('.'));
924 std::set<CString> extensions;
925 extensions.insert(ext);
926 CString kind;
927 if (file.Right(3).CompareNoCase(_T("vbs"))==0)
929 kind = _T(" //E:vbscript");
931 if (file.Right(2).CompareNoCase(_T("js"))==0)
933 kind = _T(" //E:javascript");
935 // open the file, read the first line and find possible extensions
936 // this script can handle
939 CStdioFile f(file, CFile::modeRead | CFile::shareDenyNone);
940 CString extline;
941 if (f.ReadString(extline))
943 if ((extline.GetLength() > 15 ) &&
944 ((extline.Left(15).Compare(_T("// extensions: ")) == 0) ||
945 (extline.Left(14).Compare(_T("' extensions: ")) == 0)))
947 if (extline[0] == '/')
948 extline = extline.Mid(15);
949 else
950 extline = extline.Mid(14);
951 CString sToken;
952 int curPos = 0;
953 sToken = extline.Tokenize(_T(";"), curPos);
954 while (!sToken.IsEmpty())
956 if (!sToken.IsEmpty())
958 if (sToken[0] != '.')
959 sToken = _T(".") + sToken;
960 extensions.insert(sToken);
962 sToken = extline.Tokenize(_T(";"), curPos);
966 f.Close();
968 catch (CFileException* e)
970 e->Delete();
973 for (std::set<CString>::const_iterator it = extensions.begin(); it != extensions.end(); ++it)
975 if (type.IsEmpty() || (type.Compare(_T("Diff")) == 0))
977 if (filename.Left(5).CompareNoCase(_T("diff-")) == 0)
979 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + *it);
980 CString diffregstring = diffreg;
981 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
982 diffreg = _T("wscript.exe \"") + file + _T("\" %base %mine") + kind;
985 if (type.IsEmpty() || (type.Compare(_T("Merge"))==0))
987 if (filename.Left(6).CompareNoCase(_T("merge-"))==0)
989 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\MergeTools\\") + *it);
990 CString diffregstring = diffreg;
991 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
992 diffreg = _T("wscript.exe \"") + file + _T("\" %merged %theirs %mine %base") + kind;
998 return true;
1001 bool CAppUtils::Export(CString *BashHash, const CTGitPath *orgPath)
1003 // ask from where the export has to be done
1004 CExportDlg dlg;
1005 if(BashHash)
1006 dlg.m_Revision=*BashHash;
1007 if (orgPath)
1009 if (PathIsRelative(orgPath->GetWinPath()))
1010 dlg.m_orgPath = g_Git.m_CurrentDir + _T("\\") + orgPath->GetWinPathString();
1011 else
1012 dlg.m_orgPath = *orgPath;
1015 if (dlg.DoModal() == IDOK)
1017 CString cmd;
1018 cmd.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s --"),
1019 dlg.m_strFile, g_Git.FixBranchName(dlg.m_VersionName));
1021 CProgressDlg pro;
1022 pro.m_GitCmd=cmd;
1023 CGit git;
1024 if (!dlg.m_bWholeProject && !dlg.m_orgPath.IsEmpty() && PathIsDirectory(dlg.m_orgPath.GetWinPathString()))
1026 git.m_CurrentDir = dlg.m_orgPath.GetWinPathString();
1027 pro.m_Git = &git;
1029 return (pro.DoModal() == IDOK);
1031 return false;
1034 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash, bool switch_new_brach)
1036 CCreateBranchTagDlg dlg;
1037 dlg.m_bIsTag=IsTag;
1038 dlg.m_bSwitch=switch_new_brach;
1040 if(CommitHash)
1041 dlg.m_initialRefName = *CommitHash;
1043 if(dlg.DoModal()==IDOK)
1045 CString cmd;
1046 CString force;
1047 CString track;
1048 if(dlg.m_bTrack == TRUE)
1049 track=_T(" --track ");
1050 else if(dlg.m_bTrack == FALSE)
1051 track=_T(" --no-track");
1053 if(dlg.m_bForce)
1054 force=_T(" -f ");
1056 if(IsTag)
1058 CString sign;
1059 if(dlg.m_bSign)
1060 sign=_T("-s");
1062 cmd.Format(_T("git.exe tag %s %s %s %s"),
1063 force,
1064 sign,
1065 dlg.m_BranchTagName,
1066 g_Git.FixBranchName(dlg.m_VersionName)
1069 CString tempfile=::GetTempFile();
1070 if(!dlg.m_Message.Trim().IsEmpty())
1072 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_Message);
1073 cmd += _T(" -F ")+tempfile;
1076 else
1078 cmd.Format(_T("git.exe branch %s %s %s %s"),
1079 track,
1080 force,
1081 dlg.m_BranchTagName,
1082 g_Git.FixBranchName(dlg.m_VersionName)
1085 CString out;
1086 if(g_Git.Run(cmd,&out,CP_UTF8))
1088 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1089 return FALSE;
1091 if( !IsTag && dlg.m_bSwitch )
1093 // it is a new branch and the user has requested to switch to it
1094 PerformSwitch(dlg.m_BranchTagName);
1097 return TRUE;
1099 return FALSE;
1102 bool CAppUtils::Switch(CString initialRefName)
1104 CGitSwitchDlg dlg;
1105 if(!initialRefName.IsEmpty())
1106 dlg.m_initialRefName = initialRefName;
1108 if (dlg.DoModal() == IDOK)
1110 CString branch;
1111 if (dlg.m_bBranch)
1112 branch = dlg.m_NewBranch;
1114 // if refs/heads/ is not stripped, checkout will detach HEAD
1115 // checkout prefers branches on name clashes (with tags)
1116 if (dlg.m_VersionName.Left(11) ==_T("refs/heads/") && dlg.m_bBranchOverride != TRUE)
1117 dlg.m_VersionName = dlg.m_VersionName.Mid(11);
1119 return PerformSwitch(dlg.m_VersionName, dlg.m_bForce == TRUE , branch, dlg.m_bBranchOverride == TRUE, dlg.m_bTrack, dlg.m_bMerge == TRUE);
1121 return FALSE;
1124 bool CAppUtils::PerformSwitch(CString ref, bool bForce /* false */, CString sNewBranch /* CString() */, bool bBranchOverride /* false */, BOOL bTrack /* 2 */, bool bMerge /* false */)
1126 CString cmd;
1127 CString track;
1128 CString force;
1129 CString branch;
1130 CString merge;
1132 if(!sNewBranch.IsEmpty()){
1133 if (bBranchOverride)
1135 branch.Format(_T("-B %s"), sNewBranch);
1137 else
1139 branch.Format(_T("-b %s"), sNewBranch);
1141 if (bTrack == TRUE)
1142 track = _T("--track");
1143 else if (bTrack == FALSE)
1144 track = _T("--no-track");
1146 if (bForce)
1147 force = _T("-f");
1148 if (bMerge)
1149 merge = _T("--merge");
1151 cmd.Format(_T("git.exe checkout %s %s %s %s %s --"),
1152 force,
1153 track,
1154 merge,
1155 branch,
1156 g_Git.FixBranchName(ref));
1158 while (true)
1160 CProgressDlg progress;
1161 progress.m_GitCmd = cmd;
1163 INT_PTR idPull = -1;
1164 INT_PTR idSubmoduleUpdate = -1;
1165 INT_PTR idMerge = -1;
1167 CTGitPath gitPath = g_Git.m_CurrentDir;
1168 if (gitPath.HasSubmodules())
1169 idSubmoduleUpdate = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1170 CString currentBranch;
1171 bool hasBranch = CGit::GetCurrentBranchFromFile(g_Git.m_CurrentDir, currentBranch) == 0;
1172 if (hasBranch)
1173 idMerge = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUMERGE)));
1175 progress.m_PostCmdCallback = [&] ()
1177 if (!progress.m_GitStatus)
1179 CString newBranch;
1180 if (!CGit::GetCurrentBranchFromFile(g_Git.m_CurrentDir, newBranch))
1181 idPull = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
1183 else
1185 progress.m_PostCmdList.RemoveAll();
1186 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
1187 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_SWITCH_WITH_MERGE)));
1191 INT_PTR ret = progress.DoModal();
1192 if (progress.m_GitStatus == 0)
1194 if (idSubmoduleUpdate >= 0 && ret == IDC_PROGRESS_BUTTON1 + idSubmoduleUpdate)
1196 CString sCmd;
1197 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1199 RunTortoiseGitProc(sCmd);
1200 return TRUE;
1202 else if (ret == IDC_PROGRESS_BUTTON1 + idPull)
1204 Pull();
1205 return TRUE;
1207 else if (ret == IDC_PROGRESS_BUTTON1 + idMerge)
1209 Merge(&currentBranch);
1210 return TRUE;
1212 else if (ret == IDOK)
1213 return TRUE;
1215 else if (ret == IDC_PROGRESS_BUTTON1)
1216 continue; // retry
1217 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
1219 merge = _T("--merge");
1220 cmd.Format(_T("git.exe checkout %s %s %s %s %s --"),
1221 force, track, merge, branch, g_Git.FixBranchName(ref));
1222 continue; // retry
1225 return FALSE;
1229 class CIgnoreFile : public CStdioFile
1231 public:
1232 STRING_VECTOR m_Items;
1233 CString m_eol;
1235 virtual BOOL ReadString(CString& rString)
1237 if (GetPosition() == 0)
1239 unsigned char utf8bom[] = { 0xEF, 0xBB, 0xBF };
1240 char buf[3] = { 0, 0, 0 };
1241 Read(buf, 3);
1242 if (memcpy(buf, utf8bom, sizeof(utf8bom)))
1244 SeekToBegin();
1248 CStringA strA;
1249 char lastChar = '\0';
1250 for (char c = '\0'; Read(&c, 1) == 1; lastChar = c)
1252 if (c == '\r')
1253 continue;
1254 if (c == '\n')
1256 m_eol = lastChar == '\r' ? _T("\r\n") : _T("\n");
1257 break;
1259 strA.AppendChar(c);
1261 if (strA.IsEmpty())
1262 return FALSE;
1264 rString = CUnicodeUtils::GetUnicode(strA);
1265 return TRUE;
1268 void ResetState()
1270 m_Items.clear();
1271 m_eol = _T("");
1275 bool CAppUtils::OpenIgnoreFile(CIgnoreFile &file, const CString& filename)
1277 file.ResetState();
1278 if (!file.Open(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate | CFile::typeBinary))
1280 CMessageBox::Show(NULL, filename + _T(" Open Failure"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1281 return false;
1284 if (file.GetLength() > 0)
1286 CString fileText;
1287 while (file.ReadString(fileText))
1288 file.m_Items.push_back(fileText);
1289 file.Seek(file.GetLength() - 1, 0);
1290 char lastchar[1] = { 0 };
1291 file.Read(lastchar, 1);
1292 file.SeekToEnd();
1293 if (lastchar[0] != '\n')
1295 CStringA eol = CStringA(file.m_eol.IsEmpty() ? _T("\n") : file.m_eol);
1296 file.Write(eol, eol.GetLength());
1299 else
1300 file.SeekToEnd();
1302 return true;
1305 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1307 CIgnoreDlg ignoreDlg;
1308 if (ignoreDlg.DoModal() == IDOK)
1310 CString ignorefile;
1311 ignorefile = g_Git.m_CurrentDir + _T("\\");
1313 switch (ignoreDlg.m_IgnoreFile)
1315 case 0:
1316 ignorefile += _T(".gitignore");
1317 break;
1318 case 2:
1319 g_GitAdminDir.GetAdminDirPath(g_Git.m_CurrentDir, ignorefile);
1320 ignorefile += _T("info/exclude");
1321 break;
1324 CIgnoreFile file;
1327 if (ignoreDlg.m_IgnoreFile != 1 && !OpenIgnoreFile(file, ignorefile))
1328 return false;
1330 for (int i = 0; i < path.GetCount(); ++i)
1332 if (ignoreDlg.m_IgnoreFile == 1)
1334 ignorefile = g_Git.m_CurrentDir + _T("\\") + path[i].GetContainingDirectory().GetWinPathString() + _T("\\.gitignore");
1335 if (!OpenIgnoreFile(file, ignorefile))
1336 return false;
1339 CString ignorePattern;
1340 if (ignoreDlg.m_IgnoreType == 0)
1342 if (ignoreDlg.m_IgnoreFile != 1 && !path[i].GetContainingDirectory().GetGitPathString().IsEmpty())
1343 ignorePattern += _T("/") + path[i].GetContainingDirectory().GetGitPathString();
1345 ignorePattern += _T("/");
1347 if (IsMask)
1349 ignorePattern += _T("*") + path[i].GetFileExtension();
1351 else
1353 ignorePattern += path[i].GetFileOrDirectoryName();
1356 // escape [ and ] so that files get ignored correctly
1357 ignorePattern.Replace(_T("["), _T("\\["));
1358 ignorePattern.Replace(_T("]"), _T("\\]"));
1360 bool found = false;
1361 for (size_t j = 0; j < file.m_Items.size(); ++j)
1363 if (file.m_Items[j] == ignorePattern)
1365 found = true;
1366 break;
1369 if (!found)
1371 file.m_Items.push_back(ignorePattern);
1372 ignorePattern += file.m_eol.IsEmpty() ? _T("\n") : file.m_eol;
1373 CStringA ignorePatternA = CUnicodeUtils::GetUTF8(ignorePattern);
1374 file.Write(ignorePatternA, ignorePatternA.GetLength());
1377 if (ignoreDlg.m_IgnoreFile == 1)
1378 file.Close();
1381 if (ignoreDlg.m_IgnoreFile != 1)
1382 file.Close();
1384 catch(...)
1386 file.Abort();
1387 return false;
1390 return true;
1392 return false;
1396 bool CAppUtils::GitReset(CString *CommitHash,int type)
1398 CResetDlg dlg;
1399 dlg.m_ResetType=type;
1400 dlg.m_ResetToVersion=*CommitHash;
1401 dlg.m_initialRefName = *CommitHash;
1402 if (dlg.DoModal() == IDOK)
1404 CString cmd;
1405 CString type;
1406 switch(dlg.m_ResetType)
1408 case 0:
1409 type=_T("--soft");
1410 break;
1411 case 1:
1412 type=_T("--mixed");
1413 break;
1414 case 2:
1415 type=_T("--hard");
1416 break;
1417 default:
1418 dlg.m_ResetType = 1;
1419 type=_T("--mixed");
1420 break;
1422 cmd.Format(_T("git.exe reset %s %s --"),type, dlg.m_ResetToVersion);
1424 while (true)
1426 CProgressDlg progress;
1427 progress.m_GitCmd=cmd;
1429 CTGitPath gitPath = g_Git.m_CurrentDir;
1430 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2)
1431 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1433 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
1435 INT_PTR ret;
1436 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
1438 CGitProgressDlg gitdlg;
1439 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
1440 gitdlg.SetRevision(dlg.m_ResetToVersion);
1441 gitdlg.SetResetType(dlg.m_ResetType);
1442 ret = gitdlg.DoModal();
1444 else
1445 ret = progress.DoModal();
1447 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 2 && ret == IDC_PROGRESS_BUTTON1)
1449 CString sCmd;
1450 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1452 RunTortoiseGitProc(sCmd);
1453 return TRUE;
1455 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
1456 continue; // retry
1457 else if (ret == IDOK)
1458 return TRUE;
1459 else
1460 break;
1463 return FALSE;
1466 void CAppUtils::DescribeConflictFile(bool mode, bool base,CString &descript)
1468 if(mode == FALSE)
1470 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE));
1471 return;
1473 if(base)
1475 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED));
1476 return;
1478 descript = CString(MAKEINTRESOURCE(IDS_PROC_CREATED));
1479 return;
1482 void CAppUtils::RemoveTempMergeFile(CTGitPath &path)
1484 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("LOCAL"), path));
1485 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("REMOTE"), path));
1486 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("BASE"), path));
1488 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1490 CString file;
1491 file=g_Git.m_CurrentDir+_T("\\") + merge.GetWinPathString()+_T(".")+type+merge.GetFileExtension();
1493 return file;
1496 bool ParseHashesFromLsFile(BYTE_VECTOR &out, CString &hash1, CString &hash2, CString &hash3)
1498 unsigned int pos = 0;
1499 CString one;
1500 CString part;
1502 while (pos >= 0 && pos < out.size())
1504 one.Empty();
1506 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1507 int tabstart = 0;
1508 one.Tokenize(_T("\t"), tabstart);
1510 tabstart = 0;
1511 part = one.Tokenize(_T(" "), tabstart); //Tag
1512 part = one.Tokenize(_T(" "), tabstart); //Mode
1513 part = one.Tokenize(_T(" "), tabstart); //Hash
1514 CString hash = part;
1515 part = one.Tokenize(_T("\t"), tabstart); //Stage
1516 int stage = _ttol(part);
1517 if (stage == 1)
1518 hash1 = hash;
1519 else if (stage == 2)
1520 hash2 = hash;
1521 else if (stage == 3)
1523 hash3 = hash;
1524 return true;
1527 pos = out.findNextString(pos);
1530 return false;
1533 bool CAppUtils::ConflictEdit(CTGitPath &path,bool /*bAlternativeTool*/,bool revertTheirMy)
1535 bool bRet = false;
1537 CTGitPath merge=path;
1538 CTGitPath directory = merge.GetDirectory();
1540 // we have the conflicted file (%merged)
1541 // now look for the other required files
1542 //GitStatus stat;
1543 //stat.GetStatus(merge);
1544 //if (stat.status == NULL)
1545 // return false;
1547 BYTE_VECTOR vector;
1549 CString cmd;
1550 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1552 if (g_Git.Run(cmd, &vector))
1554 return FALSE;
1557 if (merge.IsDirectory())
1559 CString baseHash, localHash, remoteHash;
1560 if (!ParseHashesFromLsFile(vector, baseHash, localHash, remoteHash))
1561 return FALSE;
1563 CString msg;
1564 msg.Format(_T("BASE: %s\nLOCAL: %s\nREMOTE: %s"), baseHash, localHash, remoteHash);
1565 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK);
1566 return TRUE;
1569 CTGitPathList list;
1570 if (list.ParserFromLsFile(vector))
1572 CMessageBox::Show(NULL, _T("Parse ls-files failed!"), _T("TortoiseGit"), MB_OK);
1573 return FALSE;
1576 if (list.IsEmpty())
1577 return FALSE;
1579 CTGitPath theirs;
1580 CTGitPath mine;
1581 CTGitPath base;
1583 if (revertTheirMy)
1585 mine.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1586 theirs.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1588 else
1590 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1591 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1593 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1595 CString format;
1597 //format=_T("git.exe cat-file blob \":%d:%s\"");
1598 format = _T("git.exe checkout-index --temp --stage=%d -- \"%s\"");
1599 CFile tempfile;
1600 //create a empty file, incase stage is not three
1601 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1602 tempfile.Close();
1603 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1604 tempfile.Close();
1605 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1606 tempfile.Close();
1608 bool b_base=false, b_local=false, b_remote=false;
1610 for (int i = 0; i< list.GetCount(); ++i)
1612 CString cmd;
1613 CString outfile;
1614 cmd.Empty();
1615 outfile.Empty();
1617 if( list[i].m_Stage == 1)
1619 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1620 b_base = true;
1621 outfile = base.GetWinPathString();
1624 if( list[i].m_Stage == 2 )
1626 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1627 b_local = true;
1628 outfile = mine.GetWinPathString();
1631 if( list[i].m_Stage == 3 )
1633 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1634 b_remote = true;
1635 outfile = theirs.GetWinPathString();
1637 CString output, err;
1638 if(!outfile.IsEmpty())
1639 if (!g_Git.Run(cmd, &output, &err, CP_UTF8))
1641 CString file;
1642 int start =0 ;
1643 file = output.Tokenize(_T("\t"), start);
1644 ::MoveFileEx(file,outfile,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
1646 else
1648 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1652 if(b_local && b_remote )
1654 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1655 if( revertTheirMy )
1656 bRet = !!CAppUtils::StartExtMerge(base, mine, theirs, merge, _T("BASE"), _T("REMOTE"), _T("LOCAL"));
1657 else
1658 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge,_T("BASE"), _T("REMOTE"), _T("LOCAL"));
1661 else
1663 ::DeleteFile(mine.GetWinPathString());
1664 ::DeleteFile(theirs.GetWinPathString());
1665 ::DeleteFile(base.GetWinPathString());
1667 CDeleteConflictDlg dlg;
1668 DescribeConflictFile(b_local, b_base,dlg.m_LocalStatus);
1669 DescribeConflictFile(b_remote,b_base,dlg.m_RemoteStatus);
1670 CGitHash localHash, remoteHash;
1671 if (!g_Git.GetHash(localHash, _T("HEAD")))
1672 dlg.m_LocalHash = localHash.ToString();
1673 if (!g_Git.GetHash(remoteHash, _T("MERGE_HEAD")))
1674 dlg.m_RemoteHash = remoteHash.ToString();
1675 else if (!g_Git.GetHash(remoteHash, _T("rebase-apply/original-commit")))
1676 dlg.m_RemoteHash = remoteHash.ToString();
1677 else if (!g_Git.GetHash(remoteHash, _T("CHERRY_PICK_HEAD")))
1678 dlg.m_RemoteHash = remoteHash.ToString();
1679 else if (!g_Git.GetHash(remoteHash, _T("REVERT_HEAD")))
1680 dlg.m_RemoteHash = remoteHash.ToString();
1681 dlg.m_bShowModifiedButton=b_base;
1682 dlg.m_File=merge.GetGitPathString();
1683 if(dlg.DoModal() == IDOK)
1685 CString cmd,out;
1686 if(dlg.m_bIsDelete)
1688 cmd.Format(_T("git.exe rm -- \"%s\""),merge.GetGitPathString());
1690 else
1691 cmd.Format(_T("git.exe add -- \"%s\""),merge.GetGitPathString());
1693 if (g_Git.Run(cmd, &out, CP_UTF8))
1695 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1696 return FALSE;
1698 return TRUE;
1700 else
1701 return FALSE;
1704 #if 0
1705 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1706 base, theirs, mine, merge);
1707 #endif
1708 #if 0
1709 if (stat.status->text_status == svn_wc_status_conflicted)
1711 // we have a text conflict, use our merge tool to resolve the conflict
1713 CTSVNPath theirs(directory);
1714 CTSVNPath mine(directory);
1715 CTSVNPath base(directory);
1716 bool bConflictData = false;
1718 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1720 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1721 bConflictData = true;
1723 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1725 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1726 bConflictData = true;
1728 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1730 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1731 bConflictData = true;
1733 else
1735 mine = merge;
1737 if (bConflictData)
1738 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1739 base, theirs, mine, merge);
1742 if (stat.status->prop_status == svn_wc_status_conflicted)
1744 // we have a property conflict
1745 CTSVNPath prej(directory);
1746 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1748 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1749 // there's a problem: the prej file contains a _description_ of the conflict, and
1750 // that description string might be translated. That means we have no way of parsing
1751 // the file to find out the conflicting values.
1752 // The only thing we can do: show a dialog with the conflict description, then
1753 // let the user either accept the existing property or open the property edit dialog
1754 // to manually change the properties and values. And a button to mark the conflict as
1755 // resolved.
1756 CEditPropConflictDlg dlg;
1757 dlg.SetPrejFile(prej);
1758 dlg.SetConflictedItem(merge);
1759 bRet = (dlg.DoModal() != IDCANCEL);
1763 if (stat.status->tree_conflict)
1765 // we have a tree conflict
1766 SVNInfo info;
1767 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1768 if (pInfoData)
1770 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1772 CTSVNPath theirs(directory);
1773 CTSVNPath mine(directory);
1774 CTSVNPath base(directory);
1775 bool bConflictData = false;
1777 if (pInfoData->treeconflict_theirfile)
1779 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1780 bConflictData = true;
1782 if (pInfoData->treeconflict_basefile)
1784 base.AppendPathString(pInfoData->treeconflict_basefile);
1785 bConflictData = true;
1787 if (pInfoData->treeconflict_myfile)
1789 mine.AppendPathString(pInfoData->treeconflict_myfile);
1790 bConflictData = true;
1792 else
1794 mine = merge;
1796 if (bConflictData)
1797 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1798 base, theirs, mine, merge);
1800 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1802 CString sConflictAction;
1803 CString sConflictReason;
1804 CString sResolveTheirs;
1805 CString sResolveMine;
1806 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1807 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1809 if (pInfoData->treeconflict_nodekind == svn_node_file)
1811 switch (pInfoData->treeconflict_operation)
1813 case svn_wc_operation_update:
1814 switch (pInfoData->treeconflict_action)
1816 case svn_wc_conflict_action_edit:
1817 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1818 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1819 break;
1820 case svn_wc_conflict_action_add:
1821 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1822 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1823 break;
1824 case svn_wc_conflict_action_delete:
1825 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1826 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1827 break;
1829 break;
1830 case svn_wc_operation_switch:
1831 switch (pInfoData->treeconflict_action)
1833 case svn_wc_conflict_action_edit:
1834 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1835 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1836 break;
1837 case svn_wc_conflict_action_add:
1838 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1839 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1840 break;
1841 case svn_wc_conflict_action_delete:
1842 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1843 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1844 break;
1846 break;
1847 case svn_wc_operation_merge:
1848 switch (pInfoData->treeconflict_action)
1850 case svn_wc_conflict_action_edit:
1851 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1852 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1853 break;
1854 case svn_wc_conflict_action_add:
1855 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1856 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1857 break;
1858 case svn_wc_conflict_action_delete:
1859 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1860 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1861 break;
1863 break;
1866 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1868 switch (pInfoData->treeconflict_operation)
1870 case svn_wc_operation_update:
1871 switch (pInfoData->treeconflict_action)
1873 case svn_wc_conflict_action_edit:
1874 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1875 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1876 break;
1877 case svn_wc_conflict_action_add:
1878 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1879 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1880 break;
1881 case svn_wc_conflict_action_delete:
1882 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1883 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1884 break;
1886 break;
1887 case svn_wc_operation_switch:
1888 switch (pInfoData->treeconflict_action)
1890 case svn_wc_conflict_action_edit:
1891 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1892 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1893 break;
1894 case svn_wc_conflict_action_add:
1895 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1896 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1897 break;
1898 case svn_wc_conflict_action_delete:
1899 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1900 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1901 break;
1903 break;
1904 case svn_wc_operation_merge:
1905 switch (pInfoData->treeconflict_action)
1907 case svn_wc_conflict_action_edit:
1908 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1909 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1910 break;
1911 case svn_wc_conflict_action_add:
1912 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1913 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1914 break;
1915 case svn_wc_conflict_action_delete:
1916 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1917 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1918 break;
1920 break;
1924 UINT uReasonID = 0;
1925 switch (pInfoData->treeconflict_reason)
1927 case svn_wc_conflict_reason_edited:
1928 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1929 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1930 break;
1931 case svn_wc_conflict_reason_obstructed:
1932 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1933 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1934 break;
1935 case svn_wc_conflict_reason_deleted:
1936 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1937 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1938 break;
1939 case svn_wc_conflict_reason_added:
1940 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1941 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1942 break;
1943 case svn_wc_conflict_reason_missing:
1944 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1945 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1946 break;
1947 case svn_wc_conflict_reason_unversioned:
1948 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1949 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1950 break;
1952 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1954 CTreeConflictEditorDlg dlg;
1955 dlg.SetConflictInfoText(sConflictReason);
1956 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1957 dlg.SetPath(treeConflictPath);
1958 INT_PTR dlgRet = dlg.DoModal();
1959 bRet = (dlgRet != IDCANCEL);
1963 #endif
1964 return bRet;
1967 bool CAppUtils::IsSSHPutty()
1969 CString sshclient=g_Git.m_Environment.GetEnv(_T("GIT_SSH"));
1970 sshclient=sshclient.MakeLower();
1971 if(sshclient.Find(_T("plink.exe"),0)>=0)
1973 return true;
1975 return false;
1978 CString CAppUtils::GetClipboardLink(const CString &skipGitPrefix, int paramsCount)
1980 if (!OpenClipboard(NULL))
1981 return CString();
1983 CString sClipboardText;
1984 HGLOBAL hglb = GetClipboardData(CF_TEXT);
1985 if (hglb)
1987 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1988 sClipboardText = CString(lpstr);
1989 GlobalUnlock(hglb);
1991 hglb = GetClipboardData(CF_UNICODETEXT);
1992 if (hglb)
1994 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
1995 sClipboardText = lpstr;
1996 GlobalUnlock(hglb);
1998 CloseClipboard();
2000 if(!sClipboardText.IsEmpty())
2002 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
2003 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
2005 if(sClipboardText.Find( _T("http://")) == 0)
2006 return sClipboardText;
2008 if(sClipboardText.Find( _T("https://")) == 0)
2009 return sClipboardText;
2011 if(sClipboardText.Find( _T("git://")) == 0)
2012 return sClipboardText;
2014 if(sClipboardText.Find( _T("ssh://")) == 0)
2015 return sClipboardText;
2017 if(sClipboardText.GetLength()>=2)
2018 if( sClipboardText[1] == _T(':') )
2019 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
2020 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
2021 return sClipboardText;
2023 // trim prefixes like "git clone "
2024 if (!skipGitPrefix.IsEmpty() && sClipboardText.Find(skipGitPrefix) == 0)
2026 sClipboardText = sClipboardText.Mid(skipGitPrefix.GetLength()).Trim();
2027 int spacePos = -1;
2028 while (paramsCount >= 0)
2030 --paramsCount;
2031 spacePos = sClipboardText.Find(_T(' '), spacePos + 1);
2032 if (spacePos == -1)
2033 break;
2035 if (spacePos > 0 && paramsCount < 0)
2036 sClipboardText = sClipboardText.Left(spacePos);
2037 return sClipboardText;
2041 return CString(_T(""));
2044 CString CAppUtils::ChooseRepository(CString *path)
2046 CBrowseFolder browseFolder;
2047 CRegString regLastResopitory = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2049 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
2050 CString strCloneDirectory;
2051 if(path)
2052 strCloneDirectory=*path;
2053 else
2055 strCloneDirectory = regLastResopitory;
2058 CString title;
2059 title.LoadString(IDS_CHOOSE_REPOSITORY);
2061 browseFolder.SetInfo(title);
2063 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
2065 regLastResopitory = strCloneDirectory;
2066 return strCloneDirectory;
2068 else
2070 return CString();
2074 bool CAppUtils::SendPatchMail(CTGitPathList& list)
2076 CSendMailDlg dlg;
2078 dlg.m_PathList = list;
2080 if(dlg.DoModal()==IDOK)
2082 if (dlg.m_PathList.IsEmpty())
2083 return FALSE;
2085 CGitProgressDlg progDlg;
2087 theApp.m_pMainWnd = &progDlg;
2088 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2090 progDlg.SetPathList(dlg.m_PathList);
2091 //ProjectProperties props;
2092 //props.ReadPropsPathList(dlg.m_pathList);
2093 //progDlg.SetProjectProperties(props);
2094 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2096 CSendMailPatch sendMailPatch(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2097 progDlg.SetSendMailOption(&sendMailPatch);
2099 progDlg.DoModal();
2101 return true;
2103 return false;
2106 bool CAppUtils::SendPatchMail(CString &cmd,CString &formatpatchoutput)
2108 CTGitPathList list;
2109 CString log=formatpatchoutput;
2110 int start=log.Find(cmd);
2111 if(start >=0)
2112 CString one=log.Tokenize(_T("\n"),start);
2113 else
2114 start = 0;
2116 while(start>=0)
2118 CString one=log.Tokenize(_T("\n"),start);
2119 one=one.Trim();
2120 if(one.IsEmpty() || one.Find(_T("Success")) == 0)
2121 continue;
2122 one.Replace(_T('/'),_T('\\'));
2123 CTGitPath path;
2124 path.SetFromWin(one);
2125 list.AddPath(path);
2127 if (!list.IsEmpty())
2129 return SendPatchMail(list);
2131 else
2133 CMessageBox::Show(NULL, IDS_ERR_NOPATCHES, IDS_APPNAME, MB_ICONINFORMATION);
2134 return true;
2139 int CAppUtils::GetLogOutputEncode(CGit *pGit)
2141 CString output;
2142 output = pGit->GetConfigValue(_T("i18n.logOutputEncoding"));
2143 if(output.IsEmpty())
2144 return CUnicodeUtils::GetCPCode(pGit->GetConfigValue(_T("i18n.commitencoding")));
2145 else
2147 return CUnicodeUtils::GetCPCode(output);
2150 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
2152 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
2153 int cp = CUnicodeUtils::GetCPCode(g_Git.GetConfigValue(_T("i18n.commitencoding")));
2155 bool stripComments = (CRegDWORD(_T("Software\\TortoiseGit\\StripCommentedLines"), FALSE) == TRUE);
2157 if (CRegDWORD(_T("Software\\TortoiseGit\\SanitizeCommitMsg"), TRUE) == TRUE)
2158 message.TrimRight(L" \r\n");
2160 int len = message.GetLength();
2161 int start = 0;
2162 while (start >= 0 && start < len)
2164 int oldStart = start;
2165 start = message.Find(L"\n", oldStart);
2166 CString line = message.Mid(oldStart);
2167 if (start != -1)
2169 line = line.Left(start - oldStart);
2170 ++start; // move forward so we don't find the same char again
2172 if (stripComments && (line.GetLength() >= 1 && line.GetAt(0) == '#') || (start < 0 && line.IsEmpty()))
2173 continue;
2174 line.TrimRight(L" \r");
2175 CStringA lineA = CUnicodeUtils::GetMulti(line + L"\n", cp);
2176 file.Write(lineA.GetBuffer(), lineA.GetLength());
2178 file.Close();
2179 return 0;
2182 bool CAppUtils::Pull(bool showPush)
2184 CPullFetchDlg dlg;
2185 dlg.m_IsPull = TRUE;
2186 if (dlg.DoModal() == IDOK)
2188 CString url = dlg.m_RemoteURL;
2190 if (dlg.m_bAutoLoad)
2192 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2195 CString cmd;
2196 CGitHash hashOld;
2197 if (g_Git.GetHash(hashOld, _T("HEAD")))
2199 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
2200 return false;
2203 CString cmdRebase;
2204 CString noff;
2205 CString ffonly;
2206 CString squash;
2207 CString nocommit;
2208 CString notags;
2210 if (dlg.m_bRebase)
2211 cmdRebase = "--rebase ";
2213 if (!dlg.m_bFetchTags)
2214 notags = _T("--no-tags");
2216 if (dlg.m_bNoFF)
2217 noff=_T("--no-ff");
2219 if (dlg.m_bFFonly)
2220 ffonly = _T("--ff-only");
2222 if (dlg.m_bSquash)
2223 squash = _T("--squash");
2225 if (dlg.m_bNoCommit)
2226 nocommit = _T("--no-commit");
2228 int ver = CAppUtils::GetMsysgitVersion();
2230 if(ver >= 0x01070203) //above 1.7.0.2
2231 cmdRebase += _T("--progress ");
2233 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);
2234 CProgressDlg progress;
2235 progress.m_GitCmd = cmd;
2236 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_DIFFS)));
2237 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_LOG)));
2238 INT_PTR pushButton = showPush ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH))) + IDC_PROGRESS_BUTTON1 : -1;
2240 CTGitPath gitPath = g_Git.m_CurrentDir;
2241 INT_PTR smUpdateButton = gitPath.HasSubmodules() ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE))) + IDC_PROGRESS_BUTTON1 : -1;
2243 INT_PTR ret = progress.DoModal();
2245 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)
2247 CChangedDlg dlg;
2248 dlg.m_pathList.AddPath(CTGitPath());
2249 dlg.DoModal();
2251 return true;
2254 CGitHash hashNew;
2255 if (g_Git.GetHash(hashNew, _T("HEAD")))
2257 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR);
2258 return FALSE;
2261 if( ret == IDC_PROGRESS_BUTTON1)
2263 if(hashOld == hashNew)
2265 if(progress.m_GitStatus == 0)
2266 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2267 return TRUE;
2270 CFileDiffDlg dlg;
2271 dlg.SetDiff(NULL, hashNew.ToString(), hashOld.ToString());
2272 dlg.DoModal();
2274 return true;
2276 else if ( ret == IDC_PROGRESS_BUTTON1 +1 )
2278 if(hashOld == hashNew)
2280 if(progress.m_GitStatus == 0)
2281 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2282 return true;
2285 CLogDlg dlg;
2286 dlg.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld.ToString() + _T("..") + hashNew.ToString(), 0);
2287 dlg.DoModal();
2289 else if (ret == pushButton)
2291 Push(_T(""));
2292 return true;
2294 else if (ret == smUpdateButton)
2296 CString sCmd;
2297 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
2299 CAppUtils::RunTortoiseGitProc(sCmd);
2300 return true;
2304 return true;
2307 bool CAppUtils::Fetch(CString remoteName, bool allowRebase, bool allRemotes)
2309 CPullFetchDlg dlg;
2310 dlg.m_PreSelectRemote = remoteName;
2311 dlg.m_bAllowRebase = allowRebase;
2312 dlg.m_IsPull=FALSE;
2313 dlg.m_bAllRemotes = allRemotes;
2315 if(dlg.DoModal()==IDOK)
2317 if(dlg.m_bAutoLoad)
2319 if (dlg.m_bAllRemotes)
2321 STRING_VECTOR list;
2322 g_Git.GetRemoteList(list);
2324 STRING_VECTOR::const_iterator it = list.begin();
2325 while (it != list.end())
2327 CString remote(*it);
2328 CAppUtils::LaunchPAgent(NULL, &remote);
2329 ++it;
2332 else
2333 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2336 CString url;
2337 url=dlg.m_RemoteURL;
2338 CString cmd;
2339 CString arg;
2341 int ver = CAppUtils::GetMsysgitVersion();
2343 if(ver >= 0x01070203) //above 1.7.0.2
2344 arg = _T("--progress ");
2346 if (dlg.m_bPrune == TRUE)
2347 arg += _T("--prune ");
2348 else if (dlg.m_bPrune == FALSE && ver >= 0x01080500)
2349 arg += _T("--no-prune ");
2351 if (dlg.m_bFetchTags == 1)
2353 arg += _T("--tags ");
2355 else if (dlg.m_bFetchTags == 0)
2357 arg += _T("--no-tags ");
2360 if (dlg.m_bAllRemotes)
2361 cmd.Format(_T("git.exe fetch --all -v %s"), arg);
2362 else
2363 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"), arg, url, dlg.m_RemoteBranchName);
2365 CProgressDlg progress;
2367 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2368 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_RESET)));
2370 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
2372 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2375 progress.m_GitCmd=cmd;
2376 INT_PTR userResponse;
2378 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
2380 CGitProgressDlg gitdlg;
2381 if (!dlg.m_bAllRemotes)
2382 gitdlg.SetUrl(url);
2383 gitdlg.SetCommand(CGitProgressList::GitProgress_Fetch);
2384 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);
2385 if (!dlg.m_bAllRemotes)
2386 gitdlg.SetRefSpec(dlg.m_RemoteBranchName);
2387 userResponse = gitdlg.DoModal();
2390 else
2391 userResponse = progress.DoModal();
2393 if (userResponse == IDC_PROGRESS_BUTTON1)
2395 CString cmd = _T("/command:log");
2396 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2397 RunTortoiseGitProc(cmd);
2398 return TRUE;
2400 else if (userResponse == IDC_PROGRESS_BUTTON1 + 1)
2402 CString currentBranch = g_Git.GetSymbolicRef();
2403 CString configName;
2404 configName.Format(_T("branch.%s.remote"), currentBranch);
2405 CString pullRemote = g_Git.GetConfigValue(configName);
2407 //Select pull-branch from current branch
2408 configName.Format(_T("branch.%s.merge"), currentBranch);
2409 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
2411 CString defaultUpstream;
2412 if (!pullRemote.IsEmpty() && !pullBranch.IsEmpty())
2413 defaultUpstream.Format(_T("remotes/%s/%s"), pullRemote, pullBranch);
2414 GitReset(&defaultUpstream, 2);
2415 return TRUE;
2417 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 2) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
2419 while(1)
2421 CRebaseDlg dlg;
2422 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2423 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
2424 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2425 INT_PTR response = dlg.DoModal();
2426 if(response == IDOK)
2428 return TRUE;
2430 else if (response == IDC_REBASE_POST_BUTTON)
2432 CString cmd = _T("/command:log");
2433 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2434 RunTortoiseGitProc(cmd);
2435 return TRUE;
2437 else if (response == IDC_REBASE_POST_BUTTON + 1)
2439 CString cmd, out, err;
2440 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2441 g_Git.m_CurrentDir,
2442 g_Git.FixBranchName(dlg.m_Upstream),
2443 g_Git.FixBranchName(dlg.m_Branch));
2444 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
2446 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2447 return FALSE;
2450 CAppUtils::SendPatchMail(cmd,out);
2451 return TRUE;
2453 else if (response == IDC_REBASE_POST_BUTTON + 2)
2454 continue;
2455 else if(response == IDCANCEL)
2456 return FALSE;
2458 return TRUE;
2460 else if (userResponse != IDCANCEL)
2461 return TRUE;
2463 return FALSE;
2466 bool CAppUtils::Push(CString selectLocalBranch)
2468 CPushDlg dlg;
2469 dlg.m_BranchSourceName = selectLocalBranch;
2470 CString error;
2471 DWORD exitcode = 0xFFFFFFFF;
2472 CTGitPathList list;
2473 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
2474 if (CHooks::Instance().PrePush(list,exitcode, error))
2476 if (exitcode)
2478 CString temp;
2479 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2480 //ReportError(temp);
2481 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2482 return false;
2486 if(dlg.DoModal()==IDOK)
2488 CString arg;
2490 if(dlg.m_bPack)
2491 arg += _T("--thin ");
2492 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
2493 arg += _T("--tags ");
2494 if(dlg.m_bForce)
2495 arg += _T("--force ");
2496 if (dlg.m_bSetUpstream)
2497 arg += _T("--set-upstream ");
2498 if (dlg.m_RecurseSubmodules == 1)
2499 arg += _T("--recurse-submodules=check ");
2500 if (dlg.m_RecurseSubmodules == 2)
2501 arg += _T("--recurse-submodules=on-demand ");
2503 int ver = CAppUtils::GetMsysgitVersion();
2505 if(ver >= 0x01070203) //above 1.7.0.2
2506 arg += _T("--progress ");
2508 CProgressDlg progress;
2510 STRING_VECTOR remotesList;
2511 if (dlg.m_bPushAllRemotes)
2512 g_Git.GetRemoteList(remotesList);
2513 else
2514 remotesList.push_back(dlg.m_URL);
2516 for (unsigned int i = 0; i < remotesList.size(); ++i)
2518 if (dlg.m_bAutoLoad)
2519 CAppUtils::LaunchPAgent(NULL, &remotesList[i]);
2521 CString cmd;
2522 if (dlg.m_bPushAllBranches)
2524 cmd.Format(_T("git.exe push --all %s \"%s\""),
2525 arg,
2526 remotesList[i]);
2528 if (dlg.m_bTags)
2530 progress.m_GitCmdList.push_back(cmd);
2531 cmd.Format(_T("git.exe push --tags %s \"%s\""), arg, remotesList[i]);
2534 else
2536 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2537 arg,
2538 remotesList[i],
2539 dlg.m_BranchSourceName);
2540 if (!dlg.m_BranchRemoteName.IsEmpty())
2542 cmd += _T(":") + dlg.m_BranchRemoteName;
2545 progress.m_GitCmdList.push_back(cmd);
2548 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2549 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2550 bool rejected = false;
2551 progress.m_PostCmdCallback = [&] ()
2553 if (progress.m_GitStatus)
2555 progress.m_PostCmdList.RemoveAll();
2556 rejected = progress.GetLogText().Find(_T("! [rejected]")) > 0;
2557 if (rejected)
2559 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
2560 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUFETCH)));
2562 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2566 INT_PTR ret = progress.DoModal();
2568 if(!progress.m_GitStatus)
2570 if (CHooks::Instance().PostPush(list,exitcode, error))
2572 if (exitcode)
2574 CString temp;
2575 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2576 //ReportError(temp);
2577 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2578 return false;
2581 if(ret == IDC_PROGRESS_BUTTON1)
2583 RequestPull(dlg.m_BranchRemoteName);
2585 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2587 Push(selectLocalBranch);
2589 return TRUE;
2591 else
2593 if (rejected)
2595 // failed, pull first
2596 if (ret == IDC_PROGRESS_BUTTON1)
2598 Pull(true);
2600 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
2602 Fetch(dlg.m_bPushAllRemotes ? _T("") : dlg.m_URL, true, !!dlg.m_bPushAllRemotes);
2604 else if (ret == IDC_PROGRESS_BUTTON1 + 2)
2606 Push(selectLocalBranch);
2609 else
2611 if (ret == IDC_PROGRESS_BUTTON1)
2613 Push(selectLocalBranch);
2618 return FALSE;
2621 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2623 CRequestPullDlg dlg;
2624 dlg.m_RepositoryURL = repositoryUrl;
2625 dlg.m_EndRevision = endrevision;
2626 if (dlg.DoModal()==IDOK)
2628 CString cmd;
2629 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2631 CSysProgressDlg sysProgressDlg;
2632 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2633 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST)));
2634 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2635 sysProgressDlg.SetShowProgressBar(false);
2636 sysProgressDlg.ShowModeless((HWND)NULL, true);
2638 CString tempFileName = GetTempFile();
2639 CString err;
2640 DeleteFile(tempFileName);
2641 CreateDirectory(tempFileName, NULL);
2642 tempFileName += _T("\\pullrequest.txt");
2643 if (g_Git.RunLogFile(cmd, tempFileName, &err))
2645 CString msg;
2646 msg.LoadString(IDS_ERR_PULLREUQESTFAILED);
2647 CMessageBox::Show(NULL, msg + _T("\n") + err, _T("TortoiseGit"), MB_OK);
2648 return false;
2651 if (sysProgressDlg.HasUserCancelled())
2653 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
2654 ::DeleteFile(tempFileName);
2655 return false;
2658 sysProgressDlg.Stop();
2660 if (dlg.m_bSendMail)
2662 CSendMailDlg dlg;
2663 dlg.m_PathList = CTGitPathList(CTGitPath(tempFileName));
2664 dlg.m_bCustomSubject = true;
2666 if (dlg.DoModal() == IDOK)
2668 if (dlg.m_PathList.IsEmpty())
2669 return FALSE;
2671 CGitProgressDlg progDlg;
2673 theApp.m_pMainWnd = &progDlg;
2674 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2676 progDlg.SetPathList(dlg.m_PathList);
2677 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2679 CSendMailCombineable sendMailCombineable(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2680 progDlg.SetSendMailOption(&sendMailCombineable);
2682 progDlg.DoModal();
2684 return true;
2686 return false;
2689 CAppUtils::LaunchAlternativeEditor(tempFileName);
2691 return true;
2694 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2696 CString strDir(szPath);
2697 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2699 strDir.AppendChar(_T('\\'));
2701 std::vector<CString> vPath;
2702 CString strTemp;
2703 bool bSuccess = false;
2705 for (int i=0;i<strDir.GetLength();++i)
2707 if (strDir.GetAt(i) != _T('\\'))
2709 strTemp.AppendChar(strDir.GetAt(i));
2711 else
2713 vPath.push_back(strTemp);
2714 strTemp.AppendChar(_T('\\'));
2718 for (auto vIter = vPath.begin(); vIter != vPath.end(); ++vIter)
2720 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2723 return bSuccess;
2726 void CAppUtils::RemoveTrailSlash(CString &path)
2728 if(path.IsEmpty())
2729 return ;
2731 // For URL, do not trim the slash just after the host name component.
2732 int index = path.Find(_T("://"));
2733 if (index >= 0)
2735 index += 4;
2736 index = path.Find(_T('/'), index);
2737 if (index == path.GetLength() - 1)
2738 return;
2741 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2743 path=path.Left(path.GetLength()-1);
2744 if(path.IsEmpty())
2745 return;
2749 bool CAppUtils::CheckUserData()
2751 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2753 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2755 CTGitPath path(g_Git.m_CurrentDir);
2756 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2757 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2758 dlg.SetTreeWidth(220);
2759 dlg.m_DefaultPage = _T("gitconfig");
2761 dlg.DoModal();
2762 dlg.HandleRestart();
2765 else
2766 return false;
2769 return true;
2772 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2773 CTGitPathList &pathList,
2774 CTGitPathList &selectedList,
2775 bool bSelectFilesForCommit)
2777 bool bFailed = true;
2779 if (!CheckUserData())
2780 return false;
2782 while (bFailed)
2784 bFailed = false;
2785 CCommitDlg dlg;
2786 dlg.m_sBugID = bugid;
2788 dlg.m_bWholeProject = bWholeProject;
2790 dlg.m_sLogMessage = sLogMsg;
2791 dlg.m_pathList = pathList;
2792 dlg.m_checkedPathList = selectedList;
2793 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2794 if (dlg.DoModal() == IDOK)
2796 if (dlg.m_pathList.IsEmpty())
2797 return false;
2798 // if the user hasn't changed the list of selected items
2799 // we don't use that list. Because if we would use the list
2800 // of pre-checked items, the dialog would show different
2801 // checked items on the next startup: it would only try
2802 // to check the parent folder (which might not even show)
2803 // instead, we simply use an empty list and let the
2804 // default checking do its job.
2805 if (!dlg.m_pathList.IsEqual(pathList))
2806 selectedList = dlg.m_pathList;
2807 pathList = dlg.m_updatedPathList;
2808 sLogMsg = dlg.m_sLogMessage;
2809 bSelectFilesForCommit = true;
2811 switch (dlg.m_PostCmd)
2813 case GIT_POSTCOMMIT_CMD_DCOMMIT:
2814 CAppUtils::SVNDCommit();
2815 break;
2816 case GIT_POSTCOMMIT_CMD_PUSH:
2817 CAppUtils::Push();
2818 break;
2819 case GIT_POSTCOMMIT_CMD_CREATETAG:
2820 CAppUtils::CreateBranchTag(TRUE);
2821 break;
2822 case GIT_POSTCOMMIT_CMD_PULL:
2823 CAppUtils::Pull(true);
2824 break;
2825 default:
2826 break;
2829 // CGitProgressDlg progDlg;
2830 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2831 // if (parser.HasVal(_T("closeonend")))
2832 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2833 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2834 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2835 // progDlg.SetPathList(dlg.m_pathList);
2836 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2837 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2838 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2839 // progDlg.SetItemCount(dlg.m_itemsCount);
2840 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2841 // progDlg.DoModal();
2842 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2843 // err = (DWORD)progDlg.DidErrorsOccur();
2844 // bFailed = progDlg.DidErrorsOccur();
2845 // bRet = progDlg.DidErrorsOccur();
2846 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2847 // if (DWORD(bFailRepeat)==0)
2848 // bFailed = false; // do not repeat if the user chose not to in the settings.
2851 return true;
2855 BOOL CAppUtils::SVNDCommit()
2857 CSVNDCommitDlg dcommitdlg;
2858 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2859 if (gitSetting == _T("")) {
2860 if (dcommitdlg.DoModal() != IDOK)
2862 return false;
2864 else
2866 if (dcommitdlg.m_remember)
2868 if (dcommitdlg.m_rmdir)
2870 gitSetting = _T("true");
2872 else
2874 gitSetting = _T("false");
2876 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2878 CString msg;
2879 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2880 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2886 BOOL IsStash = false;
2887 if(!g_Git.CheckCleanWorkTree())
2889 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2891 CSysProgressDlg sysProgressDlg;
2892 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2893 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2894 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2895 sysProgressDlg.SetShowProgressBar(false);
2896 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2897 sysProgressDlg.ShowModeless((HWND)NULL, true);
2899 CString cmd,out;
2900 cmd=_T("git.exe stash");
2901 if (g_Git.Run(cmd, &out, CP_UTF8))
2903 sysProgressDlg.Stop();
2904 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2905 return false;
2907 sysProgressDlg.Stop();
2909 IsStash =true;
2911 else
2913 return false;
2917 CProgressDlg progress;
2918 if (dcommitdlg.m_rmdir)
2920 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2922 else
2924 progress.m_GitCmd=_T("git.exe svn dcommit");
2926 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2928 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
2929 if( IsStash)
2931 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2933 CSysProgressDlg sysProgressDlg;
2934 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2935 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2936 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2937 sysProgressDlg.SetShowProgressBar(false);
2938 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2939 sysProgressDlg.ShowModeless((HWND)NULL, true);
2941 CString cmd,out;
2942 cmd=_T("git.exe stash pop");
2943 if (g_Git.Run(cmd, &out, CP_UTF8))
2945 sysProgressDlg.Stop();
2946 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2947 return false;
2949 sysProgressDlg.Stop();
2951 else
2953 return false;
2956 return TRUE;
2958 return FALSE;
2961 BOOL CAppUtils::Merge(CString *commit)
2963 if (!CheckUserData())
2964 return FALSE;
2966 CMergeDlg dlg;
2967 if(commit)
2968 dlg.m_initialRefName = *commit;
2970 if(dlg.DoModal()==IDOK)
2972 CString cmd;
2973 CString args;
2975 if(dlg.m_bNoFF)
2976 args += _T(" --no-ff");
2978 if(dlg.m_bSquash)
2979 args += _T(" --squash");
2981 if(dlg.m_bNoCommit)
2982 args += _T(" --no-commit");
2984 if (dlg.m_bLog)
2986 CString fmt;
2987 fmt.Format(_T(" --log=%d"), dlg.m_nLog);
2988 args += fmt;
2991 if (!dlg.m_MergeStrategy.IsEmpty())
2993 args += _T(" --strategy=") + dlg.m_MergeStrategy;
2994 if (!dlg.m_StrategyOption.IsEmpty())
2996 args += _T(" --strategy-option=") + dlg.m_StrategyOption;
2997 if (!dlg.m_StrategyParam.IsEmpty())
2998 args += _T("=") + dlg.m_StrategyParam;
3002 if(!dlg.m_strLogMesage.IsEmpty())
3004 CString logmsg = dlg.m_strLogMesage;
3005 logmsg.Replace(_T("\""), _T("\\\""));
3006 args += _T(" -m \"") + logmsg + _T("\"");
3008 cmd.Format(_T("git.exe merge %s %s"), args, g_Git.FixBranchName(dlg.m_VersionName));
3010 CProgressDlg Prodlg;
3011 Prodlg.m_GitCmd = cmd;
3013 BOOL hasGitSVN = CTGitPath(g_Git.m_CurrentDir).GetAdminDirMask() & ITEMIS_GITSVN;
3014 if (dlg.m_bNoCommit)
3015 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
3016 else
3018 if (dlg.m_bIsBranch)
3019 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
3021 if (hasGitSVN)
3022 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUSVNDCOMMIT)));
3025 Prodlg.m_PostCmdCallback = [&] ()
3027 if (Prodlg.m_GitStatus)
3029 Prodlg.m_PostCmdList.RemoveAll();
3031 CTGitPathList list;
3032 if (!g_Git.ListConflictFile(list) && !list.IsEmpty())
3034 // there are conflict files
3035 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROGRS_CMD_RESOLVE)));
3040 INT_PTR ret = Prodlg.DoModal();
3041 if (Prodlg.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3043 CTGitPathList pathlist;
3044 CTGitPathList selectedlist;
3045 pathlist.AddPath(g_Git.m_CurrentDir);
3046 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
3047 CString str;
3048 CAppUtils::Commit(CString(), false, str, pathlist, selectedlist, bSelectFilesForCommit);
3050 else if (ret == IDC_PROGRESS_BUTTON1)
3052 if (dlg.m_bNoCommit)
3054 CString sLogMsg;
3055 CTGitPathList pathList;
3056 CTGitPathList selectedList;
3057 return Commit(_T(""), TRUE, sLogMsg, pathList, selectedList, true);
3059 else if (dlg.m_bIsBranch)
3061 CString msg;
3062 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
3063 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
3065 CString cmd, out;
3066 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
3067 if (g_Git.Run(cmd, &out, CP_UTF8))
3068 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
3071 else if (hasGitSVN)
3072 CAppUtils::SVNDCommit();
3074 else if (ret == IDC_PROGRESS_BUTTON1 + 1 && hasGitSVN)
3075 CAppUtils::SVNDCommit();
3077 return !Prodlg.m_GitStatus;
3079 return false;
3082 BOOL CAppUtils::MergeAbort()
3084 CMergeAbortDlg dlg;
3085 if (dlg.DoModal() == IDOK)
3087 CString cmd;
3088 CString type;
3089 switch (dlg.m_ResetType)
3091 case 0:
3092 type = _T("--mixed");
3093 break;
3094 case 1:
3095 type = _T("--hard");
3096 break;
3097 default:
3098 dlg.m_ResetType = 0;
3099 type = _T("--mixed");
3100 break;
3102 cmd.Format(_T("git.exe reset %s HEAD --"), type);
3104 while (true)
3106 CProgressDlg progress;
3107 progress.m_GitCmd = cmd;
3109 CTGitPath gitPath = g_Git.m_CurrentDir;
3110 if (gitPath.HasSubmodules() && dlg.m_ResetType == 1)
3111 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3113 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
3115 INT_PTR ret;
3116 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
3118 CGitProgressDlg gitdlg;
3119 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
3120 gitdlg.SetRevision(_T("HEAD"));
3121 gitdlg.SetResetType(dlg.m_ResetType + 1);
3122 ret = gitdlg.DoModal();
3124 else
3125 ret = progress.DoModal();
3127 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 1 && ret == IDC_PROGRESS_BUTTON1)
3129 CString sCmd;
3130 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3132 CCommonAppUtils::RunTortoiseGitProc(sCmd);
3133 return TRUE;
3135 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3136 continue; // retry
3137 else if (ret == IDOK)
3138 return TRUE;
3139 else
3140 break;
3143 return FALSE;
3146 void CAppUtils::EditNote(GitRev *rev)
3148 if (!CheckUserData())
3149 return;
3151 CInputDlg dlg;
3152 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3153 dlg.m_sInputText = rev->m_Notes;
3154 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3155 //dlg.m_pProjectProperties = &m_ProjectProperties;
3156 dlg.m_bUseLogWidth = true;
3157 if(dlg.DoModal() == IDOK)
3159 CString cmd,output;
3160 cmd=_T("notes add -f -F \"");
3162 CString tempfile=::GetTempFile();
3163 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
3164 cmd += tempfile;
3165 cmd += _T("\" ");
3166 cmd += rev->m_CommitHash.ToString();
3170 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
3172 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3175 else
3177 rev->m_Notes = dlg.m_sInputText;
3179 }catch(...)
3181 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3183 ::DeleteFile(tempfile);
3188 int CAppUtils::GetMsysgitVersion()
3190 if (g_Git.ms_LastMsysGitVersion)
3191 return g_Git.ms_LastMsysGitVersion;
3193 CString cmd;
3194 CString versiondebug;
3195 CString version;
3197 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3198 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3200 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
3202 __int64 time=0;
3203 if (!g_Git.GetFileModifyTime(gitpath, &time))
3205 if((DWORD)time == regTime)
3207 g_Git.ms_LastMsysGitVersion = regVersion;
3208 return regVersion;
3212 CString err;
3213 cmd = _T("git.exe --version");
3214 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
3216 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);
3217 return -1;
3220 int start=0;
3221 int ver = 0;
3223 versiondebug = version;
3227 CString str=version.Tokenize(_T("."), start);
3228 int space = str.ReverseFind(_T(' '));
3229 str = str.Mid(space+1,start);
3230 ver = _ttol(str);
3231 ver <<=24;
3233 version = version.Mid(start);
3234 start = 0;
3236 str = version.Tokenize(_T("."), start);
3238 ver |= (_ttol(str) & 0xFF) << 16;
3240 str = version.Tokenize(_T("."), start);
3241 ver |= (_ttol(str) & 0xFF) << 8;
3243 str = version.Tokenize(_T("."), start);
3244 ver |= (_ttol(str) & 0xFF);
3246 catch(...)
3248 if (!ver)
3250 CMessageBox::Show(NULL, _T("Could not parse git.exe version number: \"") + versiondebug + _T("\""), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
3251 return -1;
3255 regTime = time&0xFFFFFFFF;
3256 regVersion = ver;
3257 g_Git.ms_LastMsysGitVersion = ver;
3259 return ver;
3262 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
3264 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
3266 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3268 if (hShell.IsValid()) {
3269 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
3270 if (pfnSHGPSFW) {
3271 IPropertyStore *pps;
3272 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
3273 if (SUCCEEDED(hr)) {
3274 PROPVARIANT var;
3275 var.vt = VT_BOOL;
3276 var.boolVal = VARIANT_TRUE;
3277 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
3278 pps->Release();
3284 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
3286 ASSERT(dialogname.GetLength() < 70);
3287 ASSERT(urlorpath.GetLength() < MAX_PATH);
3288 WCHAR pathbuf[MAX_PATH] = {0};
3290 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
3292 wcscat_s(pathbuf, L" - ");
3293 wcscat_s(pathbuf, dialogname);
3294 wcscat_s(pathbuf, L" - ");
3295 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
3296 SetWindowText(hWnd, pathbuf);
3299 bool CAppUtils::BisectStart(CString lastGood, CString firstBad)
3301 if (!g_Git.CheckCleanWorkTree())
3303 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
3305 CSysProgressDlg sysProgressDlg;
3306 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
3307 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
3308 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
3309 sysProgressDlg.SetShowProgressBar(false);
3310 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
3311 sysProgressDlg.ShowModeless((HWND)NULL, true);
3313 CString cmd, out;
3314 cmd = _T("git.exe stash");
3315 if (g_Git.Run(cmd, &out, CP_UTF8))
3317 sysProgressDlg.Stop();
3318 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK);
3319 return false;
3321 sysProgressDlg.Stop();
3323 else
3324 return false;
3327 CBisectStartDlg bisectStartDlg;
3329 if (!lastGood.IsEmpty())
3330 bisectStartDlg.m_sLastGood = lastGood;
3331 if (!firstBad.IsEmpty())
3332 bisectStartDlg.m_sFirstBad = firstBad;
3334 if (bisectStartDlg.DoModal() == IDOK)
3336 CProgressDlg progress;
3337 theApp.m_pMainWnd = &progress;
3338 progress.m_GitCmdList.push_back(_T("git.exe bisect start"));
3339 progress.m_GitCmdList.push_back(_T("git.exe bisect good ") + bisectStartDlg.m_LastGoodRevision);
3340 progress.m_GitCmdList.push_back(_T("git.exe bisect bad ") + bisectStartDlg.m_FirstBadRevision);
3342 CTGitPath path(g_Git.m_CurrentDir);
3344 if (path.HasSubmodules())
3345 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3347 INT_PTR ret = progress.DoModal();
3348 if (path.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
3350 CString sCmd;
3351 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3353 CAppUtils::RunTortoiseGitProc(sCmd);
3354 return true;
3356 else if (ret == IDOK)
3357 return true;
3360 return false;
3363 int CAppUtils::Git2GetUserPassword(git_cred **out, const char *url, const char *username_from_url, unsigned int /*allowed_types*/, void * /*payload*/)
3365 CUserPassword dlg;
3366 dlg.m_URL = CUnicodeUtils::GetUnicode(url, CP_UTF8);
3367 if (username_from_url)
3368 dlg.m_UserName = CUnicodeUtils::GetUnicode(username_from_url, CP_UTF8);
3370 CStringA username, password;
3371 if (dlg.DoModal() == IDOK)
3373 username = CUnicodeUtils::GetMulti(dlg.m_UserName, CP_UTF8);
3374 password = CUnicodeUtils::GetMulti(dlg.m_Password, CP_UTF8);
3375 return git_cred_userpass_plaintext_new(out, username, password);
3377 return -1;