Do not include "Success" in sendmail dialog
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob4eaca493d9f0c2f6bf4e57dbc3e6661cf56ac44b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2003-2011 - 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 "resource.h"
22 #include "TortoiseProc.h"
23 #include "PathUtils.h"
24 #include "AppUtils.h"
25 //#include "GitProperties.h"
26 #include "StringUtils.h"
27 #include "MessageBox.h"
28 #include "Registry.h"
29 #include "TGitPath.h"
30 #include "Git.h"
31 //#include "RepositoryBrowser.h"
32 //#include "BrowseFolder.h"
33 #include "UnicodeUtils.h"
34 #include "ExportDlg.h"
35 #include "ProgressDlg.h"
36 #include "GitAdminDir.h"
37 #include "ProgressDlg.h"
38 #include "BrowseFolder.h"
39 #include "DirFileEnum.h"
40 #include "MessageBox.h"
41 #include "GitStatus.h"
42 #include "CreateBranchTagDlg.h"
43 #include "GitSwitchDlg.h"
44 #include "ResetDlg.h"
45 #include "DeleteConflictDlg.h"
46 #include "ChangedDlg.h"
47 #include "SendMailDlg.h"
48 #include "GITProgressDlg.h"
49 #include "PushDlg.h"
50 #include "CommitDlg.h"
51 #include "MergeDlg.h"
52 #include "hooks.h"
53 #include "..\Settings\Settings.h"
54 #include "InputDlg.h"
55 #include "SVNDCommitDlg.h"
56 #include "requestpulldlg.h"
57 #include "PullFetchDlg.h"
58 #include "RebaseDlg.h"
59 #include "PropKey.h"
60 #include "StashSave.h"
61 #include "IgnoreDlg.h"
62 #include "FormatMessageWrapper.h"
63 #include "SmartHandle.h"
64 #include "BisectStartDlg.h"
65 #include "SysProgressDlg.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 if (sysProgressDlg.IsValid())
110 sysProgressDlg.Stop();
111 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
113 else
115 if (sysProgressDlg.IsValid())
116 sysProgressDlg.Stop();
117 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHSUCCESS)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
118 return true;
121 return false;
124 bool CAppUtils::StashApply(CString ref, bool showChanges /* true */)
126 CString cmd,out;
127 cmd = _T("git.exe stash apply ");
128 if (ref.Find(_T("refs/")) == 0)
129 ref = ref.Mid(5);
130 if (ref.Find(_T("stash{")) == 0)
131 ref = _T("stash@") + ref.Mid(5);
132 cmd += ref;
134 CSysProgressDlg sysProgressDlg;
135 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
136 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
137 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
138 sysProgressDlg.SetShowProgressBar(false);
139 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
140 sysProgressDlg.ShowModeless((HWND)NULL, true);
142 int ret = g_Git.Run(cmd, &out, CP_UTF8);
144 if (sysProgressDlg.IsValid())
145 sysProgressDlg.Stop();
147 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
148 if (ret && !(ret == 1 && hasConflicts))
150 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHAPPLYFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
152 else
154 CString message;
155 message.LoadString(IDS_PROC_STASHAPPLYSUCCESS);
156 if (hasConflicts)
157 message.LoadString(IDS_PROC_STASHAPPLYFAILEDCONFLICTS);
158 if (showChanges)
160 if(CMessageBox::Show(NULL,message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES))
161 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
163 CChangedDlg dlg;
164 dlg.m_pathList.AddPath(CTGitPath());
165 dlg.DoModal();
167 return true;
169 else
171 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
172 return true;
175 return false;
178 bool CAppUtils::StashPop(bool showChanges /* true */)
180 CString cmd,out;
181 cmd=_T("git.exe stash pop ");
183 CSysProgressDlg sysProgressDlg;
184 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
185 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
186 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
187 sysProgressDlg.SetShowProgressBar(false);
188 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
189 sysProgressDlg.ShowModeless((HWND)NULL, true);
191 int ret = g_Git.Run(cmd, &out, CP_UTF8);
193 if (sysProgressDlg.IsValid())
194 sysProgressDlg.Stop();
196 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
197 if (ret && !(ret == 1 && hasConflicts))
199 CMessageBox::Show(NULL,CString(MAKEINTRESOURCE(IDS_PROC_STASHPOPFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
201 else
203 CString message;
204 message.LoadString(IDS_PROC_STASHPOPSUCCESS);
205 if (hasConflicts)
206 message.LoadString(IDS_PROC_STASHPOPFAILEDCONFLICTS);
207 if (showChanges)
209 if(CMessageBox::Show(NULL,CString(message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES)))
210 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
212 CChangedDlg dlg;
213 dlg.m_pathList.AddPath(CTGitPath());
214 dlg.DoModal();
216 return true;
218 else
220 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
221 return true;
224 return false;
227 bool CAppUtils::GetMimeType(const CTGitPath& /*file*/, CString& /*mimetype*/)
229 #if 0
230 GitProperties props(file, GitRev::REV_WC, false);
231 for (int i = 0; i < props.GetCount(); ++i)
233 if (props.GetItemName(i).compare(_T("svn:mime-type"))==0)
235 mimetype = props.GetItemValue(i).c_str();
236 return true;
239 #endif
240 return false;
243 BOOL CAppUtils::StartExtMerge(
244 const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,
245 const CString& basename, const CString& theirname, const CString& yourname, const CString& mergedname, bool bReadOnly)
248 CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));
249 CString ext = mergedfile.GetFileExtension();
250 CString com = regCom;
251 bool bInternal = false;
253 CString mimetype;
254 if (ext != "")
256 // is there an extension specific merge tool?
257 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());
258 if (CString(mergetool) != "")
260 com = mergetool;
263 if (GetMimeType(yourfile, mimetype) || GetMimeType(theirfile, mimetype) || GetMimeType(basefile, mimetype))
265 // is there a mime type specific merge tool?
266 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + mimetype);
267 if (CString(mergetool) != "")
269 com = mergetool;
272 // is there a filename specific merge tool?
273 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\.") + mergedfile.GetFilename().MakeLower());
274 if (CString(mergetool) != "")
276 com = mergetool;
279 if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))
281 // use TortoiseMerge
282 bInternal = true;
283 CRegString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE);
284 com = tortoiseMergePath;
285 if (com.IsEmpty())
287 com = CPathUtils::GetAppDirectory();
288 com += _T("TortoiseMerge.exe");
290 com = _T("\"") + com + _T("\"");
291 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
292 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
294 // check if the params are set. If not, just add the files to the command line
295 if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))
297 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");
298 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");
299 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");
300 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");
302 if (basefile.IsEmpty())
304 com.Replace(_T("/base:%base"), _T(""));
305 com.Replace(_T("%base"), _T(""));
307 else
308 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));
309 if (theirfile.IsEmpty())
311 com.Replace(_T("/theirs:%theirs"), _T(""));
312 com.Replace(_T("%theirs"), _T(""));
314 else
315 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));
316 if (yourfile.IsEmpty())
318 com.Replace(_T("/mine:%mine"), _T(""));
319 com.Replace(_T("%mine"), _T(""));
321 else
322 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));
323 if (mergedfile.IsEmpty())
325 com.Replace(_T("/merged:%merged"), _T(""));
326 com.Replace(_T("%merged"), _T(""));
328 else
329 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));
330 if (basename.IsEmpty())
332 if (basefile.IsEmpty())
334 com.Replace(_T("/basename:%bname"), _T(""));
335 com.Replace(_T("%bname"), _T(""));
337 else
339 com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));
342 else
343 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));
344 if (theirname.IsEmpty())
346 if (theirfile.IsEmpty())
348 com.Replace(_T("/theirsname:%tname"), _T(""));
349 com.Replace(_T("%tname"), _T(""));
351 else
353 com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));
356 else
357 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));
358 if (yourname.IsEmpty())
360 if (yourfile.IsEmpty())
362 com.Replace(_T("/minename:%yname"), _T(""));
363 com.Replace(_T("%yname"), _T(""));
365 else
367 com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));
370 else
371 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));
372 if (mergedname.IsEmpty())
374 if (mergedfile.IsEmpty())
376 com.Replace(_T("/mergedname:%mname"), _T(""));
377 com.Replace(_T("%mname"), _T(""));
379 else
381 com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));
384 else
385 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));
387 if ((bReadOnly)&&(bInternal))
388 com += _T(" /readonly");
390 if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))
392 return FALSE;
395 return TRUE;
398 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)
400 CString viewer;
401 // use TortoiseMerge
402 viewer = CPathUtils::GetAppDirectory();
403 viewer += _T("TortoiseMerge.exe");
405 viewer = _T("\"") + viewer + _T("\"");
406 viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");
407 viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");
408 if (bReversed)
409 viewer += _T(" /reversedpatch");
410 if (!sOriginalDescription.IsEmpty())
411 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");
412 if (!sPatchedDescription.IsEmpty())
413 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");
414 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
416 return FALSE;
418 return TRUE;
421 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)
423 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file2.GetFilename().MakeLower());
424 if (!difftool.IsEmpty())
425 return difftool;
426 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\.") + file1.GetFilename().MakeLower());
427 if (!difftool.IsEmpty())
428 return difftool;
430 // Is there a mime type specific diff tool?
431 CString mimetype;
432 if (GetMimeType(file1, mimetype) || GetMimeType(file2, mimetype))
434 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + mimetype);
435 if (!difftool.IsEmpty())
436 return difftool;
439 // Is there an extension specific diff tool?
440 CString ext = file2.GetFileExtension().MakeLower();
441 if (!ext.IsEmpty())
443 difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);
444 if (!difftool.IsEmpty())
445 return difftool;
446 // Maybe we should use TortoiseIDiff?
447 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
448 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
449 (ext == _T(".png")) || (ext == _T(".ico")) ||
450 (ext == _T(".dib")) || (ext == _T(".emf")))
452 return
453 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseIDiff.exe") + _T("\"") +
454 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname");
458 // Finally, pick a generic external diff tool
459 difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));
460 return difftool;
463 bool CAppUtils::StartExtDiff(
464 const CString& file1, const CString& file2,
465 const CString& sName1, const CString& sName2,
466 const CString& originalFile1, const CString& originalFile2,
467 const git_revnum_t& hash1, const git_revnum_t& hash2,
468 const DiffFlags& flags)
470 CString viewer;
472 CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);
473 if (!flags.bBlame || !(DWORD)blamediff)
475 viewer = PickDiffTool(file1, file2);
476 // If registry entry for a diff program is commented out, use TortoiseMerge.
477 bool bCommentedOut = viewer.Left(1) == _T("#");
478 if (flags.bAlternativeTool)
480 // Invert external vs. internal diff tool selection.
481 if (bCommentedOut)
482 viewer.Delete(0); // uncomment
483 else
484 viewer = "";
486 else if (bCommentedOut)
487 viewer = "";
490 bool bInternal = viewer.IsEmpty();
491 if (bInternal)
493 viewer =
494 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseMerge.exe") + _T("\"") +
495 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
496 if (flags.bBlame)
497 viewer += _T(" /blame");
499 // check if the params are set. If not, just add the files to the command line
500 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
502 viewer += _T(" \"")+file1+_T("\"");
503 viewer += _T(" \"")+file2+_T("\"");
505 if (viewer.Find(_T("%base")) >= 0)
507 viewer.Replace(_T("%base"), _T("\"")+file1+_T("\""));
509 if (viewer.Find(_T("%mine")) >= 0)
511 viewer.Replace(_T("%mine"), _T("\"")+file2+_T("\""));
514 if (sName1.IsEmpty())
515 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));
516 else
517 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
519 if (sName2.IsEmpty())
520 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));
521 else
522 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
524 viewer.Replace(_T("%bpath"), _T("\"") + originalFile1 + _T("\""));
525 viewer.Replace(_T("%ypath"), _T("\"") + originalFile2 + _T("\""));
527 viewer.Replace(_T("%brev"), _T("\"") + hash1 + _T("\""));
528 viewer.Replace(_T("%yrev"), _T("\"") + hash2 + _T("\""));
530 if (flags.bReadOnly && bInternal)
531 viewer += _T(" /readonly");
533 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);
536 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)
538 CString viewer;
539 CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
540 viewer = v;
541 if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))
543 // use TortoiseUDiff
544 viewer = CPathUtils::GetAppDirectory();
545 viewer += _T("TortoiseUDiff.exe");
546 // enquote the path to TortoiseUDiff
547 viewer = _T("\"") + viewer + _T("\"");
548 // add the params
549 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");
552 if (viewer.Find(_T("%1"))>=0)
554 if (viewer.Find(_T("\"%1\"")) >= 0)
555 viewer.Replace(_T("%1"), patchfile);
556 else
557 viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));
559 else
560 viewer += _T(" \"") + patchfile + _T("\"");
561 if (viewer.Find(_T("%title")) >= 0)
563 viewer.Replace(_T("%title"), title);
566 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
568 return FALSE;
570 return TRUE;
573 BOOL CAppUtils::StartTextViewer(CString file)
575 CString viewer;
576 CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);
577 viewer = txt;
578 viewer = viewer + _T("\\Shell\\Open\\Command\\");
579 CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);
580 viewer = txtexe;
582 DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
583 TCHAR * buf = new TCHAR[len+1];
584 ExpandEnvironmentStrings(viewer, buf, len);
585 viewer = buf;
586 delete [] buf;
587 len = ExpandEnvironmentStrings(file, NULL, 0);
588 buf = new TCHAR[len+1];
589 ExpandEnvironmentStrings(file, buf, len);
590 file = buf;
591 delete [] buf;
592 file = _T("\"")+file+_T("\"");
593 if (viewer.IsEmpty())
595 viewer = _T("RUNDLL32 Shell32,OpenAs_RunDLL");
597 if (viewer.Find(_T("\"%1\"")) >= 0)
599 viewer.Replace(_T("\"%1\""), file);
601 else if (viewer.Find(_T("%1")) >= 0)
603 viewer.Replace(_T("%1"), file);
605 else
607 viewer += _T(" ");
608 viewer += file;
611 if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))
613 return FALSE;
615 return TRUE;
618 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)
620 DWORD length = 0;
621 CAutoFile hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
622 if (!hFile)
623 return TRUE;
624 length = ::GetFileSize(hFile, NULL);
625 if (length < 4)
626 return TRUE;
627 return FALSE;
631 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)
633 LOGFONT logFont;
634 HDC hScreenDC = ::GetDC(NULL);
635 logFont.lfHeight = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);
636 ::ReleaseDC(NULL, hScreenDC);
637 logFont.lfWidth = 0;
638 logFont.lfEscapement = 0;
639 logFont.lfOrientation = 0;
640 logFont.lfWeight = FW_NORMAL;
641 logFont.lfItalic = 0;
642 logFont.lfUnderline = 0;
643 logFont.lfStrikeOut = 0;
644 logFont.lfCharSet = DEFAULT_CHARSET;
645 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
646 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
647 logFont.lfQuality = DRAFT_QUALITY;
648 logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;
649 _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
650 VERIFY(fontToCreate.CreateFontIndirect(&logFont));
653 bool CAppUtils::LaunchPAgent(CString *keyfile,CString * pRemote)
655 CString key,remote;
656 CString cmd,out;
657 if( pRemote == NULL)
659 remote=_T("origin");
661 else
663 remote=*pRemote;
665 if(keyfile == NULL)
667 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
668 key = g_Git.GetConfigValue(cmd);
669 int start=0;
670 key = key.Tokenize(_T("\n"),start);
672 else
673 key=*keyfile;
675 if(key.IsEmpty())
676 return false;
678 CString proc=CPathUtils::GetAppDirectory();
679 proc += _T("pageant.exe \"");
680 proc += key;
681 proc += _T("\"");
683 CString tempfile = GetTempFile();
684 ::DeleteFile(tempfile);
686 proc += _T(" -c \"");
687 proc += CPathUtils::GetAppDirectory();
688 proc += _T("touch.exe\"");
689 proc += _T(" \"");
690 proc += tempfile;
691 proc += _T("\"");
693 CString appDir = CPathUtils::GetAppDirectory();
694 bool b = LaunchApplication(proc, IDS_ERR_PAGEANT, true, &appDir);
695 if(!b)
696 return b;
698 int i=0;
699 while(!::PathFileExists(tempfile))
701 Sleep(100);
702 i++;
703 if(i>10*60*5)
704 break; //timeout 5 minutes
707 if( i== 10*60*5)
709 CMessageBox::Show(NULL, IDS_ERR_PAEGENTTIMEOUT, IDS_APPNAME, MB_OK | MB_ICONERROR);
711 ::DeleteFile(tempfile);
712 return true;
714 bool CAppUtils::LaunchAlternativeEditor(const CString& filename, bool uac)
716 CString editTool = CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
717 if (editTool.IsEmpty() || (editTool.Left(1).Compare(_T("#"))==0)) {
718 editTool = CPathUtils::GetAppDirectory() + _T("notepad2.exe");
721 CString sCmd;
722 sCmd.Format(_T("\"%s\" \"%s\""), editTool, filename);
724 LaunchApplication(sCmd, NULL, false, NULL, uac);
725 return true;
727 bool CAppUtils::LaunchRemoteSetting()
729 CTGitPath path(g_Git.m_CurrentDir);
730 CSettings dlg(IDS_PROC_SETTINGS_TITLE, &path);
731 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
732 //dlg.SetTreeWidth(220);
733 dlg.m_DefaultPage = _T("gitremote");
735 dlg.DoModal();
736 dlg.HandleRestart();
737 return true;
740 * Launch the external blame viewer
742 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)
744 CString viewer = _T("\"") + CPathUtils::GetAppDirectory();
745 viewer += _T("TortoiseGitBlame.exe");
746 viewer += _T("\" \"") + sBlameFile + _T("\"");
747 //viewer += _T(" \"") + sLogFile + _T("\"");
748 //viewer += _T(" \"") + sOriginalFile + _T("\"");
749 if(!Rev.IsEmpty() && Rev != GIT_REV_ZERO)
750 viewer += CString(_T(" /rev:"))+Rev;
751 viewer += _T(" ")+sParams;
753 return LaunchApplication(viewer, IDS_ERR_TGITBLAME, false);
756 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)
758 CString sText;
759 if (pWnd == NULL)
760 return false;
761 bool bStyled = false;
762 pWnd->GetWindowText(sText);
763 // the rich edit control doesn't count the CR char!
764 // to be exact: CRLF is treated as one char.
765 sText.Remove('\r');
767 // style each line separately
768 int offset = 0;
769 int nNewlinePos;
772 nNewlinePos = sText.Find('\n', offset);
773 CString sLine = sText.Mid(offset);
774 if (nNewlinePos>=0)
775 sLine = sLine.Left(nNewlinePos-offset);
776 int start = 0;
777 int end = 0;
778 while (FindStyleChars(sLine, '*', start, end))
780 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
781 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
782 CHARFORMAT2 format;
783 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
784 format.cbSize = sizeof(CHARFORMAT2);
785 format.dwMask = CFM_BOLD;
786 format.dwEffects = CFE_BOLD;
787 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
788 bStyled = true;
789 start = end;
791 start = 0;
792 end = 0;
793 while (FindStyleChars(sLine, '^', start, end))
795 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
796 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
797 CHARFORMAT2 format;
798 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
799 format.cbSize = sizeof(CHARFORMAT2);
800 format.dwMask = CFM_ITALIC;
801 format.dwEffects = CFE_ITALIC;
802 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
803 bStyled = true;
804 start = end;
806 start = 0;
807 end = 0;
808 while (FindStyleChars(sLine, '_', start, end))
810 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
811 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
812 CHARFORMAT2 format;
813 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
814 format.cbSize = sizeof(CHARFORMAT2);
815 format.dwMask = CFM_UNDERLINE;
816 format.dwEffects = CFE_UNDERLINE;
817 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
818 bStyled = true;
819 start = end;
821 offset = nNewlinePos+1;
822 } while(nNewlinePos>=0);
823 return bStyled;
826 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)
828 int i=start;
829 bool bFoundMarker = false;
830 // find a starting marker
831 while (sText[i] != 0)
833 if (sText[i] == stylechar)
835 if (((i+1)<sText.GetLength())&&(IsCharAlphaNumeric(sText[i+1])) &&
836 (((i>0)&&(!IsCharAlphaNumeric(sText[i-1])))||(i==0)))
838 start = i+1;
839 i++;
840 bFoundMarker = true;
841 break;
844 i++;
846 if (!bFoundMarker)
847 return false;
848 // find ending marker
849 bFoundMarker = false;
850 while (sText[i] != 0)
852 if (sText[i] == stylechar)
854 if ((IsCharAlphaNumeric(sText[i-1])) &&
855 ((((i+1)<sText.GetLength())&&(!IsCharAlphaNumeric(sText[i+1])))||(i+1)==sText.GetLength()))
857 end = i;
858 i++;
859 bFoundMarker = true;
860 break;
863 i++;
865 return bFoundMarker;
868 CString CAppUtils::GetProjectNameFromURL(CString url)
870 CString name;
871 while (name.IsEmpty() || (name.CompareNoCase(_T("branches"))==0) ||
872 (name.CompareNoCase(_T("tags"))==0) ||
873 (name.CompareNoCase(_T("trunk"))==0))
875 name = url.Mid(url.ReverseFind('/')+1);
876 url = url.Left(url.ReverseFind('/'));
878 if ((name.Compare(_T("svn")) == 0)||(name.Compare(_T("svnroot")) == 0))
880 // a name of svn or svnroot indicates that it's not really the project name. In that
881 // case, we try the first part of the URL
882 // of course, this won't work in all cases (but it works for Google project hosting)
883 url.Replace(_T("http://"), _T(""));
884 url.Replace(_T("https://"), _T(""));
885 url.Replace(_T("svn://"), _T(""));
886 url.Replace(_T("svn+ssh://"), _T(""));
887 url.TrimLeft(_T("/"));
888 name = url.Left(url.Find('.'));
890 return name;
893 bool CAppUtils::StartShowUnifiedDiff(HWND /*hWnd*/, const CTGitPath& url1, const git_revnum_t& rev1,
894 const CTGitPath& /*url2*/, const git_revnum_t& rev2,
895 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
896 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */, bool /* blame = false */, bool bMerge)
899 CString tempfile=GetTempFile();
900 CString cmd;
901 if(rev2 == GitRev::GetWorkingCopy())
903 cmd.Format(_T("git.exe diff --stat -p %s "), rev1);
905 else if (rev1 == GitRev::GetWorkingCopy())
907 cmd.Format(_T("git.exe diff -R --stat -p %s "), rev2);
909 else
911 CString merge;
912 if(bMerge)
913 merge = _T("-c");
915 cmd.Format(_T("git.exe diff-tree -r -p %s --stat %s %s"),merge, rev1,rev2);
918 if( !url1.IsEmpty() )
920 cmd += _T(" -- \"");
921 cmd += url1.GetGitPathString();
922 cmd += _T("\" ");
924 g_Git.RunLogFile(cmd,tempfile);
925 CAppUtils::StartUnifiedDiffViewer(tempfile, rev1 + _T(":") + rev2);
928 #if 0
929 CString sCmd;
930 sCmd.Format(_T("%s /command:showcompare /unified"),
931 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));
932 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
933 if (rev1.IsValid())
934 sCmd += _T(" /revision1:") + rev1.ToString();
935 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
936 if (rev2.IsValid())
937 sCmd += _T(" /revision2:") + rev2.ToString();
938 if (peg.IsValid())
939 sCmd += _T(" /pegrevision:") + peg.ToString();
940 if (headpeg.IsValid())
941 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
943 if (bAlternateDiff)
944 sCmd += _T(" /alternatediff");
946 if (bIgnoreAncestry)
947 sCmd += _T(" /ignoreancestry");
949 if (hWnd)
951 sCmd += _T(" /hwnd:");
952 TCHAR buf[30];
953 _stprintf_s(buf, 30, _T("%d"), hWnd);
954 sCmd += buf;
957 return CAppUtils::LaunchApplication(sCmd, NULL, false);
958 #endif
959 return TRUE;
962 bool CAppUtils::SetupDiffScripts(bool force, const CString& type)
964 CString scriptsdir = CPathUtils::GetAppParentDirectory();
965 scriptsdir += _T("Diff-Scripts");
966 CSimpleFileFind files(scriptsdir);
967 while (files.FindNextFileNoDirectories())
969 CString file = files.GetFilePath();
970 CString filename = files.GetFileName();
971 CString ext = file.Mid(file.ReverseFind('-') + 1);
972 ext = _T(".") + ext.Left(ext.ReverseFind('.'));
973 std::set<CString> extensions;
974 extensions.insert(ext);
975 CString kind;
976 if (file.Right(3).CompareNoCase(_T("vbs"))==0)
978 kind = _T(" //E:vbscript");
980 if (file.Right(2).CompareNoCase(_T("js"))==0)
982 kind = _T(" //E:javascript");
984 // open the file, read the first line and find possible extensions
985 // this script can handle
988 CStdioFile f(file, CFile::modeRead | CFile::shareDenyNone);
989 CString extline;
990 if (f.ReadString(extline))
992 if ((extline.GetLength() > 15 ) &&
993 ((extline.Left(15).Compare(_T("// extensions: ")) == 0) ||
994 (extline.Left(14).Compare(_T("' extensions: ")) == 0)))
996 if (extline[0] == '/')
997 extline = extline.Mid(15);
998 else
999 extline = extline.Mid(14);
1000 CString sToken;
1001 int curPos = 0;
1002 sToken = extline.Tokenize(_T(";"), curPos);
1003 while (!sToken.IsEmpty())
1005 if (!sToken.IsEmpty())
1007 if (sToken[0] != '.')
1008 sToken = _T(".") + sToken;
1009 extensions.insert(sToken);
1011 sToken = extline.Tokenize(_T(";"), curPos);
1015 f.Close();
1017 catch (CFileException* e)
1019 e->Delete();
1022 for (std::set<CString>::const_iterator it = extensions.begin(); it != extensions.end(); ++it)
1024 if (type.IsEmpty() || (type.Compare(_T("Diff")) == 0))
1026 if (filename.Left(5).CompareNoCase(_T("diff-")) == 0)
1028 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + *it);
1029 CString diffregstring = diffreg;
1030 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
1031 diffreg = _T("wscript.exe \"") + file + _T("\" %base %mine") + kind;
1034 if (type.IsEmpty() || (type.Compare(_T("Merge"))==0))
1036 if (filename.Left(6).CompareNoCase(_T("merge-"))==0)
1038 CRegString diffreg = CRegString(_T("Software\\TortoiseGit\\MergeTools\\") + *it);
1039 CString diffregstring = diffreg;
1040 if (force || (diffregstring.IsEmpty()) || (diffregstring.Find(filename) >= 0))
1041 diffreg = _T("wscript.exe \"") + file + _T("\" %merged %theirs %mine %base") + kind;
1047 return true;
1050 bool CAppUtils::Export(CString *BashHash)
1052 // ask from where the export has to be done
1053 CExportDlg dlg;
1054 if(BashHash)
1055 dlg.m_Revision=*BashHash;
1057 if (dlg.DoModal() == IDOK)
1059 CString cmd;
1060 cmd.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s"),
1061 dlg.m_strExportDirectory, g_Git.FixBranchName(dlg.m_VersionName));
1063 CProgressDlg pro;
1064 pro.m_GitCmd=cmd;
1065 return (pro.DoModal() == IDOK);
1067 return false;
1070 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash, bool switch_new_brach)
1072 CCreateBranchTagDlg dlg;
1073 dlg.m_bIsTag=IsTag;
1074 dlg.m_bSwitch=switch_new_brach;
1076 if(CommitHash)
1077 dlg.m_initialRefName = *CommitHash;
1079 if(dlg.DoModal()==IDOK)
1081 CString cmd;
1082 CString force;
1083 CString track;
1084 if(dlg.m_bTrack == TRUE)
1085 track=_T(" --track ");
1086 else if(dlg.m_bTrack == FALSE)
1087 track=_T(" --no-track");
1089 if(dlg.m_bForce)
1090 force=_T(" -f ");
1092 if(IsTag)
1094 CString sign;
1095 if(dlg.m_bSign)
1096 sign=_T("-s");
1098 cmd.Format(_T("git.exe tag %s %s %s %s"),
1099 force,
1100 sign,
1101 dlg.m_BranchTagName,
1102 g_Git.FixBranchName(dlg.m_VersionName)
1105 CString tempfile=::GetTempFile();
1106 if(!dlg.m_Message.Trim().IsEmpty())
1108 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_Message);
1109 cmd += _T(" -F ")+tempfile;
1112 else
1114 cmd.Format(_T("git.exe branch %s %s %s %s"),
1115 track,
1116 force,
1117 dlg.m_BranchTagName,
1118 g_Git.FixBranchName(dlg.m_VersionName)
1121 CString out;
1122 if(g_Git.Run(cmd,&out,CP_UTF8))
1124 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1125 return FALSE;
1127 if( !IsTag && dlg.m_bSwitch )
1129 // it is a new branch and the user has requested to switch to it
1130 cmd.Format(_T("git.exe checkout %s"), dlg.m_BranchTagName);
1131 g_Git.Run(cmd,&out,CP_UTF8);
1132 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1135 return TRUE;
1137 return FALSE;
1140 bool CAppUtils::Switch(CString initialRefName, bool autoclose)
1142 CGitSwitchDlg dlg;
1143 if(!initialRefName.IsEmpty())
1144 dlg.m_initialRefName = initialRefName;
1146 if (dlg.DoModal() == IDOK)
1148 CString branch;
1149 if (dlg.m_bBranch)
1150 branch = dlg.m_NewBranch;
1152 // if refs/heads/ is not stripped, checkout will detach HEAD
1153 // checkout prefers branches on name clashes (with tags)
1154 if (dlg.m_VersionName.Left(11) ==_T("refs/heads/") && dlg.m_bBranchOverride != TRUE)
1155 dlg.m_VersionName = dlg.m_VersionName.Mid(11);
1157 return PerformSwitch(dlg.m_VersionName, dlg.m_bForce == TRUE , branch, dlg.m_bBranchOverride == TRUE, dlg.m_bTrack, autoclose);
1159 return FALSE;
1162 bool CAppUtils::PerformSwitch(CString ref, bool bForce /* false */, CString sNewBranch /* CString() */, bool bBranchOverride /* false */, BOOL bTrack /* 2 */, bool autoClose /* false */)
1164 CString cmd;
1165 CString track;
1166 CString force;
1167 CString branch;
1169 if(!sNewBranch.IsEmpty()){
1170 if (bBranchOverride)
1172 branch.Format(_T("-B %s"), sNewBranch);
1174 else
1176 branch.Format(_T("-b %s"), sNewBranch);
1178 if (bTrack == TRUE)
1179 track = _T("--track");
1180 else if (bTrack == FALSE)
1181 track = _T("--no-track");
1183 if (bForce)
1184 force = _T("-f");
1186 cmd.Format(_T("git.exe checkout %s %s %s %s"),
1187 force,
1188 track,
1189 branch,
1190 g_Git.FixBranchName(ref));
1192 CProgressDlg progress;
1193 progress.m_bAutoCloseOnSuccess = autoClose;
1194 progress.m_GitCmd = cmd;
1196 CTGitPath gitPath = g_Git.m_CurrentDir;
1197 if (gitPath.HasSubmodules())
1198 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1200 INT_PTR ret = progress.DoModal();
1201 if (gitPath.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
1203 CString sCmd;
1204 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1206 RunTortoiseProc(sCmd);
1207 return TRUE;
1209 else if (ret == IDOK)
1210 return TRUE;
1212 return FALSE;
1215 bool CAppUtils::OpenIgnoreFile(CStdioFile &file, const CString& filename)
1217 if (!file.Open(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate))
1219 CMessageBox::Show(NULL, filename + _T(" Open Failure"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1220 return false;
1223 if (file.GetLength() > 0)
1225 file.Seek(file.GetLength() - 1, 0);
1226 auto_buffer<TCHAR> buf(1);
1227 file.Read(buf, 1);
1228 file.SeekToEnd();
1229 if (buf[0] != _T('\n'))
1230 file.WriteString(_T("\n"));
1232 else
1233 file.SeekToEnd();
1235 return true;
1238 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1240 CIgnoreDlg ignoreDlg;
1241 if (ignoreDlg.DoModal() == IDOK)
1243 CString ignorefile;
1244 ignorefile = g_Git.m_CurrentDir + _T("\\");
1246 switch (ignoreDlg.m_IgnoreFile)
1248 case 0:
1249 ignorefile += _T(".gitignore");
1250 break;
1251 case 2:
1252 g_GitAdminDir.GetAdminDirPath(g_Git.m_CurrentDir, ignorefile);
1253 ignorefile += _T("info/exclude");
1254 break;
1257 CStdioFile file;
1260 if (ignoreDlg.m_IgnoreFile != 1 && !OpenIgnoreFile(file, ignorefile))
1261 return false;
1263 for (int i = 0; i < path.GetCount(); i++)
1265 if (ignoreDlg.m_IgnoreFile == 1)
1267 ignorefile = g_Git.m_CurrentDir + _T("\\") + path[i].GetContainingDirectory().GetWinPathString() + _T("\\.gitignore");
1268 if (!OpenIgnoreFile(file, ignorefile))
1269 return false;
1272 CString ignorePattern;
1273 if (ignoreDlg.m_IgnoreType == 0)
1275 if (ignoreDlg.m_IgnoreFile != 1 && !path[i].GetContainingDirectory().GetGitPathString().IsEmpty())
1276 ignorePattern += _T("/") + path[i].GetContainingDirectory().GetGitPathString();
1278 ignorePattern += _T("/");
1280 if (IsMask)
1282 ignorePattern += _T("*") + path[i].GetFileExtension();
1284 else
1286 ignorePattern += path[i].GetFileOrDirectoryName();
1289 // escape [ and ] so that files get ignored correctly
1290 ignorePattern.Replace(_T("["), _T("\\["));
1291 ignorePattern.Replace(_T("]"), _T("\\]"));
1293 ignorePattern += _T("\n");
1294 CStringA ignorePatternA = CUnicodeUtils::GetUTF8(ignorePattern);
1295 file.Write(ignorePatternA.GetBuffer(), ignorePatternA.GetLength());
1296 ignorePatternA.ReleaseBuffer();
1298 if (ignoreDlg.m_IgnoreFile == 1)
1299 file.Close();
1302 if (ignoreDlg.m_IgnoreFile != 1)
1303 file.Close();
1305 catch(...)
1307 file.Abort();
1308 return false;
1311 return true;
1313 return false;
1317 bool CAppUtils::GitReset(CString *CommitHash,int type)
1319 CResetDlg dlg;
1320 dlg.m_ResetType=type;
1321 dlg.m_ResetToVersion=*CommitHash;
1322 if (dlg.DoModal() == IDOK)
1324 CString cmd;
1325 CString type;
1326 switch(dlg.m_ResetType)
1328 case 0:
1329 type=_T("--soft");
1330 break;
1331 case 1:
1332 type=_T("--mixed");
1333 break;
1334 case 2:
1335 type=_T("--hard");
1336 break;
1337 default:
1338 type=_T("--mixed");
1339 break;
1341 cmd.Format(_T("git.exe reset %s %s"),type, *CommitHash);
1343 CProgressDlg progress;
1344 progress.m_GitCmd=cmd;
1346 CTGitPath gitPath = g_Git.m_CurrentDir;
1347 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2)
1348 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1350 INT_PTR ret = progress.DoModal();
1351 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2 && ret == IDC_PROGRESS_BUTTON1)
1353 CString sCmd;
1354 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1356 RunTortoiseProc(sCmd);
1357 return TRUE;
1359 else if (ret == IDOK)
1360 return TRUE;
1363 return FALSE;
1366 void CAppUtils::DescribeFile(bool mode, bool base,CString &descript)
1368 if(mode == FALSE)
1370 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE));
1371 return;
1373 if(base)
1375 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED));
1376 return;
1378 descript = CString(MAKEINTRESOURCE(IDS_PROC_CREATED));
1379 return;
1382 void CAppUtils::RemoveTempMergeFile(CTGitPath &path)
1384 CString tempmergefile;
1387 tempmergefile = CAppUtils::GetMergeTempFile(_T("LOCAL"),path);
1388 CFile::Remove(tempmergefile);
1389 }catch(...)
1395 tempmergefile = CAppUtils::GetMergeTempFile(_T("REMOTE"),path);
1396 CFile::Remove(tempmergefile);
1397 }catch(...)
1403 tempmergefile = CAppUtils::GetMergeTempFile(_T("BASE"),path);
1404 CFile::Remove(tempmergefile);
1405 }catch(...)
1409 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1411 CString file;
1412 file=g_Git.m_CurrentDir+_T("\\") + merge.GetWinPathString()+_T(".")+type+merge.GetFileExtension();
1414 return file;
1417 bool CAppUtils::ConflictEdit(CTGitPath &path,bool /*bAlternativeTool*/,bool revertTheirMy)
1419 bool bRet = false;
1421 CTGitPath merge=path;
1422 CTGitPath directory = merge.GetDirectory();
1424 // we have the conflicted file (%merged)
1425 // now look for the other required files
1426 //GitStatus stat;
1427 //stat.GetStatus(merge);
1428 //if (stat.status == NULL)
1429 // return false;
1431 BYTE_VECTOR vector;
1433 CString cmd;
1434 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1436 if (g_Git.Run(cmd, &vector))
1438 return FALSE;
1441 CTGitPathList list;
1442 list.ParserFromLsFile(vector);
1444 if(list.GetCount() == 0)
1445 return FALSE;
1447 CTGitPath theirs;
1448 CTGitPath mine;
1449 CTGitPath base;
1451 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"),merge));
1452 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"),merge));
1453 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1455 CString format;
1457 //format=_T("git.exe cat-file blob \":%d:%s\"");
1458 format = _T("git checkout-index --temp --stage=%d -- \"%s\"");
1459 CFile tempfile;
1460 //create a empty file, incase stage is not three
1461 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1462 tempfile.Close();
1463 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1464 tempfile.Close();
1465 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1466 tempfile.Close();
1468 bool b_base=false, b_local=false, b_remote=false;
1470 for(int i=0;i<list.GetCount();i++)
1472 CString cmd;
1473 CString outfile;
1474 cmd.Empty();
1475 outfile.Empty();
1477 if( list[i].m_Stage == 1)
1479 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1480 b_base = true;
1481 outfile = base.GetWinPathString();
1484 if( list[i].m_Stage == 2 )
1486 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1487 b_local = true;
1488 outfile = mine.GetWinPathString();
1491 if( list[i].m_Stage == 3 )
1493 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1494 b_remote = true;
1495 outfile = theirs.GetWinPathString();
1497 CString output, err;
1498 if(!outfile.IsEmpty())
1499 if (!g_Git.Run(cmd, &output, &err, CP_UTF8))
1501 CString file;
1502 int start =0 ;
1503 file = output.Tokenize(_T("\t"), start);
1504 ::MoveFileEx(file,outfile,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
1506 else
1508 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1512 if(b_local && b_remote )
1514 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1515 if( revertTheirMy )
1516 bRet = !!CAppUtils::StartExtMerge(base,mine, theirs, merge,_T("BASE"),_T("LOCAL"),_T("REMOTE"));
1517 else
1518 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge,_T("BASE"),_T("REMOTE"),_T("LOCAL"));
1521 else
1523 CFile::Remove(mine.GetWinPathString());
1524 CFile::Remove(theirs.GetWinPathString());
1525 CFile::Remove(base.GetWinPathString());
1527 CDeleteConflictDlg dlg;
1528 DescribeFile(b_local, b_base,dlg.m_LocalStatus);
1529 DescribeFile(b_remote,b_base,dlg.m_RemoteStatus);
1530 dlg.m_bShowModifiedButton=b_base;
1531 dlg.m_File=merge.GetGitPathString();
1532 if(dlg.DoModal() == IDOK)
1534 CString cmd,out;
1535 if(dlg.m_bIsDelete)
1537 cmd.Format(_T("git.exe rm -- \"%s\""),merge.GetGitPathString());
1539 else
1540 cmd.Format(_T("git.exe add -- \"%s\""),merge.GetGitPathString());
1542 if (g_Git.Run(cmd, &out, CP_UTF8))
1544 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1545 return FALSE;
1547 return TRUE;
1549 else
1550 return FALSE;
1553 #if 0
1554 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1555 base, theirs, mine, merge);
1556 #endif
1557 #if 0
1558 if (stat.status->text_status == svn_wc_status_conflicted)
1560 // we have a text conflict, use our merge tool to resolve the conflict
1562 CTSVNPath theirs(directory);
1563 CTSVNPath mine(directory);
1564 CTSVNPath base(directory);
1565 bool bConflictData = false;
1567 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1569 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1570 bConflictData = true;
1572 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1574 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1575 bConflictData = true;
1577 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1579 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1580 bConflictData = true;
1582 else
1584 mine = merge;
1586 if (bConflictData)
1587 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1588 base, theirs, mine, merge);
1591 if (stat.status->prop_status == svn_wc_status_conflicted)
1593 // we have a property conflict
1594 CTSVNPath prej(directory);
1595 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1597 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1598 // there's a problem: the prej file contains a _description_ of the conflict, and
1599 // that description string might be translated. That means we have no way of parsing
1600 // the file to find out the conflicting values.
1601 // The only thing we can do: show a dialog with the conflict description, then
1602 // let the user either accept the existing property or open the property edit dialog
1603 // to manually change the properties and values. And a button to mark the conflict as
1604 // resolved.
1605 CEditPropConflictDlg dlg;
1606 dlg.SetPrejFile(prej);
1607 dlg.SetConflictedItem(merge);
1608 bRet = (dlg.DoModal() != IDCANCEL);
1612 if (stat.status->tree_conflict)
1614 // we have a tree conflict
1615 SVNInfo info;
1616 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1617 if (pInfoData)
1619 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1621 CTSVNPath theirs(directory);
1622 CTSVNPath mine(directory);
1623 CTSVNPath base(directory);
1624 bool bConflictData = false;
1626 if (pInfoData->treeconflict_theirfile)
1628 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1629 bConflictData = true;
1631 if (pInfoData->treeconflict_basefile)
1633 base.AppendPathString(pInfoData->treeconflict_basefile);
1634 bConflictData = true;
1636 if (pInfoData->treeconflict_myfile)
1638 mine.AppendPathString(pInfoData->treeconflict_myfile);
1639 bConflictData = true;
1641 else
1643 mine = merge;
1645 if (bConflictData)
1646 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1647 base, theirs, mine, merge);
1649 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1651 CString sConflictAction;
1652 CString sConflictReason;
1653 CString sResolveTheirs;
1654 CString sResolveMine;
1655 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1656 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1658 if (pInfoData->treeconflict_nodekind == svn_node_file)
1660 switch (pInfoData->treeconflict_operation)
1662 case svn_wc_operation_update:
1663 switch (pInfoData->treeconflict_action)
1665 case svn_wc_conflict_action_edit:
1666 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1667 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1668 break;
1669 case svn_wc_conflict_action_add:
1670 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1671 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1672 break;
1673 case svn_wc_conflict_action_delete:
1674 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1675 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1676 break;
1678 break;
1679 case svn_wc_operation_switch:
1680 switch (pInfoData->treeconflict_action)
1682 case svn_wc_conflict_action_edit:
1683 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1684 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1685 break;
1686 case svn_wc_conflict_action_add:
1687 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1688 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1689 break;
1690 case svn_wc_conflict_action_delete:
1691 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1692 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1693 break;
1695 break;
1696 case svn_wc_operation_merge:
1697 switch (pInfoData->treeconflict_action)
1699 case svn_wc_conflict_action_edit:
1700 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1701 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1702 break;
1703 case svn_wc_conflict_action_add:
1704 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1705 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1706 break;
1707 case svn_wc_conflict_action_delete:
1708 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1709 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1710 break;
1712 break;
1715 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1717 switch (pInfoData->treeconflict_operation)
1719 case svn_wc_operation_update:
1720 switch (pInfoData->treeconflict_action)
1722 case svn_wc_conflict_action_edit:
1723 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1724 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1725 break;
1726 case svn_wc_conflict_action_add:
1727 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1728 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1729 break;
1730 case svn_wc_conflict_action_delete:
1731 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1732 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1733 break;
1735 break;
1736 case svn_wc_operation_switch:
1737 switch (pInfoData->treeconflict_action)
1739 case svn_wc_conflict_action_edit:
1740 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1741 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1742 break;
1743 case svn_wc_conflict_action_add:
1744 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1745 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1746 break;
1747 case svn_wc_conflict_action_delete:
1748 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1749 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1750 break;
1752 break;
1753 case svn_wc_operation_merge:
1754 switch (pInfoData->treeconflict_action)
1756 case svn_wc_conflict_action_edit:
1757 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1758 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1759 break;
1760 case svn_wc_conflict_action_add:
1761 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1762 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1763 break;
1764 case svn_wc_conflict_action_delete:
1765 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1766 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1767 break;
1769 break;
1773 UINT uReasonID = 0;
1774 switch (pInfoData->treeconflict_reason)
1776 case svn_wc_conflict_reason_edited:
1777 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1778 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1779 break;
1780 case svn_wc_conflict_reason_obstructed:
1781 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1782 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1783 break;
1784 case svn_wc_conflict_reason_deleted:
1785 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1786 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1787 break;
1788 case svn_wc_conflict_reason_added:
1789 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1790 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1791 break;
1792 case svn_wc_conflict_reason_missing:
1793 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1794 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1795 break;
1796 case svn_wc_conflict_reason_unversioned:
1797 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1798 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1799 break;
1801 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1803 CTreeConflictEditorDlg dlg;
1804 dlg.SetConflictInfoText(sConflictReason);
1805 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1806 dlg.SetPath(treeConflictPath);
1807 INT_PTR dlgRet = dlg.DoModal();
1808 bRet = (dlgRet != IDCANCEL);
1812 #endif
1813 return bRet;
1816 bool CAppUtils::IsSSHPutty()
1818 CString sshclient=g_Git.m_Environment.GetEnv(_T("GIT_SSH"));
1819 sshclient=sshclient.MakeLower();
1820 if(sshclient.Find(_T("plink.exe"),0)>=0)
1822 return true;
1824 return false;
1827 CString CAppUtils::GetClipboardLink()
1829 if (!OpenClipboard(NULL))
1830 return CString();
1832 CString sClipboardText;
1833 HGLOBAL hglb = GetClipboardData(CF_TEXT);
1834 if (hglb)
1836 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1837 sClipboardText = CString(lpstr);
1838 GlobalUnlock(hglb);
1840 hglb = GetClipboardData(CF_UNICODETEXT);
1841 if (hglb)
1843 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
1844 sClipboardText = lpstr;
1845 GlobalUnlock(hglb);
1847 CloseClipboard();
1849 if(!sClipboardText.IsEmpty())
1851 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
1852 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
1854 if(sClipboardText.Find( _T("http://")) == 0)
1855 return sClipboardText;
1857 if(sClipboardText.Find( _T("https://")) == 0)
1858 return sClipboardText;
1860 if(sClipboardText.Find( _T("git://")) == 0)
1861 return sClipboardText;
1863 if(sClipboardText.Find( _T("ssh://")) == 0)
1864 return sClipboardText;
1866 if(sClipboardText.GetLength()>=2)
1867 if( sClipboardText[1] == _T(':') )
1868 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
1869 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
1870 return sClipboardText;
1873 return CString(_T(""));
1876 CString CAppUtils::ChooseRepository(CString *path)
1878 CBrowseFolder browseFolder;
1879 CRegString regLastResopitory = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
1881 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
1882 CString strCloneDirectory;
1883 if(path)
1884 strCloneDirectory=*path;
1885 else
1887 strCloneDirectory = regLastResopitory;
1890 CString title;
1891 title.LoadString(IDS_CHOOSE_REPOSITORY);
1893 browseFolder.SetInfo(title);
1895 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
1897 regLastResopitory = strCloneDirectory;
1898 return strCloneDirectory;
1900 else
1902 return CString();
1906 bool CAppUtils::SendPatchMail(CTGitPathList &list,bool autoclose)
1908 CSendMailDlg dlg;
1910 dlg.m_PathList = list;
1912 if(dlg.DoModal()==IDOK)
1914 if(dlg.m_PathList.GetCount() == 0)
1915 return FALSE;
1917 CGitProgressDlg progDlg;
1919 theApp.m_pMainWnd = &progDlg;
1920 progDlg.SetCommand(CGitProgressDlg::GitProgress_SendMail);
1922 progDlg.SetAutoClose(autoclose);
1924 progDlg.SetPathList(dlg.m_PathList);
1925 //ProjectProperties props;
1926 //props.ReadPropsPathList(dlg.m_pathList);
1927 //progDlg.SetProjectProperties(props);
1928 progDlg.SetItemCount(dlg.m_PathList.GetCount());
1930 DWORD flags =0;
1931 if(dlg.m_bAttachment)
1932 flags |= SENDMAIL_ATTACHMENT;
1933 if(dlg.m_bCombine)
1934 flags |= SENDMAIL_COMBINED;
1935 if(dlg.m_bUseMAPI)
1936 flags |= SENDMAIL_MAPI;
1938 progDlg.SetSendMailOption(dlg.m_To,dlg.m_CC,dlg.m_Subject,flags);
1940 progDlg.DoModal();
1942 return true;
1944 return false;
1947 bool CAppUtils::SendPatchMail(CString &cmd,CString &formatpatchoutput,bool autoclose)
1949 CTGitPathList list;
1950 CString log=formatpatchoutput;
1951 int start=log.Find(cmd);
1952 if(start >=0)
1953 CString one=log.Tokenize(_T("\n"),start);
1954 else
1955 start = 0;
1957 while(start>=0)
1959 CString one=log.Tokenize(_T("\n"),start);
1960 one=one.Trim();
1961 if(one.IsEmpty() || one.Find(_T("Success")) == 0)
1962 continue;
1963 one.Replace(_T('/'),_T('\\'));
1964 CTGitPath path;
1965 path.SetFromWin(one);
1966 list.AddPath(path);
1968 if (list.GetCount() > 0)
1970 return SendPatchMail(list, autoclose);
1972 else
1974 CMessageBox::Show(NULL, IDS_ERR_NOPATCHES, IDS_APPNAME, MB_ICONINFORMATION);
1975 return true;
1980 int CAppUtils::GetLogOutputEncode(CGit *pGit)
1982 CString cmd,output;
1983 int start=0;
1985 output = pGit->GetConfigValue(_T("i18n.logOutputEncoding"));
1986 if(output.IsEmpty())
1988 output = pGit->GetConfigValue(_T("i18n.commitencoding"));
1989 if(output.IsEmpty())
1990 return CP_UTF8;
1992 int start=0;
1993 output=output.Tokenize(_T("\n"),start);
1994 return CUnicodeUtils::GetCPCode(output);
1997 else
1999 output=output.Tokenize(_T("\n"),start);
2000 return CUnicodeUtils::GetCPCode(output);
2003 int CAppUtils::GetCommitTemplate(CString &temp)
2005 CString cmd,output;
2007 output = g_Git.GetConfigValue(_T("commit.template"), CP_UTF8);
2008 if( output.IsEmpty() )
2009 return -1;
2011 if( output.GetLength()<1)
2012 return -1;
2014 if( output[0] == _T('/'))
2016 if(output.GetLength()>=3)
2017 if(output[2] == _T('/'))
2019 output.GetBuffer()[0] = output[1];
2020 output.GetBuffer()[1] = _T(':');
2024 int start=0;
2025 output=output.Tokenize(_T("\n"),start);
2027 output.Replace(_T('/'),_T('\\'));
2031 CStdioFile file(output,CFile::modeRead|CFile::typeText);
2032 CString str;
2033 while(file.ReadString(str))
2035 temp+=str+_T("\n");
2038 }catch(...)
2040 return -1;
2042 return 0;
2044 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
2046 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
2047 CString cmd,output;
2048 int cp=CP_UTF8;
2050 output= g_Git.GetConfigValue(_T("i18n.commitencoding"));
2051 if(output.IsEmpty())
2052 cp=CP_UTF8;
2054 int start=0;
2055 output=output.Tokenize(_T("\n"),start);
2056 cp=CUnicodeUtils::GetCPCode(output);
2058 int len=message.GetLength();
2060 char * buf;
2061 buf = new char[len*4 + 4];
2062 SecureZeroMemory(buf, (len*4 + 4));
2064 int lengthIncTerminator = WideCharToMultiByte(cp, 0, message, -1, buf, len*4, NULL, NULL);
2066 file.Write(buf,lengthIncTerminator-1);
2067 file.Close();
2068 delete buf;
2069 return 0;
2072 bool CAppUtils::Fetch(CString remoteName, bool allowRebase, bool autoClose)
2074 CPullFetchDlg dlg;
2075 dlg.m_PreSelectRemote = remoteName;
2076 dlg.m_bAllowRebase = allowRebase;
2077 dlg.m_IsPull=FALSE;
2079 if(dlg.DoModal()==IDOK)
2081 if(dlg.m_bAutoLoad)
2083 if (dlg.m_bAllRemotes)
2085 STRING_VECTOR list;
2086 g_Git.GetRemoteList(list);
2088 STRING_VECTOR::const_iterator it = list.begin();
2089 while (it != list.end())
2091 CString remote(*it);
2092 CAppUtils::LaunchPAgent(NULL, &remote);
2093 it++;
2096 else
2097 CAppUtils::LaunchPAgent(NULL, &dlg.m_RemoteURL);
2100 CString url;
2101 url=dlg.m_RemoteURL;
2102 CString cmd;
2103 CString arg;
2105 int ver = CAppUtils::GetMsysgitVersion();
2107 if(ver >= 0x01070203) //above 1.7.0.2
2108 arg = _T("--progress ");
2110 if (dlg.m_bPrune) {
2111 arg += _T("--prune ");
2114 if (dlg.m_bFetchTags == 1)
2116 arg += _T("--tags ");
2118 else if (dlg.m_bFetchTags == 0)
2120 arg += _T("--no-tags ");
2123 if (dlg.m_bAllRemotes)
2124 cmd.Format(_T("git.exe fetch --all -v %s"), arg);
2125 else
2126 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"), arg, url, dlg.m_RemoteBranchName);
2128 CProgressDlg progress;
2130 progress.m_bAutoCloseOnSuccess = autoClose;
2132 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
2134 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
2136 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2139 progress.m_GitCmd=cmd;
2140 INT_PTR userResponse = progress.DoModal();
2142 if (userResponse == IDC_PROGRESS_BUTTON1)
2144 CString cmd = _T("/command:log");
2145 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
2146 RunTortoiseProc(cmd);
2147 return TRUE;
2149 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 1) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
2151 while(1)
2153 CRebaseDlg dlg;
2154 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
2155 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
2156 INT_PTR response = dlg.DoModal();
2157 if(response == IDOK)
2159 return TRUE;
2161 if(response == IDC_REBASE_POST_BUTTON )
2163 CString cmd, out, err;
2164 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2165 g_Git.m_CurrentDir,
2166 g_Git.FixBranchName(dlg.m_Upstream),
2167 g_Git.FixBranchName(dlg.m_Branch));
2168 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
2170 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2171 return FALSE;
2174 CAppUtils::SendPatchMail(cmd,out);
2175 return TRUE;
2178 if(response == IDC_REBASE_POST_BUTTON +1 )
2179 continue;
2181 if(response == IDCANCEL)
2182 return FALSE;
2184 return TRUE;
2186 else if (userResponse != IDCANCEL)
2187 return TRUE;
2189 return FALSE;
2192 bool CAppUtils::Push(CString selectLocalBranch, bool autoClose)
2194 CPushDlg dlg;
2195 dlg.m_BranchSourceName = selectLocalBranch;
2196 CString error;
2197 DWORD exitcode = 0xFFFFFFFF;
2198 CTGitPathList list;
2199 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
2200 if (CHooks::Instance().PrePush(list,exitcode, error))
2202 if (exitcode)
2204 CString temp;
2205 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2206 //ReportError(temp);
2207 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2208 return false;
2212 if(dlg.DoModal()==IDOK)
2214 CString arg;
2216 if(dlg.m_bPack)
2217 arg += _T("--thin ");
2218 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
2219 arg += _T("--tags ");
2220 if(dlg.m_bForce)
2221 arg += _T("--force ");
2222 if (dlg.m_bSetUpstream)
2223 arg += _T("--set-upstream ");
2225 int ver = CAppUtils::GetMsysgitVersion();
2227 if(ver >= 0x01070203) //above 1.7.0.2
2228 arg += _T("--progress ");
2230 CProgressDlg progress;
2231 progress.m_bAutoCloseOnSuccess=autoClose;
2233 STRING_VECTOR remotesList;
2234 if (dlg.m_bPushAllRemotes)
2235 g_Git.GetRemoteList(remotesList);
2236 else
2237 remotesList.push_back(dlg.m_URL);
2239 for (unsigned int i = 0; i < remotesList.size(); i++)
2241 if (dlg.m_bAutoLoad)
2242 CAppUtils::LaunchPAgent(NULL, &remotesList[i]);
2244 CString cmd;
2245 if (dlg.m_bPushAllBranches)
2247 cmd.Format(_T("git.exe push --all %s \"%s\""),
2248 arg,
2249 remotesList[i]);
2251 else
2253 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2254 arg,
2255 remotesList[i],
2256 dlg.m_BranchSourceName);
2257 if (!dlg.m_BranchRemoteName.IsEmpty())
2259 cmd += _T(":") + dlg.m_BranchRemoteName;
2262 progress.m_GitCmdList.push_back(cmd);
2265 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2266 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2267 INT_PTR ret = progress.DoModal();
2269 if(!progress.m_GitStatus)
2271 if (CHooks::Instance().PostPush(list,exitcode, error))
2273 if (exitcode)
2275 CString temp;
2276 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2277 //ReportError(temp);
2278 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2279 return false;
2282 if(ret == IDC_PROGRESS_BUTTON1)
2284 RequestPull(dlg.m_BranchRemoteName);
2286 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2288 Push();
2290 return TRUE;
2294 return FALSE;
2297 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2299 CRequestPullDlg dlg;
2300 dlg.m_RepositoryURL = repositoryUrl;
2301 dlg.m_EndRevision = endrevision;
2302 if (dlg.DoModal()==IDOK)
2304 CString cmd;
2305 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2307 CString tempFileName = GetTempFile();
2308 if (g_Git.RunLogFile(cmd, tempFileName))
2310 CMessageBox::Show(NULL, IDS_ERR_PULLREUQESTFAILED, IDS_APPNAME, MB_OK);
2311 return false;
2313 CAppUtils::LaunchAlternativeEditor(tempFileName);
2315 return true;
2318 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2320 CString strDir(szPath);
2321 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2323 strDir.AppendChar(_T('\\'));
2325 std::vector<CString> vPath;
2326 CString strTemp;
2327 bool bSuccess = false;
2329 for (int i=0;i<strDir.GetLength();++i)
2331 if (strDir.GetAt(i) != _T('\\'))
2333 strTemp.AppendChar(strDir.GetAt(i));
2335 else
2337 vPath.push_back(strTemp);
2338 strTemp.AppendChar(_T('\\'));
2342 std::vector<CString>::const_iterator vIter;
2343 for (vIter = vPath.begin(); vIter != vPath.end(); vIter++)
2345 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2348 return bSuccess;
2351 void CAppUtils::RemoveTrailSlash(CString &path)
2353 if(path.IsEmpty())
2354 return ;
2356 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2358 path=path.Left(path.GetLength()-1);
2359 if(path.IsEmpty())
2360 return;
2364 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2365 CTGitPathList &pathList,
2366 CTGitPathList &selectedList,
2367 bool bSelectFilesForCommit,
2368 bool autoClose)
2370 bool bFailed = true;
2372 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2374 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2376 CTGitPath path(g_Git.m_CurrentDir);
2377 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2378 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2379 dlg.SetTreeWidth(220);
2380 dlg.m_DefaultPage = _T("gitconfig");
2382 dlg.DoModal();
2383 dlg.HandleRestart();
2386 else
2387 return false;
2390 while (bFailed)
2392 bFailed = false;
2393 CCommitDlg dlg;
2394 dlg.m_sBugID = bugid;
2396 dlg.m_bWholeProject = bWholeProject;
2398 dlg.m_sLogMessage = sLogMsg;
2399 dlg.m_pathList = pathList;
2400 dlg.m_checkedPathList = selectedList;
2401 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2402 dlg.m_bAutoClose = autoClose;
2403 if (dlg.DoModal() == IDOK)
2405 if (dlg.m_pathList.GetCount()==0)
2406 return false;
2407 // if the user hasn't changed the list of selected items
2408 // we don't use that list. Because if we would use the list
2409 // of pre-checked items, the dialog would show different
2410 // checked items on the next startup: it would only try
2411 // to check the parent folder (which might not even show)
2412 // instead, we simply use an empty list and let the
2413 // default checking do its job.
2414 if (!dlg.m_pathList.IsEqual(pathList))
2415 selectedList = dlg.m_pathList;
2416 pathList = dlg.m_updatedPathList;
2417 sLogMsg = dlg.m_sLogMessage;
2418 bSelectFilesForCommit = true;
2420 if( dlg.m_bPushAfterCommit )
2422 switch(dlg.m_PostCmd)
2424 case GIT_POST_CMD_DCOMMIT:
2425 CAppUtils::SVNDCommit();
2426 break;
2427 default:
2428 CAppUtils::Push();
2431 else if (dlg.m_bCreateTagAfterCommit)
2433 CAppUtils::CreateBranchTag(TRUE);
2436 // CGitProgressDlg progDlg;
2437 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2438 // if (parser.HasVal(_T("closeonend")))
2439 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2440 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2441 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2442 // progDlg.SetPathList(dlg.m_pathList);
2443 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2444 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2445 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2446 // progDlg.SetItemCount(dlg.m_itemsCount);
2447 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2448 // progDlg.DoModal();
2449 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2450 // err = (DWORD)progDlg.DidErrorsOccur();
2451 // bFailed = progDlg.DidErrorsOccur();
2452 // bRet = progDlg.DidErrorsOccur();
2453 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2454 // if (DWORD(bFailRepeat)==0)
2455 // bFailed = false; // do not repeat if the user chose not to in the settings.
2458 return true;
2462 BOOL CAppUtils::SVNDCommit()
2464 CSVNDCommitDlg dcommitdlg;
2465 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2466 if (gitSetting == _T("")) {
2467 if (dcommitdlg.DoModal() != IDOK)
2469 return false;
2471 else
2473 if (dcommitdlg.m_remember)
2475 if (dcommitdlg.m_rmdir)
2477 gitSetting = _T("true");
2479 else
2481 gitSetting = _T("false");
2483 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2485 CString msg;
2486 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2487 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2493 BOOL IsStash = false;
2494 if(!g_Git.CheckCleanWorkTree())
2496 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2498 CSysProgressDlg sysProgressDlg;
2499 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2500 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2501 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2502 sysProgressDlg.SetShowProgressBar(false);
2503 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2504 sysProgressDlg.ShowModeless((HWND)NULL, true);
2506 CString cmd,out;
2507 cmd=_T("git.exe stash");
2508 if (g_Git.Run(cmd, &out, CP_UTF8))
2510 if (sysProgressDlg.IsValid())
2511 sysProgressDlg.Stop();
2512 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2513 return false;
2515 if (sysProgressDlg.IsValid())
2516 sysProgressDlg.Stop();
2518 IsStash =true;
2520 else
2522 return false;
2526 CProgressDlg progress;
2527 if (dcommitdlg.m_rmdir)
2529 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2531 else
2533 progress.m_GitCmd=_T("git.exe svn dcommit");
2535 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2537 if( IsStash)
2539 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2541 CSysProgressDlg sysProgressDlg;
2542 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2543 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2544 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2545 sysProgressDlg.SetShowProgressBar(false);
2546 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2547 sysProgressDlg.ShowModeless((HWND)NULL, true);
2549 CString cmd,out;
2550 cmd=_T("git.exe stash pop");
2551 if (g_Git.Run(cmd, &out, CP_UTF8))
2553 if (sysProgressDlg.IsValid())
2554 sysProgressDlg.Stop();
2555 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2556 return false;
2558 if (sysProgressDlg.IsValid())
2559 sysProgressDlg.Stop();
2561 else
2563 return false;
2566 return TRUE;
2568 return FALSE;
2571 BOOL CAppUtils::Merge(CString *commit)
2573 CMergeDlg dlg;
2574 if(commit)
2575 dlg.m_initialRefName = *commit;
2577 if(dlg.DoModal()==IDOK)
2579 CString cmd;
2580 CString noff;
2581 CString squash;
2582 CString nocommit;
2583 CString msg;
2585 if(dlg.m_bNoFF)
2586 noff=_T("--no-ff");
2588 if(dlg.m_bSquash)
2589 squash=_T("--squash");
2591 if(dlg.m_bNoCommit)
2592 nocommit=_T("--no-commit");
2594 if(!dlg.m_strLogMesage.IsEmpty())
2596 msg += _T("-m \"")+dlg.m_strLogMesage+_T("\"");
2598 cmd.Format(_T("git.exe merge %s %s %s %s %s"),
2599 msg,
2600 noff,
2601 squash,
2602 nocommit,
2603 g_Git.FixBranchName(dlg.m_VersionName));
2605 CProgressDlg Prodlg;
2606 Prodlg.m_GitCmd = cmd;
2608 if (dlg.m_bNoCommit)
2609 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
2610 else if (dlg.m_bIsBranch)
2611 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
2613 INT_PTR ret = Prodlg.DoModal();
2615 if (ret == IDC_PROGRESS_BUTTON1)
2616 if (dlg.m_bNoCommit)
2618 CString sLogMsg;
2619 CTGitPathList pathList;
2620 CTGitPathList selectedList;
2621 return Commit(_T(""), TRUE, sLogMsg, pathList, selectedList, true);
2623 else if (dlg.m_bIsBranch)
2625 CString msg;
2626 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
2627 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
2629 CString cmd, out;
2630 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
2631 if (g_Git.Run(cmd, &out, CP_UTF8))
2632 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
2636 return !Prodlg.m_GitStatus;
2638 return false;
2641 void CAppUtils::EditNote(GitRev *rev)
2643 CInputDlg dlg;
2644 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
2645 dlg.m_sInputText = rev->m_Notes;
2646 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
2647 //dlg.m_pProjectProperties = &m_ProjectProperties;
2648 dlg.m_bUseLogWidth = true;
2649 if(dlg.DoModal() == IDOK)
2651 CString cmd,output;
2652 cmd=_T("notes add -f -F \"");
2654 CString tempfile=::GetTempFile();
2655 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
2656 cmd += tempfile;
2657 cmd += _T("\" ");
2658 cmd += rev->m_CommitHash.ToString();
2662 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
2664 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
2667 else
2669 rev->m_Notes = dlg.m_sInputText;
2671 }catch(...)
2673 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
2675 CFile::Remove(tempfile);
2680 int CAppUtils::GetMsysgitVersion(CString *versionstr)
2682 CString cmd;
2683 CString progressarg;
2684 CString version;
2686 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
2687 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
2689 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
2691 __int64 time=0;
2692 if(!g_Git.GetFileModifyTime(gitpath, &time) && !versionstr)
2694 if((DWORD)time == regTime)
2696 return regVersion;
2700 if(versionstr)
2701 version = *versionstr;
2702 else
2704 CString err;
2705 cmd = _T("git.exe --version");
2706 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
2708 CMessageBox::Show(NULL, _T("git have not installed (") + err + _T(")"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2709 return false;
2713 int start=0;
2714 int ver = 0;
2718 CString str=version.Tokenize(_T("."), start);
2719 int space = str.ReverseFind(_T(' '));
2720 str = str.Mid(space+1,start);
2721 ver = _ttol(str);
2722 ver <<=24;
2724 version = version.Mid(start);
2725 start = 0;
2727 str = version.Tokenize(_T("."), start);
2729 ver |= (_ttol(str) & 0xFF) << 16;
2731 str = version.Tokenize(_T("."), start);
2732 ver |= (_ttol(str) & 0xFF) << 8;
2734 str = version.Tokenize(_T("."), start);
2735 ver |= (_ttol(str) & 0xFF);
2737 catch(...)
2739 if (!ver)
2741 CMessageBox::Show(NULL, _T("Could not parse git.exe version number."), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2742 return false;
2746 regTime = time&0xFFFFFFFF;
2747 regVersion = ver;
2749 return ver;
2752 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
2754 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
2756 CAutoLibrary hShell = LoadLibrary(_T("Shell32.dll"));
2758 if (hShell.IsValid()) {
2759 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
2760 if (pfnSHGPSFW) {
2761 IPropertyStore *pps;
2762 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
2763 if (SUCCEEDED(hr)) {
2764 PROPVARIANT var;
2765 var.vt = VT_BOOL;
2766 var.boolVal = VARIANT_TRUE;
2767 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
2768 pps->Release();
2774 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
2776 ASSERT(dialogname.GetLength() < 70);
2777 ASSERT(urlorpath.GetLength() < MAX_PATH);
2778 WCHAR pathbuf[MAX_PATH] = {0};
2780 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
2782 wcscat_s(pathbuf, L" - ");
2783 wcscat_s(pathbuf, dialogname);
2784 wcscat_s(pathbuf, L" - ");
2785 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
2786 SetWindowText(hWnd, pathbuf);
2789 bool CAppUtils::BisectStart(CString lastGood, CString firstBad, bool autoClose)
2791 if (!g_Git.CheckCleanWorkTree())
2793 if (CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH, IDS_APPNAME, 1, IDI_QUESTION, IDS_STASHBUTTON, IDS_ABORTBUTTON) == 1)
2795 CSysProgressDlg sysProgressDlg;
2796 sysProgressDlg.SetTitle(CString(MAKEINTRESOURCE(IDS_APPNAME)));
2797 sysProgressDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PROC_STASHRUNNING)));
2798 sysProgressDlg.SetLine(2, CString(MAKEINTRESOURCE(IDS_PROGRESSWAIT)));
2799 sysProgressDlg.SetShowProgressBar(false);
2800 sysProgressDlg.SetCancelMsg(IDS_PROGRS_INFOFAILED);
2801 sysProgressDlg.ShowModeless((HWND)NULL, true);
2803 CString cmd, out;
2804 cmd = _T("git.exe stash");
2805 if (g_Git.Run(cmd, &out, CP_UTF8))
2807 if (sysProgressDlg.IsValid())
2808 sysProgressDlg.Stop();
2809 CMessageBox::Show(NULL, out, _T("TortoiseGit"), MB_OK);
2810 return false;
2812 if (sysProgressDlg.IsValid())
2813 sysProgressDlg.Stop();
2815 else
2816 return false;
2819 CBisectStartDlg bisectStartDlg;
2821 if (!lastGood.IsEmpty())
2822 bisectStartDlg.m_sLastGood = lastGood;
2823 if (!firstBad.IsEmpty())
2824 bisectStartDlg.m_sFirstBad = firstBad;
2826 if (bisectStartDlg.DoModal() == IDOK)
2828 CProgressDlg progress;
2829 theApp.m_pMainWnd = &progress;
2830 progress.m_bAutoCloseOnSuccess = autoClose;
2831 progress.m_GitCmdList.push_back(_T("git.exe bisect start"));
2832 progress.m_GitCmdList.push_back(_T("git.exe bisect good ") + bisectStartDlg.m_LastGoodRevision);
2833 progress.m_GitCmdList.push_back(_T("git.exe bisect bad ") + bisectStartDlg.m_FirstBadRevision);
2835 CTGitPath path(g_Git.m_CurrentDir);
2837 if (path.HasSubmodules())
2838 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
2840 INT_PTR ret = progress.DoModal();
2841 if (path.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
2843 CString sCmd;
2844 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
2846 CAppUtils::RunTortoiseProc(sCmd);
2847 return true;
2849 else if (ret == IDOK)
2850 return true;
2853 return false;