1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - 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.
22 #include "TortoiseProc.h"
23 #include "PathUtils.h"
25 //#include "GitProperties.h"
26 #include "StringUtils.h"
27 #include "MessageBox.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"
45 #include "DeleteConflictDlg.h"
46 #include "ChangedDlg.h"
47 #include "SendMailDlg.h"
48 #include "GITProgressDlg.h"
50 #include "CommitDlg.h"
53 #include "..\Settings\Settings.h"
55 #include "SVNDCommitDlg.h"
56 #include "requestpulldlg.h"
57 #include "PullFetchDlg.h"
58 #include "RebaseDlg.h"
60 #include "StashSave.h"
62 CAppUtils::CAppUtils(void)
66 CAppUtils::~CAppUtils(void)
70 bool CAppUtils::StashSave()
74 if (dlg
.DoModal() == IDOK
)
77 cmd
= _T("git.exe stash save");
79 if (dlg
.m_bIncludeUntracked
&& CAppUtils::GetMsysgitVersion() >= 0x01070700)
80 cmd
+= _T(" --include-untracked");
82 if (!dlg
.m_sMessage
.IsEmpty())
84 CString message
= dlg
.m_sMessage
;
85 message
.Replace(_T("\""), _T("\"\""));
86 cmd
+= _T(" \"") + message
+ _T("\"");
89 if (g_Git
.Run(cmd
, &out
, CP_ACP
))
91 CMessageBox::Show(NULL
, CString(_T("<ct=0x0000FF>Stash Fail!!!</ct>\n")) + out
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
95 CMessageBox::Show(NULL
, CString(_T("<ct=0xff0000>Stash Success</ct>\n")) + out
, _T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
102 int CAppUtils::StashApply(CString ref
)
105 cmd
= _T("git.exe stash apply ");
106 if (ref
.Find(_T("refs/")) == 0)
108 if (ref
.Find(_T("stash{")) == 0)
109 ref
= _T("stash@") + ref
.Mid(5);
112 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
114 CMessageBox::Show(NULL
,CString(_T("<ct=0x0000FF>Stash Apply Fail!!!</ct>\n"))+out
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
118 if(CMessageBox::Show(NULL
,CString(_T("<ct=0xff0000>Stash Apply Success</ct>\nDo you want to show change?"))
119 ,_T("TortoiseGit"),MB_YESNO
|MB_ICONINFORMATION
) == IDYES
)
122 dlg
.m_pathList
.AddPath(CTGitPath());
130 int CAppUtils::StashPop()
133 cmd
=_T("git.exe stash pop ");
135 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
137 CMessageBox::Show(NULL
,CString(_T("<ct=0x0000FF>Stash POP Fail!!!</ct>\n"))+out
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
142 if(CMessageBox::Show(NULL
,CString(_T("<ct=0xff0000>Stash POP Success</ct>\nDo you want to show change?"))
143 ,_T("TortoiseGit"),MB_YESNO
|MB_ICONINFORMATION
) == IDYES
)
146 dlg
.m_pathList
.AddPath(CTGitPath());
154 bool CAppUtils::GetMimeType(const CTGitPath
& /*file*/, CString
& /*mimetype*/)
157 GitProperties
props(file
, GitRev::REV_WC
, false);
158 for (int i
= 0; i
< props
.GetCount(); ++i
)
160 if (props
.GetItemName(i
).compare(_T("svn:mime-type"))==0)
162 mimetype
= props
.GetItemValue(i
).c_str();
170 BOOL
CAppUtils::StartExtMerge(
171 const CTGitPath
& basefile
, const CTGitPath
& theirfile
, const CTGitPath
& yourfile
, const CTGitPath
& mergedfile
,
172 const CString
& basename
, const CString
& theirname
, const CString
& yourname
, const CString
& mergedname
, bool bReadOnly
)
175 CRegString regCom
= CRegString(_T("Software\\TortoiseGit\\Merge"));
176 CString ext
= mergedfile
.GetFileExtension();
177 CString com
= regCom
;
178 bool bInternal
= false;
183 // is there an extension specific merge tool?
184 CRegString
mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext
.MakeLower());
185 if (CString(mergetool
) != "")
190 if (GetMimeType(yourfile
, mimetype
) || GetMimeType(theirfile
, mimetype
) || GetMimeType(basefile
, mimetype
))
192 // is there a mime type specific merge tool?
193 CRegString
mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + mimetype
);
194 if (CString(mergetool
) != "")
200 if (com
.IsEmpty()||(com
.Left(1).Compare(_T("#"))==0))
204 CRegString
tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE
);
205 com
= tortoiseMergePath
;
208 com
= CPathUtils::GetAppDirectory();
209 com
+= _T("TortoiseMerge.exe");
211 com
= _T("\"") + com
+ _T("\"");
212 com
= com
+ _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
213 com
= com
+ _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
215 // check if the params are set. If not, just add the files to the command line
216 if ((com
.Find(_T("%merged"))<0)&&(com
.Find(_T("%base"))<0)&&(com
.Find(_T("%theirs"))<0)&&(com
.Find(_T("%mine"))<0))
218 com
+= _T(" \"")+basefile
.GetWinPathString()+_T("\"");
219 com
+= _T(" \"")+theirfile
.GetWinPathString()+_T("\"");
220 com
+= _T(" \"")+yourfile
.GetWinPathString()+_T("\"");
221 com
+= _T(" \"")+mergedfile
.GetWinPathString()+_T("\"");
223 if (basefile
.IsEmpty())
225 com
.Replace(_T("/base:%base"), _T(""));
226 com
.Replace(_T("%base"), _T(""));
229 com
.Replace(_T("%base"), _T("\"") + basefile
.GetWinPathString() + _T("\""));
230 if (theirfile
.IsEmpty())
232 com
.Replace(_T("/theirs:%theirs"), _T(""));
233 com
.Replace(_T("%theirs"), _T(""));
236 com
.Replace(_T("%theirs"), _T("\"") + theirfile
.GetWinPathString() + _T("\""));
237 if (yourfile
.IsEmpty())
239 com
.Replace(_T("/mine:%mine"), _T(""));
240 com
.Replace(_T("%mine"), _T(""));
243 com
.Replace(_T("%mine"), _T("\"") + yourfile
.GetWinPathString() + _T("\""));
244 if (mergedfile
.IsEmpty())
246 com
.Replace(_T("/merged:%merged"), _T(""));
247 com
.Replace(_T("%merged"), _T(""));
250 com
.Replace(_T("%merged"), _T("\"") + mergedfile
.GetWinPathString() + _T("\""));
251 if (basename
.IsEmpty())
253 if (basefile
.IsEmpty())
255 com
.Replace(_T("/basename:%bname"), _T(""));
256 com
.Replace(_T("%bname"), _T(""));
260 com
.Replace(_T("%bname"), _T("\"") + basefile
.GetUIFileOrDirectoryName() + _T("\""));
264 com
.Replace(_T("%bname"), _T("\"") + basename
+ _T("\""));
265 if (theirname
.IsEmpty())
267 if (theirfile
.IsEmpty())
269 com
.Replace(_T("/theirsname:%tname"), _T(""));
270 com
.Replace(_T("%tname"), _T(""));
274 com
.Replace(_T("%tname"), _T("\"") + theirfile
.GetUIFileOrDirectoryName() + _T("\""));
278 com
.Replace(_T("%tname"), _T("\"") + theirname
+ _T("\""));
279 if (yourname
.IsEmpty())
281 if (yourfile
.IsEmpty())
283 com
.Replace(_T("/minename:%yname"), _T(""));
284 com
.Replace(_T("%yname"), _T(""));
288 com
.Replace(_T("%yname"), _T("\"") + yourfile
.GetUIFileOrDirectoryName() + _T("\""));
292 com
.Replace(_T("%yname"), _T("\"") + yourname
+ _T("\""));
293 if (mergedname
.IsEmpty())
295 if (mergedfile
.IsEmpty())
297 com
.Replace(_T("/mergedname:%mname"), _T(""));
298 com
.Replace(_T("%mname"), _T(""));
302 com
.Replace(_T("%mname"), _T("\"") + mergedfile
.GetUIFileOrDirectoryName() + _T("\""));
306 com
.Replace(_T("%mname"), _T("\"") + mergedname
+ _T("\""));
308 if ((bReadOnly
)&&(bInternal
))
309 com
+= _T(" /readonly");
311 if(!LaunchApplication(com
, IDS_ERR_EXTMERGESTART
, false))
319 BOOL
CAppUtils::StartExtPatch(const CTGitPath
& patchfile
, const CTGitPath
& dir
, const CString
& sOriginalDescription
, const CString
& sPatchedDescription
, BOOL bReversed
, BOOL bWait
)
323 viewer
= CPathUtils::GetAppDirectory();
324 viewer
+= _T("TortoiseMerge.exe");
326 viewer
= _T("\"") + viewer
+ _T("\"");
327 viewer
= viewer
+ _T(" /diff:\"") + patchfile
.GetWinPathString() + _T("\"");
328 viewer
= viewer
+ _T(" /patchpath:\"") + dir
.GetWinPathString() + _T("\"");
330 viewer
+= _T(" /reversedpatch");
331 if (!sOriginalDescription
.IsEmpty())
332 viewer
= viewer
+ _T(" /patchoriginal:\"") + sOriginalDescription
+ _T("\"");
333 if (!sPatchedDescription
.IsEmpty())
334 viewer
= viewer
+ _T(" /patchpatched:\"") + sPatchedDescription
+ _T("\"");
335 if(!LaunchApplication(viewer
, IDS_ERR_DIFFVIEWSTART
, !!bWait
))
342 CString
CAppUtils::PickDiffTool(const CTGitPath
& file1
, const CTGitPath
& file2
)
344 // Is there a mime type specific diff tool?
346 if (GetMimeType(file1
, mimetype
) || GetMimeType(file2
, mimetype
))
348 CString difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + mimetype
);
349 if (!difftool
.IsEmpty())
353 // Is there an extension specific diff tool?
354 CString ext
= file2
.GetFileExtension().MakeLower();
357 CString difftool
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext
);
358 if (!difftool
.IsEmpty())
360 // Maybe we should use TortoiseIDiff?
361 if ((ext
== _T(".jpg")) || (ext
== _T(".jpeg")) ||
362 (ext
== _T(".bmp")) || (ext
== _T(".gif")) ||
363 (ext
== _T(".png")) || (ext
== _T(".ico")) ||
364 (ext
== _T(".dib")) || (ext
== _T(".emf")))
367 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseIDiff.exe") + _T("\"") +
368 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname");
372 // Finally, pick a generic external diff tool
373 CString difftool
= CRegString(_T("Software\\TortoiseGit\\Diff"));
377 bool CAppUtils::StartExtDiff(
378 const CString
& file1
, const CString
& file2
,
379 const CString
& sName1
, const CString
& sName2
,
380 const DiffFlags
& flags
)
384 CRegDWORD
blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE
);
385 if (!flags
.bBlame
|| !(DWORD
)blamediff
)
387 viewer
= PickDiffTool(file1
, file2
);
388 // If registry entry for a diff program is commented out, use TortoiseMerge.
389 bool bCommentedOut
= viewer
.Left(1) == _T("#");
390 if (flags
.bAlternativeTool
)
392 // Invert external vs. internal diff tool selection.
394 viewer
.Delete(0); // uncomment
398 else if (bCommentedOut
)
402 bool bInternal
= viewer
.IsEmpty();
406 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseMerge.exe") + _T("\"") +
407 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
409 viewer
+= _T(" /blame");
411 // check if the params are set. If not, just add the files to the command line
412 if ((viewer
.Find(_T("%base"))<0)&&(viewer
.Find(_T("%mine"))<0))
414 viewer
+= _T(" \"")+file1
+_T("\"");
415 viewer
+= _T(" \"")+file2
+_T("\"");
417 if (viewer
.Find(_T("%base")) >= 0)
419 viewer
.Replace(_T("%base"), _T("\"")+file1
+_T("\""));
421 if (viewer
.Find(_T("%mine")) >= 0)
423 viewer
.Replace(_T("%mine"), _T("\"")+file2
+_T("\""));
426 if (sName1
.IsEmpty())
427 viewer
.Replace(_T("%bname"), _T("\"") + file1
+ _T("\""));
429 viewer
.Replace(_T("%bname"), _T("\"") + sName1
+ _T("\""));
431 if (sName2
.IsEmpty())
432 viewer
.Replace(_T("%yname"), _T("\"") + file2
+ _T("\""));
434 viewer
.Replace(_T("%yname"), _T("\"") + sName2
+ _T("\""));
436 if (flags
.bReadOnly
&& bInternal
)
437 viewer
+= _T(" /readonly");
439 return LaunchApplication(viewer
, IDS_ERR_EXTDIFFSTART
, flags
.bWait
);
442 BOOL
CAppUtils::StartExtDiffProps(const CTGitPath
& file1
, const CTGitPath
& file2
, const CString
& sName1
, const CString
& sName2
, BOOL bWait
, BOOL bReadOnly
)
444 CRegString
diffpropsexe(_T("Software\\TortoiseGit\\DiffProps"));
445 CString viewer
= diffpropsexe
;
446 bool bInternal
= false;
447 if (viewer
.IsEmpty()||(viewer
.Left(1).Compare(_T("#"))==0))
449 //no registry entry (or commented out) for a diff program
452 viewer
= CPathUtils::GetAppDirectory();
453 viewer
+= _T("TortoiseMerge.exe");
454 viewer
= _T("\"") + viewer
+ _T("\"");
455 viewer
= viewer
+ _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
457 // check if the params are set. If not, just add the files to the command line
458 if ((viewer
.Find(_T("%base"))<0)&&(viewer
.Find(_T("%mine"))<0))
460 viewer
+= _T(" \"")+file1
.GetWinPathString()+_T("\"");
461 viewer
+= _T(" \"")+file2
.GetWinPathString()+_T("\"");
463 if (viewer
.Find(_T("%base")) >= 0)
465 viewer
.Replace(_T("%base"), _T("\"")+file1
.GetWinPathString()+_T("\""));
467 if (viewer
.Find(_T("%mine")) >= 0)
469 viewer
.Replace(_T("%mine"), _T("\"")+file2
.GetWinPathString()+_T("\""));
472 if (sName1
.IsEmpty())
473 viewer
.Replace(_T("%bname"), _T("\"") + file1
.GetUIFileOrDirectoryName() + _T("\""));
475 viewer
.Replace(_T("%bname"), _T("\"") + sName1
+ _T("\""));
477 if (sName2
.IsEmpty())
478 viewer
.Replace(_T("%yname"), _T("\"") + file2
.GetUIFileOrDirectoryName() + _T("\""));
480 viewer
.Replace(_T("%yname"), _T("\"") + sName2
+ _T("\""));
482 if ((bReadOnly
)&&(bInternal
))
483 viewer
+= _T(" /readonly");
485 if(!LaunchApplication(viewer
, IDS_ERR_EXTDIFFSTART
, !!bWait
))
492 BOOL
CAppUtils::StartUnifiedDiffViewer(const CString
& patchfile
, const CString
& title
, BOOL bWait
)
495 CRegString v
= CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
497 if (viewer
.IsEmpty() || (viewer
.Left(1).Compare(_T("#"))==0))
500 viewer
= CPathUtils::GetAppDirectory();
501 viewer
+= _T("TortoiseUDiff.exe");
502 // enquote the path to TortoiseUDiff
503 viewer
= _T("\"") + viewer
+ _T("\"");
505 viewer
= viewer
+ _T(" /patchfile:%1 /title:\"%title\"");
508 if (viewer
.Find(_T("%1"))>=0)
510 if (viewer
.Find(_T("\"%1\"")) >= 0)
511 viewer
.Replace(_T("%1"), patchfile
);
513 viewer
.Replace(_T("%1"), _T("\"") + patchfile
+ _T("\""));
516 viewer
+= _T(" \"") + patchfile
+ _T("\"");
517 if (viewer
.Find(_T("%title")) >= 0)
519 viewer
.Replace(_T("%title"), title
);
522 if(!LaunchApplication(viewer
, IDS_ERR_DIFFVIEWSTART
, !!bWait
))
529 BOOL
CAppUtils::StartTextViewer(CString file
)
532 CRegString txt
= CRegString(_T(".txt\\"), _T(""), FALSE
, HKEY_CLASSES_ROOT
);
534 viewer
= viewer
+ _T("\\Shell\\Open\\Command\\");
535 CRegString txtexe
= CRegString(viewer
, _T(""), FALSE
, HKEY_CLASSES_ROOT
);
538 DWORD len
= ExpandEnvironmentStrings(viewer
, NULL
, 0);
539 TCHAR
* buf
= new TCHAR
[len
+1];
540 ExpandEnvironmentStrings(viewer
, buf
, len
);
543 len
= ExpandEnvironmentStrings(file
, NULL
, 0);
544 buf
= new TCHAR
[len
+1];
545 ExpandEnvironmentStrings(file
, buf
, len
);
548 file
= _T("\"")+file
+_T("\"");
549 if (viewer
.IsEmpty())
551 OPENFILENAME ofn
= {0}; // common dialog box structure
552 TCHAR szFile
[MAX_PATH
] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH.
553 // Initialize OPENFILENAME
554 ofn
.lStructSize
= sizeof(OPENFILENAME
);
555 ofn
.hwndOwner
= NULL
;
556 ofn
.lpstrFile
= szFile
;
557 ofn
.nMaxFile
= _countof(szFile
);
559 sFilter
.LoadString(IDS_PROGRAMSFILEFILTER
);
560 TCHAR
* pszFilters
= new TCHAR
[sFilter
.GetLength()+4];
561 _tcscpy_s (pszFilters
, sFilter
.GetLength()+4, sFilter
);
562 // Replace '|' delimiters with '\0's
563 TCHAR
*ptr
= pszFilters
+ _tcslen(pszFilters
); //set ptr at the NULL
564 while (ptr
!= pszFilters
)
570 ofn
.lpstrFilter
= pszFilters
;
571 ofn
.nFilterIndex
= 1;
572 ofn
.lpstrFileTitle
= NULL
;
573 ofn
.nMaxFileTitle
= 0;
574 ofn
.lpstrInitialDir
= NULL
;
576 temp
.LoadString(IDS_UTILS_SELECTTEXTVIEWER
);
577 CStringUtils::RemoveAccelerators(temp
);
578 ofn
.lpstrTitle
= temp
;
579 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
;
581 // Display the Open dialog box.
583 if (GetOpenFileName(&ofn
)==TRUE
)
585 delete [] pszFilters
;
586 viewer
= CString(ofn
.lpstrFile
);
590 delete [] pszFilters
;
594 if (viewer
.Find(_T("\"%1\"")) >= 0)
596 viewer
.Replace(_T("\"%1\""), file
);
598 else if (viewer
.Find(_T("%1")) >= 0)
600 viewer
.Replace(_T("%1"), file
);
608 if(!LaunchApplication(viewer
, IDS_ERR_TEXTVIEWSTART
, false))
615 BOOL
CAppUtils::CheckForEmptyDiff(const CTGitPath
& sDiffPath
)
618 HANDLE hFile
= ::CreateFile(sDiffPath
.GetWinPath(), GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
619 if (hFile
== INVALID_HANDLE_VALUE
)
621 length
= ::GetFileSize(hFile
, NULL
);
622 ::CloseHandle(hFile
);
629 void CAppUtils::CreateFontForLogs(CFont
& fontToCreate
)
632 HDC hScreenDC
= ::GetDC(NULL
);
633 logFont
.lfHeight
= -MulDiv((DWORD
)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC
, LOGPIXELSY
), 72);
634 ::ReleaseDC(NULL
, hScreenDC
);
636 logFont
.lfEscapement
= 0;
637 logFont
.lfOrientation
= 0;
638 logFont
.lfWeight
= FW_NORMAL
;
639 logFont
.lfItalic
= 0;
640 logFont
.lfUnderline
= 0;
641 logFont
.lfStrikeOut
= 0;
642 logFont
.lfCharSet
= DEFAULT_CHARSET
;
643 logFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
644 logFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
645 logFont
.lfQuality
= DRAFT_QUALITY
;
646 logFont
.lfPitchAndFamily
= FF_DONTCARE
| FIXED_PITCH
;
647 _tcscpy_s(logFont
.lfFaceName
, 32, (LPCTSTR
)(CString
)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
648 VERIFY(fontToCreate
.CreateFontIndirect(&logFont
));
651 bool CAppUtils::LaunchApplication(const CString
& sCommandLine
, UINT idErrMessageFormat
, bool bWaitForStartup
)
654 PROCESS_INFORMATION process
;
655 memset(&startup
, 0, sizeof(startup
));
656 startup
.cb
= sizeof(startup
);
657 memset(&process
, 0, sizeof(process
));
659 CString
cleanCommandLine(sCommandLine
);
661 if (CreateProcess(NULL
, const_cast<TCHAR
*>((LPCTSTR
)cleanCommandLine
), NULL
, NULL
, FALSE
, 0, 0, g_Git
.m_CurrentDir
, &startup
, &process
)==0)
663 if(idErrMessageFormat
!= 0)
666 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
667 FORMAT_MESSAGE_FROM_SYSTEM
|
668 FORMAT_MESSAGE_IGNORE_INSERTS
,
671 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
677 temp
.Format(idErrMessageFormat
, lpMsgBuf
);
678 CMessageBox::Show(NULL
, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
679 LocalFree( lpMsgBuf
);
686 WaitForInputIdle(process
.hProcess
, 10000);
689 CloseHandle(process
.hThread
);
690 CloseHandle(process
.hProcess
);
693 bool CAppUtils::LaunchPAgent(CString
*keyfile
,CString
* pRemote
)
707 cmd
.Format(_T("remote.%s.puttykeyfile"),remote
);
708 key
= g_Git
.GetConfigValue(cmd
);
710 key
= key
.Tokenize(_T("\n"),start
);
718 CString proc
=CPathUtils::GetAppDirectory();
719 proc
+= _T("pageant.exe \"");
723 CString tempfile
= GetTempFile();
724 ::DeleteFile(tempfile
);
726 proc
+= _T(" -c \"");
727 proc
+= CPathUtils::GetAppDirectory();
728 proc
+= _T("touch.exe\"");
733 bool b
= LaunchApplication(proc
, IDS_ERR_PAGEANT
, true);
738 while(!::PathFileExists(tempfile
))
743 break; //timeout 5 minutes
748 CMessageBox::Show(NULL
, _T("Fail wait for pageant finish load key"),_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
750 ::DeleteFile(tempfile
);
753 bool CAppUtils::LaunchAlternativeEditor(const CString
& filename
)
755 CString editTool
= CRegString(_T("Software\\TortoiseGit\\AlternativeEditor"));
756 if (editTool
.IsEmpty() || (editTool
.Left(1).Compare(_T("#"))==0)) {
757 editTool
= CPathUtils::GetAppDirectory() + _T("notepad2.exe");
761 sCmd
.Format(_T("\"%s\" \"%s\""), editTool
, filename
);
763 LaunchApplication(sCmd
, NULL
, false);
766 bool CAppUtils::LaunchRemoteSetting()
768 CTGitPath
path(g_Git
.m_CurrentDir
);
769 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
, &path
);
770 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
771 //dlg.SetTreeWidth(220);
772 dlg
.m_DefaultPage
= _T("gitremote");
779 * Launch the external blame viewer
781 bool CAppUtils::LaunchTortoiseBlame(const CString
& sBlameFile
,CString Rev
,const CString
& sParams
)
783 CString viewer
= _T("\"") + CPathUtils::GetAppDirectory();
784 viewer
+= _T("TortoiseGitBlame.exe");
785 viewer
+= _T("\" \"") + sBlameFile
+ _T("\"");
786 //viewer += _T(" \"") + sLogFile + _T("\"");
787 //viewer += _T(" \"") + sOriginalFile + _T("\"");
788 if(!Rev
.IsEmpty() && Rev
!= GIT_REV_ZERO
)
789 viewer
+= CString(_T(" /rev:"))+Rev
;
790 viewer
+= _T(" ")+sParams
;
792 return LaunchApplication(viewer
, IDS_ERR_TGITBLAME
, false);
795 bool CAppUtils::FormatTextInRichEditControl(CWnd
* pWnd
)
800 bool bStyled
= false;
801 pWnd
->GetWindowText(sText
);
802 // the rich edit control doesn't count the CR char!
803 // to be exact: CRLF is treated as one char.
806 // style each line separately
811 nNewlinePos
= sText
.Find('\n', offset
);
812 CString sLine
= sText
.Mid(offset
);
814 sLine
= sLine
.Left(nNewlinePos
-offset
);
817 while (FindStyleChars(sLine
, '*', start
, end
))
819 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
820 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
822 SecureZeroMemory(&format
, sizeof(CHARFORMAT2
));
823 format
.cbSize
= sizeof(CHARFORMAT2
);
824 format
.dwMask
= CFM_BOLD
;
825 format
.dwEffects
= CFE_BOLD
;
826 pWnd
->SendMessage(EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);
832 while (FindStyleChars(sLine
, '^', start
, end
))
834 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
835 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
837 SecureZeroMemory(&format
, sizeof(CHARFORMAT2
));
838 format
.cbSize
= sizeof(CHARFORMAT2
);
839 format
.dwMask
= CFM_ITALIC
;
840 format
.dwEffects
= CFE_ITALIC
;
841 pWnd
->SendMessage(EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);
847 while (FindStyleChars(sLine
, '_', start
, end
))
849 CHARRANGE range
= {(LONG
)start
+offset
, (LONG
)end
+offset
};
850 pWnd
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
852 SecureZeroMemory(&format
, sizeof(CHARFORMAT2
));
853 format
.cbSize
= sizeof(CHARFORMAT2
);
854 format
.dwMask
= CFM_UNDERLINE
;
855 format
.dwEffects
= CFE_UNDERLINE
;
856 pWnd
->SendMessage(EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);
860 offset
= nNewlinePos
+1;
861 } while(nNewlinePos
>=0);
865 bool CAppUtils::FindStyleChars(const CString
& sText
, TCHAR stylechar
, int& start
, int& end
)
868 bool bFoundMarker
= false;
869 // find a starting marker
870 while (sText
[i
] != 0)
872 if (sText
[i
] == stylechar
)
874 if (((i
+1)<sText
.GetLength())&&(IsCharAlphaNumeric(sText
[i
+1])) &&
875 (((i
>0)&&(!IsCharAlphaNumeric(sText
[i
-1])))||(i
==0)))
887 // find ending marker
888 bFoundMarker
= false;
889 while (sText
[i
] != 0)
891 if (sText
[i
] == stylechar
)
893 if ((IsCharAlphaNumeric(sText
[i
-1])) &&
894 ((((i
+1)<sText
.GetLength())&&(!IsCharAlphaNumeric(sText
[i
+1])))||(i
+1)==sText
.GetLength()))
907 bool CAppUtils::FileOpenSave(CString
& path
, int * filterindex
, UINT title
, UINT filter
, bool bOpen
, HWND hwndOwner
)
909 OPENFILENAME ofn
= {0}; // common dialog box structure
910 TCHAR szFile
[MAX_PATH
] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH.
911 ofn
.lStructSize
= sizeof(OPENFILENAME
);
912 ofn
.hwndOwner
= hwndOwner
;
913 _tcscpy_s(szFile
, MAX_PATH
, (LPCTSTR
)path
);
914 ofn
.lpstrFile
= szFile
;
915 ofn
.nMaxFile
= _countof(szFile
);
917 TCHAR
* pszFilters
= NULL
;
920 sFilter
.LoadString(filter
);
921 pszFilters
= new TCHAR
[sFilter
.GetLength()+4];
922 _tcscpy_s (pszFilters
, sFilter
.GetLength()+4, sFilter
);
923 // Replace '|' delimiters with '\0's
924 TCHAR
*ptr
= pszFilters
+ _tcslen(pszFilters
); //set ptr at the NULL
925 while (ptr
!= pszFilters
)
931 ofn
.lpstrFilter
= pszFilters
;
933 ofn
.nFilterIndex
= 1;
934 ofn
.lpstrFileTitle
= NULL
;
935 ofn
.nMaxFileTitle
= 0;
936 ofn
.lpstrInitialDir
= NULL
;
940 temp
.LoadString(title
);
941 CStringUtils::RemoveAccelerators(temp
);
943 ofn
.lpstrTitle
= temp
;
945 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_EXPLORER
;
947 ofn
.Flags
= OFN_OVERWRITEPROMPT
| OFN_EXPLORER
;
950 // Display the Open dialog box.
954 bRet
= !!GetOpenFileName(&ofn
);
958 bRet
= !!GetSaveFileName(&ofn
);
963 delete [] pszFilters
;
964 path
= CString(ofn
.lpstrFile
);
966 *filterindex
= ofn
.nFilterIndex
;
970 delete [] pszFilters
;
974 bool CAppUtils::SetListCtrlBackgroundImage(HWND hListCtrl
, UINT nID
, int width
/* = 128 */, int height
/* = 128 */)
976 ListView_SetTextBkColor(hListCtrl
, CLR_NONE
);
977 COLORREF bkColor
= ListView_GetBkColor(hListCtrl
);
978 // create a bitmap from the icon
979 HICON hIcon
= (HICON
)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(nID
), IMAGE_ICON
, width
, height
, LR_DEFAULTCOLOR
);
985 rect
.bottom
= height
;
988 HWND desktop
= ::GetDesktopWindow();
991 HDC screen_dev
= ::GetDC(desktop
);
994 // Create a compatible DC
995 HDC dst_hdc
= ::CreateCompatibleDC(screen_dev
);
998 // Create a new bitmap of icon size
999 bmp
= ::CreateCompatibleBitmap(screen_dev
, rect
.right
, rect
.bottom
);
1002 // Select it into the compatible DC
1003 HBITMAP old_dst_bmp
= (HBITMAP
)::SelectObject(dst_hdc
, bmp
);
1004 // Fill the background of the compatible DC with the given color
1005 ::SetBkColor(dst_hdc
, bkColor
);
1006 ::ExtTextOut(dst_hdc
, 0, 0, ETO_OPAQUE
, &rect
, NULL
, 0, NULL
);
1008 // Draw the icon into the compatible DC
1009 ::DrawIconEx(dst_hdc
, 0, 0, hIcon
, rect
.right
, rect
.bottom
, 0, NULL
, DI_NORMAL
);
1010 ::SelectObject(dst_hdc
, old_dst_bmp
);
1012 ::DeleteDC(dst_hdc
);
1015 ::ReleaseDC(desktop
, screen_dev
);
1025 lv
.ulFlags
= LVBKIF_TYPE_WATERMARK
;
1027 lv
.xOffsetPercent
= 100;
1028 lv
.yOffsetPercent
= 100;
1029 ListView_SetBkImage(hListCtrl
, &lv
);
1033 CString
CAppUtils::GetProjectNameFromURL(CString url
)
1036 while (name
.IsEmpty() || (name
.CompareNoCase(_T("branches"))==0) ||
1037 (name
.CompareNoCase(_T("tags"))==0) ||
1038 (name
.CompareNoCase(_T("trunk"))==0))
1040 name
= url
.Mid(url
.ReverseFind('/')+1);
1041 url
= url
.Left(url
.ReverseFind('/'));
1043 if ((name
.Compare(_T("svn")) == 0)||(name
.Compare(_T("svnroot")) == 0))
1045 // a name of svn or svnroot indicates that it's not really the project name. In that
1046 // case, we try the first part of the URL
1047 // of course, this won't work in all cases (but it works for Google project hosting)
1048 url
.Replace(_T("http://"), _T(""));
1049 url
.Replace(_T("https://"), _T(""));
1050 url
.Replace(_T("svn://"), _T(""));
1051 url
.Replace(_T("svn+ssh://"), _T(""));
1052 url
.TrimLeft(_T("/"));
1053 name
= url
.Left(url
.Find('.'));
1058 bool CAppUtils::StartShowUnifiedDiff(HWND
/*hWnd*/, const CTGitPath
& url1
, const git_revnum_t
& rev1
,
1059 const CTGitPath
& /*url2*/, const git_revnum_t
& rev2
,
1060 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
1061 bool /*bAlternateDiff*/ /* = false */, bool /*bIgnoreAncestry*/ /* = false */, bool /* blame = false */, bool bMerge
)
1064 CString tempfile
=GetTempFile();
1066 if(rev1
== GitRev::GetWorkingCopy())
1068 cmd
.Format(_T("git.exe diff --stat -p %s "),rev2
);
1076 cmd
.Format(_T("git.exe diff-tree -r -p %s --stat %s %s"),merge
, rev1
,rev2
);
1079 if( !url1
.IsEmpty() )
1082 cmd
+= url1
.GetGitPathString();
1085 g_Git
.RunLogFile(cmd
,tempfile
);
1086 CAppUtils::StartUnifiedDiffViewer(tempfile
,rev1
.Left(6)+_T(":")+rev2
.Left(6));
1091 sCmd
.Format(_T("%s /command:showcompare /unified"),
1092 (LPCTSTR
)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));
1093 sCmd
+= _T(" /url1:\"") + url1
.GetGitPathString() + _T("\"");
1095 sCmd
+= _T(" /revision1:") + rev1
.ToString();
1096 sCmd
+= _T(" /url2:\"") + url2
.GetGitPathString() + _T("\"");
1098 sCmd
+= _T(" /revision2:") + rev2
.ToString();
1100 sCmd
+= _T(" /pegrevision:") + peg
.ToString();
1101 if (headpeg
.IsValid())
1102 sCmd
+= _T(" /headpegrevision:") + headpeg
.ToString();
1105 sCmd
+= _T(" /alternatediff");
1107 if (bIgnoreAncestry
)
1108 sCmd
+= _T(" /ignoreancestry");
1112 sCmd
+= _T(" /hwnd:");
1114 _stprintf_s(buf
, 30, _T("%d"), hWnd
);
1118 return CAppUtils::LaunchApplication(sCmd
, NULL
, false);
1124 bool CAppUtils::Export(CString
*BashHash
)
1128 // ask from where the export has to be done
1131 dlg
.m_Revision
=*BashHash
;
1133 if (dlg
.DoModal() == IDOK
)
1136 cmd
.Format(_T("git.exe archive --format=zip --verbose %s"),
1137 g_Git
.FixBranchName(dlg
.m_VersionName
));
1139 //g_Git.RunLogFile(cmd,dlg.m_strExportDirectory);
1142 pro
.m_LogFile
=dlg
.m_strExportDirectory
;
1149 bool CAppUtils::CreateBranchTag(bool IsTag
,CString
*CommitHash
, bool switch_new_brach
)
1151 CCreateBranchTagDlg dlg
;
1153 dlg
.m_bSwitch
=switch_new_brach
;
1156 dlg
.m_Base
= *CommitHash
;
1158 if(dlg
.DoModal()==IDOK
)
1164 track
=_T(" --track ");
1175 cmd
.Format(_T("git.exe tag %s %s %s %s %s"),
1179 dlg
.m_BranchTagName
,
1180 g_Git
.FixBranchName(dlg
.m_VersionName
)
1183 CString tempfile
=::GetTempFile();
1184 if(!dlg
.m_Message
.Trim().IsEmpty())
1186 CAppUtils::SaveCommitUnicodeFile(tempfile
,dlg
.m_Message
);
1187 cmd
+= _T(" -F ")+tempfile
;
1192 cmd
.Format(_T("git.exe branch %s %s %s %s"),
1195 dlg
.m_BranchTagName
,
1196 g_Git
.FixBranchName(dlg
.m_VersionName
)
1200 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1202 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1204 if( !IsTag
&& dlg
.m_bSwitch
)
1206 // it is a new branch and the user has requested to switch to it
1207 cmd
.Format(_T("git.exe checkout %s"), dlg
.m_BranchTagName
);
1208 g_Git
.Run(cmd
,&out
,CP_UTF8
);
1209 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1217 bool CAppUtils::Switch(CString
*CommitHash
, CString initialRefName
, bool autoclose
)
1221 dlg
.m_Base
=*CommitHash
;
1222 if(!initialRefName
.IsEmpty())
1223 dlg
.m_initialRefName
= initialRefName
;
1225 if (dlg
.DoModal() == IDOK
)
1229 branch
= dlg
.m_NewBranch
;
1231 return PerformSwitch(dlg
.m_VersionName
, dlg
.m_bForce
== TRUE
, branch
, dlg
.m_bBranchOverride
== TRUE
, dlg
.m_bTrack
== TRUE
, autoclose
);
1236 bool CAppUtils::PerformSwitch(CString ref
, bool bForce
/* false */, CString sNewBranch
/* CString() */, bool bBranchOverride
/* false */, bool bTrack
/* false */, bool autoClose
/* false */)
1243 if(!sNewBranch
.IsEmpty()){
1244 if (bBranchOverride
)
1246 branch
.Format(_T("-B %s"), sNewBranch
);
1250 branch
.Format(_T("-b %s"), sNewBranch
);
1253 track
= _T("--track");
1258 cmd
.Format(_T("git.exe checkout %s %s %s %s"),
1262 g_Git
.FixBranchName(ref
));
1264 CProgressDlg progress
;
1265 progress
.m_bAutoCloseOnSuccess
= autoClose
;
1266 progress
.m_GitCmd
= cmd
;
1268 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
1269 if (gitPath
.HasSubmodules())
1270 progress
.m_PostCmdList
.Add(_T("Update Submodules"));
1272 int ret
= progress
.DoModal();
1273 if (gitPath
.HasSubmodules() && ret
== IDC_PROGRESS_BUTTON1
)
1276 sCmd
.Format(_T("\"%s\" /command:subupdate /bkpath:\"%s\""), (LPCTSTR
)(CPathUtils::GetAppDirectory() + _T("TortoiseProc.exe")), (LPCTSTR
)g_Git
.m_CurrentDir
);
1278 LaunchApplication(sCmd
, NULL
, false);
1281 else if (ret
== IDOK
)
1287 bool CAppUtils::IgnoreFile(CTGitPathList
&path
,bool IsMask
)
1290 ignorefile
=g_Git
.m_CurrentDir
+_T("\\");
1294 ignorefile
+=path
.GetCommonRoot().GetDirectory().GetWinPathString()+_T("\\.gitignore");
1299 ignorefile
+= _T("\\.gitignore");
1303 if(!file
.Open(ignorefile
,CFile::modeCreate
|CFile::modeReadWrite
|CFile::modeNoTruncate
))
1305 CMessageBox::Show(NULL
,ignorefile
+_T(" Open Failure"),_T("TortoiseGit"),MB_OK
);
1313 //file.ReadString(ignorelist);
1315 for(int i
=0;i
<path
.GetCount();i
++)
1319 mask
=_T("*")+path
[i
].GetFileExtension();
1320 if(ignorelist
.Find(mask
)<0)
1321 ignorelist
+= _T("\n")+mask
;
1325 ignorelist
+= _T("\n/")+path
[i
].GetGitPathString();
1328 file
.WriteString(ignorelist
);
1342 bool CAppUtils::GitReset(CString
*CommitHash
,int type
)
1345 dlg
.m_ResetType
=type
;
1346 dlg
.m_ResetToVersion
=*CommitHash
;
1347 if (dlg
.DoModal() == IDOK
)
1351 switch(dlg
.m_ResetType
)
1366 cmd
.Format(_T("git.exe reset %s %s"),type
, *CommitHash
);
1368 CProgressDlg progress
;
1369 progress
.m_GitCmd
=cmd
;
1371 CTGitPath gitPath
= g_Git
.m_CurrentDir
;
1372 if (gitPath
.HasSubmodules() && dlg
.m_ResetType
== 2)
1373 progress
.m_PostCmdList
.Add(_T("Update Submodules"));
1375 int ret
= progress
.DoModal();
1376 if (gitPath
.HasSubmodules() && dlg
.m_ResetType
== 2 && ret
== IDC_PROGRESS_BUTTON1
)
1379 sCmd
.Format(_T("\"%s\" /command:subupdate /bkpath:\"%s\""), (LPCTSTR
)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")), (LPCTSTR
)g_Git
.m_CurrentDir
);
1381 LaunchApplication(sCmd
, NULL
, false);
1384 else if (ret
== IDOK
)
1391 void CAppUtils::DescribeFile(bool mode
, bool base
,CString
&descript
)
1395 descript
=_T("Deleted");
1400 descript
=_T("Modified");
1403 descript
=_T("Created");
1407 void CAppUtils::RemoveTempMergeFile(CTGitPath
&path
)
1409 CString tempmergefile
;
1412 tempmergefile
= CAppUtils::GetMergeTempFile(_T("LOCAL"),path
);
1413 CFile::Remove(tempmergefile
);
1420 tempmergefile
= CAppUtils::GetMergeTempFile(_T("REMOTE"),path
);
1421 CFile::Remove(tempmergefile
);
1428 tempmergefile
= CAppUtils::GetMergeTempFile(_T("BASE"),path
);
1429 CFile::Remove(tempmergefile
);
1434 CString
CAppUtils::GetMergeTempFile(CString type
,CTGitPath
&merge
)
1437 file
=g_Git
.m_CurrentDir
+_T("\\") + merge
.GetWinPathString()+_T(".")+type
+merge
.GetFileExtension();
1442 bool CAppUtils::ConflictEdit(CTGitPath
&path
,bool /*bAlternativeTool*/,bool revertTheirMy
)
1446 CTGitPath merge
=path
;
1447 CTGitPath directory
= merge
.GetDirectory();
1449 // we have the conflicted file (%merged)
1450 // now look for the other required files
1452 //stat.GetStatus(merge);
1453 //if (stat.status == NULL)
1459 cmd
.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge
.GetGitPathString());
1461 if (g_Git
.Run(cmd
, &vector
))
1467 list
.ParserFromLsFile(vector
);
1469 if(list
.GetCount() == 0)
1476 mine
.SetFromGit(GetMergeTempFile(_T("LOCAL"),merge
));
1477 theirs
.SetFromGit(GetMergeTempFile(_T("REMOTE"),merge
));
1478 base
.SetFromGit(GetMergeTempFile(_T("BASE"),merge
));
1482 //format=_T("git.exe cat-file blob \":%d:%s\"");
1483 format
= _T("git checkout-index --temp --stage=%d -- \"%s\"");
1485 //create a empty file, incase stage is not three
1486 tempfile
.Open(mine
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1488 tempfile
.Open(theirs
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1490 tempfile
.Open(base
.GetWinPathString(),CFile::modeCreate
|CFile::modeReadWrite
);
1493 bool b_base
=false, b_local
=false, b_remote
=false;
1495 for(int i
=0;i
<list
.GetCount();i
++)
1502 if( list
[i
].m_Stage
== 1)
1504 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1506 outfile
= base
.GetWinPathString();
1509 if( list
[i
].m_Stage
== 2 )
1511 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1513 outfile
= mine
.GetWinPathString();
1516 if( list
[i
].m_Stage
== 3 )
1518 cmd
.Format(format
, list
[i
].m_Stage
, list
[i
].GetGitPathString());
1520 outfile
= theirs
.GetWinPathString();
1522 CString output
, err
;
1523 if(!outfile
.IsEmpty())
1524 if (!g_Git
.Run(cmd
, &output
, &err
, CP_ACP
))
1528 file
= output
.Tokenize(_T("\t"), start
);
1529 ::MoveFileEx(file
,outfile
,MOVEFILE_REPLACE_EXISTING
|MOVEFILE_COPY_ALLOWED
);
1533 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
1537 if(b_local
&& b_remote
)
1539 merge
.SetFromWin(g_Git
.m_CurrentDir
+_T("\\")+merge
.GetWinPathString());
1541 bRet
= !!CAppUtils::StartExtMerge(base
,mine
, theirs
, merge
,_T("BASE"),_T("LOCAL"),_T("REMOTE"));
1543 bRet
= !!CAppUtils::StartExtMerge(base
, theirs
, mine
, merge
,_T("BASE"),_T("REMOTE"),_T("LOCAL"));
1548 CFile::Remove(mine
.GetWinPathString());
1549 CFile::Remove(theirs
.GetWinPathString());
1550 CFile::Remove(base
.GetWinPathString());
1552 CDeleteConflictDlg dlg
;
1553 DescribeFile(b_local
, b_base
,dlg
.m_LocalStatus
);
1554 DescribeFile(b_remote
,b_base
,dlg
.m_RemoteStatus
);
1555 dlg
.m_bShowModifiedButton
=b_base
;
1556 dlg
.m_File
=merge
.GetGitPathString();
1557 if(dlg
.DoModal() == IDOK
)
1562 cmd
.Format(_T("git.exe rm -- \"%s\""),merge
.GetGitPathString());
1565 cmd
.Format(_T("git.exe add -- \"%s\""),merge
.GetGitPathString());
1567 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
1569 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
1579 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1580 base
, theirs
, mine
, merge
);
1583 if (stat
.status
->text_status
== svn_wc_status_conflicted
)
1585 // we have a text conflict, use our merge tool to resolve the conflict
1587 CTSVNPath
theirs(directory
);
1588 CTSVNPath
mine(directory
);
1589 CTSVNPath
base(directory
);
1590 bool bConflictData
= false;
1592 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_new
))
1594 theirs
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_new
));
1595 bConflictData
= true;
1597 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_old
))
1599 base
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_old
));
1600 bConflictData
= true;
1602 if ((stat
.status
->entry
)&&(stat
.status
->entry
->conflict_wrk
))
1604 mine
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->conflict_wrk
));
1605 bConflictData
= true;
1612 bRet
= !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1613 base
, theirs
, mine
, merge
);
1616 if (stat
.status
->prop_status
== svn_wc_status_conflicted
)
1618 // we have a property conflict
1619 CTSVNPath
prej(directory
);
1620 if ((stat
.status
->entry
)&&(stat
.status
->entry
->prejfile
))
1622 prej
.AppendPathString(CUnicodeUtils::GetUnicode(stat
.status
->entry
->prejfile
));
1623 // there's a problem: the prej file contains a _description_ of the conflict, and
1624 // that description string might be translated. That means we have no way of parsing
1625 // the file to find out the conflicting values.
1626 // The only thing we can do: show a dialog with the conflict description, then
1627 // let the user either accept the existing property or open the property edit dialog
1628 // to manually change the properties and values. And a button to mark the conflict as
1630 CEditPropConflictDlg dlg
;
1631 dlg
.SetPrejFile(prej
);
1632 dlg
.SetConflictedItem(merge
);
1633 bRet
= (dlg
.DoModal() != IDCANCEL
);
1637 if (stat
.status
->tree_conflict
)
1639 // we have a tree conflict
1641 const SVNInfoData
* pInfoData
= info
.GetFirstFileInfo(merge
, SVNRev(), SVNRev());
1644 if (pInfoData
->treeconflict_kind
== svn_wc_conflict_kind_text
)
1646 CTSVNPath
theirs(directory
);
1647 CTSVNPath
mine(directory
);
1648 CTSVNPath
base(directory
);
1649 bool bConflictData
= false;
1651 if (pInfoData
->treeconflict_theirfile
)
1653 theirs
.AppendPathString(pInfoData
->treeconflict_theirfile
);
1654 bConflictData
= true;
1656 if (pInfoData
->treeconflict_basefile
)
1658 base
.AppendPathString(pInfoData
->treeconflict_basefile
);
1659 bConflictData
= true;
1661 if (pInfoData
->treeconflict_myfile
)
1663 mine
.AppendPathString(pInfoData
->treeconflict_myfile
);
1664 bConflictData
= true;
1671 bRet
= !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool
),
1672 base
, theirs
, mine
, merge
);
1674 else if (pInfoData
->treeconflict_kind
== svn_wc_conflict_kind_tree
)
1676 CString sConflictAction
;
1677 CString sConflictReason
;
1678 CString sResolveTheirs
;
1679 CString sResolveMine
;
1680 CTSVNPath treeConflictPath
= CTSVNPath(pInfoData
->treeconflict_path
);
1681 CString sItemName
= treeConflictPath
.GetUIFileOrDirectoryName();
1683 if (pInfoData
->treeconflict_nodekind
== svn_node_file
)
1685 switch (pInfoData
->treeconflict_operation
)
1687 case svn_wc_operation_update
:
1688 switch (pInfoData
->treeconflict_action
)
1690 case svn_wc_conflict_action_edit
:
1691 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEEDIT
, (LPCTSTR
)sItemName
);
1692 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1694 case svn_wc_conflict_action_add
:
1695 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEADD
, (LPCTSTR
)sItemName
);
1696 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1698 case svn_wc_conflict_action_delete
:
1699 sConflictAction
.Format(IDS_TREECONFLICT_FILEUPDATEDELETE
, (LPCTSTR
)sItemName
);
1700 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1704 case svn_wc_operation_switch
:
1705 switch (pInfoData
->treeconflict_action
)
1707 case svn_wc_conflict_action_edit
:
1708 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHEDIT
, (LPCTSTR
)sItemName
);
1709 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1711 case svn_wc_conflict_action_add
:
1712 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHADD
, (LPCTSTR
)sItemName
);
1713 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1715 case svn_wc_conflict_action_delete
:
1716 sConflictAction
.Format(IDS_TREECONFLICT_FILESWITCHDELETE
, (LPCTSTR
)sItemName
);
1717 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1721 case svn_wc_operation_merge
:
1722 switch (pInfoData
->treeconflict_action
)
1724 case svn_wc_conflict_action_edit
:
1725 sConflictAction
.Format(IDS_TREECONFLICT_FILEMERGEEDIT
, (LPCTSTR
)sItemName
);
1726 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1728 case svn_wc_conflict_action_add
:
1729 sResolveTheirs
.Format(IDS_TREECONFLICT_FILEMERGEADD
, (LPCTSTR
)sItemName
);
1730 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE
);
1732 case svn_wc_conflict_action_delete
:
1733 sConflictAction
.Format(IDS_TREECONFLICT_FILEMERGEDELETE
, (LPCTSTR
)sItemName
);
1734 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1740 else if (pInfoData
->treeconflict_nodekind
== svn_node_dir
)
1742 switch (pInfoData
->treeconflict_operation
)
1744 case svn_wc_operation_update
:
1745 switch (pInfoData
->treeconflict_action
)
1747 case svn_wc_conflict_action_edit
:
1748 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEEDIT
, (LPCTSTR
)sItemName
);
1749 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1751 case svn_wc_conflict_action_add
:
1752 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEADD
, (LPCTSTR
)sItemName
);
1753 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1755 case svn_wc_conflict_action_delete
:
1756 sConflictAction
.Format(IDS_TREECONFLICT_DIRUPDATEDELETE
, (LPCTSTR
)sItemName
);
1757 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1761 case svn_wc_operation_switch
:
1762 switch (pInfoData
->treeconflict_action
)
1764 case svn_wc_conflict_action_edit
:
1765 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHEDIT
, (LPCTSTR
)sItemName
);
1766 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1768 case svn_wc_conflict_action_add
:
1769 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHADD
, (LPCTSTR
)sItemName
);
1770 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1772 case svn_wc_conflict_action_delete
:
1773 sConflictAction
.Format(IDS_TREECONFLICT_DIRSWITCHDELETE
, (LPCTSTR
)sItemName
);
1774 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1778 case svn_wc_operation_merge
:
1779 switch (pInfoData
->treeconflict_action
)
1781 case svn_wc_conflict_action_edit
:
1782 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEEDIT
, (LPCTSTR
)sItemName
);
1783 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1785 case svn_wc_conflict_action_add
:
1786 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEADD
, (LPCTSTR
)sItemName
);
1787 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR
);
1789 case svn_wc_conflict_action_delete
:
1790 sConflictAction
.Format(IDS_TREECONFLICT_DIRMERGEDELETE
, (LPCTSTR
)sItemName
);
1791 sResolveTheirs
.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR
);
1799 switch (pInfoData
->treeconflict_reason
)
1801 case svn_wc_conflict_reason_edited
:
1802 uReasonID
= IDS_TREECONFLICT_REASON_EDITED
;
1803 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1805 case svn_wc_conflict_reason_obstructed
:
1806 uReasonID
= IDS_TREECONFLICT_REASON_OBSTRUCTED
;
1807 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1809 case svn_wc_conflict_reason_deleted
:
1810 uReasonID
= IDS_TREECONFLICT_REASON_DELETED
;
1811 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_REMOVEDIR
: IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1813 case svn_wc_conflict_reason_added
:
1814 uReasonID
= IDS_TREECONFLICT_REASON_ADDED
;
1815 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1817 case svn_wc_conflict_reason_missing
:
1818 uReasonID
= IDS_TREECONFLICT_REASON_MISSING
;
1819 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_REMOVEDIR
: IDS_TREECONFLICT_RESOLVE_REMOVEFILE
);
1821 case svn_wc_conflict_reason_unversioned
:
1822 uReasonID
= IDS_TREECONFLICT_REASON_UNVERSIONED
;
1823 sResolveMine
.LoadString(pInfoData
->treeconflict_nodekind
== svn_node_dir
? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR
: IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE
);
1826 sConflictReason
.Format(uReasonID
, (LPCTSTR
)sConflictAction
);
1828 CTreeConflictEditorDlg dlg
;
1829 dlg
.SetConflictInfoText(sConflictReason
);
1830 dlg
.SetResolveTexts(sResolveTheirs
, sResolveMine
);
1831 dlg
.SetPath(treeConflictPath
);
1832 INT_PTR dlgRet
= dlg
.DoModal();
1833 bRet
= (dlgRet
!= IDCANCEL
);
1842 * FUNCTION : FormatDateAndTime
1843 * DESCRIPTION : Generates a displayable string from a CTime object in
1844 * system short or long format or as a relative value
1846 * option - DATE_SHORTDATE or DATE_LONGDATE
1847 * bIncluedeTime - whether to show time as well as date
1848 * bRelative - if true then relative time is shown if reasonable
1849 * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format
1850 * rather than locale
1851 * RETURN : CString containing date/time
1853 CString
CAppUtils::FormatDateAndTime( const CTime
& cTime
, DWORD option
, bool bIncludeTime
/*=true*/,
1854 bool bRelative
/*=false*/)
1859 datetime
= ToRelativeTimeString( cTime
);
1863 // should we use the locale settings for formatting the date/time?
1864 if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE
))
1868 cTime
.GetAsSystemTime( sysTime
);
1872 GetDateFormat(LOCALE_USER_DEFAULT
, option
, &sysTime
, NULL
, buf
,
1877 datetime
+= _T(" ");
1878 GetTimeFormat(LOCALE_USER_DEFAULT
, 0, &sysTime
, NULL
, buf
, _countof(buf
) - 1);
1884 // no, so fixed format
1887 datetime
= cTime
.Format(_T("%Y-%m-%d %H:%M:%S"));
1891 datetime
= cTime
.Format(_T("%Y-%m-%d"));
1899 * Converts a given time to a relative display string (relative to current time)
1900 * Given time must be in local timezone
1902 CString
CAppUtils::ToRelativeTimeString(CTime time
)
1905 // convert to COleDateTime
1907 time
.GetAsSystemTime( sysTime
);
1908 COleDateTime
oleTime( sysTime
);
1909 answer
= ToRelativeTimeString(oleTime
, COleDateTime::GetCurrentTime());
1914 * Generates a display string showing the relative time between the two given times as COleDateTimes
1916 CString
CAppUtils::ToRelativeTimeString(COleDateTime time
,COleDateTime RelativeTo
)
1919 COleDateTimeSpan ts
= RelativeTo
- time
;
1921 if(fabs(ts
.GetTotalDays()) >= 3*365)
1923 answer
= ExpandRelativeTime( (int)ts
.GetTotalDays()/365, IDS_YEAR_AGO
, IDS_YEARS_AGO
);
1926 if(fabs(ts
.GetTotalDays()) >= 60)
1928 answer
= ExpandRelativeTime( (int)ts
.GetTotalDays()/30, IDS_MONTH_AGO
, IDS_MONTHS_AGO
);
1932 if(fabs(ts
.GetTotalDays()) >= 14)
1934 answer
= ExpandRelativeTime( (int)ts
.GetTotalDays()/7, IDS_WEEK_AGO
, IDS_WEEKS_AGO
);
1938 if(fabs(ts
.GetTotalDays()) >= 2)
1940 answer
= ExpandRelativeTime( (int)ts
.GetTotalDays(), IDS_DAY_AGO
, IDS_DAYS_AGO
);
1944 if(fabs(ts
.GetTotalHours()) >= 2)
1946 answer
= ExpandRelativeTime( (int)ts
.GetTotalHours(), IDS_HOUR_AGO
, IDS_HOURS_AGO
);
1950 if(fabs(ts
.GetTotalMinutes()) >= 2)
1952 answer
= ExpandRelativeTime( (int)ts
.GetTotalMinutes(), IDS_MINUTE_AGO
, IDS_MINUTES_AGO
);
1956 answer
= ExpandRelativeTime( (int)ts
.GetTotalSeconds(), IDS_SECOND_AGO
, IDS_SECONDS_AGO
);
1961 * Passed a value and two resource string ids
1962 * if count is 1 then FormatString is called with format_1 and the value
1963 * otherwise format_2 is used
1964 * the formatted string is returned
1966 CString
CAppUtils::ExpandRelativeTime( int count
, UINT format_1
, UINT format_n
)
1971 answer
.FormatMessage( format_1
, count
);
1975 answer
.FormatMessage( format_n
, count
);
1980 bool CAppUtils::IsSSHPutty()
1982 CString sshclient
=g_Git
.m_Environment
.GetEnv(_T("GIT_SSH"));
1983 sshclient
=sshclient
.MakeLower();
1984 if(sshclient
.Find(_T("plink.exe"),0)>=0)
1991 CString
CAppUtils::GetClipboardLink()
1993 if (!OpenClipboard(NULL
))
1996 CString sClipboardText
;
1997 HGLOBAL hglb
= GetClipboardData(CF_TEXT
);
2000 LPCSTR lpstr
= (LPCSTR
)GlobalLock(hglb
);
2001 sClipboardText
= CString(lpstr
);
2004 hglb
= GetClipboardData(CF_UNICODETEXT
);
2007 LPCTSTR lpstr
= (LPCTSTR
)GlobalLock(hglb
);
2008 sClipboardText
= lpstr
;
2013 if(!sClipboardText
.IsEmpty())
2015 if(sClipboardText
[0] == _T('\"') && sClipboardText
[sClipboardText
.GetLength()-1] == _T('\"'))
2016 sClipboardText
=sClipboardText
.Mid(1,sClipboardText
.GetLength()-2);
2018 if(sClipboardText
.Find( _T("http://")) == 0)
2019 return sClipboardText
;
2021 if(sClipboardText
.Find( _T("https://")) == 0)
2022 return sClipboardText
;
2024 if(sClipboardText
.Find( _T("git://")) == 0)
2025 return sClipboardText
;
2027 if(sClipboardText
.Find( _T("ssh://")) == 0)
2028 return sClipboardText
;
2030 if(sClipboardText
.GetLength()>=2)
2031 if( sClipboardText
[1] == _T(':') )
2032 if( (sClipboardText
[0] >= 'A' && sClipboardText
[0] <= 'Z')
2033 || (sClipboardText
[0] >= 'a' && sClipboardText
[0] <= 'z') )
2034 return sClipboardText
;
2037 return CString(_T(""));
2040 CString
CAppUtils::ChooseRepository(CString
*path
)
2042 CBrowseFolder browseFolder
;
2043 CRegString regLastResopitory
= CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\LastRepo"),_T(""));
2045 browseFolder
.m_style
= BIF_EDITBOX
| BIF_NEWDIALOGSTYLE
| BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
;
2046 CString strCloneDirectory
;
2048 strCloneDirectory
=*path
;
2051 strCloneDirectory
= regLastResopitory
;
2055 title
.LoadString(IDS_CHOOSE_REPOSITORY
);
2057 browseFolder
.SetInfo(title
);
2059 if (browseFolder
.Show(NULL
, strCloneDirectory
) == CBrowseFolder::OK
)
2061 regLastResopitory
= strCloneDirectory
;
2062 return strCloneDirectory
;
2070 bool CAppUtils::SendPatchMail(CTGitPathList
&list
,bool autoclose
)
2074 dlg
.m_PathList
= list
;
2076 if(dlg
.DoModal()==IDOK
)
2078 if(dlg
.m_PathList
.GetCount() == 0)
2081 CGitProgressDlg progDlg
;
2083 theApp
.m_pMainWnd
= &progDlg
;
2084 progDlg
.SetCommand(CGitProgressDlg::GitProgress_SendMail
);
2086 progDlg
.SetAutoClose(autoclose
);
2088 progDlg
.SetPathList(dlg
.m_PathList
);
2089 //ProjectProperties props;
2090 //props.ReadPropsPathList(dlg.m_pathList);
2091 //progDlg.SetProjectProperties(props);
2092 progDlg
.SetItemCount(dlg
.m_PathList
.GetCount());
2095 if(dlg
.m_bAttachment
)
2096 flags
|= SENDMAIL_ATTACHMENT
;
2098 flags
|= SENDMAIL_COMBINED
;
2100 flags
|= SENDMAIL_MAPI
;
2102 progDlg
.SetSendMailOption(dlg
.m_To
,dlg
.m_CC
,dlg
.m_Subject
,flags
);
2111 bool CAppUtils::SendPatchMail(CString
&cmd
,CString
&formatpatchoutput
,bool autoclose
)
2114 CString log
=formatpatchoutput
;
2115 int start
=log
.Find(cmd
);
2117 CString one
=log
.Tokenize(_T("\n"),start
);
2123 CString one
=log
.Tokenize(_T("\n"),start
);
2125 if(one
.IsEmpty() || one
== _T("Success"))
2127 one
.Replace(_T('/'),_T('\\'));
2129 path
.SetFromWin(one
);
2132 if (list
.GetCount() > 0)
2134 return SendPatchMail(list
, autoclose
);
2138 CMessageBox::Show(NULL
, _T("Not patches generated."), _T("TortoiseGit"), MB_ICONINFORMATION
);
2144 int CAppUtils::GetLogOutputEncode(CGit
*pGit
)
2149 output
= pGit
->GetConfigValue(_T("i18n.logOutputEncoding"));
2150 if(output
.IsEmpty())
2152 output
= pGit
->GetConfigValue(_T("i18n.commitencoding"));
2153 if(output
.IsEmpty())
2157 output
=output
.Tokenize(_T("\n"),start
);
2158 return CUnicodeUtils::GetCPCode(output
);
2163 output
=output
.Tokenize(_T("\n"),start
);
2164 return CUnicodeUtils::GetCPCode(output
);
2167 int CAppUtils::GetCommitTemplate(CString
&temp
)
2171 output
= g_Git
.GetConfigValue(_T("commit.template"),CP_ACP
);
2172 if( output
.IsEmpty() )
2175 if( output
.GetLength()<1)
2178 if( output
[0] == _T('/'))
2180 if(output
.GetLength()>=3)
2181 if(output
[2] == _T('/'))
2183 output
.GetBuffer()[0] = output
[1];
2184 output
.GetBuffer()[1] = _T(':');
2189 output
=output
.Tokenize(_T("\n"),start
);
2191 output
.Replace(_T('/'),_T('\\'));
2195 CStdioFile
file(output
,CFile::modeRead
|CFile::typeText
);
2197 while(file
.ReadString(str
))
2208 int CAppUtils::SaveCommitUnicodeFile(CString
&filename
, CString
&message
)
2210 CFile
file(filename
,CFile::modeReadWrite
|CFile::modeCreate
);
2214 output
= g_Git
.GetConfigValue(_T("i18n.commitencoding"));
2215 if(output
.IsEmpty())
2219 output
=output
.Tokenize(_T("\n"),start
);
2220 cp
=CUnicodeUtils::GetCPCode(output
);
2222 int len
=message
.GetLength();
2225 buf
= new char[len
*4 + 4];
2226 SecureZeroMemory(buf
, (len
*4 + 4));
2228 int lengthIncTerminator
= WideCharToMultiByte(cp
, 0, message
, -1, buf
, len
*4, NULL
, NULL
);
2230 file
.Write(buf
,lengthIncTerminator
-1);
2236 bool CAppUtils::Fetch(CString remoteName
, bool allowRebase
, bool autoClose
)
2239 dlg
.m_PreSelectRemote
= remoteName
;
2240 dlg
.m_bAllowRebase
= allowRebase
;
2243 if(dlg
.DoModal()==IDOK
)
2247 CAppUtils::LaunchPAgent(NULL
,&dlg
.m_RemoteURL
);
2251 url
=dlg
.m_RemoteURL
;
2255 int ver
= CAppUtils::GetMsysgitVersion();
2257 if(ver
>= 0x01070203) //above 1.7.0.2
2258 arg
= _T("--progress ");
2261 arg
+= _T("--prune ");
2264 if (dlg
.m_bFetchTags
) {
2265 arg
+= _T("--tags ");
2268 cmd
.Format(_T("git.exe fetch -v %s \"%s\" %s"),arg
, url
,dlg
.m_RemoteBranchName
);
2269 CProgressDlg progress
;
2271 progress
.m_bAutoCloseOnSuccess
= autoClose
;
2273 progress
.m_PostCmdList
.Add(_T("Show Log"));
2277 progress
.m_PostCmdList
.Add(_T("&Rebase"));
2280 progress
.m_GitCmd
=cmd
;
2281 int userResponse
=progress
.DoModal();
2283 if (userResponse
== IDC_PROGRESS_BUTTON1
)
2286 cmd
= CPathUtils::GetAppDirectory() + _T("TortoiseProc.exe");
2287 cmd
+= _T(" /command:log");
2288 cmd
+= _T(" /path:\"") + g_Git
.m_CurrentDir
+ _T("\"");
2289 CAppUtils::LaunchApplication(cmd
, IDS_ERR_PROC
, false);
2292 else if ((userResponse
== IDC_PROGRESS_BUTTON1
+ 1) || (progress
.m_GitStatus
== 0 && dlg
.m_bRebase
))
2297 dlg
.m_PostButtonTexts
.Add(_T("Email &Patch..."));
2298 dlg
.m_PostButtonTexts
.Add(_T("Restart Rebase"));
2299 int response
= dlg
.DoModal();
2300 if(response
== IDOK
)
2304 if(response
== IDC_REBASE_POST_BUTTON
)
2306 CString cmd
, out
, err
;
2307 cmd
.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
2309 g_Git
.FixBranchName(dlg
.m_Upstream
),
2310 g_Git
.FixBranchName(dlg
.m_Branch
));
2311 if (g_Git
.Run(cmd
, &out
, &err
, CP_ACP
))
2313 CMessageBox::Show(NULL
, out
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
2317 CAppUtils::SendPatchMail(cmd
,out
);
2321 if(response
== IDC_REBASE_POST_BUTTON
+1 )
2324 if(response
== IDCANCEL
)
2333 bool CAppUtils::Push(CString selectLocalBranch
, bool autoClose
)
2336 dlg
.m_BranchSourceName
= selectLocalBranch
;
2338 DWORD exitcode
= 0xFFFFFFFF;
2340 list
.AddPath(CTGitPath(g_Git
.m_CurrentDir
));
2341 if (CHooks::Instance().PrePush(list
,exitcode
, error
))
2346 temp
.Format(IDS_ERR_HOOKFAILED
, (LPCTSTR
)error
);
2347 //ReportError(temp);
2348 CMessageBox::Show(NULL
,temp
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
2353 if(dlg
.DoModal()==IDOK
)
2360 CAppUtils::LaunchPAgent(NULL
,&dlg
.m_URL
);
2364 arg
+= _T("--thin ");
2365 if(dlg
.m_bTags
&& !dlg
.m_bPushAllBranches
)
2366 arg
+= _T("--tags ");
2368 arg
+= _T("--force ");
2370 int ver
= CAppUtils::GetMsysgitVersion();
2372 if(ver
>= 0x01070203) //above 1.7.0.2
2373 arg
+= _T("--progress ");
2375 if (dlg
.m_bPushAllBranches
)
2377 cmd
.Format(_T("git.exe push --all %s \"%s\""),
2383 cmd
.Format(_T("git.exe push %s \"%s\" %s"),
2386 dlg
.m_BranchSourceName
);
2387 if (!dlg
.m_BranchRemoteName
.IsEmpty())
2389 cmd
+= _T(":") + dlg
.m_BranchRemoteName
;
2393 CProgressDlg progress
;
2394 progress
.m_bAutoCloseOnSuccess
=autoClose
;
2395 progress
.m_GitCmd
=cmd
;
2396 progress
.m_PostCmdList
.Add(_T("&Request pull"));
2397 progress
.m_PostCmdList
.Add(_T("Re&Push"));
2398 int ret
= progress
.DoModal();
2400 if(!progress
.m_GitStatus
)
2402 if (CHooks::Instance().PostPush(list
,exitcode
, error
))
2407 temp
.Format(IDS_ERR_HOOKFAILED
, (LPCTSTR
)error
);
2408 //ReportError(temp);
2409 CMessageBox::Show(NULL
,temp
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
2413 if(ret
== IDC_PROGRESS_BUTTON1
)
2415 RequestPull(dlg
.m_BranchRemoteName
);
2417 else if(ret
== IDC_PROGRESS_BUTTON1
+ 1)
2428 bool CAppUtils::RequestPull(CString endrevision
, CString repositoryUrl
)
2430 CRequestPullDlg dlg
;
2431 dlg
.m_RepositoryURL
= repositoryUrl
;
2432 dlg
.m_EndRevision
= endrevision
;
2433 if (dlg
.DoModal()==IDOK
)
2436 cmd
.Format(_T("git.exe request-pull %s \"%s\" %s"), dlg
.m_StartRevision
, dlg
.m_RepositoryURL
, dlg
.m_EndRevision
);
2438 CProgressDlg progress
;
2439 progress
.m_GitCmd
=cmd
;
2445 bool CAppUtils::CreateMultipleDirectory(const CString
& szPath
)
2447 CString
strDir(szPath
);
2448 if (strDir
.GetAt(strDir
.GetLength()-1)!=_T('\\'))
2450 strDir
.AppendChar(_T('\\'));
2452 std::vector
<CString
> vPath
;
2454 bool bSuccess
= false;
2456 for (int i
=0;i
<strDir
.GetLength();++i
)
2458 if (strDir
.GetAt(i
) != _T('\\'))
2460 strTemp
.AppendChar(strDir
.GetAt(i
));
2464 vPath
.push_back(strTemp
);
2465 strTemp
.AppendChar(_T('\\'));
2469 std::vector
<CString
>::const_iterator vIter
;
2470 for (vIter
= vPath
.begin(); vIter
!= vPath
.end(); vIter
++)
2472 bSuccess
= CreateDirectory(*vIter
, NULL
) ? true : false;
2478 void CAppUtils::RemoveTrailSlash(CString
&path
)
2483 while(path
[path
.GetLength()-1] == _T('\\') || path
[path
.GetLength()-1] == _T('/' ) )
2485 path
=path
.Left(path
.GetLength()-1);
2491 BOOL
CAppUtils::Commit(CString bugid
,BOOL bWholeProject
,CString
&sLogMsg
,
2492 CTGitPathList
&pathList
,
2493 CTGitPathList
&selectedList
,
2494 bool bSelectFilesForCommit
,
2497 bool bFailed
= true;
2499 while(g_Git
.GetUserName().IsEmpty() || g_Git
.GetConfigValue(_T("user.email")).IsEmpty())
2501 if(CMessageBox::Show(NULL
,_T("User name and email must be set before commit.\r\n Do you want to set these now?\r\n"),
2502 _T("TortoiseGit"),MB_YESNO
| MB_ICONERROR
) == IDYES
)
2504 CTGitPath
path(g_Git
.m_CurrentDir
);
2505 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
,&path
);
2506 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
2507 dlg
.SetTreeWidth(220);
2508 dlg
.m_DefaultPage
= _T("gitconfig");
2511 dlg
.HandleRestart();
2522 dlg
.m_sBugID
= bugid
;
2524 dlg
.m_bWholeProject
= bWholeProject
;
2526 dlg
.m_sLogMessage
= sLogMsg
;
2527 dlg
.m_pathList
= pathList
;
2528 dlg
.m_checkedPathList
= selectedList
;
2529 dlg
.m_bSelectFilesForCommit
= bSelectFilesForCommit
;
2530 dlg
.m_bAutoClose
= autoClose
;
2531 if (dlg
.DoModal() == IDOK
)
2533 if (dlg
.m_pathList
.GetCount()==0)
2535 // if the user hasn't changed the list of selected items
2536 // we don't use that list. Because if we would use the list
2537 // of pre-checked items, the dialog would show different
2538 // checked items on the next startup: it would only try
2539 // to check the parent folder (which might not even show)
2540 // instead, we simply use an empty list and let the
2541 // default checking do its job.
2542 if (!dlg
.m_pathList
.IsEqual(pathList
))
2543 selectedList
= dlg
.m_pathList
;
2544 pathList
= dlg
.m_updatedPathList
;
2545 sLogMsg
= dlg
.m_sLogMessage
;
2546 bSelectFilesForCommit
= true;
2548 if( dlg
.m_bPushAfterCommit
)
2550 switch(dlg
.m_PostCmd
)
2552 case GIT_POST_CMD_DCOMMIT
:
2553 CAppUtils::SVNDCommit();
2559 // CGitProgressDlg progDlg;
2560 // progDlg.SetChangeList(dlg.m_sChangeList, !!dlg.m_bKeepChangeList);
2561 // if (parser.HasVal(_T("closeonend")))
2562 // progDlg.SetAutoClose(parser.GetLongVal(_T("closeonend")));
2563 // progDlg.SetCommand(CGitProgressDlg::GitProgress_Commit);
2564 // progDlg.SetOptions(dlg.m_bKeepLocks ? ProgOptKeeplocks : ProgOptNone);
2565 // progDlg.SetPathList(dlg.m_pathList);
2566 // progDlg.SetCommitMessage(dlg.m_sLogMessage);
2567 // progDlg.SetDepth(dlg.m_bRecursive ? Git_depth_infinity : svn_depth_empty);
2568 // progDlg.SetSelectedList(dlg.m_selectedPathList);
2569 // progDlg.SetItemCount(dlg.m_itemsCount);
2570 // progDlg.SetBugTraqProvider(dlg.m_BugTraqProvider);
2571 // progDlg.DoModal();
2572 // CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
2573 // err = (DWORD)progDlg.DidErrorsOccur();
2574 // bFailed = progDlg.DidErrorsOccur();
2575 // bRet = progDlg.DidErrorsOccur();
2576 // CRegDWORD bFailRepeat = CRegDWORD(_T("Software\\TortoiseGit\\CommitReopen"), FALSE);
2577 // if (DWORD(bFailRepeat)==0)
2578 // bFailed = false; // do not repeat if the user chose not to in the settings.
2585 BOOL
CAppUtils::SVNDCommit()
2587 CSVNDCommitDlg dcommitdlg
;
2588 CString gitSetting
= g_Git
.GetConfigValue(_T("svn.rmdir"));
2589 if (gitSetting
== _T("")) {
2590 if (dcommitdlg
.DoModal() != IDOK
)
2596 if (dcommitdlg
.m_remember
)
2598 if (dcommitdlg
.m_rmdir
)
2600 gitSetting
= _T("true");
2604 gitSetting
= _T("false");
2606 if(g_Git
.SetConfigValue(_T("svn.rmdir"),gitSetting
))
2608 CMessageBox::Show(NULL
,_T("Fail to set config"),_T("TortoiseGit"),MB_OK
);
2614 BOOL IsStash
= false;
2615 if(!g_Git
.CheckCleanWorkTree())
2617 if(CMessageBox::Show(NULL
, IDS_ERROR_NOCLEAN_STASH
,IDS_APPNAME
,MB_YESNO
|MB_ICONINFORMATION
)==IDYES
)
2620 cmd
=_T("git.exe stash");
2621 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
2623 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
2635 CProgressDlg progress
;
2636 if (dcommitdlg
.m_rmdir
)
2638 progress
.m_GitCmd
=_T("git.exe svn dcommit --rmdir");
2642 progress
.m_GitCmd
=_T("git.exe svn dcommit");
2644 if(progress
.DoModal()==IDOK
&& progress
.m_GitStatus
== 0)
2648 if(CMessageBox::Show(NULL
,IDS_DCOMMIT_STASH_POP
,IDS_APPNAME
,MB_YESNO
|MB_ICONINFORMATION
)==IDYES
)
2651 cmd
=_T("git.exe stash pop");
2652 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
2654 CMessageBox::Show(NULL
,out
,_T("TortoiseGit"),MB_OK
);
2669 BOOL
CAppUtils::Merge(CString
*commit
)
2673 dlg
.m_initialRefName
= *commit
;
2675 if(dlg
.DoModal()==IDOK
)
2687 squash
=_T("--squash");
2690 nocommit
=_T("--no-commit");
2692 if(!dlg
.m_strLogMesage
.IsEmpty())
2694 msg
+= _T("-m \"")+dlg
.m_strLogMesage
+_T("\"");
2696 cmd
.Format(_T("git.exe merge %s %s %s %s %s"),
2701 g_Git
.FixBranchName(dlg
.m_VersionName
));
2703 CProgressDlg Prodlg
;
2704 Prodlg
.m_GitCmd
= cmd
;
2706 if (dlg
.m_bNoCommit
)
2707 Prodlg
.m_PostCmdList
.Add(_T("Commit"));
2709 int ret
= Prodlg
.DoModal();
2711 if (ret
== IDC_PROGRESS_BUTTON1
)
2712 return Commit(_T(""), TRUE
, CString(), CTGitPathList(), CTGitPathList(), true);
2714 return !Prodlg
.m_GitStatus
;
2719 void CAppUtils::EditNote(GitRev
*rev
)
2722 dlg
.m_sHintText
=_T("Edit Notes");
2723 dlg
.m_sInputText
= rev
->m_Notes
;
2724 dlg
.m_sTitle
=_T("Edit Notes");
2725 //dlg.m_pProjectProperties = &m_ProjectProperties;
2726 dlg
.m_bUseLogWidth
= true;
2727 if(dlg
.DoModal() == IDOK
)
2730 cmd
=_T("notes add -f -F \"");
2732 CString tempfile
=::GetTempFile();
2733 CAppUtils::SaveCommitUnicodeFile(tempfile
,dlg
.m_sInputText
);
2736 cmd
+= rev
->m_CommitHash
.ToString();
2740 if(git_run_cmd("notes", CUnicodeUtils::GetMulti(cmd
,CP_ACP
).GetBuffer()))
2742 CMessageBox::Show(NULL
,_T("Edit Note Fail"), _T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
2747 rev
->m_Notes
= dlg
.m_sInputText
;
2751 CMessageBox::Show(NULL
,_T("Edit Note Fail"), _T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
2753 CFile::Remove(tempfile
);
2758 int CAppUtils::GetMsysgitVersion(CString
*versionstr
)
2761 CString progressarg
;
2764 CRegDWORD regTime
= CRegDWORD(_T("Software\\TortoiseGit\\git_file_time"));
2765 CRegDWORD regVersion
= CRegDWORD(_T("Software\\TortoiseGit\\git_cached_version"));
2767 CString gitpath
= CGit::ms_LastMsysGitDir
+_T("\\git.exe");
2770 if(!g_Git
.GetFileModifyTime(gitpath
, &time
) && !versionstr
)
2772 if((DWORD
)time
== regTime
)
2779 version
= *versionstr
;
2783 cmd
= _T("git.exe --version");
2784 if(g_Git
.Run(cmd
, &version
, &err
, CP_ACP
))
2786 CMessageBox::Show(NULL
, _T("git have not installed (") + err
+ _T(")"), _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
2794 CString str
=version
.Tokenize(_T("."),start
);
2795 int space
= str
.ReverseFind(_T(' '));
2796 str
=str
.Mid(space
+1,start
);
2800 version
= version
.Mid(start
);
2802 str
= version
.Tokenize(_T("."),start
);
2804 ver
|= (_ttol(str
)&0xFF)<<16;
2806 str
= version
.Tokenize(_T("."),start
);
2807 ver
|= (_ttol(str
)&0xFF)<<8;
2809 str
= version
.Tokenize(_T("."),start
);
2810 ver
|= (_ttol(str
)&0xFF);
2812 regTime
= time
&0xFFFFFFFF;
2818 void CAppUtils::MarkWindowAsUnpinnable(HWND hWnd
)
2820 typedef HRESULT (WINAPI
*SHGPSFW
) (HWND hwnd
,REFIID riid
,void** ppv
);
2822 HMODULE hShell
= LoadLibrary(_T("Shell32.dll"));
2825 SHGPSFW pfnSHGPSFW
= (SHGPSFW
)::GetProcAddress(hShell
, "SHGetPropertyStoreForWindow");
2827 IPropertyStore
*pps
;
2828 HRESULT hr
= pfnSHGPSFW(hWnd
, IID_PPV_ARGS(&pps
));
2829 if (SUCCEEDED(hr
)) {
2832 var
.boolVal
= VARIANT_TRUE
;
2833 hr
= pps
->SetValue(PKEY_AppUserModel_PreventPinning
, var
);
2837 FreeLibrary(hShell
);
2841 void CAppUtils::SetWindowTitle(HWND hWnd
, const CString
& urlorpath
, const CString
& dialogname
)
2843 ASSERT(dialogname
.GetLength() < 70);
2844 ASSERT(urlorpath
.GetLength() < MAX_PATH
);
2845 WCHAR pathbuf
[MAX_PATH
] = {0};
2847 PathCompactPathEx(pathbuf
, urlorpath
, 70 - dialogname
.GetLength(), 0);
2849 wcscat_s(pathbuf
, L
" - ");
2850 wcscat_s(pathbuf
, dialogname
);
2851 wcscat_s(pathbuf
, L
" - ");
2852 wcscat_s(pathbuf
, CString(MAKEINTRESOURCE(IDS_APPNAME
)));
2853 SetWindowText(hWnd
, pathbuf
);