Avoid CString.GetBuffer() and make use of default (LPCTSTR) operator
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob8def0fa9e813749cf44d31a4ed6fd3af005b8646
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 CProgressDlg progress;
1159 progress.m_GitCmd = cmd;
1161 INT_PTR idPull = -1;
1162 INT_PTR idSubmoduleUpdate = -1;
1163 INT_PTR idMerge = -1;
1165 CTGitPath gitPath = g_Git.m_CurrentDir;
1166 if (gitPath.HasSubmodules())
1167 idSubmoduleUpdate = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1168 idPull = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
1169 CString currentBranch;
1170 bool hasBranch = CGit::GetCurrentBranchFromFile(g_Git.m_CurrentDir, currentBranch) == 0;
1171 if (hasBranch)
1172 idMerge = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUMERGE)));
1174 INT_PTR ret = progress.DoModal();
1175 if (idSubmoduleUpdate >= 0 && ret == IDC_PROGRESS_BUTTON1 + idSubmoduleUpdate)
1177 CString sCmd;
1178 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1180 RunTortoiseGitProc(sCmd);
1181 return TRUE;
1183 else if (ret == IDC_PROGRESS_BUTTON1 + idPull)
1185 Pull();
1186 return TRUE;
1188 else if (ret == IDC_PROGRESS_BUTTON1 + idMerge)
1190 Merge(&currentBranch);
1191 return TRUE;
1193 else if (ret == IDOK)
1194 return TRUE;
1196 return FALSE;
1199 class CIgnoreFile : public CStdioFile
1201 public:
1202 STRING_VECTOR m_Items;
1203 CString m_eol;
1205 virtual BOOL ReadString(CString& rString)
1207 if (GetPosition() == 0)
1209 unsigned char utf8bom[] = { 0xEF, 0xBB, 0xBF };
1210 char buf[3] = { 0, 0, 0 };
1211 Read(buf, 3);
1212 if (memcpy(buf, utf8bom, sizeof(utf8bom)))
1214 SeekToBegin();
1218 CStringA strA;
1219 char lastChar = '\0';
1220 for (char c = '\0'; Read(&c, 1) == 1; lastChar = c)
1222 if (c == '\r')
1223 continue;
1224 if (c == '\n')
1226 m_eol = lastChar == '\r' ? _T("\r\n") : _T("\n");
1227 break;
1229 strA.AppendChar(c);
1231 if (strA.IsEmpty())
1232 return FALSE;
1234 rString = CUnicodeUtils::GetUnicode(strA);
1235 return TRUE;
1238 void ResetState()
1240 m_Items.clear();
1241 m_eol = _T("");
1245 bool CAppUtils::OpenIgnoreFile(CIgnoreFile &file, const CString& filename)
1247 file.ResetState();
1248 if (!file.Open(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate | CFile::typeBinary))
1250 CMessageBox::Show(NULL, filename + _T(" Open Failure"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1251 return false;
1254 if (file.GetLength() > 0)
1256 CString fileText;
1257 while (file.ReadString(fileText))
1258 file.m_Items.push_back(fileText);
1259 file.Seek(file.GetLength() - 1, 0);
1260 char lastchar[1] = { 0 };
1261 file.Read(lastchar, 1);
1262 file.SeekToEnd();
1263 if (lastchar[0] != '\n')
1265 CStringA eol = CStringA(file.m_eol.IsEmpty() ? _T("\n") : file.m_eol);
1266 file.Write(eol, eol.GetLength());
1269 else
1270 file.SeekToEnd();
1272 return true;
1275 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1277 CIgnoreDlg ignoreDlg;
1278 if (ignoreDlg.DoModal() == IDOK)
1280 CString ignorefile;
1281 ignorefile = g_Git.m_CurrentDir + _T("\\");
1283 switch (ignoreDlg.m_IgnoreFile)
1285 case 0:
1286 ignorefile += _T(".gitignore");
1287 break;
1288 case 2:
1289 g_GitAdminDir.GetAdminDirPath(g_Git.m_CurrentDir, ignorefile);
1290 ignorefile += _T("info/exclude");
1291 break;
1294 CIgnoreFile file;
1297 if (ignoreDlg.m_IgnoreFile != 1 && !OpenIgnoreFile(file, ignorefile))
1298 return false;
1300 for (int i = 0; i < path.GetCount(); ++i)
1302 if (ignoreDlg.m_IgnoreFile == 1)
1304 ignorefile = g_Git.m_CurrentDir + _T("\\") + path[i].GetContainingDirectory().GetWinPathString() + _T("\\.gitignore");
1305 if (!OpenIgnoreFile(file, ignorefile))
1306 return false;
1309 CString ignorePattern;
1310 if (ignoreDlg.m_IgnoreType == 0)
1312 if (ignoreDlg.m_IgnoreFile != 1 && !path[i].GetContainingDirectory().GetGitPathString().IsEmpty())
1313 ignorePattern += _T("/") + path[i].GetContainingDirectory().GetGitPathString();
1315 ignorePattern += _T("/");
1317 if (IsMask)
1319 ignorePattern += _T("*") + path[i].GetFileExtension();
1321 else
1323 ignorePattern += path[i].GetFileOrDirectoryName();
1326 // escape [ and ] so that files get ignored correctly
1327 ignorePattern.Replace(_T("["), _T("\\["));
1328 ignorePattern.Replace(_T("]"), _T("\\]"));
1330 bool found = false;
1331 for (size_t j = 0; j < file.m_Items.size(); ++j)
1333 if (file.m_Items[j] == ignorePattern)
1335 found = true;
1336 break;
1339 if (!found)
1341 file.m_Items.push_back(ignorePattern);
1342 ignorePattern += file.m_eol.IsEmpty() ? _T("\n") : file.m_eol;
1343 CStringA ignorePatternA = CUnicodeUtils::GetUTF8(ignorePattern);
1344 file.Write(ignorePatternA, ignorePatternA.GetLength());
1347 if (ignoreDlg.m_IgnoreFile == 1)
1348 file.Close();
1351 if (ignoreDlg.m_IgnoreFile != 1)
1352 file.Close();
1354 catch(...)
1356 file.Abort();
1357 return false;
1360 return true;
1362 return false;
1366 bool CAppUtils::GitReset(CString *CommitHash,int type)
1368 CResetDlg dlg;
1369 dlg.m_ResetType=type;
1370 dlg.m_ResetToVersion=*CommitHash;
1371 dlg.m_initialRefName = *CommitHash;
1372 if (dlg.DoModal() == IDOK)
1374 CString cmd;
1375 CString type;
1376 switch(dlg.m_ResetType)
1378 case 0:
1379 type=_T("--soft");
1380 break;
1381 case 1:
1382 type=_T("--mixed");
1383 break;
1384 case 2:
1385 type=_T("--hard");
1386 break;
1387 default:
1388 dlg.m_ResetType = 1;
1389 type=_T("--mixed");
1390 break;
1392 cmd.Format(_T("git.exe reset %s %s --"),type, dlg.m_ResetToVersion);
1394 while (true)
1396 CProgressDlg progress;
1397 progress.m_GitCmd=cmd;
1399 CTGitPath gitPath = g_Git.m_CurrentDir;
1400 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2)
1401 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1403 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
1405 INT_PTR ret;
1406 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
1408 CGitProgressDlg gitdlg;
1409 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
1410 gitdlg.SetRevision(dlg.m_ResetToVersion);
1411 gitdlg.SetResetType(dlg.m_ResetType);
1412 ret = gitdlg.DoModal();
1414 else
1415 ret = progress.DoModal();
1417 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 2 && ret == IDC_PROGRESS_BUTTON1)
1419 CString sCmd;
1420 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1422 RunTortoiseGitProc(sCmd);
1423 return TRUE;
1425 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
1426 continue; // retry
1427 else if (ret == IDOK)
1428 return TRUE;
1429 else
1430 break;
1433 return FALSE;
1436 void CAppUtils::DescribeConflictFile(bool mode, bool base,CString &descript)
1438 if(mode == FALSE)
1440 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE));
1441 return;
1443 if(base)
1445 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED));
1446 return;
1448 descript = CString(MAKEINTRESOURCE(IDS_PROC_CREATED));
1449 return;
1452 void CAppUtils::RemoveTempMergeFile(CTGitPath &path)
1454 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("LOCAL"), path));
1455 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("REMOTE"), path));
1456 ::DeleteFile(CAppUtils::GetMergeTempFile(_T("BASE"), path));
1458 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1460 CString file;
1461 file=g_Git.m_CurrentDir+_T("\\") + merge.GetWinPathString()+_T(".")+type+merge.GetFileExtension();
1463 return file;
1466 bool ParseHashesFromLsFile(BYTE_VECTOR &out, CString &hash1, CString &hash2, CString &hash3)
1468 unsigned int pos = 0;
1469 CString one;
1470 CString part;
1472 while (pos >= 0 && pos < out.size())
1474 one.Empty();
1476 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1477 int tabstart = 0;
1478 one.Tokenize(_T("\t"), tabstart);
1480 tabstart = 0;
1481 part = one.Tokenize(_T(" "), tabstart); //Tag
1482 part = one.Tokenize(_T(" "), tabstart); //Mode
1483 part = one.Tokenize(_T(" "), tabstart); //Hash
1484 CString hash = part;
1485 part = one.Tokenize(_T("\t"), tabstart); //Stage
1486 int stage = _ttol(part);
1487 if (stage == 1)
1488 hash1 = hash;
1489 else if (stage == 2)
1490 hash2 = hash;
1491 else if (stage == 3)
1493 hash3 = hash;
1494 return true;
1497 pos = out.findNextString(pos);
1500 return false;
1503 bool CAppUtils::ConflictEdit(CTGitPath &path,bool /*bAlternativeTool*/,bool revertTheirMy)
1505 bool bRet = false;
1507 CTGitPath merge=path;
1508 CTGitPath directory = merge.GetDirectory();
1510 // we have the conflicted file (%merged)
1511 // now look for the other required files
1512 //GitStatus stat;
1513 //stat.GetStatus(merge);
1514 //if (stat.status == NULL)
1515 // return false;
1517 BYTE_VECTOR vector;
1519 CString cmd;
1520 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1522 if (g_Git.Run(cmd, &vector))
1524 return FALSE;
1527 if (merge.IsDirectory())
1529 CString baseHash, localHash, remoteHash;
1530 if (!ParseHashesFromLsFile(vector, baseHash, localHash, remoteHash))
1531 return FALSE;
1533 CString msg;
1534 msg.Format(_T("BASE: %s\nLOCAL: %s\nREMOTE: %s"), baseHash, localHash, remoteHash);
1535 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK);
1536 return TRUE;
1539 CTGitPathList list;
1540 if (list.ParserFromLsFile(vector))
1542 CMessageBox::Show(NULL, _T("Parse ls-files failed!"), _T("TortoiseGit"), MB_OK);
1543 return FALSE;
1546 if (list.IsEmpty())
1547 return FALSE;
1549 CTGitPath theirs;
1550 CTGitPath mine;
1551 CTGitPath base;
1553 if (revertTheirMy)
1555 mine.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1556 theirs.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1558 else
1560 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1561 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1563 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1565 CString format;
1567 //format=_T("git.exe cat-file blob \":%d:%s\"");
1568 format = _T("git.exe checkout-index --temp --stage=%d -- \"%s\"");
1569 CFile tempfile;
1570 //create a empty file, incase stage is not three
1571 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1572 tempfile.Close();
1573 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1574 tempfile.Close();
1575 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1576 tempfile.Close();
1578 bool b_base=false, b_local=false, b_remote=false;
1580 for (int i = 0; i< list.GetCount(); ++i)
1582 CString cmd;
1583 CString outfile;
1584 cmd.Empty();
1585 outfile.Empty();
1587 if( list[i].m_Stage == 1)
1589 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1590 b_base = true;
1591 outfile = base.GetWinPathString();
1594 if( list[i].m_Stage == 2 )
1596 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1597 b_local = true;
1598 outfile = mine.GetWinPathString();
1601 if( list[i].m_Stage == 3 )
1603 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1604 b_remote = true;
1605 outfile = theirs.GetWinPathString();
1607 CString output, err;
1608 if(!outfile.IsEmpty())
1609 if (!g_Git.Run(cmd, &output, &err, CP_UTF8))
1611 CString file;
1612 int start =0 ;
1613 file = output.Tokenize(_T("\t"), start);
1614 ::MoveFileEx(file,outfile,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
1616 else
1618 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1622 if(b_local && b_remote )
1624 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1625 if( revertTheirMy )
1626 bRet = !!CAppUtils::StartExtMerge(base, mine, theirs, merge, _T("BASE"), _T("REMOTE"), _T("LOCAL"));
1627 else
1628 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge,_T("BASE"), _T("REMOTE"), _T("LOCAL"));
1631 else
1633 ::DeleteFile(mine.GetWinPathString());
1634 ::DeleteFile(theirs.GetWinPathString());
1635 ::DeleteFile(base.GetWinPathString());
1637 CDeleteConflictDlg dlg;
1638 DescribeConflictFile(b_local, b_base,dlg.m_LocalStatus);
1639 DescribeConflictFile(b_remote,b_base,dlg.m_RemoteStatus);
1640 CGitHash localHash, remoteHash;
1641 if (!g_Git.GetHash(localHash, _T("HEAD")))
1642 dlg.m_LocalHash = localHash.ToString();
1643 if (!g_Git.GetHash(remoteHash, _T("MERGE_HEAD")))
1644 dlg.m_RemoteHash = remoteHash.ToString();
1645 else if (!g_Git.GetHash(remoteHash, _T("rebase-apply/original-commit")))
1646 dlg.m_RemoteHash = remoteHash.ToString();
1647 else if (!g_Git.GetHash(remoteHash, _T("CHERRY_PICK_HEAD")))
1648 dlg.m_RemoteHash = remoteHash.ToString();
1649 else if (!g_Git.GetHash(remoteHash, _T("REVERT_HEAD")))
1650 dlg.m_RemoteHash = remoteHash.ToString();
1651 dlg.m_bShowModifiedButton=b_base;
1652 dlg.m_File=merge.GetGitPathString();
1653 if(dlg.DoModal() == IDOK)
1655 CString cmd,out;
1656 if(dlg.m_bIsDelete)
1658 cmd.Format(_T("git.exe rm -- \"%s\""),merge.GetGitPathString());
1660 else
1661 cmd.Format(_T("git.exe add -- \"%s\""),merge.GetGitPathString());
1663 if (g_Git.Run(cmd, &out, CP_UTF8))
1665 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1666 return FALSE;
1668 return TRUE;
1670 else
1671 return FALSE;
1674 #if 0
1675 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1676 base, theirs, mine, merge);
1677 #endif
1678 #if 0
1679 if (stat.status->text_status == svn_wc_status_conflicted)
1681 // we have a text conflict, use our merge tool to resolve the conflict
1683 CTSVNPath theirs(directory);
1684 CTSVNPath mine(directory);
1685 CTSVNPath base(directory);
1686 bool bConflictData = false;
1688 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1690 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1691 bConflictData = true;
1693 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1695 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1696 bConflictData = true;
1698 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1700 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1701 bConflictData = true;
1703 else
1705 mine = merge;
1707 if (bConflictData)
1708 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1709 base, theirs, mine, merge);
1712 if (stat.status->prop_status == svn_wc_status_conflicted)
1714 // we have a property conflict
1715 CTSVNPath prej(directory);
1716 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1718 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1719 // there's a problem: the prej file contains a _description_ of the conflict, and
1720 // that description string might be translated. That means we have no way of parsing
1721 // the file to find out the conflicting values.
1722 // The only thing we can do: show a dialog with the conflict description, then
1723 // let the user either accept the existing property or open the property edit dialog
1724 // to manually change the properties and values. And a button to mark the conflict as
1725 // resolved.
1726 CEditPropConflictDlg dlg;
1727 dlg.SetPrejFile(prej);
1728 dlg.SetConflictedItem(merge);
1729 bRet = (dlg.DoModal() != IDCANCEL);
1733 if (stat.status->tree_conflict)
1735 // we have a tree conflict
1736 SVNInfo info;
1737 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1738 if (pInfoData)
1740 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1742 CTSVNPath theirs(directory);
1743 CTSVNPath mine(directory);
1744 CTSVNPath base(directory);
1745 bool bConflictData = false;
1747 if (pInfoData->treeconflict_theirfile)
1749 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1750 bConflictData = true;
1752 if (pInfoData->treeconflict_basefile)
1754 base.AppendPathString(pInfoData->treeconflict_basefile);
1755 bConflictData = true;
1757 if (pInfoData->treeconflict_myfile)
1759 mine.AppendPathString(pInfoData->treeconflict_myfile);
1760 bConflictData = true;
1762 else
1764 mine = merge;
1766 if (bConflictData)
1767 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1768 base, theirs, mine, merge);
1770 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1772 CString sConflictAction;
1773 CString sConflictReason;
1774 CString sResolveTheirs;
1775 CString sResolveMine;
1776 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1777 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1779 if (pInfoData->treeconflict_nodekind == svn_node_file)
1781 switch (pInfoData->treeconflict_operation)
1783 case svn_wc_operation_update:
1784 switch (pInfoData->treeconflict_action)
1786 case svn_wc_conflict_action_edit:
1787 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1788 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1789 break;
1790 case svn_wc_conflict_action_add:
1791 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1792 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1793 break;
1794 case svn_wc_conflict_action_delete:
1795 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1796 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1797 break;
1799 break;
1800 case svn_wc_operation_switch:
1801 switch (pInfoData->treeconflict_action)
1803 case svn_wc_conflict_action_edit:
1804 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1805 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1806 break;
1807 case svn_wc_conflict_action_add:
1808 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1809 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1810 break;
1811 case svn_wc_conflict_action_delete:
1812 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1813 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1814 break;
1816 break;
1817 case svn_wc_operation_merge:
1818 switch (pInfoData->treeconflict_action)
1820 case svn_wc_conflict_action_edit:
1821 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1822 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1823 break;
1824 case svn_wc_conflict_action_add:
1825 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1826 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1827 break;
1828 case svn_wc_conflict_action_delete:
1829 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1830 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1831 break;
1833 break;
1836 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1838 switch (pInfoData->treeconflict_operation)
1840 case svn_wc_operation_update:
1841 switch (pInfoData->treeconflict_action)
1843 case svn_wc_conflict_action_edit:
1844 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1845 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1846 break;
1847 case svn_wc_conflict_action_add:
1848 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1849 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1850 break;
1851 case svn_wc_conflict_action_delete:
1852 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1853 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1854 break;
1856 break;
1857 case svn_wc_operation_switch:
1858 switch (pInfoData->treeconflict_action)
1860 case svn_wc_conflict_action_edit:
1861 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1862 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1863 break;
1864 case svn_wc_conflict_action_add:
1865 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1866 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1867 break;
1868 case svn_wc_conflict_action_delete:
1869 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1870 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1871 break;
1873 break;
1874 case svn_wc_operation_merge:
1875 switch (pInfoData->treeconflict_action)
1877 case svn_wc_conflict_action_edit:
1878 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1879 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1880 break;
1881 case svn_wc_conflict_action_add:
1882 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1883 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1884 break;
1885 case svn_wc_conflict_action_delete:
1886 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1887 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1888 break;
1890 break;
1894 UINT uReasonID = 0;
1895 switch (pInfoData->treeconflict_reason)
1897 case svn_wc_conflict_reason_edited:
1898 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1899 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1900 break;
1901 case svn_wc_conflict_reason_obstructed:
1902 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1903 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1904 break;
1905 case svn_wc_conflict_reason_deleted:
1906 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1907 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1908 break;
1909 case svn_wc_conflict_reason_added:
1910 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1911 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1912 break;
1913 case svn_wc_conflict_reason_missing:
1914 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1915 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1916 break;
1917 case svn_wc_conflict_reason_unversioned:
1918 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1919 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1920 break;
1922 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1924 CTreeConflictEditorDlg dlg;
1925 dlg.SetConflictInfoText(sConflictReason);
1926 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1927 dlg.SetPath(treeConflictPath);
1928 INT_PTR dlgRet = dlg.DoModal();
1929 bRet = (dlgRet != IDCANCEL);
1933 #endif
1934 return bRet;
1937 bool CAppUtils::IsSSHPutty()
1939 CString sshclient=g_Git.m_Environment.GetEnv(_T("GIT_SSH"));
1940 sshclient=sshclient.MakeLower();
1941 if(sshclient.Find(_T("plink.exe"),0)>=0)
1943 return true;
1945 return false;
1948 CString CAppUtils::GetClipboardLink(const CString &skipGitPrefix, int paramsCount)
1950 if (!OpenClipboard(NULL))
1951 return CString();
1953 CString sClipboardText;
1954 HGLOBAL hglb = GetClipboardData(CF_TEXT);
1955 if (hglb)
1957 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1958 sClipboardText = CString(lpstr);
1959 GlobalUnlock(hglb);
1961 hglb = GetClipboardData(CF_UNICODETEXT);
1962 if (hglb)
1964 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
1965 sClipboardText = lpstr;
1966 GlobalUnlock(hglb);
1968 CloseClipboard();
1970 if(!sClipboardText.IsEmpty())
1972 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
1973 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
1975 if(sClipboardText.Find( _T("http://")) == 0)
1976 return sClipboardText;
1978 if(sClipboardText.Find( _T("https://")) == 0)
1979 return sClipboardText;
1981 if(sClipboardText.Find( _T("git://")) == 0)
1982 return sClipboardText;
1984 if(sClipboardText.Find( _T("ssh://")) == 0)
1985 return sClipboardText;
1987 if(sClipboardText.GetLength()>=2)
1988 if( sClipboardText[1] == _T(':') )
1989 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
1990 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
1991 return sClipboardText;
1993 // trim prefixes like "git clone "
1994 if (!skipGitPrefix.IsEmpty() && sClipboardText.Find(skipGitPrefix) == 0)
1996 sClipboardText = sClipboardText.Mid(skipGitPrefix.GetLength()).Trim();
1997 int spacePos = -1;
1998 while (paramsCount >= 0)
2000 --paramsCount;
2001 spacePos = sClipboardText.Find(_T(' '), spacePos + 1);
2002 if (spacePos == -1)
2003 break;
2005 if (spacePos > 0 && paramsCount < 0)
2006 sClipboardText = sClipboardText.Left(spacePos);
2007 return sClipboardText;
2011 return CString(_T(""));
2014 CString CAppUtils::ChooseRepository(CString *path)
2016 CBrowseFolder browseFolder;
2017 CRegString regLastResopitory = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2019 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
2020 CString strCloneDirectory;
2021 if(path)
2022 strCloneDirectory=*path;
2023 else
2025 strCloneDirectory = regLastResopitory;
2028 CString title;
2029 title.LoadString(IDS_CHOOSE_REPOSITORY);
2031 browseFolder.SetInfo(title);
2033 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
2035 regLastResopitory = strCloneDirectory;
2036 return strCloneDirectory;
2038 else
2040 return CString();
2044 bool CAppUtils::SendPatchMail(CTGitPathList& list)
2046 CSendMailDlg dlg;
2048 dlg.m_PathList = list;
2050 if(dlg.DoModal()==IDOK)
2052 if (dlg.m_PathList.IsEmpty())
2053 return FALSE;
2055 CGitProgressDlg progDlg;
2057 theApp.m_pMainWnd = &progDlg;
2058 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2060 progDlg.SetPathList(dlg.m_PathList);
2061 //ProjectProperties props;
2062 //props.ReadPropsPathList(dlg.m_pathList);
2063 //progDlg.SetProjectProperties(props);
2064 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2066 CSendMailPatch sendMailPatch(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2067 progDlg.SetSendMailOption(&sendMailPatch);
2069 progDlg.DoModal();
2071 return true;
2073 return false;
2076 bool CAppUtils::SendPatchMail(CString &cmd,CString &formatpatchoutput)
2078 CTGitPathList list;
2079 CString log=formatpatchoutput;
2080 int start=log.Find(cmd);
2081 if(start >=0)
2082 CString one=log.Tokenize(_T("\n"),start);
2083 else
2084 start = 0;
2086 while(start>=0)
2088 CString one=log.Tokenize(_T("\n"),start);
2089 one=one.Trim();
2090 if(one.IsEmpty() || one.Find(_T("Success")) == 0)
2091 continue;
2092 one.Replace(_T('/'),_T('\\'));
2093 CTGitPath path;
2094 path.SetFromWin(one);
2095 list.AddPath(path);
2097 if (!list.IsEmpty())
2099 return SendPatchMail(list);
2101 else
2103 CMessageBox::Show(NULL, IDS_ERR_NOPATCHES, IDS_APPNAME, MB_ICONINFORMATION);
2104 return true;
2109 int CAppUtils::GetLogOutputEncode(CGit *pGit)
2111 CString output;
2112 output = pGit->GetConfigValue(_T("i18n.logOutputEncoding"));
2113 if(output.IsEmpty())
2114 return CUnicodeUtils::GetCPCode(pGit->GetConfigValue(_T("i18n.commitencoding")));
2115 else
2117 return CUnicodeUtils::GetCPCode(output);
2120 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
2122 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
2123 int cp = CUnicodeUtils::GetCPCode(g_Git.GetConfigValue(_T("i18n.commitencoding")));
2125 bool stripComments = (CRegDWORD(_T("Software\\TortoiseGit\\StripCommentedLines"), FALSE) == TRUE);
2127 message.TrimRight(L" \r\n");
2129 int len = message.GetLength();
2130 int start = 0;
2131 while (start >= 0 && start < len)
2133 int oldStart = start;
2134 start = message.Find(L"\n", oldStart);
2135 CString line = message.Mid(oldStart);
2136 if (start != -1)
2138 line = line.Left(start - oldStart);
2139 ++start; // move forward so we don't find the same char again
2141 if (stripComments && (line.GetLength() >= 1 && line.GetAt(0) == '#') || (start < 0 && line.IsEmpty()))
2142 continue;
2143 line.TrimRight(L" \r");
2144 CStringA lineA = CUnicodeUtils::GetMulti(line + L"\n", cp);
2145 file.Write(lineA.GetBuffer(), lineA.GetLength());
2147 file.Close();
2148 return 0;
2151 bool CAppUtils::Pull(bool showPush)
2153 CPullFetchDlg dlg;
2154 dlg.m_IsPull = TRUE;
2155 if (dlg.DoModal() == IDOK)
2157 CString url = dlg.m_RemoteURL;
2159 if (dlg.m_bAutoLoad)
2161 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2164 CString cmd;
2165 CGitHash hashOld;
2166 if (g_Git.GetHash(hashOld, _T("HEAD")))
2168 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
2169 return false;
2172 CString cmdRebase;
2173 CString noff;
2174 CString ffonly;
2175 CString squash;
2176 CString nocommit;
2177 CString notags;
2179 if (dlg.m_bRebase)
2180 cmdRebase = "--rebase ";
2182 if (!dlg.m_bFetchTags)
2183 notags = _T("--no-tags");
2185 if (dlg.m_bNoFF)
2186 noff=_T("--no-ff");
2188 if (dlg.m_bFFonly)
2189 ffonly = _T("--ff-only");
2191 if (dlg.m_bSquash)
2192 squash = _T("--squash");
2194 if (dlg.m_bNoCommit)
2195 nocommit = _T("--no-commit");
2197 int ver = CAppUtils::GetMsysgitVersion();
2199 if(ver >= 0x01070203) //above 1.7.0.2
2200 cmdRebase += _T("--progress ");
2202 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);
2203 CProgressDlg progress;
2204 progress.m_GitCmd = cmd;
2205 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_DIFFS)));
2206 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_LOG)));
2207 INT_PTR pushButton = showPush ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH))) + IDC_PROGRESS_BUTTON1 : -1;
2209 CTGitPath gitPath = g_Git.m_CurrentDir;
2210 INT_PTR smUpdateButton = gitPath.HasSubmodules() ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE))) + IDC_PROGRESS_BUTTON1 : -1;
2212 INT_PTR ret = progress.DoModal();
2214 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)
2216 CChangedDlg dlg;
2217 dlg.m_pathList.AddPath(CTGitPath());
2218 dlg.DoModal();
2220 return true;
2223 CGitHash hashNew;
2224 if (g_Git.GetHash(hashNew, _T("HEAD")))
2226 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR);
2227 return FALSE;
2230 if( ret == IDC_PROGRESS_BUTTON1)
2232 if(hashOld == hashNew)
2234 if(progress.m_GitStatus == 0)
2235 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2236 return TRUE;
2239 CFileDiffDlg dlg;
2240 dlg.SetDiff(NULL, hashNew.ToString(), hashOld.ToString());
2241 dlg.DoModal();
2243 return true;
2245 else if ( ret == IDC_PROGRESS_BUTTON1 +1 )
2247 if(hashOld == hashNew)
2249 if(progress.m_GitStatus == 0)
2250 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2251 return true;
2254 CLogDlg dlg;
2255 dlg.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld.ToString() + _T("..") + hashNew.ToString(), 0);
2256 dlg.DoModal();
2258 else if (ret == pushButton)
2260 Push(_T(""));
2261 return true;
2263 else if (ret == smUpdateButton)
2265 CString sCmd;
2266 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
2268 CAppUtils::RunTortoiseGitProc(sCmd);
2269 return true;
2273 return true;
2276 bool CAppUtils::Fetch(CString remoteName, bool allowRebase)
2278 CPullFetchDlg dlg;
2279 dlg.m_PreSelectRemote = remoteName;
2280 dlg.m_bAllowRebase = allowRebase;
2281 dlg.m_IsPull=FALSE;
2283 if(dlg.DoModal()==IDOK)
2285 if(dlg.m_bAutoLoad)
2287 if (dlg.m_bAllRemotes)
2289 STRING_VECTOR list;
2290 g_Git.GetRemoteList(list);
2292 STRING_VECTOR::const_iterator it = list.begin();
2293 while (it != list.end())
2295 CString remote(*it);
2296 CAppUtils::LaunchPAgent(NULL, &remote);
2297 ++it;
2300 else
2301 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2304 CString url;
2305 url=dlg.m_RemoteURL;
2306 CString cmd;
2307 CString arg;
2309 int ver = CAppUtils::GetMsysgitVersion();
2311 if(ver >= 0x01070203) //above 1.7.0.2
2312 arg = _T("--progress ");
2314 if (dlg.m_bPrune == TRUE)
2315 arg += _T("--prune ");
2316 else if (dlg.m_bPrune == FALSE && ver >= 0x01080500)
2317 arg += _T("--no-prune ");
2319 if (dlg.m_bFetchTags == 1)
2321 arg += _T("--tags ");
2323 else if (dlg.m_bFetchTags == 0)
2325 arg += _T("--no-tags ");
2328 if (dlg.m_bAllRemotes)
2329 cmd.Format(_T("git.exe fetch --all -v %s"), arg);
2330 else
2331 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"), arg, url, dlg.m_RemoteBranchName);
2333 CProgressDlg progress;
2335 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2336 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_RESET)));
2338 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
2340 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2343 progress.m_GitCmd=cmd;
2344 INT_PTR userResponse;
2346 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
2348 CGitProgressDlg gitdlg;
2349 if (!dlg.m_bAllRemotes)
2350 gitdlg.SetUrl(url);
2351 gitdlg.SetCommand(CGitProgressList::GitProgress_Fetch);
2352 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);
2353 if (!dlg.m_bAllRemotes)
2354 gitdlg.SetRefSpec(dlg.m_RemoteBranchName);
2355 userResponse = gitdlg.DoModal();
2358 else
2359 userResponse = progress.DoModal();
2361 if (userResponse == IDC_PROGRESS_BUTTON1)
2363 CString cmd = _T("/command:log");
2364 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2365 RunTortoiseGitProc(cmd);
2366 return TRUE;
2368 else if (userResponse == IDC_PROGRESS_BUTTON1 + 1)
2370 CString currentBranch = g_Git.GetSymbolicRef();
2371 CString configName;
2372 configName.Format(_T("branch.%s.remote"), currentBranch);
2373 CString pullRemote = g_Git.GetConfigValue(configName);
2375 //Select pull-branch from current branch
2376 configName.Format(_T("branch.%s.merge"), currentBranch);
2377 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
2379 CString defaultUpstream;
2380 if (!pullRemote.IsEmpty() && !pullBranch.IsEmpty())
2381 defaultUpstream.Format(_T("remotes/%s/%s"), pullRemote, pullBranch);
2382 GitReset(&defaultUpstream, 2);
2383 return TRUE;
2385 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 2) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
2387 while(1)
2389 CRebaseDlg dlg;
2390 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2391 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
2392 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2393 INT_PTR response = dlg.DoModal();
2394 if(response == IDOK)
2396 return TRUE;
2398 else if (response == IDC_REBASE_POST_BUTTON)
2400 CString cmd = _T("/command:log");
2401 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2402 RunTortoiseGitProc(cmd);
2403 return TRUE;
2405 else if (response == IDC_REBASE_POST_BUTTON + 1)
2407 CString cmd, out, err;
2408 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2409 g_Git.m_CurrentDir,
2410 g_Git.FixBranchName(dlg.m_Upstream),
2411 g_Git.FixBranchName(dlg.m_Branch));
2412 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
2414 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2415 return FALSE;
2418 CAppUtils::SendPatchMail(cmd,out);
2419 return TRUE;
2421 else if (response == IDC_REBASE_POST_BUTTON + 2)
2422 continue;
2423 else if(response == IDCANCEL)
2424 return FALSE;
2426 return TRUE;
2428 else if (userResponse != IDCANCEL)
2429 return TRUE;
2431 return FALSE;
2434 static void PushCallback(CProgressDlg *dlg, void *caller, int result)
2436 if (result)
2438 dlg->m_PostCmdList.RemoveAll();
2439 bool rejected = dlg->GetLogText().Find(_T("! [rejected]")) > 0;
2440 *(bool*)caller = rejected;
2441 if (rejected)
2442 dlg->m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
2443 dlg->m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2447 bool CAppUtils::Push(CString selectLocalBranch)
2449 CPushDlg dlg;
2450 dlg.m_BranchSourceName = selectLocalBranch;
2451 CString error;
2452 DWORD exitcode = 0xFFFFFFFF;
2453 CTGitPathList list;
2454 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
2455 if (CHooks::Instance().PrePush(list,exitcode, error))
2457 if (exitcode)
2459 CString temp;
2460 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2461 //ReportError(temp);
2462 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2463 return false;
2467 if(dlg.DoModal()==IDOK)
2469 CString arg;
2471 if(dlg.m_bPack)
2472 arg += _T("--thin ");
2473 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
2474 arg += _T("--tags ");
2475 if(dlg.m_bForce)
2476 arg += _T("--force ");
2477 if (dlg.m_bSetUpstream)
2478 arg += _T("--set-upstream ");
2479 if (dlg.m_RecurseSubmodules == 1)
2480 arg += _T("--recurse-submodules=check ");
2481 if (dlg.m_RecurseSubmodules == 2)
2482 arg += _T("--recurse-submodules=on-demand ");
2484 int ver = CAppUtils::GetMsysgitVersion();
2486 if(ver >= 0x01070203) //above 1.7.0.2
2487 arg += _T("--progress ");
2489 CProgressDlg progress;
2491 STRING_VECTOR remotesList;
2492 if (dlg.m_bPushAllRemotes)
2493 g_Git.GetRemoteList(remotesList);
2494 else
2495 remotesList.push_back(dlg.m_URL);
2497 for (unsigned int i = 0; i < remotesList.size(); ++i)
2499 if (dlg.m_bAutoLoad)
2500 CAppUtils::LaunchPAgent(NULL, &remotesList[i]);
2502 CString cmd;
2503 if (dlg.m_bPushAllBranches)
2505 cmd.Format(_T("git.exe push --all %s \"%s\""),
2506 arg,
2507 remotesList[i]);
2509 if (dlg.m_bTags)
2511 progress.m_GitCmdList.push_back(cmd);
2512 cmd.Format(_T("git.exe push --tags %s \"%s\""), arg, remotesList[i]);
2515 else
2517 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2518 arg,
2519 remotesList[i],
2520 dlg.m_BranchSourceName);
2521 if (!dlg.m_BranchRemoteName.IsEmpty())
2523 cmd += _T(":") + dlg.m_BranchRemoteName;
2526 progress.m_GitCmdList.push_back(cmd);
2529 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2530 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2531 bool rejected = false;
2532 progress.m_caller = &rejected;
2533 progress.m_PostCmdCallback = PushCallback;
2534 INT_PTR ret = progress.DoModal();
2536 if(!progress.m_GitStatus)
2538 if (CHooks::Instance().PostPush(list,exitcode, error))
2540 if (exitcode)
2542 CString temp;
2543 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2544 //ReportError(temp);
2545 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2546 return false;
2549 if(ret == IDC_PROGRESS_BUTTON1)
2551 RequestPull(dlg.m_BranchRemoteName);
2553 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2555 Push(selectLocalBranch);
2557 return TRUE;
2559 else
2561 if (rejected)
2563 // failed, pull first
2564 if (ret == IDC_PROGRESS_BUTTON1)
2566 Pull(true);
2568 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
2570 Push(selectLocalBranch);
2573 else
2575 if (ret == IDC_PROGRESS_BUTTON1)
2577 Push(selectLocalBranch);
2582 return FALSE;
2585 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2587 CRequestPullDlg dlg;
2588 dlg.m_RepositoryURL = repositoryUrl;
2589 dlg.m_EndRevision = endrevision;
2590 if (dlg.DoModal()==IDOK)
2592 CString cmd;
2593 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2595 CSysProgressDlg sysProgressDlg;
2596 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2597 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST)));
2598 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2599 sysProgressDlg.SetShowProgressBar(false);
2600 sysProgressDlg.ShowModeless((HWND)NULL, true);
2602 CString tempFileName = GetTempFile();
2603 CString err;
2604 DeleteFile(tempFileName);
2605 CreateDirectory(tempFileName, NULL);
2606 tempFileName += _T("\\pullrequest.txt");
2607 if (g_Git.RunLogFile(cmd, tempFileName, &err))
2609 CString msg;
2610 msg.LoadString(IDS_ERR_PULLREUQESTFAILED);
2611 CMessageBox::Show(NULL, msg + _T("\n") + err, _T("TortoiseGit"), MB_OK);
2612 return false;
2615 if (sysProgressDlg.HasUserCancelled())
2617 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
2618 ::DeleteFile(tempFileName);
2619 return false;
2622 sysProgressDlg.Stop();
2624 if (dlg.m_bSendMail)
2626 CSendMailDlg dlg;
2627 dlg.m_PathList = CTGitPathList(CTGitPath(tempFileName));
2628 dlg.m_bCustomSubject = true;
2630 if (dlg.DoModal() == IDOK)
2632 if (dlg.m_PathList.IsEmpty())
2633 return FALSE;
2635 CGitProgressDlg progDlg;
2637 theApp.m_pMainWnd = &progDlg;
2638 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2640 progDlg.SetPathList(dlg.m_PathList);
2641 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2643 CSendMailCombineable sendMailCombineable(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2644 progDlg.SetSendMailOption(&sendMailCombineable);
2646 progDlg.DoModal();
2648 return true;
2650 return false;
2653 CAppUtils::LaunchAlternativeEditor(tempFileName);
2655 return true;
2658 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2660 CString strDir(szPath);
2661 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2663 strDir.AppendChar(_T('\\'));
2665 std::vector<CString> vPath;
2666 CString strTemp;
2667 bool bSuccess = false;
2669 for (int i=0;i<strDir.GetLength();++i)
2671 if (strDir.GetAt(i) != _T('\\'))
2673 strTemp.AppendChar(strDir.GetAt(i));
2675 else
2677 vPath.push_back(strTemp);
2678 strTemp.AppendChar(_T('\\'));
2682 for (auto vIter = vPath.begin(); vIter != vPath.end(); ++vIter)
2684 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2687 return bSuccess;
2690 void CAppUtils::RemoveTrailSlash(CString &path)
2692 if(path.IsEmpty())
2693 return ;
2695 // For URL, do not trim the slash just after the host name component.
2696 int index = path.Find(_T("://"));
2697 if (index >= 0)
2699 index += 4;
2700 index = path.Find(_T('/'), index);
2701 if (index == path.GetLength() - 1)
2702 return;
2705 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2707 path=path.Left(path.GetLength()-1);
2708 if(path.IsEmpty())
2709 return;
2713 bool CAppUtils::CheckUserData()
2715 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2717 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2719 CTGitPath path(g_Git.m_CurrentDir);
2720 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2721 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2722 dlg.SetTreeWidth(220);
2723 dlg.m_DefaultPage = _T("gitconfig");
2725 dlg.DoModal();
2726 dlg.HandleRestart();
2729 else
2730 return false;
2733 return true;
2736 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2737 CTGitPathList &pathList,
2738 CTGitPathList &selectedList,
2739 bool bSelectFilesForCommit)
2741 bool bFailed = true;
2743 if (!CheckUserData())
2744 return false;
2746 while (bFailed)
2748 bFailed = false;
2749 CCommitDlg dlg;
2750 dlg.m_sBugID = bugid;
2752 dlg.m_bWholeProject = bWholeProject;
2754 dlg.m_sLogMessage = sLogMsg;
2755 dlg.m_pathList = pathList;
2756 dlg.m_checkedPathList = selectedList;
2757 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2758 if (dlg.DoModal() == IDOK)
2760 if (dlg.m_pathList.IsEmpty())
2761 return false;
2762 // if the user hasn't changed the list of selected items
2763 // we don't use that list. Because if we would use the list
2764 // of pre-checked items, the dialog would show different
2765 // checked items on the next startup: it would only try
2766 // to check the parent folder (which might not even show)
2767 // instead, we simply use an empty list and let the
2768 // default checking do its job.
2769 if (!dlg.m_pathList.IsEqual(pathList))
2770 selectedList = dlg.m_pathList;
2771 pathList = dlg.m_updatedPathList;
2772 sLogMsg = dlg.m_sLogMessage;
2773 bSelectFilesForCommit = true;
2775 switch (dlg.m_PostCmd)
2777 case GIT_POSTCOMMIT_CMD_DCOMMIT:
2778 CAppUtils::SVNDCommit();
2779 break;
2780 case GIT_POSTCOMMIT_CMD_PUSH:
2781 CAppUtils::Push();
2782 break;
2783 case GIT_POSTCOMMIT_CMD_CREATETAG:
2784 CAppUtils::CreateBranchTag(TRUE);
2785 break;
2786 case GIT_POSTCOMMIT_CMD_PULL:
2787 CAppUtils::Pull(true);
2788 break;
2789 default:
2790 break;
2793 // CGitProgressDlg progDlg;
2794 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2795 // if (parser.HasVal(_T("closeonend")))
2796 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2797 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2798 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2799 // progDlg.SetPathList(dlg.m_pathList);
2800 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2801 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2802 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2803 // progDlg.SetItemCount(dlg.m_itemsCount);
2804 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2805 // progDlg.DoModal();
2806 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2807 // err = (DWORD)progDlg.DidErrorsOccur();
2808 // bFailed = progDlg.DidErrorsOccur();
2809 // bRet = progDlg.DidErrorsOccur();
2810 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2811 // if (DWORD(bFailRepeat)==0)
2812 // bFailed = false; // do not repeat if the user chose not to in the settings.
2815 return true;
2819 BOOL CAppUtils::SVNDCommit()
2821 CSVNDCommitDlg dcommitdlg;
2822 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2823 if (gitSetting == _T("")) {
2824 if (dcommitdlg.DoModal() != IDOK)
2826 return false;
2828 else
2830 if (dcommitdlg.m_remember)
2832 if (dcommitdlg.m_rmdir)
2834 gitSetting = _T("true");
2836 else
2838 gitSetting = _T("false");
2840 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2842 CString msg;
2843 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2844 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2850 BOOL IsStash = false;
2851 if(!g_Git.CheckCleanWorkTree())
2853 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2855 CSysProgressDlg sysProgressDlg;
2856 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2857 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2858 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2859 sysProgressDlg.SetShowProgressBar(false);
2860 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2861 sysProgressDlg.ShowModeless((HWND)NULL, true);
2863 CString cmd,out;
2864 cmd=_T("git.exe stash");
2865 if (g_Git.Run(cmd, &out, CP_UTF8))
2867 sysProgressDlg.Stop();
2868 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2869 return false;
2871 sysProgressDlg.Stop();
2873 IsStash =true;
2875 else
2877 return false;
2881 CProgressDlg progress;
2882 if (dcommitdlg.m_rmdir)
2884 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2886 else
2888 progress.m_GitCmd=_T("git.exe svn dcommit");
2890 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2892 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
2893 if( IsStash)
2895 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2897 CSysProgressDlg sysProgressDlg;
2898 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2899 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2900 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2901 sysProgressDlg.SetShowProgressBar(false);
2902 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2903 sysProgressDlg.ShowModeless((HWND)NULL, true);
2905 CString cmd,out;
2906 cmd=_T("git.exe stash pop");
2907 if (g_Git.Run(cmd, &out, CP_UTF8))
2909 sysProgressDlg.Stop();
2910 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2911 return false;
2913 sysProgressDlg.Stop();
2915 else
2917 return false;
2920 return TRUE;
2922 return FALSE;
2925 static void MergeCallback(CProgressDlg *dlg, void * /*caller*/, int result)
2927 if (result)
2929 dlg->m_PostCmdList.RemoveAll();
2931 CTGitPathList list;
2932 if (!g_Git.ListConflictFile(list) && !list.IsEmpty())
2934 // there are conflict files
2935 dlg->m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROGRS_CMD_RESOLVE)));
2940 BOOL CAppUtils::Merge(CString *commit)
2942 if (!CheckUserData())
2943 return FALSE;
2945 CMergeDlg dlg;
2946 if(commit)
2947 dlg.m_initialRefName = *commit;
2949 if(dlg.DoModal()==IDOK)
2951 CString cmd;
2952 CString args;
2954 if(dlg.m_bNoFF)
2955 args += _T(" --no-ff");
2957 if(dlg.m_bSquash)
2958 args += _T(" --squash");
2960 if(dlg.m_bNoCommit)
2961 args += _T(" --no-commit");
2963 if (dlg.m_bLog)
2965 CString fmt;
2966 fmt.Format(_T(" --log=%d"), dlg.m_nLog);
2967 args += fmt;
2970 if (!dlg.m_MergeStrategy.IsEmpty())
2972 args += _T(" --strategy=") + dlg.m_MergeStrategy;
2973 if (!dlg.m_StrategyOption.IsEmpty())
2975 args += _T(" --strategy-option=") + dlg.m_StrategyOption;
2976 if (!dlg.m_StrategyParam.IsEmpty())
2977 args += _T("=") + dlg.m_StrategyParam;
2981 if(!dlg.m_strLogMesage.IsEmpty())
2983 CString logmsg = dlg.m_strLogMesage;
2984 logmsg.Replace(_T("\""), _T("\\\""));
2985 args += _T(" -m \"") + logmsg + _T("\"");
2987 cmd.Format(_T("git.exe merge %s %s"), args, g_Git.FixBranchName(dlg.m_VersionName));
2989 CProgressDlg Prodlg;
2990 Prodlg.m_GitCmd = cmd;
2992 BOOL hasGitSVN = CTGitPath(g_Git.m_CurrentDir).GetAdminDirMask() & ITEMIS_GITSVN;
2993 if (dlg.m_bNoCommit)
2994 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
2995 else
2997 if (dlg.m_bIsBranch)
2998 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
3000 if (hasGitSVN)
3001 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUSVNDCOMMIT)));
3004 Prodlg.m_PostCmdCallback = MergeCallback;
3006 INT_PTR ret = Prodlg.DoModal();
3007 if (Prodlg.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3009 CTGitPathList pathlist;
3010 CTGitPathList selectedlist;
3011 pathlist.AddPath(g_Git.m_CurrentDir);
3012 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
3013 CString str;
3014 CAppUtils::Commit(CString(), false, str, pathlist, selectedlist, bSelectFilesForCommit);
3016 else if (ret == IDC_PROGRESS_BUTTON1)
3018 if (dlg.m_bNoCommit)
3020 CString sLogMsg;
3021 CTGitPathList pathList;
3022 CTGitPathList selectedList;
3023 return Commit(_T(""), TRUE, sLogMsg, pathList, selectedList, true);
3025 else if (dlg.m_bIsBranch)
3027 CString msg;
3028 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
3029 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
3031 CString cmd, out;
3032 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
3033 if (g_Git.Run(cmd, &out, CP_UTF8))
3034 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
3037 else if (hasGitSVN)
3038 CAppUtils::SVNDCommit();
3040 else if (ret == IDC_PROGRESS_BUTTON1 + 1 && hasGitSVN)
3041 CAppUtils::SVNDCommit();
3043 return !Prodlg.m_GitStatus;
3045 return false;
3048 BOOL CAppUtils::MergeAbort()
3050 CMergeAbortDlg dlg;
3051 if (dlg.DoModal() == IDOK)
3053 CString cmd;
3054 CString type;
3055 switch (dlg.m_ResetType)
3057 case 0:
3058 type = _T("--mixed");
3059 break;
3060 case 1:
3061 type = _T("--hard");
3062 break;
3063 default:
3064 dlg.m_ResetType = 0;
3065 type = _T("--mixed");
3066 break;
3068 cmd.Format(_T("git.exe reset %s HEAD --"), type);
3070 while (true)
3072 CProgressDlg progress;
3073 progress.m_GitCmd = cmd;
3075 CTGitPath gitPath = g_Git.m_CurrentDir;
3076 if (gitPath.HasSubmodules() && dlg.m_ResetType == 1)
3077 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3079 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
3081 INT_PTR ret;
3082 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
3084 CGitProgressDlg gitdlg;
3085 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
3086 gitdlg.SetRevision(_T("HEAD"));
3087 gitdlg.SetResetType(dlg.m_ResetType + 1);
3088 ret = gitdlg.DoModal();
3090 else
3091 ret = progress.DoModal();
3093 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 1 && ret == IDC_PROGRESS_BUTTON1)
3095 CString sCmd;
3096 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3098 CCommonAppUtils::RunTortoiseGitProc(sCmd);
3099 return TRUE;
3101 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3102 continue; // retry
3103 else if (ret == IDOK)
3104 return TRUE;
3105 else
3106 break;
3109 return FALSE;
3112 void CAppUtils::EditNote(GitRev *rev)
3114 if (!CheckUserData())
3115 return;
3117 CInputDlg dlg;
3118 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3119 dlg.m_sInputText = rev->m_Notes;
3120 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3121 //dlg.m_pProjectProperties = &m_ProjectProperties;
3122 dlg.m_bUseLogWidth = true;
3123 if(dlg.DoModal() == IDOK)
3125 CString cmd,output;
3126 cmd=_T("notes add -f -F \"");
3128 CString tempfile=::GetTempFile();
3129 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
3130 cmd += tempfile;
3131 cmd += _T("\" ");
3132 cmd += rev->m_CommitHash.ToString();
3136 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
3138 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3141 else
3143 rev->m_Notes = dlg.m_sInputText;
3145 }catch(...)
3147 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3149 ::DeleteFile(tempfile);
3154 int CAppUtils::GetMsysgitVersion()
3156 if (g_Git.ms_LastMsysGitVersion)
3157 return g_Git.ms_LastMsysGitVersion;
3159 CString cmd;
3160 CString versiondebug;
3161 CString version;
3163 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3164 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3166 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
3168 __int64 time=0;
3169 if (!g_Git.GetFileModifyTime(gitpath, &time))
3171 if((DWORD)time == regTime)
3173 g_Git.ms_LastMsysGitVersion = regVersion;
3174 return regVersion;
3178 CString err;
3179 cmd = _T("git.exe --version");
3180 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
3182 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);
3183 return -1;
3186 int start=0;
3187 int ver = 0;
3189 versiondebug = version;
3193 CString str=version.Tokenize(_T("."), start);
3194 int space = str.ReverseFind(_T(' '));
3195 str = str.Mid(space+1,start);
3196 ver = _ttol(str);
3197 ver <<=24;
3199 version = version.Mid(start);
3200 start = 0;
3202 str = version.Tokenize(_T("."), start);
3204 ver |= (_ttol(str) & 0xFF) << 16;
3206 str = version.Tokenize(_T("."), start);
3207 ver |= (_ttol(str) & 0xFF) << 8;
3209 str = version.Tokenize(_T("."), start);
3210 ver |= (_ttol(str) & 0xFF);
3212 catch(...)
3214 if (!ver)
3216 CMessageBox::Show(NULL, _T("Could not parse git.exe version number: \"") + versiondebug + _T("\""), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
3217 return -1;
3221 regTime = time&0xFFFFFFFF;
3222 regVersion = ver;
3223 g_Git.ms_LastMsysGitVersion = ver;
3225 return ver;
3228 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
3230 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
3232 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3234 if (hShell.IsValid()) {
3235 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
3236 if (pfnSHGPSFW) {
3237 IPropertyStore *pps;
3238 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
3239 if (SUCCEEDED(hr)) {
3240 PROPVARIANT var;
3241 var.vt = VT_BOOL;
3242 var.boolVal = VARIANT_TRUE;
3243 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
3244 pps->Release();
3250 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
3252 ASSERT(dialogname.GetLength() < 70);
3253 ASSERT(urlorpath.GetLength() < MAX_PATH);
3254 WCHAR pathbuf[MAX_PATH] = {0};
3256 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
3258 wcscat_s(pathbuf, L" - ");
3259 wcscat_s(pathbuf, dialogname);
3260 wcscat_s(pathbuf, L" - ");
3261 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
3262 SetWindowText(hWnd, pathbuf);
3265 bool CAppUtils::BisectStart(CString lastGood, CString firstBad)
3267 if (!g_Git.CheckCleanWorkTree())
3269 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
3271 CSysProgressDlg sysProgressDlg;
3272 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
3273 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
3274 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
3275 sysProgressDlg.SetShowProgressBar(false);
3276 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
3277 sysProgressDlg.ShowModeless((HWND)NULL, true);
3279 CString cmd, out;
3280 cmd = _T("git.exe stash");
3281 if (g_Git.Run(cmd, &out, CP_UTF8))
3283 sysProgressDlg.Stop();
3284 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK);
3285 return false;
3287 sysProgressDlg.Stop();
3289 else
3290 return false;
3293 CBisectStartDlg bisectStartDlg;
3295 if (!lastGood.IsEmpty())
3296 bisectStartDlg.m_sLastGood = lastGood;
3297 if (!firstBad.IsEmpty())
3298 bisectStartDlg.m_sFirstBad = firstBad;
3300 if (bisectStartDlg.DoModal() == IDOK)
3302 CProgressDlg progress;
3303 theApp.m_pMainWnd = &progress;
3304 progress.m_GitCmdList.push_back(_T("git.exe bisect start"));
3305 progress.m_GitCmdList.push_back(_T("git.exe bisect good ") + bisectStartDlg.m_LastGoodRevision);
3306 progress.m_GitCmdList.push_back(_T("git.exe bisect bad ") + bisectStartDlg.m_FirstBadRevision);
3308 CTGitPath path(g_Git.m_CurrentDir);
3310 if (path.HasSubmodules())
3311 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3313 INT_PTR ret = progress.DoModal();
3314 if (path.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
3316 CString sCmd;
3317 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3319 CAppUtils::RunTortoiseGitProc(sCmd);
3320 return true;
3322 else if (ret == IDOK)
3323 return true;
3326 return false;
3329 int CAppUtils::Git2GetUserPassword(git_cred **out, const char *url, const char *username_from_url, unsigned int /*allowed_types*/, void * /*payload*/)
3331 CUserPassword dlg;
3332 dlg.m_URL = CUnicodeUtils::GetUnicode(url, CP_UTF8);
3333 if (username_from_url)
3334 dlg.m_UserName = CUnicodeUtils::GetUnicode(username_from_url, CP_UTF8);
3336 CStringA username, password;
3337 if (dlg.DoModal() == IDOK)
3339 username = CUnicodeUtils::GetMulti(dlg.m_UserName, CP_UTF8);
3340 password = CUnicodeUtils::GetMulti(dlg.m_Password, CP_UTF8);
3341 return git_cred_userpass_plaintext_new(out, username, password);
3343 return -1;