Refactored IgnoreFile method
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blob50f8ea6302a951462e5f6777c916affb00e77155
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 "FormatMessageWrapper.h"
62 #include "SmartHandle.h"
64 CAppUtils::CAppUtils(void)
68 CAppUtils::~CAppUtils(void)
72 bool CAppUtils::StashSave()
74 CStashSaveDlg dlg;
76 if (dlg.DoModal() == IDOK)
78 CString cmd, out;
79 cmd = _T("git.exe stash save");
81 if (dlg.m_bIncludeUntracked && CAppUtils::GetMsysgitVersion() >= 0x01070700)
82 cmd += _T(" --include-untracked");
84 if (!dlg.m_sMessage.IsEmpty())
86 CString message = dlg.m_sMessage;
87 message.Replace(_T("\""), _T("\"\""));
88 cmd += _T(" \"") + message + _T("\"");
91 if (g_Git.Run(cmd, &out, CP_UTF8))
93 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
95 else
97 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHSUCCESS)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
98 return true;
101 return false;
104 int CAppUtils::StashApply(CString ref, bool showChanges /* true */)
106 CString cmd,out;
107 cmd = _T("git.exe stash apply ");
108 if (ref.Find(_T("refs/")) == 0)
109 ref = ref.Mid(5);
110 if (ref.Find(_T("stash{")) == 0)
111 ref = _T("stash@") + ref.Mid(5);
112 cmd += ref;
114 int ret = g_Git.Run(cmd, &out, CP_UTF8);
115 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
116 if (ret && !(ret == 1 && hasConflicts))
118 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_STASHAPPLYFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
120 else
122 CString message;
123 message.LoadString(IDS_PROC_STASHAPPLYSUCCESS);
124 if (hasConflicts)
125 message.LoadString(IDS_PROC_STASHAPPLYFAILEDCONFLICTS);
126 if (showChanges)
128 if(CMessageBox::Show(NULL,message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES))
129 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
131 CChangedDlg dlg;
132 dlg.m_pathList.AddPath(CTGitPath());
133 dlg.DoModal();
135 return 0;
137 else
139 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
140 return 0;
143 return -1;
146 int CAppUtils::StashPop(bool showChanges /* true */)
148 CString cmd,out;
149 cmd=_T("git.exe stash pop ");
151 int ret = g_Git.Run(cmd, &out, CP_UTF8);
152 bool hasConflicts = (out.Find(_T("CONFLICT")) >= 0);
153 if (ret && !(ret == 1 && hasConflicts))
155 CMessageBox::Show(NULL,CString(MAKEINTRESOURCE(IDS_PROC_STASHPOPFAILED)) + _T("\n") + out, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
157 else
159 CString message;
160 message.LoadString(IDS_PROC_STASHPOPSUCCESS);
161 if (hasConflicts)
162 message.LoadString(IDS_PROC_STASHPOPFAILEDCONFLICTS);
163 if (showChanges)
165 if(CMessageBox::Show(NULL,CString(message + _T("\n") + CString(MAKEINTRESOURCE(IDS_SEECHANGES)))
166 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
168 CChangedDlg dlg;
169 dlg.m_pathList.AddPath(CTGitPath());
170 dlg.DoModal();
172 return 0;
174 else
176 CMessageBox::Show(NULL, message ,_T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
177 return 0;
180 return -1;
183 bool CAppUtils::GetMimeType(const CTGitPath& /*file*/, CString& /*mimetype*/)
185 #if 0
186 GitProperties props(file, GitRev::REV_WC, false);
187 for (int i = 0; i < props.GetCount(); ++i)
189 if (props.GetItemName(i).compare(_T("svn:mime-type"))==0)
191 mimetype = props.GetItemValue(i).c_str();
192 return true;
195 #endif
196 return false;
199 BOOL CAppUtils::StartExtMerge(
200 const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,
201 const CString& basename, const CString& theirname, const CString& yourname, const CString& mergedname, bool bReadOnly)
204 CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));
205 CString ext = mergedfile.GetFileExtension();
206 CString com = regCom;
207 bool bInternal = false;
209 CString mimetype;
210 if (ext != "")
212 // is there an extension specific merge tool?
213 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());
214 if (CString(mergetool) != "")
216 com = mergetool;
219 if (GetMimeType(yourfile, mimetype) || GetMimeType(theirfile, mimetype) || GetMimeType(basefile, mimetype))
221 // is there a mime type specific merge tool?
222 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + mimetype);
223 if (CString(mergetool) != "")
225 com = mergetool;
229 if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))
231 // use TortoiseMerge
232 bInternal = true;
233 CRegString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE);
234 com = tortoiseMergePath;
235 if (com.IsEmpty())
237 com = CPathUtils::GetAppDirectory();
238 com += _T("TortoiseMerge.exe");
240 com = _T("\"") + com + _T("\"");
241 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
242 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
244 // check if the params are set. If not, just add the files to the command line
245 if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))
247 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");
248 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");
249 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");
250 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");
252 if (basefile.IsEmpty())
254 com.Replace(_T("/base:%base"), _T(""));
255 com.Replace(_T("%base"), _T(""));
257 else
258 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));
259 if (theirfile.IsEmpty())
261 com.Replace(_T("/theirs:%theirs"), _T(""));
262 com.Replace(_T("%theirs"), _T(""));
264 else
265 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));
266 if (yourfile.IsEmpty())
268 com.Replace(_T("/mine:%mine"), _T(""));
269 com.Replace(_T("%mine"), _T(""));
271 else
272 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));
273 if (mergedfile.IsEmpty())
275 com.Replace(_T("/merged:%merged"), _T(""));
276 com.Replace(_T("%merged"), _T(""));
278 else
279 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));
280 if (basename.IsEmpty())
282 if (basefile.IsEmpty())
284 com.Replace(_T("/basename:%bname"), _T(""));
285 com.Replace(_T("%bname"), _T(""));
287 else
289 com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));
292 else
293 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));
294 if (theirname.IsEmpty())
296 if (theirfile.IsEmpty())
298 com.Replace(_T("/theirsname:%tname"), _T(""));
299 com.Replace(_T("%tname"), _T(""));
301 else
303 com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));
306 else
307 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));
308 if (yourname.IsEmpty())
310 if (yourfile.IsEmpty())
312 com.Replace(_T("/minename:%yname"), _T(""));
313 com.Replace(_T("%yname"), _T(""));
315 else
317 com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));
320 else
321 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));
322 if (mergedname.IsEmpty())
324 if (mergedfile.IsEmpty())
326 com.Replace(_T("/mergedname:%mname"), _T(""));
327 com.Replace(_T("%mname"), _T(""));
329 else
331 com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));
334 else
335 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));
337 if ((bReadOnly)&&(bInternal))
338 com += _T(" /readonly");
340 if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))
342 return FALSE;
345 return TRUE;
348 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)
350 CString viewer;
351 // use TortoiseMerge
352 viewer = CPathUtils::GetAppDirectory();
353 viewer += _T("TortoiseMerge.exe");
355 viewer = _T("\"") + viewer + _T("\"");
356 viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");
357 viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");
358 if (bReversed)
359 viewer += _T(" /reversedpatch");
360 if (!sOriginalDescription.IsEmpty())
361 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");
362 if (!sPatchedDescription.IsEmpty())
363 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");
364 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
366 return FALSE;
368 return TRUE;
371 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)
373 // Is there a mime type specific diff tool?
374 CString mimetype;
375 if (GetMimeType(file1, mimetype) || GetMimeType(file2, mimetype))
377 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + mimetype);
378 if (!difftool.IsEmpty())
379 return difftool;
382 // Is there an extension specific diff tool?
383 CString ext = file2.GetFileExtension().MakeLower();
384 if (!ext.IsEmpty())
386 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);
387 if (!difftool.IsEmpty())
388 return difftool;
389 // Maybe we should use TortoiseIDiff?
390 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
391 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
392 (ext == _T(".png")) || (ext == _T(".ico")) ||
393 (ext == _T(".dib")) || (ext == _T(".emf")))
395 return
396 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseIDiff.exe") + _T("\"") +
397 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname");
401 // Finally, pick a generic external diff tool
402 CString difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));
403 return difftool;
406 bool CAppUtils::StartExtDiff(
407 const CString& file1, const CString& file2,
408 const CString& sName1, const CString& sName2,
409 const DiffFlags& flags)
411 CString viewer;
413 CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);
414 if (!flags.bBlame || !(DWORD)blamediff)
416 viewer = PickDiffTool(file1, file2);
417 // If registry entry for a diff program is commented out, use TortoiseMerge.
418 bool bCommentedOut = viewer.Left(1) == _T("#");
419 if (flags.bAlternativeTool)
421 // Invert external vs. internal diff tool selection.
422 if (bCommentedOut)
423 viewer.Delete(0); // uncomment
424 else
425 viewer = "";
427 else if (bCommentedOut)
428 viewer = "";
431 bool bInternal = viewer.IsEmpty();
432 if (bInternal)
434 viewer =
435 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseMerge.exe") + _T("\"") +
436 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
437 if (flags.bBlame)
438 viewer += _T(" /blame");
440 // check if the params are set. If not, just add the files to the command line
441 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
443 viewer += _T(" \"")+file1+_T("\"");
444 viewer += _T(" \"")+file2+_T("\"");
446 if (viewer.Find(_T("%base")) >= 0)
448 viewer.Replace(_T("%base"), _T("\"")+file1+_T("\""));
450 if (viewer.Find(_T("%mine")) >= 0)
452 viewer.Replace(_T("%mine"), _T("\"")+file2+_T("\""));
455 if (sName1.IsEmpty())
456 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));
457 else
458 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
460 if (sName2.IsEmpty())
461 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));
462 else
463 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
465 if (flags.bReadOnly && bInternal)
466 viewer += _T(" /readonly");
468 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);
471 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)
473 CString viewer;
474 CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
475 viewer = v;
476 if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))
478 // use TortoiseUDiff
479 viewer = CPathUtils::GetAppDirectory();
480 viewer += _T("TortoiseUDiff.exe");
481 // enquote the path to TortoiseUDiff
482 viewer = _T("\"") + viewer + _T("\"");
483 // add the params
484 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");
487 if (viewer.Find(_T("%1"))>=0)
489 if (viewer.Find(_T("\"%1\"")) >= 0)
490 viewer.Replace(_T("%1"), patchfile);
491 else
492 viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));
494 else
495 viewer += _T(" \"") + patchfile + _T("\"");
496 if (viewer.Find(_T("%title")) >= 0)
498 viewer.Replace(_T("%title"), title);
501 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
503 return FALSE;
505 return TRUE;
508 BOOL CAppUtils::StartTextViewer(CString file)
510 CString viewer;
511 CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);
512 viewer = txt;
513 viewer = viewer + _T("\\Shell\\Open\\Command\\");
514 CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);
515 viewer = txtexe;
517 DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
518 TCHAR * buf = new TCHAR[len+1];
519 ExpandEnvironmentStrings(viewer, buf, len);
520 viewer = buf;
521 delete [] buf;
522 len = ExpandEnvironmentStrings(file, NULL, 0);
523 buf = new TCHAR[len+1];
524 ExpandEnvironmentStrings(file, buf, len);
525 file = buf;
526 delete [] buf;
527 file = _T("\"")+file+_T("\"");
528 if (viewer.IsEmpty())
530 viewer = _T("RUNDLL32 Shell32,OpenAs_RunDLL");
532 if (viewer.Find(_T("\"%1\"")) >= 0)
534 viewer.Replace(_T("\"%1\""), file);
536 else if (viewer.Find(_T("%1")) >= 0)
538 viewer.Replace(_T("%1"), file);
540 else
542 viewer += _T(" ");
543 viewer += file;
546 if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))
548 return FALSE;
550 return TRUE;
553 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)
555 DWORD length = 0;
556 CAutoFile hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
557 if (!hFile)
558 return TRUE;
559 length = ::GetFileSize(hFile, NULL);
560 if (length < 4)
561 return TRUE;
562 return FALSE;
566 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)
568 LOGFONT logFont;
569 HDC hScreenDC = ::GetDC(NULL);
570 logFont.lfHeight = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);
571 ::ReleaseDC(NULL, hScreenDC);
572 logFont.lfWidth = 0;
573 logFont.lfEscapement = 0;
574 logFont.lfOrientation = 0;
575 logFont.lfWeight = FW_NORMAL;
576 logFont.lfItalic = 0;
577 logFont.lfUnderline = 0;
578 logFont.lfStrikeOut = 0;
579 logFont.lfCharSet = DEFAULT_CHARSET;
580 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
581 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
582 logFont.lfQuality = DRAFT_QUALITY;
583 logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;
584 _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
585 VERIFY(fontToCreate.CreateFontIndirect(&logFont));
588 bool CAppUtils::LaunchPAgent(CString *keyfile,CString * pRemote)
590 CString key,remote;
591 CString cmd,out;
592 if( pRemote == NULL)
594 remote=_T("origin");
596 else
598 remote=*pRemote;
600 if(keyfile == NULL)
602 cmd.Format(_T("remote.%s.puttykeyfile"),remote);
603 key = g_Git.GetConfigValue(cmd);
604 int start=0;
605 key = key.Tokenize(_T("\n"),start);
607 else
608 key=*keyfile;
610 if(key.IsEmpty())
611 return false;
613 CString proc=CPathUtils::GetAppDirectory();
614 proc += _T("pageant.exe \"");
615 proc += key;
616 proc += _T("\"");
618 CString tempfile = GetTempFile();
619 ::DeleteFile(tempfile);
621 proc += _T(" -c \"");
622 proc += CPathUtils::GetAppDirectory();
623 proc += _T("touch.exe\"");
624 proc += _T(" \"");
625 proc += tempfile;
626 proc += _T("\"");
628 bool b = LaunchApplication(proc, IDS_ERR_PAGEANT, true, &CPathUtils::GetAppDirectory());
629 if(!b)
630 return b;
632 int i=0;
633 while(!::PathFileExists(tempfile))
635 Sleep(100);
636 i++;
637 if(i>10*60*5)
638 break; //timeout 5 minutes
641 if( i== 10*60*5)
643 CMessageBox::Show(NULL, IDS_ERR_PAEGENTTIMEOUT, IDS_APPNAME, MB_OK | MB_ICONERROR);
645 ::DeleteFile(tempfile);
646 return true;
648 bool CAppUtils::LaunchAlternativeEditor(const CString& filename)
650 CString editTool = CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
651 if (editTool.IsEmpty() || (editTool.Left(1).Compare(_T("#"))==0)) {
652 editTool = CPathUtils::GetAppDirectory() + _T("notepad2.exe");
655 CString sCmd;
656 sCmd.Format(_T("\"%s\" \"%s\""), editTool, filename);
658 LaunchApplication(sCmd, NULL, false);
659 return true;
661 bool CAppUtils::LaunchRemoteSetting()
663 CTGitPath path(g_Git.m_CurrentDir);
664 CSettings dlg(IDS_PROC_SETTINGS_TITLE, &path);
665 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
666 //dlg.SetTreeWidth(220);
667 dlg.m_DefaultPage = _T("gitremote");
669 dlg.DoModal();
670 dlg.HandleRestart();
671 return true;
674 * Launch the external blame viewer
676 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)
678 CString viewer = _T("\"") + CPathUtils::GetAppDirectory();
679 viewer += _T("TortoiseGitBlame.exe");
680 viewer += _T("\" \"") + sBlameFile + _T("\"");
681 //viewer += _T(" \"") + sLogFile + _T("\"");
682 //viewer += _T(" \"") + sOriginalFile + _T("\"");
683 if(!Rev.IsEmpty() && Rev != GIT_REV_ZERO)
684 viewer += CString(_T(" /rev:"))+Rev;
685 viewer += _T(" ")+sParams;
687 return LaunchApplication(viewer, IDS_ERR_TGITBLAME, false);
690 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)
692 CString sText;
693 if (pWnd == NULL)
694 return false;
695 bool bStyled = false;
696 pWnd->GetWindowText(sText);
697 // the rich edit control doesn't count the CR char!
698 // to be exact: CRLF is treated as one char.
699 sText.Remove('\r');
701 // style each line separately
702 int offset = 0;
703 int nNewlinePos;
706 nNewlinePos = sText.Find('\n', offset);
707 CString sLine = sText.Mid(offset);
708 if (nNewlinePos>=0)
709 sLine = sLine.Left(nNewlinePos-offset);
710 int start = 0;
711 int end = 0;
712 while (FindStyleChars(sLine, '*', start, end))
714 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
715 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
716 CHARFORMAT2 format;
717 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
718 format.cbSize = sizeof(CHARFORMAT2);
719 format.dwMask = CFM_BOLD;
720 format.dwEffects = CFE_BOLD;
721 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
722 bStyled = true;
723 start = end;
725 start = 0;
726 end = 0;
727 while (FindStyleChars(sLine, '^', start, end))
729 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
730 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
731 CHARFORMAT2 format;
732 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
733 format.cbSize = sizeof(CHARFORMAT2);
734 format.dwMask = CFM_ITALIC;
735 format.dwEffects = CFE_ITALIC;
736 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
737 bStyled = true;
738 start = end;
740 start = 0;
741 end = 0;
742 while (FindStyleChars(sLine, '_', start, end))
744 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
745 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
746 CHARFORMAT2 format;
747 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
748 format.cbSize = sizeof(CHARFORMAT2);
749 format.dwMask = CFM_UNDERLINE;
750 format.dwEffects = CFE_UNDERLINE;
751 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
752 bStyled = true;
753 start = end;
755 offset = nNewlinePos+1;
756 } while(nNewlinePos>=0);
757 return bStyled;
760 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)
762 int i=start;
763 bool bFoundMarker = false;
764 // find a starting marker
765 while (sText[i] != 0)
767 if (sText[i] == stylechar)
769 if (((i+1)<sText.GetLength())&&(IsCharAlphaNumeric(sText[i+1])) &&
770 (((i>0)&&(!IsCharAlphaNumeric(sText[i-1])))||(i==0)))
772 start = i+1;
773 i++;
774 bFoundMarker = true;
775 break;
778 i++;
780 if (!bFoundMarker)
781 return false;
782 // find ending marker
783 bFoundMarker = false;
784 while (sText[i] != 0)
786 if (sText[i] == stylechar)
788 if ((IsCharAlphaNumeric(sText[i-1])) &&
789 ((((i+1)<sText.GetLength())&&(!IsCharAlphaNumeric(sText[i+1])))||(i+1)==sText.GetLength()))
791 end = i;
792 i++;
793 bFoundMarker = true;
794 break;
797 i++;
799 return bFoundMarker;
802 CString CAppUtils::GetProjectNameFromURL(CString url)
804 CString name;
805 while (name.IsEmpty() || (name.CompareNoCase(_T("branches"))==0) ||
806 (name.CompareNoCase(_T("tags"))==0) ||
807 (name.CompareNoCase(_T("trunk"))==0))
809 name = url.Mid(url.ReverseFind('/')+1);
810 url = url.Left(url.ReverseFind('/'));
812 if ((name.Compare(_T("svn")) == 0)||(name.Compare(_T("svnroot")) == 0))
814 // a name of svn or svnroot indicates that it's not really the project name. In that
815 // case, we try the first part of the URL
816 // of course, this won't work in all cases (but it works for Google project hosting)
817 url.Replace(_T("http://"), _T(""));
818 url.Replace(_T("https://"), _T(""));
819 url.Replace(_T("svn://"), _T(""));
820 url.Replace(_T("svn+ssh://"), _T(""));
821 url.TrimLeft(_T("/"));
822 name = url.Left(url.Find('.'));
824 return name;
827 bool CAppUtils::StartShowUnifiedDiff(HWND /*hWnd*/, const CTGitPath& url1, const git_revnum_t& rev1,
828 const CTGitPath& /*url2*/, const git_revnum_t& rev2,
829 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
830 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */, bool /* blame = false */, bool bMerge)
833 CString tempfile=GetTempFile();
834 CString cmd;
835 if(rev2 == GitRev::GetWorkingCopy())
837 cmd.Format(_T("git.exe diff --stat -p %s "), rev1);
839 else
841 CString merge;
842 if(bMerge)
843 merge = _T("-c");
845 cmd.Format(_T("git.exe diff-tree -r -p %s --stat %s %s"),merge, rev1,rev2);
848 if( !url1.IsEmpty() )
850 cmd += _T(" -- \"");
851 cmd += url1.GetGitPathString();
852 cmd += _T("\" ");
854 g_Git.RunLogFile(cmd,tempfile);
855 CAppUtils::StartUnifiedDiffViewer(tempfile, rev1 + _T(":") + rev2);
858 #if 0
859 CString sCmd;
860 sCmd.Format(_T("%s /command:showcompare /unified"),
861 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));
862 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
863 if (rev1.IsValid())
864 sCmd += _T(" /revision1:") + rev1.ToString();
865 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
866 if (rev2.IsValid())
867 sCmd += _T(" /revision2:") + rev2.ToString();
868 if (peg.IsValid())
869 sCmd += _T(" /pegrevision:") + peg.ToString();
870 if (headpeg.IsValid())
871 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
873 if (bAlternateDiff)
874 sCmd += _T(" /alternatediff");
876 if (bIgnoreAncestry)
877 sCmd += _T(" /ignoreancestry");
879 if (hWnd)
881 sCmd += _T(" /hwnd:");
882 TCHAR buf[30];
883 _stprintf_s(buf, 30, _T("%d"), hWnd);
884 sCmd += buf;
887 return CAppUtils::LaunchApplication(sCmd, NULL, false);
888 #endif
889 return TRUE;
893 bool CAppUtils::Export(CString *BashHash)
895 bool bRet = false;
897 // ask from where the export has to be done
898 CExportDlg dlg;
899 if(BashHash)
900 dlg.m_Revision=*BashHash;
902 if (dlg.DoModal() == IDOK)
904 CString cmd;
905 cmd.Format(_T("git.exe archive --output=\"%s\" --format=zip --verbose %s"),
906 dlg.m_strExportDirectory, g_Git.FixBranchName(dlg.m_VersionName));
908 CProgressDlg pro;
909 pro.m_GitCmd=cmd;
910 pro.DoModal();
911 return TRUE;
913 return bRet;
916 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash, bool switch_new_brach)
918 CCreateBranchTagDlg dlg;
919 dlg.m_bIsTag=IsTag;
920 dlg.m_bSwitch=switch_new_brach;
922 if(CommitHash)
923 dlg.m_Base = *CommitHash;
925 if(dlg.DoModal()==IDOK)
927 CString cmd;
928 CString force;
929 CString track;
930 if(dlg.m_bTrack == TRUE)
931 track=_T(" --track ");
932 else if(dlg.m_bTrack == FALSE)
933 track=_T(" --no-track");
935 if(dlg.m_bForce)
936 force=_T(" -f ");
938 if(IsTag)
940 CString sign;
941 if(dlg.m_bSign)
942 sign=_T("-s");
944 cmd.Format(_T("git.exe tag %s %s %s %s"),
945 force,
946 sign,
947 dlg.m_BranchTagName,
948 g_Git.FixBranchName(dlg.m_VersionName)
951 CString tempfile=::GetTempFile();
952 if(!dlg.m_Message.Trim().IsEmpty())
954 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_Message);
955 cmd += _T(" -F ")+tempfile;
958 else
960 cmd.Format(_T("git.exe branch %s %s %s %s"),
961 track,
962 force,
963 dlg.m_BranchTagName,
964 g_Git.FixBranchName(dlg.m_VersionName)
967 CString out;
968 if(g_Git.Run(cmd,&out,CP_UTF8))
970 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
972 if( !IsTag && dlg.m_bSwitch )
974 // it is a new branch and the user has requested to switch to it
975 cmd.Format(_T("git.exe checkout %s"), dlg.m_BranchTagName);
976 g_Git.Run(cmd,&out,CP_UTF8);
977 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
980 return TRUE;
982 return FALSE;
985 bool CAppUtils::Switch(CString *CommitHash, CString initialRefName, bool autoclose)
987 CGitSwitchDlg dlg;
988 if(CommitHash)
989 dlg.m_Base=*CommitHash;
990 if(!initialRefName.IsEmpty())
991 dlg.m_initialRefName = initialRefName;
993 if (dlg.DoModal() == IDOK)
995 CString branch;
996 if (dlg.m_bBranch)
997 branch = dlg.m_NewBranch;
999 return PerformSwitch(dlg.m_VersionName, dlg.m_bForce == TRUE , branch, dlg.m_bBranchOverride == TRUE, dlg.m_bTrack == TRUE, autoclose);
1001 return FALSE;
1004 bool CAppUtils::PerformSwitch(CString ref, bool bForce /* false */, CString sNewBranch /* CString() */, bool bBranchOverride /* false */, bool bTrack /* false */, bool autoClose /* false */)
1006 CString cmd;
1007 CString track;
1008 CString force;
1009 CString branch;
1011 if(!sNewBranch.IsEmpty()){
1012 if (bBranchOverride)
1014 branch.Format(_T("-B %s"), sNewBranch);
1016 else
1018 branch.Format(_T("-b %s"), sNewBranch);
1020 if (bTrack)
1021 track = _T("--track");
1023 if (bForce)
1024 force = _T("-f");
1026 cmd.Format(_T("git.exe checkout %s %s %s %s"),
1027 force,
1028 track,
1029 branch,
1030 g_Git.FixBranchName(ref));
1032 CProgressDlg progress;
1033 progress.m_bAutoCloseOnSuccess = autoClose;
1034 progress.m_GitCmd = cmd;
1036 CTGitPath gitPath = g_Git.m_CurrentDir;
1037 if (gitPath.HasSubmodules())
1038 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1040 int ret = progress.DoModal();
1041 if (gitPath.HasSubmodules() && ret == IDC_PROGRESS_BUTTON1)
1043 CString sCmd;
1044 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1046 RunTortoiseProc(sCmd);
1047 return TRUE;
1049 else if (ret == IDOK)
1050 return TRUE;
1052 return FALSE;
1055 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1057 CString ignorefile;
1058 ignorefile=g_Git.m_CurrentDir+_T("\\");
1060 if(IsMask)
1062 ignorefile+=path.GetCommonRoot().GetDirectory().GetWinPathString()+_T("\\.gitignore");
1065 else
1067 ignorefile += _T("\\.gitignore");
1070 CStdioFile file;
1071 if(!file.Open(ignorefile,CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate))
1073 CMessageBox::Show(NULL,ignorefile+_T(" Open Failure"),_T("TortoiseGit"),MB_OK);
1074 return FALSE;
1079 file.SeekToEnd();
1080 for(int i=0;i<path.GetCount();i++)
1082 CString ignorePattern;
1083 if(IsMask)
1085 ignorePattern += _T("*") + path[i].GetFileExtension();
1087 else
1089 ignorePattern += _T("/") + path[i].GetGitPathString();
1091 file.WriteString(_T("\n") + ignorePattern);
1094 file.Close();
1095 }catch(...)
1097 file.Close();
1098 return FALSE;
1101 return TRUE;
1105 bool CAppUtils::GitReset(CString *CommitHash,int type)
1107 CResetDlg dlg;
1108 dlg.m_ResetType=type;
1109 dlg.m_ResetToVersion=*CommitHash;
1110 if (dlg.DoModal() == IDOK)
1112 CString cmd;
1113 CString type;
1114 switch(dlg.m_ResetType)
1116 case 0:
1117 type=_T("--soft");
1118 break;
1119 case 1:
1120 type=_T("--mixed");
1121 break;
1122 case 2:
1123 type=_T("--hard");
1124 break;
1125 default:
1126 type=_T("--mixed");
1127 break;
1129 cmd.Format(_T("git.exe reset %s %s"),type, *CommitHash);
1131 CProgressDlg progress;
1132 progress.m_GitCmd=cmd;
1134 CTGitPath gitPath = g_Git.m_CurrentDir;
1135 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2)
1136 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_SUBMODULESUPDATE)));
1138 int ret = progress.DoModal();
1139 if (gitPath.HasSubmodules() && dlg.m_ResetType == 2 && ret == IDC_PROGRESS_BUTTON1)
1141 CString sCmd;
1142 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
1144 RunTortoiseProc(sCmd);
1145 return TRUE;
1147 else if (ret == IDOK)
1148 return TRUE;
1151 return FALSE;
1154 void CAppUtils::DescribeFile(bool mode, bool base,CString &descript)
1156 if(mode == FALSE)
1158 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_DELETE));
1159 return;
1161 if(base)
1163 descript = CString(MAKEINTRESOURCE(IDS_SVNACTION_MODIFIED));
1164 return;
1166 descript = CString(MAKEINTRESOURCE(IDS_PROC_CREATED));
1167 return;
1170 void CAppUtils::RemoveTempMergeFile(CTGitPath &path)
1172 CString tempmergefile;
1175 tempmergefile = CAppUtils::GetMergeTempFile(_T("LOCAL"),path);
1176 CFile::Remove(tempmergefile);
1177 }catch(...)
1183 tempmergefile = CAppUtils::GetMergeTempFile(_T("REMOTE"),path);
1184 CFile::Remove(tempmergefile);
1185 }catch(...)
1191 tempmergefile = CAppUtils::GetMergeTempFile(_T("BASE"),path);
1192 CFile::Remove(tempmergefile);
1193 }catch(...)
1197 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1199 CString file;
1200 file=g_Git.m_CurrentDir+_T("\\") + merge.GetWinPathString()+_T(".")+type+merge.GetFileExtension();
1202 return file;
1205 bool CAppUtils::ConflictEdit(CTGitPath &path,bool /*bAlternativeTool*/,bool revertTheirMy)
1207 bool bRet = false;
1209 CTGitPath merge=path;
1210 CTGitPath directory = merge.GetDirectory();
1212 // we have the conflicted file (%merged)
1213 // now look for the other required files
1214 //GitStatus stat;
1215 //stat.GetStatus(merge);
1216 //if (stat.status == NULL)
1217 // return false;
1219 BYTE_VECTOR vector;
1221 CString cmd;
1222 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1224 if (g_Git.Run(cmd, &vector))
1226 return FALSE;
1229 CTGitPathList list;
1230 list.ParserFromLsFile(vector);
1232 if(list.GetCount() == 0)
1233 return FALSE;
1235 CTGitPath theirs;
1236 CTGitPath mine;
1237 CTGitPath base;
1239 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"),merge));
1240 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"),merge));
1241 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1243 CString format;
1245 //format=_T("git.exe cat-file blob \":%d:%s\"");
1246 format = _T("git checkout-index --temp --stage=%d -- \"%s\"");
1247 CFile tempfile;
1248 //create a empty file, incase stage is not three
1249 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1250 tempfile.Close();
1251 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1252 tempfile.Close();
1253 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1254 tempfile.Close();
1256 bool b_base=false, b_local=false, b_remote=false;
1258 for(int i=0;i<list.GetCount();i++)
1260 CString cmd;
1261 CString outfile;
1262 cmd.Empty();
1263 outfile.Empty();
1265 if( list[i].m_Stage == 1)
1267 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1268 b_base = true;
1269 outfile = base.GetWinPathString();
1272 if( list[i].m_Stage == 2 )
1274 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1275 b_local = true;
1276 outfile = mine.GetWinPathString();
1279 if( list[i].m_Stage == 3 )
1281 cmd.Format(format, list[i].m_Stage, list[i].GetGitPathString());
1282 b_remote = true;
1283 outfile = theirs.GetWinPathString();
1285 CString output, err;
1286 if(!outfile.IsEmpty())
1287 if (!g_Git.Run(cmd, &output, &err, CP_UTF8))
1289 CString file;
1290 int start =0 ;
1291 file = output.Tokenize(_T("\t"), start);
1292 ::MoveFileEx(file,outfile,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
1294 else
1296 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1300 if(b_local && b_remote )
1302 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1303 if( revertTheirMy )
1304 bRet = !!CAppUtils::StartExtMerge(base,mine, theirs, merge,_T("BASE"),_T("LOCAL"),_T("REMOTE"));
1305 else
1306 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge,_T("BASE"),_T("REMOTE"),_T("LOCAL"));
1309 else
1311 CFile::Remove(mine.GetWinPathString());
1312 CFile::Remove(theirs.GetWinPathString());
1313 CFile::Remove(base.GetWinPathString());
1315 CDeleteConflictDlg dlg;
1316 DescribeFile(b_local, b_base,dlg.m_LocalStatus);
1317 DescribeFile(b_remote,b_base,dlg.m_RemoteStatus);
1318 dlg.m_bShowModifiedButton=b_base;
1319 dlg.m_File=merge.GetGitPathString();
1320 if(dlg.DoModal() == IDOK)
1322 CString cmd,out;
1323 if(dlg.m_bIsDelete)
1325 cmd.Format(_T("git.exe rm -- \"%s\""),merge.GetGitPathString());
1327 else
1328 cmd.Format(_T("git.exe add -- \"%s\""),merge.GetGitPathString());
1330 if (g_Git.Run(cmd, &out, CP_UTF8))
1332 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1333 return FALSE;
1335 return TRUE;
1337 else
1338 return FALSE;
1341 #if 0
1342 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1343 base, theirs, mine, merge);
1344 #endif
1345 #if 0
1346 if (stat.status->text_status == svn_wc_status_conflicted)
1348 // we have a text conflict, use our merge tool to resolve the conflict
1350 CTSVNPath theirs(directory);
1351 CTSVNPath mine(directory);
1352 CTSVNPath base(directory);
1353 bool bConflictData = false;
1355 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1357 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1358 bConflictData = true;
1360 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1362 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1363 bConflictData = true;
1365 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1367 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1368 bConflictData = true;
1370 else
1372 mine = merge;
1374 if (bConflictData)
1375 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1376 base, theirs, mine, merge);
1379 if (stat.status->prop_status == svn_wc_status_conflicted)
1381 // we have a property conflict
1382 CTSVNPath prej(directory);
1383 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1385 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1386 // there's a problem: the prej file contains a _description_ of the conflict, and
1387 // that description string might be translated. That means we have no way of parsing
1388 // the file to find out the conflicting values.
1389 // The only thing we can do: show a dialog with the conflict description, then
1390 // let the user either accept the existing property or open the property edit dialog
1391 // to manually change the properties and values. And a button to mark the conflict as
1392 // resolved.
1393 CEditPropConflictDlg dlg;
1394 dlg.SetPrejFile(prej);
1395 dlg.SetConflictedItem(merge);
1396 bRet = (dlg.DoModal() != IDCANCEL);
1400 if (stat.status->tree_conflict)
1402 // we have a tree conflict
1403 SVNInfo info;
1404 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1405 if (pInfoData)
1407 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1409 CTSVNPath theirs(directory);
1410 CTSVNPath mine(directory);
1411 CTSVNPath base(directory);
1412 bool bConflictData = false;
1414 if (pInfoData->treeconflict_theirfile)
1416 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1417 bConflictData = true;
1419 if (pInfoData->treeconflict_basefile)
1421 base.AppendPathString(pInfoData->treeconflict_basefile);
1422 bConflictData = true;
1424 if (pInfoData->treeconflict_myfile)
1426 mine.AppendPathString(pInfoData->treeconflict_myfile);
1427 bConflictData = true;
1429 else
1431 mine = merge;
1433 if (bConflictData)
1434 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1435 base, theirs, mine, merge);
1437 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1439 CString sConflictAction;
1440 CString sConflictReason;
1441 CString sResolveTheirs;
1442 CString sResolveMine;
1443 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1444 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1446 if (pInfoData->treeconflict_nodekind == svn_node_file)
1448 switch (pInfoData->treeconflict_operation)
1450 case svn_wc_operation_update:
1451 switch (pInfoData->treeconflict_action)
1453 case svn_wc_conflict_action_edit:
1454 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1455 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1456 break;
1457 case svn_wc_conflict_action_add:
1458 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1459 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1460 break;
1461 case svn_wc_conflict_action_delete:
1462 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1463 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1464 break;
1466 break;
1467 case svn_wc_operation_switch:
1468 switch (pInfoData->treeconflict_action)
1470 case svn_wc_conflict_action_edit:
1471 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1472 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1473 break;
1474 case svn_wc_conflict_action_add:
1475 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1476 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1477 break;
1478 case svn_wc_conflict_action_delete:
1479 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1480 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1481 break;
1483 break;
1484 case svn_wc_operation_merge:
1485 switch (pInfoData->treeconflict_action)
1487 case svn_wc_conflict_action_edit:
1488 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1489 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1490 break;
1491 case svn_wc_conflict_action_add:
1492 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1493 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1494 break;
1495 case svn_wc_conflict_action_delete:
1496 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1497 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1498 break;
1500 break;
1503 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1505 switch (pInfoData->treeconflict_operation)
1507 case svn_wc_operation_update:
1508 switch (pInfoData->treeconflict_action)
1510 case svn_wc_conflict_action_edit:
1511 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1512 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1513 break;
1514 case svn_wc_conflict_action_add:
1515 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1516 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1517 break;
1518 case svn_wc_conflict_action_delete:
1519 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1520 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1521 break;
1523 break;
1524 case svn_wc_operation_switch:
1525 switch (pInfoData->treeconflict_action)
1527 case svn_wc_conflict_action_edit:
1528 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1529 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1530 break;
1531 case svn_wc_conflict_action_add:
1532 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1533 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1534 break;
1535 case svn_wc_conflict_action_delete:
1536 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1537 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1538 break;
1540 break;
1541 case svn_wc_operation_merge:
1542 switch (pInfoData->treeconflict_action)
1544 case svn_wc_conflict_action_edit:
1545 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1546 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1547 break;
1548 case svn_wc_conflict_action_add:
1549 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1550 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1551 break;
1552 case svn_wc_conflict_action_delete:
1553 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1554 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1555 break;
1557 break;
1561 UINT uReasonID = 0;
1562 switch (pInfoData->treeconflict_reason)
1564 case svn_wc_conflict_reason_edited:
1565 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1566 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1567 break;
1568 case svn_wc_conflict_reason_obstructed:
1569 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1570 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1571 break;
1572 case svn_wc_conflict_reason_deleted:
1573 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1574 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1575 break;
1576 case svn_wc_conflict_reason_added:
1577 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1578 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1579 break;
1580 case svn_wc_conflict_reason_missing:
1581 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1582 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1583 break;
1584 case svn_wc_conflict_reason_unversioned:
1585 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1586 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1587 break;
1589 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1591 CTreeConflictEditorDlg dlg;
1592 dlg.SetConflictInfoText(sConflictReason);
1593 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1594 dlg.SetPath(treeConflictPath);
1595 INT_PTR dlgRet = dlg.DoModal();
1596 bRet = (dlgRet != IDCANCEL);
1600 #endif
1601 return bRet;
1604 bool CAppUtils::IsSSHPutty()
1606 CString sshclient=g_Git.m_Environment.GetEnv(_T("GIT_SSH"));
1607 sshclient=sshclient.MakeLower();
1608 if(sshclient.Find(_T("plink.exe"),0)>=0)
1610 return true;
1612 return false;
1615 CString CAppUtils::GetClipboardLink()
1617 if (!OpenClipboard(NULL))
1618 return CString();
1620 CString sClipboardText;
1621 HGLOBAL hglb = GetClipboardData(CF_TEXT);
1622 if (hglb)
1624 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1625 sClipboardText = CString(lpstr);
1626 GlobalUnlock(hglb);
1628 hglb = GetClipboardData(CF_UNICODETEXT);
1629 if (hglb)
1631 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
1632 sClipboardText = lpstr;
1633 GlobalUnlock(hglb);
1635 CloseClipboard();
1637 if(!sClipboardText.IsEmpty())
1639 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
1640 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
1642 if(sClipboardText.Find( _T("http://")) == 0)
1643 return sClipboardText;
1645 if(sClipboardText.Find( _T("https://")) == 0)
1646 return sClipboardText;
1648 if(sClipboardText.Find( _T("git://")) == 0)
1649 return sClipboardText;
1651 if(sClipboardText.Find( _T("ssh://")) == 0)
1652 return sClipboardText;
1654 if(sClipboardText.GetLength()>=2)
1655 if( sClipboardText[1] == _T(':') )
1656 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
1657 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
1658 return sClipboardText;
1661 return CString(_T(""));
1664 CString CAppUtils::ChooseRepository(CString *path)
1666 CBrowseFolder browseFolder;
1667 CRegString regLastResopitory = CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
1669 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
1670 CString strCloneDirectory;
1671 if(path)
1672 strCloneDirectory=*path;
1673 else
1675 strCloneDirectory = regLastResopitory;
1678 CString title;
1679 title.LoadString(IDS_CHOOSE_REPOSITORY);
1681 browseFolder.SetInfo(title);
1683 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
1685 regLastResopitory = strCloneDirectory;
1686 return strCloneDirectory;
1688 else
1690 return CString();
1694 bool CAppUtils::SendPatchMail(CTGitPathList &list,bool autoclose)
1696 CSendMailDlg dlg;
1698 dlg.m_PathList = list;
1700 if(dlg.DoModal()==IDOK)
1702 if(dlg.m_PathList.GetCount() == 0)
1703 return FALSE;
1705 CGitProgressDlg progDlg;
1707 theApp.m_pMainWnd = &progDlg;
1708 progDlg.SetCommand(CGitProgressDlg::GitProgress_SendMail);
1710 progDlg.SetAutoClose(autoclose);
1712 progDlg.SetPathList(dlg.m_PathList);
1713 //ProjectProperties props;
1714 //props.ReadPropsPathList(dlg.m_pathList);
1715 //progDlg.SetProjectProperties(props);
1716 progDlg.SetItemCount(dlg.m_PathList.GetCount());
1718 DWORD flags =0;
1719 if(dlg.m_bAttachment)
1720 flags |= SENDMAIL_ATTACHMENT;
1721 if(dlg.m_bCombine)
1722 flags |= SENDMAIL_COMBINED;
1723 if(dlg.m_bUseMAPI)
1724 flags |= SENDMAIL_MAPI;
1726 progDlg.SetSendMailOption(dlg.m_To,dlg.m_CC,dlg.m_Subject,flags);
1728 progDlg.DoModal();
1730 return true;
1732 return false;
1735 bool CAppUtils::SendPatchMail(CString &cmd,CString &formatpatchoutput,bool autoclose)
1737 CTGitPathList list;
1738 CString log=formatpatchoutput;
1739 int start=log.Find(cmd);
1740 if(start >=0)
1741 CString one=log.Tokenize(_T("\n"),start);
1742 else
1743 start = 0;
1745 while(start>=0)
1747 CString one=log.Tokenize(_T("\n"),start);
1748 one=one.Trim();
1749 if(one.IsEmpty() || one == _T("Success"))
1750 continue;
1751 one.Replace(_T('/'),_T('\\'));
1752 CTGitPath path;
1753 path.SetFromWin(one);
1754 list.AddPath(path);
1756 if (list.GetCount() > 0)
1758 return SendPatchMail(list, autoclose);
1760 else
1762 CMessageBox::Show(NULL, IDS_ERR_NOPATCHES, IDS_APPNAME, MB_ICONINFORMATION);
1763 return true;
1768 int CAppUtils::GetLogOutputEncode(CGit *pGit)
1770 CString cmd,output;
1771 int start=0;
1773 output = pGit->GetConfigValue(_T("i18n.logOutputEncoding"));
1774 if(output.IsEmpty())
1776 output = pGit->GetConfigValue(_T("i18n.commitencoding"));
1777 if(output.IsEmpty())
1778 return CP_UTF8;
1780 int start=0;
1781 output=output.Tokenize(_T("\n"),start);
1782 return CUnicodeUtils::GetCPCode(output);
1785 else
1787 output=output.Tokenize(_T("\n"),start);
1788 return CUnicodeUtils::GetCPCode(output);
1791 int CAppUtils::GetCommitTemplate(CString &temp)
1793 CString cmd,output;
1795 output = g_Git.GetConfigValue(_T("commit.template"), CP_UTF8);
1796 if( output.IsEmpty() )
1797 return -1;
1799 if( output.GetLength()<1)
1800 return -1;
1802 if( output[0] == _T('/'))
1804 if(output.GetLength()>=3)
1805 if(output[2] == _T('/'))
1807 output.GetBuffer()[0] = output[1];
1808 output.GetBuffer()[1] = _T(':');
1812 int start=0;
1813 output=output.Tokenize(_T("\n"),start);
1815 output.Replace(_T('/'),_T('\\'));
1819 CStdioFile file(output,CFile::modeRead|CFile::typeText);
1820 CString str;
1821 while(file.ReadString(str))
1823 temp+=str+_T("\n");
1826 }catch(...)
1828 return -1;
1830 return 0;
1832 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
1834 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
1835 CString cmd,output;
1836 int cp=CP_UTF8;
1838 output= g_Git.GetConfigValue(_T("i18n.commitencoding"));
1839 if(output.IsEmpty())
1840 cp=CP_UTF8;
1842 int start=0;
1843 output=output.Tokenize(_T("\n"),start);
1844 cp=CUnicodeUtils::GetCPCode(output);
1846 int len=message.GetLength();
1848 char * buf;
1849 buf = new char[len*4 + 4];
1850 SecureZeroMemory(buf, (len*4 + 4));
1852 int lengthIncTerminator = WideCharToMultiByte(cp, 0, message, -1, buf, len*4, NULL, NULL);
1854 file.Write(buf,lengthIncTerminator-1);
1855 file.Close();
1856 delete buf;
1857 return 0;
1860 bool CAppUtils::Fetch(CString remoteName, bool allowRebase, bool autoClose)
1862 CPullFetchDlg dlg;
1863 dlg.m_PreSelectRemote = remoteName;
1864 dlg.m_bAllowRebase = allowRebase;
1865 dlg.m_IsPull=FALSE;
1867 if(dlg.DoModal()==IDOK)
1869 if(dlg.m_bAutoLoad)
1871 CAppUtils::LaunchPAgent(NULL,&dlg.m_RemoteURL);
1874 CString url;
1875 url=dlg.m_RemoteURL;
1876 CString cmd;
1877 CString arg;
1879 int ver = CAppUtils::GetMsysgitVersion();
1881 if(ver >= 0x01070203) //above 1.7.0.2
1882 arg = _T("--progress ");
1884 if (dlg.m_bPrune) {
1885 arg += _T("--prune ");
1888 if (dlg.m_bFetchTags == 1)
1890 arg += _T("--tags ");
1892 else if (dlg.m_bFetchTags == 0)
1894 arg += _T("--no-tags ");
1897 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"),arg, url,dlg.m_RemoteBranchName);
1898 CProgressDlg progress;
1900 progress.m_bAutoCloseOnSuccess = autoClose;
1902 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENULOG)));
1904 if(!dlg.m_bRebase && !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
1906 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
1909 progress.m_GitCmd=cmd;
1910 int userResponse=progress.DoModal();
1912 if (userResponse == IDC_PROGRESS_BUTTON1)
1914 CString cmd = _T("/command:log");
1915 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
1916 RunTortoiseProc(cmd);
1917 return TRUE;
1919 else if ((userResponse == IDC_PROGRESS_BUTTON1 + 1) || (progress.m_GitStatus == 0 && dlg.m_bRebase))
1921 while(1)
1923 CRebaseDlg dlg;
1924 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUDESSENDMAIL)));
1925 dlg.m_PostButtonTexts.Add(CString(MAKEINTRESOURCE(IDS_MENUREBASE)));
1926 int response = dlg.DoModal();
1927 if(response == IDOK)
1929 return TRUE;
1931 if(response == IDC_REBASE_POST_BUTTON )
1933 CString cmd, out, err;
1934 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
1935 g_Git.m_CurrentDir,
1936 g_Git.FixBranchName(dlg.m_Upstream),
1937 g_Git.FixBranchName(dlg.m_Branch));
1938 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
1940 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
1941 return FALSE;
1944 CAppUtils::SendPatchMail(cmd,out);
1945 return TRUE;
1948 if(response == IDC_REBASE_POST_BUTTON +1 )
1949 continue;
1951 if(response == IDCANCEL)
1952 return FALSE;
1954 return TRUE;
1956 else if (userResponse != IDCANCEL)
1957 return TRUE;
1959 return FALSE;
1962 bool CAppUtils::Push(CString selectLocalBranch, bool autoClose)
1964 CPushDlg dlg;
1965 dlg.m_BranchSourceName = selectLocalBranch;
1966 CString error;
1967 DWORD exitcode = 0xFFFFFFFF;
1968 CTGitPathList list;
1969 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
1970 if (CHooks::Instance().PrePush(list,exitcode, error))
1972 if (exitcode)
1974 CString temp;
1975 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
1976 //ReportError(temp);
1977 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1978 return false;
1982 if(dlg.DoModal()==IDOK)
1984 CString cmd;
1985 CString arg;
1987 if(dlg.m_bAutoLoad)
1989 CAppUtils::LaunchPAgent(NULL,&dlg.m_URL);
1992 if(dlg.m_bPack)
1993 arg += _T("--thin ");
1994 if(dlg.m_bTags && !dlg.m_bPushAllBranches)
1995 arg += _T("--tags ");
1996 if(dlg.m_bForce)
1997 arg += _T("--force ");
1999 int ver = CAppUtils::GetMsysgitVersion();
2001 if(ver >= 0x01070203) //above 1.7.0.2
2002 arg += _T("--progress ");
2004 if (dlg.m_bPushAllBranches)
2006 cmd.Format(_T("git.exe push --all %s \"%s\""),
2007 arg,
2008 dlg.m_URL);
2010 else
2012 cmd.Format(_T("git.exe push %s \"%s\" %s"),
2013 arg,
2014 dlg.m_URL,
2015 dlg.m_BranchSourceName);
2016 if (!dlg.m_BranchRemoteName.IsEmpty())
2018 cmd += _T(":") + dlg.m_BranchRemoteName;
2022 CProgressDlg progress;
2023 progress.m_bAutoCloseOnSuccess=autoClose;
2024 progress.m_GitCmd=cmd;
2025 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REQUESTPULL)));
2026 progress.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUPUSH)));
2027 int ret = progress.DoModal();
2029 if(!progress.m_GitStatus)
2031 if (CHooks::Instance().PostPush(list,exitcode, error))
2033 if (exitcode)
2035 CString temp;
2036 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
2037 //ReportError(temp);
2038 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
2039 return false;
2042 if(ret == IDC_PROGRESS_BUTTON1)
2044 RequestPull(dlg.m_BranchRemoteName);
2046 else if(ret == IDC_PROGRESS_BUTTON1 + 1)
2048 Push();
2050 return TRUE;
2054 return FALSE;
2057 bool CAppUtils::RequestPull(CString endrevision, CString repositoryUrl)
2059 CRequestPullDlg dlg;
2060 dlg.m_RepositoryURL = repositoryUrl;
2061 dlg.m_EndRevision = endrevision;
2062 if (dlg.DoModal()==IDOK)
2064 CString cmd;
2065 cmd.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg.m_StartRevision, dlg.m_RepositoryURL, dlg.m_EndRevision);
2067 CString tempFileName = GetTempFile();
2068 if (g_Git.RunLogFile(cmd, tempFileName))
2070 CMessageBox::Show(NULL, IDS_ERR_PULLREUQESTFAILED, IDS_APPNAME, MB_OK);
2071 return false;
2073 CAppUtils::LaunchAlternativeEditor(tempFileName);
2075 return true;
2078 bool CAppUtils::CreateMultipleDirectory(const CString& szPath)
2080 CString strDir(szPath);
2081 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2083 strDir.AppendChar(_T('\\'));
2085 std::vector<CString> vPath;
2086 CString strTemp;
2087 bool bSuccess = false;
2089 for (int i=0;i<strDir.GetLength();++i)
2091 if (strDir.GetAt(i) != _T('\\'))
2093 strTemp.AppendChar(strDir.GetAt(i));
2095 else
2097 vPath.push_back(strTemp);
2098 strTemp.AppendChar(_T('\\'));
2102 std::vector<CString>::const_iterator vIter;
2103 for (vIter = vPath.begin(); vIter != vPath.end(); vIter++)
2105 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2108 return bSuccess;
2111 void CAppUtils::RemoveTrailSlash(CString &path)
2113 if(path.IsEmpty())
2114 return ;
2116 while(path[path.GetLength()-1] == _T('\\') || path[path.GetLength()-1] == _T('/' ) )
2118 path=path.Left(path.GetLength()-1);
2119 if(path.IsEmpty())
2120 return;
2124 BOOL CAppUtils::Commit(CString bugid,BOOL bWholeProject,CString &sLogMsg,
2125 CTGitPathList &pathList,
2126 CTGitPathList &selectedList,
2127 bool bSelectFilesForCommit,
2128 bool autoClose)
2130 bool bFailed = true;
2132 while(g_Git.GetUserName().IsEmpty() || g_Git.GetUserEmail().IsEmpty())
2134 if(CMessageBox::Show(NULL, IDS_PROC_NOUSERDATA, IDS_APPNAME, MB_YESNO| MB_ICONERROR) == IDYES)
2136 CTGitPath path(g_Git.m_CurrentDir);
2137 CSettings dlg(IDS_PROC_SETTINGS_TITLE,&path);
2138 dlg.SetTreeViewMode(TRUE, TRUE, TRUE);
2139 dlg.SetTreeWidth(220);
2140 dlg.m_DefaultPage = _T("gitconfig");
2142 dlg.DoModal();
2143 dlg.HandleRestart();
2146 else
2147 return false;
2150 while (bFailed)
2152 bFailed = false;
2153 CCommitDlg dlg;
2154 dlg.m_sBugID = bugid;
2156 dlg.m_bWholeProject = bWholeProject;
2158 dlg.m_sLogMessage = sLogMsg;
2159 dlg.m_pathList = pathList;
2160 dlg.m_checkedPathList = selectedList;
2161 dlg.m_bSelectFilesForCommit = bSelectFilesForCommit;
2162 dlg.m_bAutoClose = autoClose;
2163 if (dlg.DoModal() == IDOK)
2165 if (dlg.m_pathList.GetCount()==0)
2166 return false;
2167 // if the user hasn't changed the list of selected items
2168 // we don't use that list. Because if we would use the list
2169 // of pre-checked items, the dialog would show different
2170 // checked items on the next startup: it would only try
2171 // to check the parent folder (which might not even show)
2172 // instead, we simply use an empty list and let the
2173 // default checking do its job.
2174 if (!dlg.m_pathList.IsEqual(pathList))
2175 selectedList = dlg.m_pathList;
2176 pathList = dlg.m_updatedPathList;
2177 sLogMsg = dlg.m_sLogMessage;
2178 bSelectFilesForCommit = true;
2180 if( dlg.m_bPushAfterCommit )
2182 switch(dlg.m_PostCmd)
2184 case GIT_POST_CMD_DCOMMIT:
2185 CAppUtils::SVNDCommit();
2186 break;
2187 default:
2188 CAppUtils::Push();
2191 else if (dlg.m_bCreateTagAfterCommit)
2193 CAppUtils::CreateBranchTag(TRUE);
2196 // CGitProgressDlg progDlg;
2197 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2198 // if (parser.HasVal(_T("closeonend")))
2199 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2200 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2201 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2202 // progDlg.SetPathList(dlg.m_pathList);
2203 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2204 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2205 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2206 // progDlg.SetItemCount(dlg.m_itemsCount);
2207 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2208 // progDlg.DoModal();
2209 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2210 // err = (DWORD)progDlg.DidErrorsOccur();
2211 // bFailed = progDlg.DidErrorsOccur();
2212 // bRet = progDlg.DidErrorsOccur();
2213 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2214 // if (DWORD(bFailRepeat)==0)
2215 // bFailed = false; // do not repeat if the user chose not to in the settings.
2218 return true;
2222 BOOL CAppUtils::SVNDCommit()
2224 CSVNDCommitDlg dcommitdlg;
2225 CString gitSetting = g_Git.GetConfigValue(_T("svn.rmdir"));
2226 if (gitSetting == _T("")) {
2227 if (dcommitdlg.DoModal() != IDOK)
2229 return false;
2231 else
2233 if (dcommitdlg.m_remember)
2235 if (dcommitdlg.m_rmdir)
2237 gitSetting = _T("true");
2239 else
2241 gitSetting = _T("false");
2243 if(g_Git.SetConfigValue(_T("svn.rmdir"),gitSetting))
2245 CString msg;
2246 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("svn.rmdir"), gitSetting);
2247 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
2253 BOOL IsStash = false;
2254 if(!g_Git.CheckCleanWorkTree())
2256 if(CMessageBox::Show(NULL, IDS_ERROR_NOCLEAN_STASH,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2258 CString cmd,out;
2259 cmd=_T("git.exe stash");
2260 if (g_Git.Run(cmd, &out, CP_UTF8))
2262 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2263 return false;
2265 IsStash =true;
2268 else
2270 return false;
2274 CProgressDlg progress;
2275 if (dcommitdlg.m_rmdir)
2277 progress.m_GitCmd=_T("git.exe svn dcommit --rmdir");
2279 else
2281 progress.m_GitCmd=_T("git.exe svn dcommit");
2283 if(progress.DoModal()==IDOK && progress.m_GitStatus == 0)
2285 if( IsStash)
2287 if(CMessageBox::Show(NULL,IDS_DCOMMIT_STASH_POP,IDS_APPNAME,MB_YESNO|MB_ICONINFORMATION)==IDYES)
2289 CString cmd,out;
2290 cmd=_T("git.exe stash pop");
2291 if (g_Git.Run(cmd, &out, CP_UTF8))
2293 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
2294 return false;
2298 else
2300 return false;
2303 return TRUE;
2305 return FALSE;
2308 BOOL CAppUtils::Merge(CString *commit)
2310 CMergeDlg dlg;
2311 if(commit)
2312 dlg.m_initialRefName = *commit;
2314 if(dlg.DoModal()==IDOK)
2316 CString cmd;
2317 CString noff;
2318 CString squash;
2319 CString nocommit;
2320 CString msg;
2322 if(dlg.m_bNoFF)
2323 noff=_T("--no-ff");
2325 if(dlg.m_bSquash)
2326 squash=_T("--squash");
2328 if(dlg.m_bNoCommit)
2329 nocommit=_T("--no-commit");
2331 if(!dlg.m_strLogMesage.IsEmpty())
2333 msg += _T("-m \"")+dlg.m_strLogMesage+_T("\"");
2335 cmd.Format(_T("git.exe merge %s %s %s %s %s"),
2336 msg,
2337 noff,
2338 squash,
2339 nocommit,
2340 g_Git.FixBranchName(dlg.m_VersionName));
2342 CProgressDlg Prodlg;
2343 Prodlg.m_GitCmd = cmd;
2345 if (dlg.m_bNoCommit)
2346 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_MENUCOMMIT)));
2347 else if (dlg.m_bIsBranch)
2348 Prodlg.m_PostCmdList.Add(CString(MAKEINTRESOURCE(IDS_PROC_REMOVEBRANCH)));
2350 int ret = Prodlg.DoModal();
2352 if (ret == IDC_PROGRESS_BUTTON1)
2353 if (dlg.m_bNoCommit)
2354 return Commit(_T(""), TRUE, CString(), CTGitPathList(), CTGitPathList(), true);
2355 else if (dlg.m_bIsBranch)
2357 CString msg;
2358 msg.Format(IDS_PROC_DELETEBRANCHTAG, dlg.m_VersionName);
2359 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
2361 CString cmd, out;
2362 cmd.Format(_T("git.exe branch -D -- %s"), dlg.m_VersionName);
2363 if (g_Git.Run(cmd, &out, CP_UTF8))
2364 MessageBox(NULL, out, _T("TortoiseGit"), MB_OK);
2368 return !Prodlg.m_GitStatus;
2370 return false;
2373 void CAppUtils::EditNote(GitRev *rev)
2375 CInputDlg dlg;
2376 dlg.m_sHintText = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
2377 dlg.m_sInputText = rev->m_Notes;
2378 dlg.m_sTitle = CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_EDITNOTES));
2379 //dlg.m_pProjectProperties = &m_ProjectProperties;
2380 dlg.m_bUseLogWidth = true;
2381 if(dlg.DoModal() == IDOK)
2383 CString cmd,output;
2384 cmd=_T("notes add -f -F \"");
2386 CString tempfile=::GetTempFile();
2387 CAppUtils::SaveCommitUnicodeFile(tempfile,dlg.m_sInputText);
2388 cmd += tempfile;
2389 cmd += _T("\" ");
2390 cmd += rev->m_CommitHash.ToString();
2394 if (git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
2396 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
2399 else
2401 rev->m_Notes = dlg.m_sInputText;
2403 }catch(...)
2405 CMessageBox::Show(NULL, IDS_PROC_FAILEDSAVINGNOTES, IDS_APPNAME, MB_OK | MB_ICONERROR);
2407 CFile::Remove(tempfile);
2412 int CAppUtils::GetMsysgitVersion(CString *versionstr)
2414 CString cmd;
2415 CString progressarg;
2416 CString version;
2418 CRegDWORD regTime = CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
2419 CRegDWORD regVersion = CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
2421 CString gitpath = CGit::ms_LastMsysGitDir+_T("\\git.exe");
2423 __int64 time=0;
2424 if(!g_Git.GetFileModifyTime(gitpath, &time) && !versionstr)
2426 if((DWORD)time == regTime)
2428 return regVersion;
2432 if(versionstr)
2433 version = *versionstr;
2434 else
2436 CString err;
2437 cmd = _T("git.exe --version");
2438 if (g_Git.Run(cmd, &version, &err, CP_UTF8))
2440 CMessageBox::Show(NULL, _T("git have not installed (") + err + _T(")"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
2441 return false;
2445 int start=0;
2446 int ver;
2448 CString str=version.Tokenize(_T("."),start);
2449 int space = str.ReverseFind(_T(' '));
2450 str=str.Mid(space+1,start);
2451 ver = _ttol(str);
2452 ver <<=24;
2454 version = version.Mid(start);
2455 start = 0;
2456 str = version.Tokenize(_T("."),start);
2458 ver |= (_ttol(str)&0xFF)<<16;
2460 str = version.Tokenize(_T("."),start);
2461 ver |= (_ttol(str)&0xFF)<<8;
2463 str = version.Tokenize(_T("."),start);
2464 ver |= (_ttol(str)&0xFF);
2466 regTime = time&0xFFFFFFFF;
2467 regVersion = ver;
2469 return ver;
2472 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd)
2474 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
2476 CAutoLibrary hShell = LoadLibrary(_T("Shell32.dll"));
2478 if (hShell.IsValid()) {
2479 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
2480 if (pfnSHGPSFW) {
2481 IPropertyStore *pps;
2482 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
2483 if (SUCCEEDED(hr)) {
2484 PROPVARIANT var;
2485 var.vt = VT_BOOL;
2486 var.boolVal = VARIANT_TRUE;
2487 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
2488 pps->Release();
2494 void CAppUtils::SetWindowTitle(HWND hWnd, const CString& urlorpath, const CString& dialogname)
2496 ASSERT(dialogname.GetLength() < 70);
2497 ASSERT(urlorpath.GetLength() < MAX_PATH);
2498 WCHAR pathbuf[MAX_PATH] = {0};
2500 PathCompactPathEx(pathbuf, urlorpath, 70 - dialogname.GetLength(), 0);
2502 wcscat_s(pathbuf, L" - ");
2503 wcscat_s(pathbuf, dialogname);
2504 wcscat_s(pathbuf, L" - ");
2505 wcscat_s(pathbuf, CString(MAKEINTRESOURCE(IDS_APPNAME)));
2506 SetWindowText(hWnd, pathbuf);