Sync default values of setting
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob2024ac5ea23c21c6db72382d2006fa10923359c0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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"
67 CAppUtils::CAppUtils(void)
71 CAppUtils::~CAppUtils(void)
75 bool CAppUtils::StashSave()
77 CStashSaveDlg dlg;
79 if (dlg.DoModal() == IDOK)
81 CString cmd, out;
82 cmd = _T("git.exe stash save");
84 if (CAppUtils::GetMsysgitVersion() >= 0x01070700)
86 if (dlg.m_bIncludeUntracked)
87 cmd += _T(" --include-untracked");
88 else if (dlg.m_bAll)
89 cmd += _T(" --all");
92 if (!dlg.m_sMessage.IsEmpty())
94 CString message = dlg.m_sMessage;
95 message.Replace(_T("\""), _T("\"\""));
96 cmd += _T(" \"") + message + _T("\"");
99 CSysProgressDlg sysProgressDlg;
100 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
101 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
102 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
103 sysProgressDlg.SetShowProgressBar(false);
104 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
105 sysProgressDlg.ShowModeless((HWND)NULL, true);
107 if (g_Git.Run(cmd, &out, CP_UTF8))
109 sysProgressDlg.Stop();
110 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
112 else
114 sysProgressDlg.Stop();
115 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHSUCCESS)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
116 return true;
119 return false;
122 bool CAppUtils::StashApply(CString ref, bool showChanges /* true */)
124 CString cmd,out;
125 cmd = _T("git.exe stash apply ");
126 if (ref.Find(_T("refs/")) == 0)
127 ref = ref.Mid(5);
128 if (ref.Find(_T("stash{")) == 0)
129 ref = _T("stash@") + ref.Mid(5);
130 cmd += ref;
132 CSysProgressDlg sysProgressDlg;
133 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
134 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
135 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
136 sysProgressDlg.SetShowProgressBar(false);
137 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
138 sysProgressDlg.ShowModeless((HWND)NULL, true);
140 int ret = g_Git.Run(cmd, &out, CP_UTF8);
142 sysProgressDlg.Stop();
144 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
145 if (ret && !(ret == 1 && hasConflicts))
147 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHAPPLYFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
149 else
151 CString message;
152 message.LoadString(IDS_PROC_STASHAPPLYSUCCESS);
153 if (hasConflicts)
154 message.LoadString(IDS_PROC_STASHAPPLYFAILEDCONFLICTS);
155 if (showChanges)
157 if(CMessageBox::Show(NULL,message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES))
158 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
160 CChangedDlg dlg;
161 dlg.m_pathList.AddPath(CTGitPath());
162 dlg.DoModal();
164 return true;
166 else
168 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
169 return true;
172 return false;
175 bool CAppUtils::StashPop(bool showChanges /* true */)
177 CString cmd,out;
178 cmd=_T("git.exe stash pop ");
180 CSysProgressDlg sysProgressDlg;
181 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
182 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
183 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
184 sysProgressDlg.SetShowProgressBar(false);
185 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
186 sysProgressDlg.ShowModeless((HWND)NULL, true);
188 int ret = g_Git.Run(cmd, &out, CP_UTF8);
190 sysProgressDlg.Stop();
192 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
193 if (ret && !(ret == 1 && hasConflicts))
195 CMessageBox::Show(NULL,CString(MAKEINTRESOURCE(IDS_PROC_STASHPOPFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
197 else
199 CString message;
200 message.LoadString(IDS_PROC_STASHPOPSUCCESS);
201 if (hasConflicts)
202 message.LoadString(IDS_PROC_STASHPOPFAILEDCONFLICTS);
203 if (showChanges)
205 if(CMessageBox::Show(NULL,CString(message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES)))
206 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
208 CChangedDlg dlg;
209 dlg.m_pathList.AddPath(CTGitPath());
210 dlg.DoModal();
212 return true;
214 else
216 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
217 return true;
220 return false;
223 bool CAppUtils::GetMimeType(const CTGitPath& /*file*/, CString& /*mimetype*/)
225 #if 0
226 GitProperties props(file, GitRev::REV_WC, false);
227 for (int i = 0; i < props.GetCount(); ++i)
229 if (props.GetItemName(i).compare(_T("svn:mime-type"))==0)
231 mimetype = props.GetItemValue(i).c_str();
232 return true;
235 #endif
236 return false;
239 BOOL CAppUtils::StartExtMerge(
240 const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,
241 const CString& basename, const CString& theirname, const CString& yourname, const CString& mergedname, bool bReadOnly)
244 CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));
245 CString ext = mergedfile.GetFileExtension();
246 CString com = regCom;
247 bool bInternal = false;
249 CString mimetype;
251 if (!ext.IsEmpty())
253 // is there an extension specific merge tool?
254 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());
255 if (!CString(mergetool).IsEmpty())
257 com = mergetool;
260 if (GetMimeType(yourfile, mimetype) || GetMimeType(theirfile, mimetype) || GetMimeType(basefile, mimetype))
262 // is there a mime type specific merge tool?
263 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + mimetype);
264 if (CString(mergetool) != "")
266 com = mergetool;
269 // is there a filename specific merge tool?
270 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\.") + mergedfile.GetFilename().MakeLower());
271 if (!CString(mergetool).IsEmpty())
273 com = mergetool;
276 if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))
278 // Maybe we should use TortoiseIDiff?
279 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
280 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
281 (ext == _T(".png")) || (ext == _T(".ico")) ||
282 (ext == _T(".tif")) || (ext == _T(".tiff")) ||
283 (ext == _T(".dib")) || (ext == _T(".emf")))
285 com = CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe");
286 com = _T("\"") + com + _T("\"");
287 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /result:%merged");
288 com = com + _T(" /basetitle:%bname /theirstitle:%tname /minetitle:%yname");
290 else
292 // use TortoiseGitMerge
293 bInternal = true;
294 CRegString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE);
295 com = tortoiseMergePath;
296 if (com.IsEmpty())
298 com = CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe");
300 com = _T("\"") + com + _T("\"");
301 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
302 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
304 if (!g_sGroupingUUID.IsEmpty())
306 com += L" /groupuuid:\"";
307 com += g_sGroupingUUID;
308 com += L"\"";
311 // check if the params are set. If not, just add the files to the command line
312 if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))
314 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");
315 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");
316 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");
317 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");
319 if (basefile.IsEmpty())
321 com.Replace(_T("/base:%base"), _T(""));
322 com.Replace(_T("%base"), _T(""));
324 else
325 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));
326 if (theirfile.IsEmpty())
328 com.Replace(_T("/theirs:%theirs"), _T(""));
329 com.Replace(_T("%theirs"), _T(""));
331 else
332 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));
333 if (yourfile.IsEmpty())
335 com.Replace(_T("/mine:%mine"), _T(""));
336 com.Replace(_T("%mine"), _T(""));
338 else
339 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));
340 if (mergedfile.IsEmpty())
342 com.Replace(_T("/merged:%merged"), _T(""));
343 com.Replace(_T("%merged"), _T(""));
345 else
346 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));
347 if (basename.IsEmpty())
349 if (basefile.IsEmpty())
351 com.Replace(_T("/basename:%bname"), _T(""));
352 com.Replace(_T("%bname"), _T(""));
354 else
356 com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));
359 else
360 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));
361 if (theirname.IsEmpty())
363 if (theirfile.IsEmpty())
365 com.Replace(_T("/theirsname:%tname"), _T(""));
366 com.Replace(_T("%tname"), _T(""));
368 else
370 com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));
373 else
374 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));
375 if (yourname.IsEmpty())
377 if (yourfile.IsEmpty())
379 com.Replace(_T("/minename:%yname"), _T(""));
380 com.Replace(_T("%yname"), _T(""));
382 else
384 com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));
387 else
388 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));
389 if (mergedname.IsEmpty())
391 if (mergedfile.IsEmpty())
393 com.Replace(_T("/mergedname:%mname"), _T(""));
394 com.Replace(_T("%mname"), _T(""));
396 else
398 com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));
401 else
402 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));
404 if ((bReadOnly)&&(bInternal))
405 com += _T(" /readonly");
407 if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))
409 return FALSE;
412 return TRUE;
415 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)
417 CString viewer;
418 // use TortoiseGitMerge
419 viewer = CPathUtils::GetAppDirectory();
420 viewer += _T("TortoiseGitMerge.exe");
422 viewer = _T("\"") + viewer + _T("\"");
423 viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");
424 viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");
425 if (bReversed)
426 viewer += _T(" /reversedpatch");
427 if (!sOriginalDescription.IsEmpty())
428 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");
429 if (!sPatchedDescription.IsEmpty())
430 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");
431 if (!g_sGroupingUUID.IsEmpty())
433 viewer += L" /groupuuid:\"";
434 viewer += g_sGroupingUUID;
435 viewer += L"\"";
437 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
439 return FALSE;
441 return TRUE;
444 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)
446 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file2.GetFilename().MakeLower());
447 if (!difftool.IsEmpty())
448 return difftool;
449 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file1.GetFilename().MakeLower());
450 if (!difftool.IsEmpty())
451 return difftool;
453 // Is there a mime type specific diff tool?
454 CString mimetype;
455 if (GetMimeType(file1, mimetype) || GetMimeType(file2, mimetype))
457 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + mimetype);
458 if (!difftool.IsEmpty())
459 return difftool;
462 // Is there an extension specific diff tool?
463 CString ext = file2.GetFileExtension().MakeLower();
464 if (!ext.IsEmpty())
466 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);
467 if (!difftool.IsEmpty())
468 return difftool;
469 // Maybe we should use TortoiseIDiff?
470 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
471 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
472 (ext == _T(".png")) || (ext == _T(".ico")) ||
473 (ext == _T(".dib")) || (ext == _T(".emf")))
475 return
476 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitIDiff.exe") + _T("\"") +
477 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname") +
478 L" /groupuuid:\"" + g_sGroupingUUID + L"\"";
482 // Finally, pick a generic external diff tool
483 difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));
484 return difftool;
487 bool CAppUtils::StartExtDiff(
488 const CString& file1, const CString& file2,
489 const CString& sName1, const CString& sName2,
490 const CString& originalFile1, const CString& originalFile2,
491 const git_revnum_t& hash1, const git_revnum_t& hash2,
492 const DiffFlags& flags)
494 CString viewer;
496 CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);
497 if (!flags.bBlame || !(DWORD)blamediff)
499 viewer = PickDiffTool(file1, file2);
500 // If registry entry for a diff program is commented out, use TortoiseGitMerge.
501 bool bCommentedOut = viewer.Left(1) == _T("#");
502 if (flags.bAlternativeTool)
504 // Invert external vs. internal diff tool selection.
505 if (bCommentedOut)
506 viewer.Delete(0); // uncomment
507 else
508 viewer = "";
510 else if (bCommentedOut)
511 viewer = "";
514 bool bInternal = viewer.IsEmpty();
515 if (bInternal)
517 viewer =
518 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseGitMerge.exe") + _T("\"") +
519 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
520 if (!g_sGroupingUUID.IsEmpty())
522 viewer += L" /groupuuid:\"";
523 viewer += g_sGroupingUUID;
524 viewer += L"\"";
526 if (flags.bBlame)
527 viewer += _T(" /blame");
529 // check if the params are set. If not, just add the files to the command line
530 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
532 viewer += _T(" \"")+file1+_T("\"");
533 viewer += _T(" \"")+file2+_T("\"");
535 if (viewer.Find(_T("%base")) >= 0)
537 viewer.Replace(_T("%base"), _T("\"")+file1+_T("\""));
539 if (viewer.Find(_T("%mine")) >= 0)
541 viewer.Replace(_T("%mine"), _T("\"")+file2+_T("\""));
544 if (sName1.IsEmpty())
545 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));
546 else
547 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
549 if (sName2.IsEmpty())
550 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));
551 else
552 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
554 viewer.Replace(_T("%bpath"), _T("\"") + originalFile1 + _T("\""));
555 viewer.Replace(_T("%ypath"), _T("\"") + originalFile2 + _T("\""));
557 viewer.Replace(_T("%brev"), _T("\"") + hash1 + _T("\""));
558 viewer.Replace(_T("%yrev"), _T("\"") + hash2 + _T("\""));
560 if (flags.bReadOnly && bInternal)
561 viewer += _T(" /readonly");
563 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);
566 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)
568 CString viewer;
569 CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
570 viewer = v;
571 if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))
573 // use TortoiseGitUDiff
574 viewer = CPathUtils::GetAppDirectory();
575 viewer += _T("TortoiseGitUDiff.exe");
576 // enquote the path to TortoiseGitUDiff
577 viewer = _T("\"") + viewer + _T("\"");
578 // add the params
579 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");
580 if (!g_sGroupingUUID.IsEmpty())
582 viewer += L" /groupuuid:\"";
583 viewer += g_sGroupingUUID;
584 viewer += L"\"";
587 if (viewer.Find(_T("%1"))>=0)
589 if (viewer.Find(_T("\"%1\"")) >= 0)
590 viewer.Replace(_T("%1"), patchfile);
591 else
592 viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));
594 else
595 viewer += _T(" \"") + patchfile + _T("\"");
596 if (viewer.Find(_T("%title")) >= 0)
598 viewer.Replace(_T("%title"), title);
601 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
603 return FALSE;
605 return TRUE;
608 BOOL CAppUtils::StartTextViewer(CString file)
610 CString viewer;
611 CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);
612 viewer = txt;
613 viewer = viewer + _T("\\Shell\\Open\\Command\\");
614 CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);
615 viewer = txtexe;
617 DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
618 std::unique_ptr<TCHAR[]> buf(new TCHAR[len + 1]);
619 ExpandEnvironmentStrings(viewer, buf.get(), len);
620 viewer = buf.get();
621 len = ExpandEnvironmentStrings(file, NULL, 0);
622 std::unique_ptr<TCHAR[]> buf2(new TCHAR[len + 1]);
623 ExpandEnvironmentStrings(file, buf2.get(), len);
624 file = buf2.get();
625 file = _T("\"")+file+_T("\"");
626 if (viewer.IsEmpty())
628 viewer = _T("RUNDLL32 Shell32,OpenAs_RunDLL");
630 if (viewer.Find(_T("\"%1\"")) >= 0)
632 viewer.Replace(_T("\"%1\""), file);
634 else if (viewer.Find(_T("%1")) >= 0)
636 viewer.Replace(_T("%1"), file);
638 else
640 viewer += _T(" ");
641 viewer += file;
644 if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))
646 return FALSE;
648 return TRUE;
651 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)
653 DWORD length = 0;
654 CAutoFile hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
655 if (!hFile)
656 return TRUE;
657 length = ::GetFileSize(hFile, NULL);
658 if (length < 4)
659 return TRUE;
660 return FALSE;
664 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)
666 LOGFONT logFont;
667 HDC hScreenDC = ::GetDC(NULL);
668 logFont.lfHeight = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);
669 ::ReleaseDC(NULL, hScreenDC);
670 logFont.lfWidth = 0;
671 logFont.lfEscapement = 0;
672 logFont.lfOrientation = 0;
673 logFont.lfWeight = FW_NORMAL;
674 logFont.lfItalic = 0;
675 logFont.lfUnderline = 0;
676 logFont.lfStrikeOut = 0;
677 logFont.lfCharSet = DEFAULT_CHARSET;
678 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
679 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
680 logFont.lfQuality = DRAFT_QUALITY;
681 logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;
682 _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
683 VERIFY(fontToCreate.CreateFontIndirect(&logFont));
686 bool CAppUtils::LaunchPAgent(CString *keyfile,CString * pRemote)
688 CString key,remote;
689 CString cmd,out;
690 if( pRemote == NULL)
692 remote=_T("origin");
694 else
696 remote=*pRemote;
698 if(keyfile == NULL)
700 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
701 key = g_Git.GetConfigValue(cmd);
703 else
704 key=*keyfile;
706 if(key.IsEmpty())
707 return false;
709 CString proc=CPathUtils::GetAppDirectory();
710 proc += _T("pageant.exe \"");
711 proc += key;
712 proc += _T("\"");
714 CString tempfile = GetTempFile();
715 ::DeleteFile(tempfile);
717 proc += _T(" -c \"");
718 proc += CPathUtils::GetAppDirectory();
719 proc += _T("tgittouch.exe\"");
720 proc += _T(" \"");
721 proc += tempfile;
722 proc += _T("\"");
724 CString appDir = CPathUtils::GetAppDirectory();
725 bool b = LaunchApplication(proc, IDS_ERR_PAGEANT, true, &appDir);
726 if(!b)
727 return b;
729 int i=0;
730 while(!::PathFileExists(tempfile))
732 Sleep(100);
733 ++i;
734 if(i>10*60*5)
735 break; //timeout 5 minutes
738 if( i== 10*60*5)
740 CMessageBox::Show(NULL, IDS_ERR_PAEGENTTIMEOUT, IDS_APPNAME, MB_OK | MB_ICONERROR);
742 ::DeleteFile(tempfile);
743 return true;
745 bool CAppUtils::LaunchAlternativeEditor(const CString& filename, bool uac)
747 CString editTool = CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
748 if (editTool.IsEmpty() || (editTool.Left(1).Compare(_T("#"))==0)) {
749 editTool = CPathUtils::GetAppDirectory() + _T("notepad2.exe");
752 CString sCmd;
753 sCmd.Format(_T("\"%s\" \"%s\""), editTool, filename);
755 LaunchApplication(sCmd, NULL, false, NULL, uac);
756 return true;
758 bool CAppUtils::LaunchRemoteSetting()
760 CTGitPath path(g_Git.m_CurrentDir);
761 CSettings dlg(IDS_PROC_SETTINGS_TITLE, &path);
762 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
763 dlg.SetTreeWidth(220);
764 dlg.m_DefaultPage = _T("gitremote");
766 dlg.DoModal();
767 dlg.HandleRestart();
768 return true;
771 * Launch the external blame viewer
773 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)
775 CString viewer = _T("\"") + CPathUtils::GetAppDirectory();
776 viewer += _T("TortoiseGitBlame.exe");
777 viewer += _T("\" \"") + sBlameFile + _T("\"");
778 //viewer += _T(" \"") + sLogFile + _T("\"");
779 //viewer += _T(" \"") + sOriginalFile + _T("\"");
780 if(!Rev.IsEmpty() && Rev != GIT_REV_ZERO)
781 viewer += CString(_T(" /rev:"))+Rev;
782 if (!g_sGroupingUUID.IsEmpty())
784 viewer += L" /groupuuid:\"";
785 viewer += g_sGroupingUUID;
786 viewer += L"\"";
788 viewer += _T(" ")+sParams;
790 return LaunchApplication(viewer, IDS_ERR_TGITBLAME, false);
793 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)
795 CString sText;
796 if (pWnd == NULL)
797 return false;
798 bool bStyled = false;
799 pWnd->GetWindowText(sText);
800 // the rich edit control doesn't count the CR char!
801 // to be exact: CRLF is treated as one char.
802 sText.Remove(_T('\r'));
804 // style each line separately
805 int offset = 0;
806 int nNewlinePos;
809 nNewlinePos = sText.Find('\n', offset);
810 CString sLine = nNewlinePos >= 0 ? sText.Mid(offset, nNewlinePos - offset) : sText.Mid(offset);
812 int start = 0;
813 int end = 0;
814 while (FindStyleChars(sLine, '*', start, end))
816 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
817 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
818 SetCharFormat(pWnd, CFM_BOLD, CFE_BOLD);
819 bStyled = true;
820 start = end;
822 start = 0;
823 end = 0;
824 while (FindStyleChars(sLine, '^', start, end))
826 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
827 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
828 SetCharFormat(pWnd, CFM_ITALIC, CFE_ITALIC);
829 bStyled = true;
830 start = end;
832 start = 0;
833 end = 0;
834 while (FindStyleChars(sLine, '_', start, end))
836 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
837 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
838 SetCharFormat(pWnd, CFM_UNDERLINE, CFE_UNDERLINE);
839 bStyled = true;
840 start = end;
842 offset = nNewlinePos+1;
843 } while(nNewlinePos>=0);
844 return bStyled;
847 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)
849 int i=start;
850 int last = sText.GetLength() - 1;
851 bool bFoundMarker = false;
852 TCHAR c = i == 0 ? _T('\0') : sText[i - 1];
853 TCHAR nextChar = i >= last ? _T('\0') : sText[i + 1];
855 // find a starting marker
856 while (i < last)
858 TCHAR prevChar = c;
859 c = nextChar;
860 nextChar = sText[i + 1];
862 // IsCharAlphaNumeric can be somewhat expensive.
863 // Long lines of "*****" or "----" will be pre-empted efficiently
864 // by the (c != nextChar) condition.
866 if ((c == stylechar) && (c != nextChar))
868 if (IsCharAlphaNumeric(nextChar) && !IsCharAlphaNumeric(prevChar))
870 start = ++i;
871 bFoundMarker = true;
872 break;
875 ++i;
877 if (!bFoundMarker)
878 return false;
880 // find ending marker
881 // c == sText[i - 1]
883 bFoundMarker = false;
884 while (i <= last)
886 TCHAR prevChar = c;
887 c = sText[i];
888 if (c == stylechar)
890 if ((i == last) || (!IsCharAlphaNumeric(sText[i + 1]) && IsCharAlphaNumeric(prevChar)))
892 end = i;
893 ++i;
894 bFoundMarker = true;
895 break;
898 ++i;
900 return bFoundMarker;
903 bool CAppUtils::StartShowUnifiedDiff(HWND /*hWnd*/, const CTGitPath& url1, const git_revnum_t& rev1,
904 const CTGitPath& /*url2*/, const git_revnum_t& rev2,
905 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
906 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */,
907 bool /* blame = false */,
908 bool bMerge,
909 bool bCombine)
911 int diffContext = 0;
912 if (GetMsysgitVersion() > 0x01080100)
913 diffContext = _ttoi(g_Git.GetConfigValue(_T("diff.context")));
914 CString tempfile=GetTempFile();
915 if (g_Git.GetUnifiedDiff(url1, rev1, rev2, tempfile, bMerge, bCombine, diffContext))
917 TRACE("Fail get unified diff\n");
918 return false;
920 CAppUtils::StartUnifiedDiffViewer(tempfile, rev1 + _T(":") + rev2);
922 #if 0
923 CString sCmd;
924 sCmd.Format(_T("%s /command:showcompare /unified"),
925 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseGitProc.exe")));
926 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
927 if (rev1.IsValid())
928 sCmd += _T(" /revision1:") + rev1.ToString();
929 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
930 if (rev2.IsValid())
931 sCmd += _T(" /revision2:") + rev2.ToString();
932 if (peg.IsValid())
933 sCmd += _T(" /pegrevision:") + peg.ToString();
934 if (headpeg.IsValid())
935 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
937 if (bAlternateDiff)
938 sCmd += _T(" /alternatediff");
940 if (bIgnoreAncestry)
941 sCmd += _T(" /ignoreancestry");
943 if (hWnd)
945 sCmd += _T(" /hwnd:");
946 TCHAR buf[30];
947 _stprintf_s(buf, 30, _T("%d"), hWnd);
948 sCmd += buf;
951 return CAppUtils::LaunchApplication(sCmd, NULL, false);
952 #endif
953 return TRUE;
956 bool CAppUtils::SetupDiffScripts(bool force, const CString& type)
958 CString scriptsdir = CPathUtils::GetAppParentDirectory();
959 scriptsdir += _T("Diff-Scripts");
960 CSimpleFileFind files(scriptsdir);
961 while (files.FindNextFileNoDirectories())
963 CString file = files.GetFilePath();
964 CString filename = files.GetFileName();
965 CString ext = file.Mid(file.ReverseFind('-') + 1);
966 ext = _T(".") + ext.Left(ext.ReverseFind('.'));
967 std::set<CString> extensions;
968 extensions.insert(ext);
969 CString kind;
970 if (file.Right(3).CompareNoCase(_T("vbs"))==0)
972 kind = _T(" //E:vbscript");
974 if (file.Right(2).CompareNoCase(_T("js"))==0)
976 kind = _T(" //E:javascript");
978 // open the file, read the first line and find possible extensions
979 // this script can handle
982 CStdioFile f(file, CFile::modeRead | CFile::shareDenyNone);
983 CString extline;
984 if (f.ReadString(extline))
986 if ((extline.GetLength() > 15 ) &&
987 ((extline.Left(15).Compare(_T("// extensions: ")) == 0) ||
988 (extline.Left(14).Compare(_T("' extensions: ")) == 0)))
990 if (extline[0] == '/')
991 extline = extline.Mid(15);
992 else
993 extline = extline.Mid(14);
994 CString sToken;
995 int curPos = 0;
996 sToken = extline.Tokenize(_T(";"), curPos);
997 while (!sToken.IsEmpty())
999 if (!sToken.IsEmpty())
1001 if (sToken[0] != '.')
1002 sToken = _T(".") + sToken;
1003 extensions.insert(sToken);
1005 sToken = extline.Tokenize(_T(";"), curPos);
1009 f.Close();
1011 catch (CFileException* e)
1013 e->Delete();
1016 for (std::set<CString>::const_iterator it = extensions.begin(); it != extensions.end(); ++it)
1018 if (type.IsEmpty() || (type.Compare(_T("Diff")) == 0))
1020 if (filename.Left(5).CompareNoCase(_T("diff-")) == 0)
1022 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + *it);
1023 CString diffregstring = diffreg;
1024 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
1025 diffreg = _T("wscript.exe \"") + file + _T("\" %base %mine") + kind;
1028 if (type.IsEmpty() || (type.Compare(_T("Merge"))==0))
1030 if (filename.Left(6).CompareNoCase(_T("merge-"))==0)
1032 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\MergeTools\\") + *it);
1033 CString diffregstring = diffreg;
1034 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
1035 diffreg = _T("wscript.exe \"") + file + _T("\" %merged %theirs %mine %base") + kind;
1041 return true;
1044 bool CAppUtils::Export(CString *BashHash, const CTGitPath *orgPath)
1046 // ask from where the export has to be done
1047 CExportDlg dlg;
1048 if(BashHash)
1049 dlg.m_Revision=*BashHash;
1050 if (orgPath)
1052 if (PathIsRelative(orgPath->GetWinPath()))
1053 dlg.m_orgPath = g_Git.m_CurrentDir + _T("\\") + orgPath->GetWinPathString();
1054 else
1055 dlg.m_orgPath = *orgPath;
1058 if (dlg.DoModal() == IDOK)
1060 CString cmd;
1061 cmd.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s --"),
1062 dlg.m_strFile, g_Git.FixBranchName(dlg.m_VersionName));
1064 CProgressDlg pro;
1065 pro.m_GitCmd=cmd;
1066 CGit git;
1067 if (!dlg.m_bWholeProject && !dlg.m_orgPath.IsEmpty() && PathIsDirectory(dlg.m_orgPath.GetWinPathString()))
1069 git.m_CurrentDir = dlg.m_orgPath.GetWinPathString();
1070 pro.m_Git = &git;
1072 return (pro.DoModal() == IDOK);
1074 return false;
1077 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash, bool switch_new_brach)
1079 CCreateBranchTagDlg dlg;
1080 dlg.m_bIsTag=IsTag;
1081 dlg.m_bSwitch=switch_new_brach;
1083 if(CommitHash)
1084 dlg.m_initialRefName = *CommitHash;
1086 if(dlg.DoModal()==IDOK)
1088 CString cmd;
1089 CString force;
1090 CString track;
1091 if(dlg.m_bTrack == TRUE)
1092 track=_T(" --track ");
1093 else if(dlg.m_bTrack == FALSE)
1094 track=_T(" --no-track");
1096 if(dlg.m_bForce)
1097 force=_T(" -f ");
1099 if(IsTag)
1101 CString sign;
1102 if(dlg.m_bSign)
1103 sign=_T("-s");
1105 cmd.Format(_T("git.exe tag %s %s %s %s"),
1106 force,
1107 sign,
1108 dlg.m_BranchTagName,
1109 g_Git.FixBranchName(dlg.m_VersionName)
1112 CString tempfile=::GetTempFile();
1113 if(!dlg.m_Message.Trim().IsEmpty())
1115 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_Message);
1116 cmd += _T(" -F ")+tempfile;
1119 else
1121 cmd.Format(_T("git.exe branch %s %s %s %s"),
1122 track,
1123 force,
1124 dlg.m_BranchTagName,
1125 g_Git.FixBranchName(dlg.m_VersionName)
1128 CString out;
1129 if(g_Git.Run(cmd,&out,CP_UTF8))
1131 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1132 return FALSE;
1134 if( !IsTag && dlg.m_bSwitch )
1136 // it is a new branch and the user has requested to switch to it
1137 PerformSwitch(dlg.m_BranchTagName);
1140 return TRUE;
1142 return FALSE;
1145 bool CAppUtils::Switch(CString initialRefName, bool autoclose)
1147 CGitSwitchDlg dlg;
1148 if(!initialRefName.IsEmpty())
1149 dlg.m_initialRefName = initialRefName;
1151 if (dlg.DoModal() == IDOK)
1153 CString branch;
1154 if (dlg.m_bBranch)
1155 branch = dlg.m_NewBranch;
1157 // if refs/heads/ is not stripped, checkout will detach HEAD
1158 // checkout prefers branches on name clashes (with tags)
1159 if (dlg.m_VersionName.Left(11) ==_T("refs/heads/") && dlg.m_bBranchOverride != TRUE)
1160 dlg.m_VersionName = dlg.m_VersionName.Mid(11);
1162 return PerformSwitch(dlg.m_VersionName, dlg.m_bForce == TRUE , branch, dlg.m_bBranchOverride == TRUE, dlg.m_bTrack, autoclose, dlg.m_bMerge == TRUE);
1164 return FALSE;
1167 bool CAppUtils::PerformSwitch(CString ref, bool bForce /* false */, CString sNewBranch /* CString() */, bool bBranchOverride /* false */, BOOL bTrack /* 2 */, bool autoClose /* false */, bool bMerge /* false */)
1169 CString cmd;
1170 CString track;
1171 CString force;
1172 CString branch;
1173 CString merge;
1175 if(!sNewBranch.IsEmpty()){
1176 if (bBranchOverride)
1178 branch.Format(_T("-B %s"), sNewBranch);
1180 else
1182 branch.Format(_T("-b %s"), sNewBranch);
1184 if (bTrack == TRUE)
1185 track = _T("--track");
1186 else if (bTrack == FALSE)
1187 track = _T("--no-track");
1189 if (bForce)
1190 force = _T("-f");
1191 if (bMerge)
1192 merge = _T("--merge");
1194 cmd.Format(_T("git.exe checkout %s %s %s %s %s --"),
1195 force,
1196 track,
1197 merge,
1198 branch,
1199 g_Git.FixBranchName(ref));
1201 CProgressDlg progress;
1202 progress.m_bAutoCloseOnSuccess = autoClose;
1203 progress.m_GitCmd = cmd;
1205 INT_PTR idPull = -1;
1206 INT_PTR idSubmoduleUpdate = -1;
1207 INT_PTR idMerge = -1;
1209 CTGitPath gitPath = g_Git.m_CurrentDir;
1210 if (gitPath.HasSubmodules())
1211 idSubmoduleUpdate = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1212 idPull = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
1213 CString currentBranch;
1214 bool hasBranch = g_Git.GetCurrentBranchFromFile(g_Git.m_CurrentDir, currentBranch) == 0;
1215 if (hasBranch)
1216 idMerge = progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUMERGE)));
1218 INT_PTR ret = progress.DoModal();
1219 if (idSubmoduleUpdate >= 0 && ret == IDC_PROGRESS_BUTTON1 + idSubmoduleUpdate)
1221 CString sCmd;
1222 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1224 RunTortoiseGitProc(sCmd);
1225 return TRUE;
1227 else if (ret == IDC_PROGRESS_BUTTON1 + idPull)
1229 Pull();
1230 return TRUE;
1232 else if (ret == IDC_PROGRESS_BUTTON1 + idMerge)
1234 Merge(&currentBranch);
1235 return TRUE;
1237 else if (ret == IDOK)
1238 return TRUE;
1240 return FALSE;
1243 class CIgnoreFile : public CStdioFile
1245 public:
1246 STRING_VECTOR m_Items;
1247 CString m_eol;
1249 virtual BOOL ReadString(CString& rString)
1251 if (GetPosition() == 0)
1253 unsigned char utf8bom[] = { 0xEF, 0xBB, 0xBF };
1254 char buf[3] = { 0, 0, 0 };
1255 Read(buf, 3);
1256 if (memcpy(buf, utf8bom, sizeof(utf8bom)))
1258 SeekToBegin();
1262 CStringA strA;
1263 char lastChar = '\0';
1264 for (char c = '\0'; Read(&c, 1) == 1; lastChar = c)
1266 if (c == '\r')
1267 continue;
1268 if (c == '\n')
1270 m_eol = lastChar == '\r' ? _T("\r\n") : _T("\n");
1271 break;
1273 strA.AppendChar(c);
1275 if (strA.IsEmpty())
1276 return FALSE;
1278 rString = CUnicodeUtils::GetUnicode(strA);
1279 return TRUE;
1282 void ResetState()
1284 m_Items.clear();
1285 m_eol = _T("");
1289 bool CAppUtils::OpenIgnoreFile(CIgnoreFile &file, const CString& filename)
1291 file.ResetState();
1292 if (!file.Open(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate | CFile::typeBinary))
1294 CMessageBox::Show(NULL, filename + _T(" Open Failure"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1295 return false;
1298 if (file.GetLength() > 0)
1300 CString fileText;
1301 while (file.ReadString(fileText))
1302 file.m_Items.push_back(fileText);
1303 file.Seek(file.GetLength() - 1, 0);
1304 char lastchar[1];
1305 file.Read(lastchar, 1);
1306 file.SeekToEnd();
1307 if (lastchar[0] != '\n')
1309 CStringA eol = CStringA(file.m_eol.IsEmpty() ? _T("\n") : file.m_eol);
1310 file.Write(eol.GetBuffer(), eol.GetLength());
1311 eol.ReleaseBuffer();
1314 else
1315 file.SeekToEnd();
1317 return true;
1320 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1322 CIgnoreDlg ignoreDlg;
1323 if (ignoreDlg.DoModal() == IDOK)
1325 CString ignorefile;
1326 ignorefile = g_Git.m_CurrentDir + _T("\\");
1328 switch (ignoreDlg.m_IgnoreFile)
1330 case 0:
1331 ignorefile += _T(".gitignore");
1332 break;
1333 case 2:
1334 g_GitAdminDir.GetAdminDirPath(g_Git.m_CurrentDir, ignorefile);
1335 ignorefile += _T("info/exclude");
1336 break;
1339 CIgnoreFile file;
1342 if (ignoreDlg.m_IgnoreFile != 1 && !OpenIgnoreFile(file, ignorefile))
1343 return false;
1345 for (int i = 0; i < path.GetCount(); ++i)
1347 if (ignoreDlg.m_IgnoreFile == 1)
1349 ignorefile = g_Git.m_CurrentDir + _T("\\") + path[i].GetContainingDirectory().GetWinPathString() + _T("\\.gitignore");
1350 if (!OpenIgnoreFile(file, ignorefile))
1351 return false;
1354 CString ignorePattern;
1355 if (ignoreDlg.m_IgnoreType == 0)
1357 if (ignoreDlg.m_IgnoreFile != 1 && !path[i].GetContainingDirectory().GetGitPathString().IsEmpty())
1358 ignorePattern += _T("/") + path[i].GetContainingDirectory().GetGitPathString();
1360 ignorePattern += _T("/");
1362 if (IsMask)
1364 ignorePattern += _T("*") + path[i].GetFileExtension();
1366 else
1368 ignorePattern += path[i].GetFileOrDirectoryName();
1371 // escape [ and ] so that files get ignored correctly
1372 ignorePattern.Replace(_T("["), _T("\\["));
1373 ignorePattern.Replace(_T("]"), _T("\\]"));
1375 bool found = false;
1376 for (int j = 0; j < file.m_Items.size(); ++j)
1378 if (file.m_Items[j] == ignorePattern)
1380 found = true;
1381 break;
1384 if (!found)
1386 file.m_Items.push_back(ignorePattern);
1387 ignorePattern += file.m_eol.IsEmpty() ? _T("\n") : file.m_eol;
1388 CStringA ignorePatternA = CUnicodeUtils::GetUTF8(ignorePattern);
1389 file.Write(ignorePatternA.GetBuffer(), ignorePatternA.GetLength());
1390 ignorePatternA.ReleaseBuffer();
1393 if (ignoreDlg.m_IgnoreFile == 1)
1394 file.Close();
1397 if (ignoreDlg.m_IgnoreFile != 1)
1398 file.Close();
1400 catch(...)
1402 file.Abort();
1403 return false;
1406 return true;
1408 return false;
1412 bool CAppUtils::GitReset(CString *CommitHash,int type)
1414 CResetDlg dlg;
1415 dlg.m_ResetType=type;
1416 dlg.m_ResetToVersion=*CommitHash;
1417 dlg.m_initialRefName = *CommitHash;
1418 if (dlg.DoModal() == IDOK)
1420 CString cmd;
1421 CString type;
1422 switch(dlg.m_ResetType)
1424 case 0:
1425 type=_T("--soft");
1426 break;
1427 case 1:
1428 type=_T("--mixed");
1429 break;
1430 case 2:
1431 type=_T("--hard");
1432 break;
1433 default:
1434 dlg.m_ResetType = 1;
1435 type=_T("--mixed");
1436 break;
1438 cmd.Format(_T("git.exe reset %s %s --"),type, dlg.m_ResetToVersion);
1440 while (true)
1442 CProgressDlg progress;
1443 progress.m_GitCmd=cmd;
1445 CTGitPath gitPath = g_Git.m_CurrentDir;
1446 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2)
1447 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1449 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
1451 INT_PTR ret;
1452 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
1454 CGitProgressDlg gitdlg;
1455 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
1456 gitdlg.SetRevision(dlg.m_ResetToVersion);
1457 gitdlg.SetResetType(dlg.m_ResetType);
1458 ret = gitdlg.DoModal();
1460 else
1461 ret = progress.DoModal();
1463 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 2 && ret == IDC_PROGRESS_BUTTON1)
1465 CString sCmd;
1466 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1468 RunTortoiseGitProc(sCmd);
1469 return TRUE;
1471 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
1472 continue; // retry
1473 else if (ret == IDOK)
1474 return TRUE;
1475 else
1476 break;
1479 return FALSE;
1482 void CAppUtils::DescribeFile(bool mode, bool base,CString &descript)
1484 if(mode == FALSE)
1486 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE));
1487 return;
1489 if(base)
1491 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED));
1492 return;
1494 descript = CString(MAKEINTRESOURCE(IDS_PROC_CREATED));
1495 return;
1498 void CAppUtils::RemoveTempMergeFile(CTGitPath &path)
1500 CString tempmergefile;
1503 tempmergefile = CAppUtils::GetMergeTempFile(_T("LOCAL"),path);
1504 CFile::Remove(tempmergefile);
1505 }catch(...)
1511 tempmergefile = CAppUtils::GetMergeTempFile(_T("REMOTE"),path);
1512 CFile::Remove(tempmergefile);
1513 }catch(...)
1519 tempmergefile = CAppUtils::GetMergeTempFile(_T("BASE"),path);
1520 CFile::Remove(tempmergefile);
1521 }catch(...)
1525 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1527 CString file;
1528 file=g_Git.m_CurrentDir+_T("\\") + merge.GetWinPathString()+_T(".")+type+merge.GetFileExtension();
1530 return file;
1533 bool ParseHashesFromLsFile(BYTE_VECTOR &out, CString &hash1, CString &hash2, CString &hash3)
1535 unsigned int pos = 0;
1536 CString one;
1537 CString part;
1539 while (pos >= 0 && pos < out.size())
1541 one.Empty();
1543 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1544 int tabstart = 0;
1545 one.Tokenize(_T("\t"), tabstart);
1547 tabstart = 0;
1548 part = one.Tokenize(_T(" "), tabstart); //Tag
1549 part = one.Tokenize(_T(" "), tabstart); //Mode
1550 part = one.Tokenize(_T(" "), tabstart); //Hash
1551 CString hash = part;
1552 part = one.Tokenize(_T("\t"), tabstart); //Stage
1553 int stage = _ttol(part);
1554 if (stage == 1)
1555 hash1 = hash;
1556 else if (stage == 2)
1557 hash2 = hash;
1558 else if (stage == 3)
1560 hash3 = hash;
1561 return true;
1564 pos = out.findNextString(pos);
1567 return false;
1570 bool CAppUtils::ConflictEdit(CTGitPath &path,bool /*bAlternativeTool*/,bool revertTheirMy)
1572 bool bRet = false;
1574 CTGitPath merge=path;
1575 CTGitPath directory = merge.GetDirectory();
1577 // we have the conflicted file (%merged)
1578 // now look for the other required files
1579 //GitStatus stat;
1580 //stat.GetStatus(merge);
1581 //if (stat.status == NULL)
1582 // return false;
1584 BYTE_VECTOR vector;
1586 CString cmd;
1587 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1589 if (g_Git.Run(cmd, &vector))
1591 return FALSE;
1594 if (merge.IsDirectory())
1596 CString baseHash, localHash, remoteHash;
1597 if (!ParseHashesFromLsFile(vector, baseHash, localHash, remoteHash))
1598 return FALSE;
1600 CString msg;
1601 msg.Format(_T("BASE: %s\nLOCAL: %s\nREMOTE: %s"), baseHash, localHash, remoteHash);
1602 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK);
1603 return TRUE;
1606 CTGitPathList list;
1607 if (list.ParserFromLsFile(vector))
1609 CMessageBox::Show(NULL, _T("Parse ls-files failed!"), _T("TortoiseGit"), MB_OK);
1610 return FALSE;
1613 if(list.GetCount() == 0)
1614 return FALSE;
1616 CTGitPath theirs;
1617 CTGitPath mine;
1618 CTGitPath base;
1620 if (revertTheirMy)
1622 mine.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1623 theirs.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1625 else
1627 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"), merge));
1628 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"), merge));
1630 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1632 CString format;
1634 //format=_T("git.exe cat-file blob \":%d:%s\"");
1635 format = _T("git.exe checkout-index --temp --stage=%d -- \"%s\"");
1636 CFile tempfile;
1637 //create a empty file, incase stage is not three
1638 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1639 tempfile.Close();
1640 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1641 tempfile.Close();
1642 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1643 tempfile.Close();
1645 bool b_base=false, b_local=false, b_remote=false;
1647 for (int i = 0; i< list.GetCount(); ++i)
1649 CString cmd;
1650 CString outfile;
1651 cmd.Empty();
1652 outfile.Empty();
1654 if( list[i].m_Stage == 1)
1656 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1657 b_base = true;
1658 outfile = base.GetWinPathString();
1661 if( list[i].m_Stage == 2 )
1663 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1664 b_local = true;
1665 outfile = mine.GetWinPathString();
1668 if( list[i].m_Stage == 3 )
1670 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1671 b_remote = true;
1672 outfile = theirs.GetWinPathString();
1674 CString output, err;
1675 if(!outfile.IsEmpty())
1676 if (!g_Git.Run(cmd, &output, &err, CP_UTF8))
1678 CString file;
1679 int start =0 ;
1680 file = output.Tokenize(_T("\t"), start);
1681 ::MoveFileEx(file,outfile,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
1683 else
1685 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1689 if(b_local && b_remote )
1691 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1692 if( revertTheirMy )
1693 bRet = !!CAppUtils::StartExtMerge(base, mine, theirs, merge, _T("BASE"), _T("REMOTE"), _T("LOCAL"));
1694 else
1695 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge,_T("BASE"), _T("REMOTE"), _T("LOCAL"));
1698 else
1700 CFile::Remove(mine.GetWinPathString());
1701 CFile::Remove(theirs.GetWinPathString());
1702 CFile::Remove(base.GetWinPathString());
1704 CDeleteConflictDlg dlg;
1705 DescribeFile(b_local, b_base,dlg.m_LocalStatus);
1706 DescribeFile(b_remote,b_base,dlg.m_RemoteStatus);
1707 dlg.m_bShowModifiedButton=b_base;
1708 dlg.m_File=merge.GetGitPathString();
1709 if(dlg.DoModal() == IDOK)
1711 CString cmd,out;
1712 if(dlg.m_bIsDelete)
1714 cmd.Format(_T("git.exe rm -- \"%s\""),merge.GetGitPathString());
1716 else
1717 cmd.Format(_T("git.exe add -- \"%s\""),merge.GetGitPathString());
1719 if (g_Git.Run(cmd, &out, CP_UTF8))
1721 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1722 return FALSE;
1724 return TRUE;
1726 else
1727 return FALSE;
1730 #if 0
1731 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1732 base, theirs, mine, merge);
1733 #endif
1734 #if 0
1735 if (stat.status->text_status == svn_wc_status_conflicted)
1737 // we have a text conflict, use our merge tool to resolve the conflict
1739 CTSVNPath theirs(directory);
1740 CTSVNPath mine(directory);
1741 CTSVNPath base(directory);
1742 bool bConflictData = false;
1744 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1746 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1747 bConflictData = true;
1749 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1751 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1752 bConflictData = true;
1754 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1756 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1757 bConflictData = true;
1759 else
1761 mine = merge;
1763 if (bConflictData)
1764 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1765 base, theirs, mine, merge);
1768 if (stat.status->prop_status == svn_wc_status_conflicted)
1770 // we have a property conflict
1771 CTSVNPath prej(directory);
1772 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1774 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1775 // there's a problem: the prej file contains a _description_ of the conflict, and
1776 // that description string might be translated. That means we have no way of parsing
1777 // the file to find out the conflicting values.
1778 // The only thing we can do: show a dialog with the conflict description, then
1779 // let the user either accept the existing property or open the property edit dialog
1780 // to manually change the properties and values. And a button to mark the conflict as
1781 // resolved.
1782 CEditPropConflictDlg dlg;
1783 dlg.SetPrejFile(prej);
1784 dlg.SetConflictedItem(merge);
1785 bRet = (dlg.DoModal() != IDCANCEL);
1789 if (stat.status->tree_conflict)
1791 // we have a tree conflict
1792 SVNInfo info;
1793 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1794 if (pInfoData)
1796 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1798 CTSVNPath theirs(directory);
1799 CTSVNPath mine(directory);
1800 CTSVNPath base(directory);
1801 bool bConflictData = false;
1803 if (pInfoData->treeconflict_theirfile)
1805 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1806 bConflictData = true;
1808 if (pInfoData->treeconflict_basefile)
1810 base.AppendPathString(pInfoData->treeconflict_basefile);
1811 bConflictData = true;
1813 if (pInfoData->treeconflict_myfile)
1815 mine.AppendPathString(pInfoData->treeconflict_myfile);
1816 bConflictData = true;
1818 else
1820 mine = merge;
1822 if (bConflictData)
1823 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1824 base, theirs, mine, merge);
1826 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1828 CString sConflictAction;
1829 CString sConflictReason;
1830 CString sResolveTheirs;
1831 CString sResolveMine;
1832 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1833 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1835 if (pInfoData->treeconflict_nodekind == svn_node_file)
1837 switch (pInfoData->treeconflict_operation)
1839 case svn_wc_operation_update:
1840 switch (pInfoData->treeconflict_action)
1842 case svn_wc_conflict_action_edit:
1843 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1844 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1845 break;
1846 case svn_wc_conflict_action_add:
1847 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1848 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1849 break;
1850 case svn_wc_conflict_action_delete:
1851 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1852 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1853 break;
1855 break;
1856 case svn_wc_operation_switch:
1857 switch (pInfoData->treeconflict_action)
1859 case svn_wc_conflict_action_edit:
1860 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1861 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1862 break;
1863 case svn_wc_conflict_action_add:
1864 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1865 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1866 break;
1867 case svn_wc_conflict_action_delete:
1868 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1869 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1870 break;
1872 break;
1873 case svn_wc_operation_merge:
1874 switch (pInfoData->treeconflict_action)
1876 case svn_wc_conflict_action_edit:
1877 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1878 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1879 break;
1880 case svn_wc_conflict_action_add:
1881 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1882 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1883 break;
1884 case svn_wc_conflict_action_delete:
1885 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1886 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1887 break;
1889 break;
1892 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1894 switch (pInfoData->treeconflict_operation)
1896 case svn_wc_operation_update:
1897 switch (pInfoData->treeconflict_action)
1899 case svn_wc_conflict_action_edit:
1900 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1901 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1902 break;
1903 case svn_wc_conflict_action_add:
1904 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1905 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1906 break;
1907 case svn_wc_conflict_action_delete:
1908 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1909 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1910 break;
1912 break;
1913 case svn_wc_operation_switch:
1914 switch (pInfoData->treeconflict_action)
1916 case svn_wc_conflict_action_edit:
1917 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1918 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1919 break;
1920 case svn_wc_conflict_action_add:
1921 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1922 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1923 break;
1924 case svn_wc_conflict_action_delete:
1925 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1926 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1927 break;
1929 break;
1930 case svn_wc_operation_merge:
1931 switch (pInfoData->treeconflict_action)
1933 case svn_wc_conflict_action_edit:
1934 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1935 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1936 break;
1937 case svn_wc_conflict_action_add:
1938 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1939 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1940 break;
1941 case svn_wc_conflict_action_delete:
1942 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1943 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1944 break;
1946 break;
1950 UINT uReasonID = 0;
1951 switch (pInfoData->treeconflict_reason)
1953 case svn_wc_conflict_reason_edited:
1954 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1955 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1956 break;
1957 case svn_wc_conflict_reason_obstructed:
1958 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1959 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1960 break;
1961 case svn_wc_conflict_reason_deleted:
1962 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1963 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1964 break;
1965 case svn_wc_conflict_reason_added:
1966 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1967 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1968 break;
1969 case svn_wc_conflict_reason_missing:
1970 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1971 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1972 break;
1973 case svn_wc_conflict_reason_unversioned:
1974 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1975 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1976 break;
1978 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1980 CTreeConflictEditorDlg dlg;
1981 dlg.SetConflictInfoText(sConflictReason);
1982 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1983 dlg.SetPath(treeConflictPath);
1984 INT_PTR dlgRet = dlg.DoModal();
1985 bRet = (dlgRet != IDCANCEL);
1989 #endif
1990 return bRet;
1993 bool CAppUtils::IsSSHPutty()
1995 CString sshclient=g_Git.m_Environment.GetEnv(_T("GIT_SSH"));
1996 sshclient=sshclient.MakeLower();
1997 if(sshclient.Find(_T("plink.exe"),0)>=0)
1999 return true;
2001 return false;
2004 CString CAppUtils::GetClipboardLink(const CString &skipGitPrefix, int paramsCount)
2006 if (!OpenClipboard(NULL))
2007 return CString();
2009 CString sClipboardText;
2010 HGLOBAL hglb = GetClipboardData(CF_TEXT);
2011 if (hglb)
2013 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
2014 sClipboardText = CString(lpstr);
2015 GlobalUnlock(hglb);
2017 hglb = GetClipboardData(CF_UNICODETEXT);
2018 if (hglb)
2020 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
2021 sClipboardText = lpstr;
2022 GlobalUnlock(hglb);
2024 CloseClipboard();
2026 if(!sClipboardText.IsEmpty())
2028 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
2029 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
2031 if(sClipboardText.Find( _T("http://")) == 0)
2032 return sClipboardText;
2034 if(sClipboardText.Find( _T("https://")) == 0)
2035 return sClipboardText;
2037 if(sClipboardText.Find( _T("git://")) == 0)
2038 return sClipboardText;
2040 if(sClipboardText.Find( _T("ssh://")) == 0)
2041 return sClipboardText;
2043 if(sClipboardText.GetLength()>=2)
2044 if( sClipboardText[1] == _T(':') )
2045 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
2046 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
2047 return sClipboardText;
2049 // trim prefixes like "git clone "
2050 if (!skipGitPrefix.IsEmpty() && sClipboardText.Find(skipGitPrefix) == 0)
2052 sClipboardText = sClipboardText.Mid(skipGitPrefix.GetLength()).Trim();
2053 int spacePos = -1;
2054 while (paramsCount >= 0)
2056 --paramsCount;
2057 spacePos = sClipboardText.Find(_T(' '), spacePos + 1);
2058 if (spacePos == -1)
2059 break;
2061 if (spacePos > 0 && paramsCount < 0)
2062 sClipboardText = sClipboardText.Left(spacePos);
2063 return sClipboardText;
2067 return CString(_T(""));
2070 CString CAppUtils::ChooseRepository(CString *path)
2072 CBrowseFolder browseFolder;
2073 CRegString regLastResopitory = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2075 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
2076 CString strCloneDirectory;
2077 if(path)
2078 strCloneDirectory=*path;
2079 else
2081 strCloneDirectory = regLastResopitory;
2084 CString title;
2085 title.LoadString(IDS_CHOOSE_REPOSITORY);
2087 browseFolder.SetInfo(title);
2089 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
2091 regLastResopitory = strCloneDirectory;
2092 return strCloneDirectory;
2094 else
2096 return CString();
2100 bool CAppUtils::SendPatchMail(CTGitPathList &list,bool autoclose)
2102 CSendMailDlg dlg;
2104 dlg.m_PathList = list;
2106 if(dlg.DoModal()==IDOK)
2108 if(dlg.m_PathList.GetCount() == 0)
2109 return FALSE;
2111 CGitProgressDlg progDlg;
2113 theApp.m_pMainWnd = &progDlg;
2114 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2116 progDlg.SetAutoClose(autoclose);
2118 progDlg.SetPathList(dlg.m_PathList);
2119 //ProjectProperties props;
2120 //props.ReadPropsPathList(dlg.m_pathList);
2121 //progDlg.SetProjectProperties(props);
2122 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2124 CSendMailPatch sendMailPatch(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2125 progDlg.SetSendMailOption(&sendMailPatch);
2127 progDlg.DoModal();
2129 return true;
2131 return false;
2134 bool CAppUtils::SendPatchMail(CString &cmd,CString &formatpatchoutput,bool autoclose)
2136 CTGitPathList list;
2137 CString log=formatpatchoutput;
2138 int start=log.Find(cmd);
2139 if(start >=0)
2140 CString one=log.Tokenize(_T("\n"),start);
2141 else
2142 start = 0;
2144 while(start>=0)
2146 CString one=log.Tokenize(_T("\n"),start);
2147 one=one.Trim();
2148 if(one.IsEmpty() || one.Find(_T("Success")) == 0)
2149 continue;
2150 one.Replace(_T('/'),_T('\\'));
2151 CTGitPath path;
2152 path.SetFromWin(one);
2153 list.AddPath(path);
2155 if (list.GetCount() > 0)
2157 return SendPatchMail(list, autoclose);
2159 else
2161 CMessageBox::Show(NULL, IDS_ERR_NOPATCHES, IDS_APPNAME, MB_ICONINFORMATION);
2162 return true;
2167 int CAppUtils::GetLogOutputEncode(CGit *pGit)
2169 CString output;
2170 output = pGit->GetConfigValue(_T("i18n.logOutputEncoding"));
2171 if(output.IsEmpty())
2172 return CUnicodeUtils::GetCPCode(pGit->GetConfigValue(_T("i18n.commitencoding")));
2173 else
2175 return CUnicodeUtils::GetCPCode(output);
2178 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
2180 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
2181 int cp = CUnicodeUtils::GetCPCode(g_Git.GetConfigValue(_T("i18n.commitencoding")));
2183 bool stripComments = (CRegDWORD(_T("Software\\TortoiseGit\\StripCommentedLines"), FALSE) == TRUE);
2185 message.TrimRight(L" \r\n");
2187 int len = message.GetLength();
2188 int start = 0;
2189 while (start >= 0 && start < len)
2191 int oldStart = start;
2192 start = message.Find(L"\n", oldStart);
2193 CString line = message.Mid(oldStart);
2194 if (start != -1)
2196 line = line.Left(start - oldStart);
2197 ++start; // move forward so we don't find the same char again
2199 if (stripComments && (line.GetLength() >= 1 && line.GetAt(0) == '#') || (start < 0 && line.IsEmpty()))
2200 continue;
2201 line.TrimRight(L" \r");
2202 CStringA lineA = CUnicodeUtils::GetMulti(line, cp) + L"\n";
2203 file.Write(lineA.GetBuffer(), lineA.GetLength());
2205 file.Close();
2206 return 0;
2209 bool CAppUtils::Pull(bool showPush, bool autoClose)
2211 CPullFetchDlg dlg;
2212 dlg.m_IsPull = TRUE;
2213 if (dlg.DoModal() == IDOK)
2215 CString url = dlg.m_RemoteURL;
2217 if (dlg.m_bAutoLoad)
2219 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2222 CString cmd;
2223 CGitHash hashOld;
2224 if (g_Git.GetHash(hashOld, _T("HEAD")))
2226 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
2227 return false;
2230 CString cmdRebase;
2231 CString noff;
2232 CString ffonly;
2233 CString squash;
2234 CString nocommit;
2235 CString notags;
2237 if (dlg.m_bRebase)
2238 cmdRebase = "--rebase ";
2240 if (!dlg.m_bFetchTags)
2241 notags = _T("--no-tags");
2243 if (dlg.m_bNoFF)
2244 noff=_T("--no-ff");
2246 if (dlg.m_bFFonly)
2247 ffonly = _T("--ff-only");
2249 if (dlg.m_bSquash)
2250 squash = _T("--squash");
2252 if (dlg.m_bNoCommit)
2253 nocommit = _T("--no-commit");
2255 int ver = CAppUtils::GetMsysgitVersion();
2257 if(ver >= 0x01070203) //above 1.7.0.2
2258 cmdRebase += _T("--progress ");
2260 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);
2261 CProgressDlg progress;
2262 progress.m_GitCmd = cmd;
2263 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_DIFFS)));
2264 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_PULL_LOG)));
2265 INT_PTR pushButton = showPush ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH))) + IDC_PROGRESS_BUTTON1 : -1;
2267 CTGitPath gitPath = g_Git.m_CurrentDir;
2268 INT_PTR smUpdateButton = gitPath.HasSubmodules() ? progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE))) + IDC_PROGRESS_BUTTON1 : -1;
2270 progress.m_bAutoCloseOnSuccess = autoClose;
2272 INT_PTR ret = progress.DoModal();
2274 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)
2276 CChangedDlg dlg;
2277 dlg.m_pathList.AddPath(CTGitPath());
2278 dlg.DoModal();
2280 return FALSE;
2283 CGitHash hashNew;
2284 if (g_Git.GetHash(hashNew, _T("HEAD")))
2286 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR);
2287 return FALSE;
2290 if( ret == IDC_PROGRESS_BUTTON1)
2292 if(hashOld == hashNew)
2294 if(progress.m_GitStatus == 0)
2295 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2296 return TRUE;
2299 CFileDiffDlg dlg;
2300 dlg.SetDiff(NULL, hashNew.ToString(), hashOld.ToString());
2301 dlg.DoModal();
2303 return TRUE;
2305 else if ( ret == IDC_PROGRESS_BUTTON1 +1 )
2307 if(hashOld == hashNew)
2309 if(progress.m_GitStatus == 0)
2310 CMessageBox::Show(NULL, IDS_UPTODATE, IDS_APPNAME, MB_OK | MB_ICONINFORMATION);
2311 return true;
2314 CLogDlg dlg;
2315 dlg.SetParams(CTGitPath(_T("")), CTGitPath(_T("")), _T(""), hashOld.ToString() + _T("..") + hashNew.ToString(), 0);
2316 dlg.DoModal();
2318 else if (ret == pushButton)
2320 Push(_T(""), autoClose);
2322 else if (ret == smUpdateButton)
2324 CString sCmd;
2325 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
2327 CAppUtils::RunTortoiseGitProc(sCmd);
2331 return false;
2334 bool CAppUtils::Fetch(CString remoteName, bool allowRebase, bool autoClose)
2336 CPullFetchDlg dlg;
2337 dlg.m_PreSelectRemote = remoteName;
2338 dlg.m_bAllowRebase = allowRebase;
2339 dlg.m_IsPull=FALSE;
2341 if(dlg.DoModal()==IDOK)
2343 if(dlg.m_bAutoLoad)
2345 if (dlg.m_bAllRemotes)
2347 STRING_VECTOR list;
2348 g_Git.GetRemoteList(list);
2350 STRING_VECTOR::const_iterator it = list.begin();
2351 while (it != list.end())
2353 CString remote(*it);
2354 CAppUtils::LaunchPAgent(NULL, &remote);
2355 ++it;
2358 else
2359 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2362 CString url;
2363 url=dlg.m_RemoteURL;
2364 CString cmd;
2365 CString arg;
2367 int ver = CAppUtils::GetMsysgitVersion();
2369 if(ver >= 0x01070203) //above 1.7.0.2
2370 arg = _T("--progress ");
2372 if (dlg.m_bPrune) {
2373 arg += _T("--prune ");
2376 if (dlg.m_bFetchTags == 1)
2378 arg += _T("--tags ");
2380 else if (dlg.m_bFetchTags == 0)
2382 arg += _T("--no-tags ");
2385 if (dlg.m_bAllRemotes)
2386 cmd.Format(_T("git.exe fetch --all -v %s"), arg);
2387 else
2388 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"), arg, url, dlg.m_RemoteBranchName);
2390 CProgressDlg progress;
2392 progress.m_bAutoCloseOnSuccess = autoClose;
2394 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2395 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_RESET)));
2397 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
2399 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2402 progress.m_GitCmd=cmd;
2403 INT_PTR userResponse;
2405 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
2407 CGitProgressDlg gitdlg;
2408 if (!dlg.m_bAllRemotes)
2409 gitdlg.SetUrl(url);
2410 gitdlg.SetCommand(CGitProgressList::GitProgress_Fetch);
2411 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);
2412 if (!dlg.m_bAllRemotes)
2413 gitdlg.SetRefSpec(dlg.m_RemoteBranchName);
2414 userResponse = gitdlg.DoModal();
2417 else
2418 userResponse = progress.DoModal();
2420 if (userResponse == IDC_PROGRESS_BUTTON1)
2422 CString cmd = _T("/command:log");
2423 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2424 RunTortoiseGitProc(cmd);
2425 return TRUE;
2427 else if (userResponse == IDC_PROGRESS_BUTTON1 + 1)
2429 CString currentBranch = g_Git.GetSymbolicRef();
2430 CString configName;
2431 configName.Format(_T("branch.%s.remote"), currentBranch);
2432 CString pullRemote = g_Git.GetConfigValue(configName);
2434 //Select pull-branch from current branch
2435 configName.Format(_T("branch.%s.merge"), currentBranch);
2436 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
2438 CString defaultUpstream;
2439 if (!pullRemote.IsEmpty() && !pullBranch.IsEmpty())
2440 defaultUpstream.Format(_T("remotes/%s/%s"), pullRemote, pullBranch);
2441 GitReset(&defaultUpstream, 2);
2442 return TRUE;
2444 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 2) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
2446 while(1)
2448 CRebaseDlg dlg;
2449 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2450 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
2451 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2452 INT_PTR response = dlg.DoModal();
2453 if(response == IDOK)
2455 return TRUE;
2457 else if (response == IDC_REBASE_POST_BUTTON)
2459 CString cmd = _T("/command:log");
2460 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2461 RunTortoiseGitProc(cmd);
2462 return TRUE;
2464 else if (response == IDC_REBASE_POST_BUTTON + 1)
2466 CString cmd, out, err;
2467 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2468 g_Git.m_CurrentDir,
2469 g_Git.FixBranchName(dlg.m_Upstream),
2470 g_Git.FixBranchName(dlg.m_Branch));
2471 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
2473 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2474 return FALSE;
2477 CAppUtils::SendPatchMail(cmd,out);
2478 return TRUE;
2480 else if (response == IDC_REBASE_POST_BUTTON + 2)
2481 continue;
2482 else if(response == IDCANCEL)
2483 return FALSE;
2485 return TRUE;
2487 else if (userResponse != IDCANCEL)
2488 return TRUE;
2490 return FALSE;
2493 static void PushCallback(CProgressDlg *dlg, void *caller, int result)
2495 if (result)
2497 dlg->m_PostCmdList.RemoveAll();
2498 bool rejected = dlg->GetLogText().Find(_T("! [rejected]")) > 0;
2499 *(bool*)caller = rejected;
2500 if (rejected)
2501 dlg->m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPULL)));
2502 dlg->m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2506 bool CAppUtils::Push(CString selectLocalBranch, bool autoClose)
2508 CPushDlg dlg;
2509 dlg.m_BranchSourceName = selectLocalBranch;
2510 CString error;
2511 DWORD exitcode = 0xFFFFFFFF;
2512 CTGitPathList list;
2513 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
2514 if (CHooks::Instance().PrePush(list,exitcode, error))
2516 if (exitcode)
2518 CString temp;
2519 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2520 //ReportError(temp);
2521 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2522 return false;
2526 if(dlg.DoModal()==IDOK)
2528 CString arg;
2530 if(dlg.m_bPack)
2531 arg += _T("--thin ");
2532 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
2533 arg += _T("--tags ");
2534 if(dlg.m_bForce)
2535 arg += _T("--force ");
2536 if (dlg.m_bSetUpstream)
2537 arg += _T("--set-upstream ");
2538 if (dlg.m_RecurseSubmodules == 1)
2539 arg += _T("--recurse-submodules=check ");
2540 if (dlg.m_RecurseSubmodules == 2)
2541 arg += _T("--recurse-submodules=on-demand ");
2543 int ver = CAppUtils::GetMsysgitVersion();
2545 if(ver >= 0x01070203) //above 1.7.0.2
2546 arg += _T("--progress ");
2548 CProgressDlg progress;
2549 progress.m_bAutoCloseOnSuccess=autoClose;
2551 STRING_VECTOR remotesList;
2552 if (dlg.m_bPushAllRemotes)
2553 g_Git.GetRemoteList(remotesList);
2554 else
2555 remotesList.push_back(dlg.m_URL);
2557 for (unsigned int i = 0; i < remotesList.size(); ++i)
2559 if (dlg.m_bAutoLoad)
2560 CAppUtils::LaunchPAgent(NULL, &remotesList[i]);
2562 CString cmd;
2563 if (dlg.m_bPushAllBranches)
2565 cmd.Format(_T("git.exe push --all %s \"%s\""),
2566 arg,
2567 remotesList[i]);
2569 if (dlg.m_bTags)
2571 progress.m_GitCmdList.push_back(cmd);
2572 cmd.Format(_T("git.exe push --tags %s \"%s\""), arg, remotesList[i]);
2575 else
2577 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2578 arg,
2579 remotesList[i],
2580 dlg.m_BranchSourceName);
2581 if (!dlg.m_BranchRemoteName.IsEmpty())
2583 cmd += _T(":") + dlg.m_BranchRemoteName;
2586 progress.m_GitCmdList.push_back(cmd);
2589 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2590 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2591 bool rejected = false;
2592 progress.m_caller = &rejected;
2593 progress.m_PostCmdCallback = PushCallback;
2594 INT_PTR ret = progress.DoModal();
2596 if(!progress.m_GitStatus)
2598 if (CHooks::Instance().PostPush(list,exitcode, error))
2600 if (exitcode)
2602 CString temp;
2603 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2604 //ReportError(temp);
2605 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2606 return false;
2609 if(ret == IDC_PROGRESS_BUTTON1)
2611 RequestPull(dlg.m_BranchRemoteName);
2613 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2615 Push(selectLocalBranch, autoClose);
2617 return TRUE;
2619 else
2621 if (rejected)
2623 // failed, pull first
2624 if (ret == IDC_PROGRESS_BUTTON1)
2626 Pull(true, autoClose);
2628 else if (ret == IDC_PROGRESS_BUTTON1 + 1)
2630 Push(selectLocalBranch, autoClose);
2633 else
2635 if (ret == IDC_PROGRESS_BUTTON1)
2637 Push(selectLocalBranch, autoClose);
2642 return FALSE;
2645 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2647 CRequestPullDlg dlg;
2648 dlg.m_RepositoryURL = repositoryUrl;
2649 dlg.m_EndRevision = endrevision;
2650 if (dlg.DoModal()==IDOK)
2652 CString cmd;
2653 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2655 CSysProgressDlg sysProgressDlg;
2656 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2657 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_CREATINGPULLREUQEST)));
2658 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2659 sysProgressDlg.SetShowProgressBar(false);
2660 sysProgressDlg.ShowModeless((HWND)NULL, true);
2662 CString tempFileName = GetTempFile();
2663 DeleteFile(tempFileName);
2664 CreateDirectory(tempFileName, NULL);
2665 tempFileName += _T("\\pullrequest.txt");
2666 if (g_Git.RunLogFile(cmd, tempFileName))
2668 CMessageBox::Show(NULL, IDS_ERR_PULLREUQESTFAILED, IDS_APPNAME, MB_OK);
2669 return false;
2672 if (sysProgressDlg.HasUserCancelled())
2674 CMessageBox::Show(NULL, IDS_SVN_USERCANCELLED, IDS_APPNAME, MB_OK);
2675 ::DeleteFile(tempFileName);
2676 return false;
2679 sysProgressDlg.Stop();
2681 if (dlg.m_bSendMail)
2683 CSendMailDlg dlg;
2684 dlg.m_PathList = CTGitPathList(CTGitPath(tempFileName));
2685 dlg.m_bCustomSubject = true;
2687 if (dlg.DoModal() == IDOK)
2689 if(dlg.m_PathList.GetCount() == 0)
2690 return FALSE;
2692 CGitProgressDlg progDlg;
2694 theApp.m_pMainWnd = &progDlg;
2695 progDlg.SetCommand(CGitProgressList::GitProgress_SendMail);
2697 progDlg.SetPathList(dlg.m_PathList);
2698 progDlg.SetItemCount(dlg.m_PathList.GetCount());
2700 CSendMailCombineable sendMailCombineable(dlg.m_To, dlg.m_CC, dlg.m_Subject, !!dlg.m_bAttachment, !!dlg.m_bCombine);
2701 progDlg.SetSendMailOption(&sendMailCombineable);
2703 progDlg.DoModal();
2705 return true;
2707 return false;
2710 CAppUtils::LaunchAlternativeEditor(tempFileName);
2712 return true;
2715 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2717 CString strDir(szPath);
2718 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2720 strDir.AppendChar(_T('\\'));
2722 std::vector<CString> vPath;
2723 CString strTemp;
2724 bool bSuccess = false;
2726 for (int i=0;i<strDir.GetLength();++i)
2728 if (strDir.GetAt(i) != _T('\\'))
2730 strTemp.AppendChar(strDir.GetAt(i));
2732 else
2734 vPath.push_back(strTemp);
2735 strTemp.AppendChar(_T('\\'));
2739 for (auto vIter = vPath.begin(); vIter != vPath.end(); ++vIter)
2741 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2744 return bSuccess;
2747 void CAppUtils::RemoveTrailSlash(CString &path)
2749 if(path.IsEmpty())
2750 return ;
2752 // For URL, do not trim the slash just after the host name component.
2753 int index = path.Find(_T("://"));
2754 if (index >= 0)
2756 index += 4;
2757 index = path.Find(_T('/'), index);
2758 if (index == path.GetLength() - 1)
2759 return;
2762 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2764 path=path.Left(path.GetLength()-1);
2765 if(path.IsEmpty())
2766 return;
2770 bool CAppUtils::CheckUserData()
2772 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2774 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2776 CTGitPath path(g_Git.m_CurrentDir);
2777 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2778 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2779 dlg.SetTreeWidth(220);
2780 dlg.m_DefaultPage = _T("gitconfig");
2782 dlg.DoModal();
2783 dlg.HandleRestart();
2786 else
2787 return false;
2790 return true;
2793 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2794 CTGitPathList &pathList,
2795 CTGitPathList &selectedList,
2796 bool bSelectFilesForCommit,
2797 bool autoClose)
2799 bool bFailed = true;
2801 if (!CheckUserData())
2802 return false;
2804 while (bFailed)
2806 bFailed = false;
2807 CCommitDlg dlg;
2808 dlg.m_sBugID = bugid;
2810 dlg.m_bWholeProject = bWholeProject;
2812 dlg.m_sLogMessage = sLogMsg;
2813 dlg.m_pathList = pathList;
2814 dlg.m_checkedPathList = selectedList;
2815 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2816 dlg.m_bAutoClose = autoClose;
2817 if (dlg.DoModal() == IDOK)
2819 if (dlg.m_pathList.GetCount()==0)
2820 return false;
2821 // if the user hasn't changed the list of selected items
2822 // we don't use that list. Because if we would use the list
2823 // of pre-checked items, the dialog would show different
2824 // checked items on the next startup: it would only try
2825 // to check the parent folder (which might not even show)
2826 // instead, we simply use an empty list and let the
2827 // default checking do its job.
2828 if (!dlg.m_pathList.IsEqual(pathList))
2829 selectedList = dlg.m_pathList;
2830 pathList = dlg.m_updatedPathList;
2831 sLogMsg = dlg.m_sLogMessage;
2832 bSelectFilesForCommit = true;
2834 if( dlg.m_bPushAfterCommit )
2836 switch(dlg.m_PostCmd)
2838 case GIT_POST_CMD_DCOMMIT:
2839 CAppUtils::SVNDCommit();
2840 break;
2841 default:
2842 CAppUtils::Push();
2845 else if (dlg.m_bCreateTagAfterCommit)
2847 CAppUtils::CreateBranchTag(TRUE);
2850 // CGitProgressDlg progDlg;
2851 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2852 // if (parser.HasVal(_T("closeonend")))
2853 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2854 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2855 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2856 // progDlg.SetPathList(dlg.m_pathList);
2857 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2858 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2859 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2860 // progDlg.SetItemCount(dlg.m_itemsCount);
2861 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2862 // progDlg.DoModal();
2863 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2864 // err = (DWORD)progDlg.DidErrorsOccur();
2865 // bFailed = progDlg.DidErrorsOccur();
2866 // bRet = progDlg.DidErrorsOccur();
2867 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2868 // if (DWORD(bFailRepeat)==0)
2869 // bFailed = false; // do not repeat if the user chose not to in the settings.
2872 return true;
2876 BOOL CAppUtils::SVNDCommit()
2878 CSVNDCommitDlg dcommitdlg;
2879 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2880 if (gitSetting == _T("")) {
2881 if (dcommitdlg.DoModal() != IDOK)
2883 return false;
2885 else
2887 if (dcommitdlg.m_remember)
2889 if (dcommitdlg.m_rmdir)
2891 gitSetting = _T("true");
2893 else
2895 gitSetting = _T("false");
2897 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2899 CString msg;
2900 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2901 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2907 BOOL IsStash = false;
2908 if(!g_Git.CheckCleanWorkTree())
2910 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2912 CSysProgressDlg sysProgressDlg;
2913 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2914 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2915 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2916 sysProgressDlg.SetShowProgressBar(false);
2917 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2918 sysProgressDlg.ShowModeless((HWND)NULL, true);
2920 CString cmd,out;
2921 cmd=_T("git.exe stash");
2922 if (g_Git.Run(cmd, &out, CP_UTF8))
2924 sysProgressDlg.Stop();
2925 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2926 return false;
2928 sysProgressDlg.Stop();
2930 IsStash =true;
2932 else
2934 return false;
2938 CProgressDlg progress;
2939 if (dcommitdlg.m_rmdir)
2941 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2943 else
2945 progress.m_GitCmd=_T("git.exe svn dcommit");
2947 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2949 if( IsStash)
2951 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2953 CSysProgressDlg sysProgressDlg;
2954 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2955 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2956 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2957 sysProgressDlg.SetShowProgressBar(false);
2958 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2959 sysProgressDlg.ShowModeless((HWND)NULL, true);
2961 CString cmd,out;
2962 cmd=_T("git.exe stash pop");
2963 if (g_Git.Run(cmd, &out, CP_UTF8))
2965 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
2966 sysProgressDlg.Stop();
2967 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2968 return false;
2970 ::DeleteFile(g_Git.m_CurrentDir + _T("\\sys$command"));
2971 sysProgressDlg.Stop();
2973 else
2975 return false;
2978 return TRUE;
2980 return FALSE;
2983 static void MergeCallback(CProgressDlg *dlg, void * /*caller*/, int result)
2985 if (result)
2987 dlg->m_PostCmdList.RemoveAll();
2989 CTGitPathList list;
2990 if (!g_Git.ListConflictFile(list) && list.GetCount() > 0)
2992 // there are conflict files
2993 dlg->m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROGRS_CMD_RESOLVE)));
2998 BOOL CAppUtils::Merge(CString *commit)
3000 if (!CheckUserData())
3001 return FALSE;
3003 CMergeDlg dlg;
3004 if(commit)
3005 dlg.m_initialRefName = *commit;
3007 if(dlg.DoModal()==IDOK)
3009 CString cmd;
3010 CString args;
3012 if(dlg.m_bNoFF)
3013 args += _T(" --no-ff");
3015 if(dlg.m_bSquash)
3016 args += _T(" --squash");
3018 if(dlg.m_bNoCommit)
3019 args += _T(" --no-commit");
3021 if (dlg.m_bLog)
3023 CString fmt;
3024 fmt.Format(_T(" --log=%d"), dlg.m_nLog);
3025 args += fmt;
3028 if (!dlg.m_MergeStrategy.IsEmpty())
3030 args += _T(" --strategy=") + dlg.m_MergeStrategy;
3031 if (!dlg.m_StrategyOption.IsEmpty())
3033 args += _T(" --strategy-option=") + dlg.m_StrategyOption;
3034 if (!dlg.m_StrategyParam.IsEmpty())
3035 args += _T("=") + dlg.m_StrategyParam;
3039 if(!dlg.m_strLogMesage.IsEmpty())
3041 CString logmsg = dlg.m_strLogMesage;
3042 logmsg.Replace(_T("\""), _T("\\\""));
3043 args += _T(" -m \"") + logmsg + _T("\"");
3045 cmd.Format(_T("git.exe merge %s %s"), args, g_Git.FixBranchName(dlg.m_VersionName));
3047 CProgressDlg Prodlg;
3048 Prodlg.m_GitCmd = cmd;
3050 if (dlg.m_bNoCommit)
3051 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
3052 else if (dlg.m_bIsBranch)
3053 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
3055 Prodlg.m_PostCmdCallback = MergeCallback;
3057 INT_PTR ret = Prodlg.DoModal();
3058 if (Prodlg.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3060 CTGitPathList pathlist;
3061 CTGitPathList selectedlist;
3062 pathlist.AddPath(g_Git.m_CurrentDir);
3063 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
3064 CString str;
3065 CAppUtils::Commit(CString(), false, str, pathlist, selectedlist, bSelectFilesForCommit);
3067 else if (ret == IDC_PROGRESS_BUTTON1)
3069 if (dlg.m_bNoCommit)
3071 CString sLogMsg;
3072 CTGitPathList pathList;
3073 CTGitPathList selectedList;
3074 return Commit(_T(""), TRUE, sLogMsg, pathList, selectedList, true);
3076 else if (dlg.m_bIsBranch)
3078 CString msg;
3079 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
3080 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
3082 CString cmd, out;
3083 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
3084 if (g_Git.Run(cmd, &out, CP_UTF8))
3085 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
3090 return !Prodlg.m_GitStatus;
3092 return false;
3095 BOOL CAppUtils::MergeAbort()
3097 CMergeAbortDlg dlg;
3098 if (dlg.DoModal() == IDOK)
3100 CString cmd;
3101 CString type;
3102 switch (dlg.m_ResetType)
3104 case 0:
3105 type = _T("--mixed");
3106 break;
3107 case 1:
3108 type = _T("--hard");
3109 break;
3110 default:
3111 dlg.m_ResetType = 0;
3112 type = _T("--mixed");
3113 break;
3115 cmd.Format(_T("git.exe reset %s HEAD --"), type);
3117 while (true)
3119 CProgressDlg progress;
3120 progress.m_GitCmd = cmd;
3122 CTGitPath gitPath = g_Git.m_CurrentDir;
3123 if (gitPath.HasSubmodules() && dlg.m_ResetType == 1)
3124 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3126 progress.m_PostFailCmdList.Add(CString(MAKEINTRESOURCE(IDS_MSGBOX_RETRY)));
3128 INT_PTR ret;
3129 if (g_Git.UsingLibGit2(CGit::GIT_CMD_RESET))
3131 CGitProgressDlg gitdlg;
3132 gitdlg.SetCommand(CGitProgressList::GitProgress_Reset);
3133 gitdlg.SetRevision(_T("HEAD"));
3134 gitdlg.SetResetType(dlg.m_ResetType + 1);
3135 ret = gitdlg.DoModal();
3137 else
3138 ret = progress.DoModal();
3140 if (progress.m_GitStatus == 0 && gitPath.HasSubmodules() && dlg.m_ResetType == 1 && ret == IDC_PROGRESS_BUTTON1)
3142 CString sCmd;
3143 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3145 CCommonAppUtils::RunTortoiseGitProc(sCmd);
3146 return TRUE;
3148 else if (progress.m_GitStatus != 0 && ret == IDC_PROGRESS_BUTTON1)
3149 continue; // retry
3150 else if (ret == IDOK)
3151 return TRUE;
3152 else
3153 break;
3156 return FALSE;
3159 void CAppUtils::EditNote(GitRev *rev)
3161 if (!CheckUserData())
3162 return;
3164 CInputDlg dlg;
3165 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3166 dlg.m_sInputText = rev->m_Notes;
3167 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
3168 //dlg.m_pProjectProperties = &m_ProjectProperties;
3169 dlg.m_bUseLogWidth = true;
3170 if(dlg.DoModal() == IDOK)
3172 CString cmd,output;
3173 cmd=_T("notes add -f -F \"");
3175 CString tempfile=::GetTempFile();
3176 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
3177 cmd += tempfile;
3178 cmd += _T("\" ");
3179 cmd += rev->m_CommitHash.ToString();
3183 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
3185 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3188 else
3190 rev->m_Notes = dlg.m_sInputText;
3192 }catch(...)
3194 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
3196 CFile::Remove(tempfile);
3201 int CAppUtils::GetMsysgitVersion()
3203 if (g_Git.ms_LastMsysGitVersion)
3204 return g_Git.ms_LastMsysGitVersion;
3206 CString cmd;
3207 CString versiondebug;
3208 CString version;
3210 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
3211 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
3213 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
3215 __int64 time=0;
3216 if (!g_Git.GetFileModifyTime(gitpath, &time))
3218 if((DWORD)time == regTime)
3220 g_Git.ms_LastMsysGitVersion = regVersion;
3221 return regVersion;
3225 CString err;
3226 cmd = _T("git.exe --version");
3227 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
3229 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);
3230 return -1;
3233 int start=0;
3234 int ver = 0;
3236 versiondebug = version;
3240 CString str=version.Tokenize(_T("."), start);
3241 int space = str.ReverseFind(_T(' '));
3242 str = str.Mid(space+1,start);
3243 ver = _ttol(str);
3244 ver <<=24;
3246 version = version.Mid(start);
3247 start = 0;
3249 str = version.Tokenize(_T("."), start);
3251 ver |= (_ttol(str) & 0xFF) << 16;
3253 str = version.Tokenize(_T("."), start);
3254 ver |= (_ttol(str) & 0xFF) << 8;
3256 str = version.Tokenize(_T("."), start);
3257 ver |= (_ttol(str) & 0xFF);
3259 catch(...)
3261 if (!ver)
3263 CMessageBox::Show(NULL, _T("Could not parse git.exe version number: \"") + versiondebug + _T("\""), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
3264 return -1;
3268 regTime = time&0xFFFFFFFF;
3269 regVersion = ver;
3270 g_Git.ms_LastMsysGitVersion = ver;
3272 return ver;
3275 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
3277 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
3279 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
3281 if (hShell.IsValid()) {
3282 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
3283 if (pfnSHGPSFW) {
3284 IPropertyStore *pps;
3285 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
3286 if (SUCCEEDED(hr)) {
3287 PROPVARIANT var;
3288 var.vt = VT_BOOL;
3289 var.boolVal = VARIANT_TRUE;
3290 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
3291 pps->Release();
3297 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
3299 ASSERT(dialogname.GetLength() < 70);
3300 ASSERT(urlorpath.GetLength() < MAX_PATH);
3301 WCHAR pathbuf[MAX_PATH] = {0};
3303 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
3305 wcscat_s(pathbuf, L" - ");
3306 wcscat_s(pathbuf, dialogname);
3307 wcscat_s(pathbuf, L" - ");
3308 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
3309 SetWindowText(hWnd, pathbuf);
3312 bool CAppUtils::BisectStart(CString lastGood, CString firstBad, bool autoClose)
3314 if (!g_Git.CheckCleanWorkTree())
3316 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
3318 CSysProgressDlg sysProgressDlg;
3319 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
3320 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
3321 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
3322 sysProgressDlg.SetShowProgressBar(false);
3323 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
3324 sysProgressDlg.ShowModeless((HWND)NULL, true);
3326 CString cmd, out;
3327 cmd = _T("git.exe stash");
3328 if (g_Git.Run(cmd, &out, CP_UTF8))
3330 sysProgressDlg.Stop();
3331 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK);
3332 return false;
3334 sysProgressDlg.Stop();
3336 else
3337 return false;
3340 CBisectStartDlg bisectStartDlg;
3342 if (!lastGood.IsEmpty())
3343 bisectStartDlg.m_sLastGood = lastGood;
3344 if (!firstBad.IsEmpty())
3345 bisectStartDlg.m_sFirstBad = firstBad;
3347 if (bisectStartDlg.DoModal() == IDOK)
3349 CProgressDlg progress;
3350 theApp.m_pMainWnd = &progress;
3351 progress.m_bAutoCloseOnSuccess = autoClose;
3352 progress.m_GitCmdList.push_back(_T("git.exe bisect start"));
3353 progress.m_GitCmdList.push_back(_T("git.exe bisect good ") + bisectStartDlg.m_LastGoodRevision);
3354 progress.m_GitCmdList.push_back(_T("git.exe bisect bad ") + bisectStartDlg.m_FirstBadRevision);
3356 CTGitPath path(g_Git.m_CurrentDir);
3358 if (path.HasSubmodules())
3359 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
3361 INT_PTR ret = progress.DoModal();
3362 if (path.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
3364 CString sCmd;
3365 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
3367 CAppUtils::RunTortoiseGitProc(sCmd);
3368 return true;
3370 else if (ret == IDOK)
3371 return true;
3374 return false;
3377 int CAppUtils::Git2GetUserPassword(git_cred **out, const char *url, const char *username_from_url, unsigned int /*allowed_types*/, void * /*payload*/)
3379 CUserPassword dlg;
3380 dlg.m_URL = CUnicodeUtils::GetUnicode(url, CP_UTF8);
3381 if (username_from_url)
3382 dlg.m_UserName = CUnicodeUtils::GetUnicode(username_from_url, CP_UTF8);
3384 CStringA username, password;
3385 if (dlg.DoModal() == IDOK)
3387 username = CUnicodeUtils::GetMulti(dlg.m_UserName, CP_UTF8);
3388 password = CUnicodeUtils::GetMulti(dlg.m_Password, CP_UTF8);
3389 return git_cred_userpass_plaintext_new(out, username, password);
3391 return -1;