Support Git svn-clone at clone dialog.
[TortoiseGit.git] / src / TortoiseProc / AppUtils.cpp
blobfe45e2f5a84b8a22f01595b9b81e3b1ebbea1a39
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "resource.h"
21 #include "TortoiseProc.h"
22 #include "PathUtils.h"
23 #include "AppUtils.h"
24 //#include "GitProperties.h"
25 #include "StringUtils.h"
26 #include "MessageBox.h"
27 #include "Registry.h"
28 #include "TGitPath.h"
29 #include "Git.h"
30 //#include "RepositoryBrowser.h"
31 //#include "BrowseFolder.h"
32 #include "UnicodeUtils.h"
33 #include "ExportDlg.h"
34 #include "ProgressDlg.h"
35 #include "GitAdminDir.h"
36 #include "ProgressDlg.h"
37 #include "BrowseFolder.h"
38 #include "DirFileEnum.h"
39 #include "MessageBox.h"
40 #include "GitStatus.h"
41 #include "CreateBranchTagDlg.h"
42 #include "GitSwitchDlg.h"
43 #include "ResetDlg.h"
44 #include "DeleteConflictDlg.h"
45 #include "ChangedDlg.h"
46 #include "SendMailDlg.h"
47 #include "SVNProgressDlg.h"
48 #include "PushDlg.h"
50 CAppUtils::CAppUtils(void)
54 CAppUtils::~CAppUtils(void)
58 int CAppUtils::StashApply(CString ref)
60 CString cmd,out;
61 cmd=_T("git.exe stash apply ");
62 cmd+=ref;
64 if(g_Git.Run(cmd,&out,CP_ACP))
66 CMessageBox::Show(NULL,CString(_T("<ct=0x0000FF>Stash Apply Fail!!!</ct>\n"))+out,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
68 }else
70 if(CMessageBox::Show(NULL,CString(_T("<ct=0xff0000>Stash Apply Success</ct>\nDo you want to show change?"))
71 ,_T("TortoiseGit"),MB_YESNO|MB_ICONINFORMATION) == IDYES)
73 CChangedDlg dlg;
74 dlg.m_pathList.AddPath(CTGitPath());
75 dlg.DoModal();
77 return 0;
79 return -1;
81 bool CAppUtils::GetMimeType(const CTGitPath& file, CString& mimetype)
83 #if 0
84 GitProperties props(file, GitRev::REV_WC, false);
85 for (int i = 0; i < props.GetCount(); ++i)
87 if (props.GetItemName(i).compare(_T("svn:mime-type"))==0)
89 mimetype = props.GetItemValue(i).c_str();
90 return true;
93 #endif
94 return false;
97 BOOL CAppUtils::StartExtMerge(
98 const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,
99 const CString& basename, const CString& theirname, const CString& yourname, const CString& mergedname, bool bReadOnly)
102 CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));
103 CString ext = mergedfile.GetFileExtension();
104 CString com = regCom;
105 bool bInternal = false;
107 CString mimetype;
108 if (ext != "")
110 // is there an extension specific merge tool?
111 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());
112 if (CString(mergetool) != "")
114 com = mergetool;
117 if (GetMimeType(yourfile, mimetype) || GetMimeType(theirfile, mimetype) || GetMimeType(basefile, mimetype))
119 // is there a mime type specific merge tool?
120 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + mimetype);
121 if (CString(mergetool) != "")
123 com = mergetool;
127 if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))
129 // use TortoiseMerge
130 bInternal = true;
131 CRegString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE);
132 com = tortoiseMergePath;
133 if (com.IsEmpty())
135 com = CPathUtils::GetAppDirectory();
136 com += _T("TortoiseMerge.exe");
138 com = _T("\"") + com + _T("\"");
139 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");
140 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");
142 // check if the params are set. If not, just add the files to the command line
143 if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))
145 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");
146 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");
147 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");
148 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");
150 if (basefile.IsEmpty())
152 com.Replace(_T("/base:%base"), _T(""));
153 com.Replace(_T("%base"), _T(""));
155 else
156 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));
157 if (theirfile.IsEmpty())
159 com.Replace(_T("/theirs:%theirs"), _T(""));
160 com.Replace(_T("%theirs"), _T(""));
162 else
163 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));
164 if (yourfile.IsEmpty())
166 com.Replace(_T("/mine:%mine"), _T(""));
167 com.Replace(_T("%mine"), _T(""));
169 else
170 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));
171 if (mergedfile.IsEmpty())
173 com.Replace(_T("/merged:%merged"), _T(""));
174 com.Replace(_T("%merged"), _T(""));
176 else
177 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));
178 if (basename.IsEmpty())
180 if (basefile.IsEmpty())
182 com.Replace(_T("/basename:%bname"), _T(""));
183 com.Replace(_T("%bname"), _T(""));
185 else
187 com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));
190 else
191 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));
192 if (theirname.IsEmpty())
194 if (theirfile.IsEmpty())
196 com.Replace(_T("/theirsname:%tname"), _T(""));
197 com.Replace(_T("%tname"), _T(""));
199 else
201 com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));
204 else
205 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));
206 if (yourname.IsEmpty())
208 if (yourfile.IsEmpty())
210 com.Replace(_T("/minename:%yname"), _T(""));
211 com.Replace(_T("%yname"), _T(""));
213 else
215 com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));
218 else
219 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));
220 if (mergedname.IsEmpty())
222 if (mergedfile.IsEmpty())
224 com.Replace(_T("/mergedname:%mname"), _T(""));
225 com.Replace(_T("%mname"), _T(""));
227 else
229 com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));
232 else
233 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));
235 if ((bReadOnly)&&(bInternal))
236 com += _T(" /readonly");
238 if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))
240 return FALSE;
243 return TRUE;
246 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)
248 CString viewer;
249 // use TortoiseMerge
250 viewer = CPathUtils::GetAppDirectory();
251 viewer += _T("TortoiseMerge.exe");
253 viewer = _T("\"") + viewer + _T("\"");
254 viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");
255 viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");
256 if (bReversed)
257 viewer += _T(" /reversedpatch");
258 if (!sOriginalDescription.IsEmpty())
259 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");
260 if (!sPatchedDescription.IsEmpty())
261 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");
262 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
264 return FALSE;
266 return TRUE;
269 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)
271 // Is there a mime type specific diff tool?
272 CString mimetype;
273 if (GetMimeType(file1, mimetype) || GetMimeType(file2, mimetype))
275 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + mimetype);
276 if (!difftool.IsEmpty())
277 return difftool;
280 // Is there an extension specific diff tool?
281 CString ext = file2.GetFileExtension().MakeLower();
282 if (!ext.IsEmpty())
284 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);
285 if (!difftool.IsEmpty())
286 return difftool;
287 // Maybe we should use TortoiseIDiff?
288 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||
289 (ext == _T(".bmp")) || (ext == _T(".gif")) ||
290 (ext == _T(".png")) || (ext == _T(".ico")) ||
291 (ext == _T(".dib")) || (ext == _T(".emf")))
293 return
294 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseIDiff.exe") + _T("\"") +
295 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname");
299 // Finally, pick a generic external diff tool
300 CString difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));
301 return difftool;
304 bool CAppUtils::StartExtDiff(
305 const CString& file1, const CString& file2,
306 const CString& sName1, const CString& sName2,
307 const DiffFlags& flags)
309 CString viewer;
311 CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);
312 if (!flags.bBlame || !(DWORD)blamediff)
314 viewer = PickDiffTool(file1, file2);
315 // If registry entry for a diff program is commented out, use TortoiseMerge.
316 bool bCommentedOut = viewer.Left(1) == _T("#");
317 if (flags.bAlternativeTool)
319 // Invert external vs. internal diff tool selection.
320 if (bCommentedOut)
321 viewer.Delete(0); // uncomment
322 else
323 viewer = "";
325 else if (bCommentedOut)
326 viewer = "";
329 bool bInternal = viewer.IsEmpty();
330 if (bInternal)
332 viewer =
333 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseMerge.exe") + _T("\"") +
334 _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
335 if (flags.bBlame)
336 viewer += _T(" /blame");
338 // check if the params are set. If not, just add the files to the command line
339 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
341 viewer += _T(" \"")+file1+_T("\"");
342 viewer += _T(" \"")+file2+_T("\"");
344 if (viewer.Find(_T("%base")) >= 0)
346 viewer.Replace(_T("%base"), _T("\"")+file1+_T("\""));
348 if (viewer.Find(_T("%mine")) >= 0)
350 viewer.Replace(_T("%mine"), _T("\"")+file2+_T("\""));
353 if (sName1.IsEmpty())
354 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));
355 else
356 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
358 if (sName2.IsEmpty())
359 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));
360 else
361 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
363 if (flags.bReadOnly && bInternal)
364 viewer += _T(" /readonly");
366 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);
369 BOOL CAppUtils::StartExtDiffProps(const CTGitPath& file1, const CTGitPath& file2, const CString& sName1, const CString& sName2, BOOL bWait, BOOL bReadOnly)
371 CRegString diffpropsexe(_T("Software\\TortoiseGit\\DiffProps"));
372 CString viewer = diffpropsexe;
373 bool bInternal = false;
374 if (viewer.IsEmpty()||(viewer.Left(1).Compare(_T("#"))==0))
376 //no registry entry (or commented out) for a diff program
377 //use TortoiseMerge
378 bInternal = true;
379 viewer = CPathUtils::GetAppDirectory();
380 viewer += _T("TortoiseMerge.exe");
381 viewer = _T("\"") + viewer + _T("\"");
382 viewer = viewer + _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");
384 // check if the params are set. If not, just add the files to the command line
385 if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))
387 viewer += _T(" \"")+file1.GetWinPathString()+_T("\"");
388 viewer += _T(" \"")+file2.GetWinPathString()+_T("\"");
390 if (viewer.Find(_T("%base")) >= 0)
392 viewer.Replace(_T("%base"), _T("\"")+file1.GetWinPathString()+_T("\""));
394 if (viewer.Find(_T("%mine")) >= 0)
396 viewer.Replace(_T("%mine"), _T("\"")+file2.GetWinPathString()+_T("\""));
399 if (sName1.IsEmpty())
400 viewer.Replace(_T("%bname"), _T("\"") + file1.GetUIFileOrDirectoryName() + _T("\""));
401 else
402 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));
404 if (sName2.IsEmpty())
405 viewer.Replace(_T("%yname"), _T("\"") + file2.GetUIFileOrDirectoryName() + _T("\""));
406 else
407 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));
409 if ((bReadOnly)&&(bInternal))
410 viewer += _T(" /readonly");
412 if(!LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, !!bWait))
414 return FALSE;
416 return TRUE;
419 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)
421 CString viewer;
422 CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));
423 viewer = v;
424 if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))
426 // use TortoiseUDiff
427 viewer = CPathUtils::GetAppDirectory();
428 viewer += _T("TortoiseUDiff.exe");
429 // enquote the path to TortoiseUDiff
430 viewer = _T("\"") + viewer + _T("\"");
431 // add the params
432 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");
435 if (viewer.Find(_T("%1"))>=0)
437 if (viewer.Find(_T("\"%1\"")) >= 0)
438 viewer.Replace(_T("%1"), patchfile);
439 else
440 viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));
442 else
443 viewer += _T(" \"") + patchfile + _T("\"");
444 if (viewer.Find(_T("%title")) >= 0)
446 viewer.Replace(_T("%title"), title);
449 if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))
451 return FALSE;
453 return TRUE;
456 BOOL CAppUtils::StartTextViewer(CString file)
458 CString viewer;
459 CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);
460 viewer = txt;
461 viewer = viewer + _T("\\Shell\\Open\\Command\\");
462 CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);
463 viewer = txtexe;
465 DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);
466 TCHAR * buf = new TCHAR[len+1];
467 ExpandEnvironmentStrings(viewer, buf, len);
468 viewer = buf;
469 delete [] buf;
470 len = ExpandEnvironmentStrings(file, NULL, 0);
471 buf = new TCHAR[len+1];
472 ExpandEnvironmentStrings(file, buf, len);
473 file = buf;
474 delete [] buf;
475 file = _T("\"")+file+_T("\"");
476 if (viewer.IsEmpty())
478 OPENFILENAME ofn = {0}; // common dialog box structure
479 TCHAR szFile[MAX_PATH] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH.
480 // Initialize OPENFILENAME
481 ofn.lStructSize = sizeof(OPENFILENAME);
482 ofn.hwndOwner = NULL;
483 ofn.lpstrFile = szFile;
484 ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
485 CString sFilter;
486 sFilter.LoadString(IDS_PROGRAMSFILEFILTER);
487 TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];
488 _tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
489 // Replace '|' delimiters with '\0's
490 TCHAR *ptr = pszFilters + _tcslen(pszFilters); //set ptr at the NULL
491 while (ptr != pszFilters)
493 if (*ptr == '|')
494 *ptr = '\0';
495 ptr--;
497 ofn.lpstrFilter = pszFilters;
498 ofn.nFilterIndex = 1;
499 ofn.lpstrFileTitle = NULL;
500 ofn.nMaxFileTitle = 0;
501 ofn.lpstrInitialDir = NULL;
502 CString temp;
503 temp.LoadString(IDS_UTILS_SELECTTEXTVIEWER);
504 CStringUtils::RemoveAccelerators(temp);
505 ofn.lpstrTitle = temp;
506 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
508 // Display the Open dialog box.
510 if (GetOpenFileName(&ofn)==TRUE)
512 delete [] pszFilters;
513 viewer = CString(ofn.lpstrFile);
515 else
517 delete [] pszFilters;
518 return FALSE;
521 if (viewer.Find(_T("\"%1\"")) >= 0)
523 viewer.Replace(_T("\"%1\""), file);
525 else if (viewer.Find(_T("%1")) >= 0)
527 viewer.Replace(_T("%1"), file);
529 else
531 viewer += _T(" ");
532 viewer += file;
535 if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))
537 return FALSE;
539 return TRUE;
542 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)
544 DWORD length = 0;
545 HANDLE hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
546 if (hFile == INVALID_HANDLE_VALUE)
547 return TRUE;
548 length = ::GetFileSize(hFile, NULL);
549 ::CloseHandle(hFile);
550 if (length < 4)
551 return TRUE;
552 return FALSE;
556 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)
558 LOGFONT logFont;
559 HDC hScreenDC = ::GetDC(NULL);
560 logFont.lfHeight = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);
561 ::ReleaseDC(NULL, hScreenDC);
562 logFont.lfWidth = 0;
563 logFont.lfEscapement = 0;
564 logFont.lfOrientation = 0;
565 logFont.lfWeight = FW_NORMAL;
566 logFont.lfItalic = 0;
567 logFont.lfUnderline = 0;
568 logFont.lfStrikeOut = 0;
569 logFont.lfCharSet = DEFAULT_CHARSET;
570 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
571 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
572 logFont.lfQuality = DRAFT_QUALITY;
573 logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;
574 _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));
575 VERIFY(fontToCreate.CreateFontIndirect(&logFont));
578 bool CAppUtils::LaunchApplication(const CString& sCommandLine, UINT idErrMessageFormat, bool bWaitForStartup)
580 STARTUPINFO startup;
581 PROCESS_INFORMATION process;
582 memset(&startup, 0, sizeof(startup));
583 startup.cb = sizeof(startup);
584 memset(&process, 0, sizeof(process));
586 CString cleanCommandLine(sCommandLine);
588 if (CreateProcess(NULL, const_cast<TCHAR*>((LPCTSTR)cleanCommandLine), NULL, NULL, FALSE, 0, 0, sOrigCWD, &startup, &process)==0)
590 if(idErrMessageFormat != 0)
592 LPVOID lpMsgBuf;
593 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
594 FORMAT_MESSAGE_FROM_SYSTEM |
595 FORMAT_MESSAGE_IGNORE_INSERTS,
596 NULL,
597 GetLastError(),
598 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
599 (LPTSTR) &lpMsgBuf,
601 NULL
603 CString temp;
604 temp.Format(idErrMessageFormat, lpMsgBuf);
605 CMessageBox::Show(NULL, temp, _T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
606 LocalFree( lpMsgBuf );
608 return false;
611 if (bWaitForStartup)
613 WaitForInputIdle(process.hProcess, 10000);
616 CloseHandle(process.hThread);
617 CloseHandle(process.hProcess);
618 return true;
620 bool CAppUtils::LaunchPAgent(CString *keyfile,CString * pRemote)
622 CString key,remote;
623 CString cmd,out;
624 if( pRemote == NULL)
626 remote=_T("origin");
627 }else
629 remote=*pRemote;
631 if(keyfile == NULL)
633 cmd.Format(_T("git.exe config remote.%s.puttykeyfile"),remote);
634 g_Git.Run(cmd,&key,CP_ACP);
635 int start=0;
636 key = key.Tokenize(_T("\n"),start);
638 else
639 key=*keyfile;
641 if(key.IsEmpty())
642 return false;
644 CString proc=CPathUtils::GetAppDirectory();
645 proc += _T("pageant.exe \"");
646 proc += key;
647 proc += _T("\"");
649 return LaunchApplication(proc, IDS_ERR_PAGEANT, false);
651 bool CAppUtils::LaunchRemoteSetting()
653 CString proc=CPathUtils::GetAppDirectory();
654 proc += _T("TortoiseProc.exe /command:settings");
655 proc += _T(" /path:\"");
656 proc += g_Git.m_CurrentDir;
657 proc += _T("\" /page:remote");
658 return LaunchApplication(proc, IDS_ERR_EXTDIFFSTART, false);
661 * Launch the external blame viewer
663 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)
665 CString viewer = CPathUtils::GetAppDirectory();
666 viewer += _T("TortoiseGitBlame.exe");
667 viewer += _T(" \"") + sBlameFile + _T("\"");
668 //viewer += _T(" \"") + sLogFile + _T("\"");
669 //viewer += _T(" \"") + sOriginalFile + _T("\"");
670 if(!Rev.IsEmpty())
671 viewer += CString(_T(" /rev:"))+Rev;
672 viewer += _T(" ")+sParams;
674 return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, false);
677 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)
679 CString sText;
680 if (pWnd == NULL)
681 return false;
682 bool bStyled = false;
683 pWnd->GetWindowText(sText);
684 // the rich edit control doesn't count the CR char!
685 // to be exact: CRLF is treated as one char.
686 sText.Replace(_T("\r"), _T(""));
688 // style each line separately
689 int offset = 0;
690 int nNewlinePos;
693 nNewlinePos = sText.Find('\n', offset);
694 CString sLine = sText.Mid(offset);
695 if (nNewlinePos>=0)
696 sLine = sLine.Left(nNewlinePos-offset);
697 int start = 0;
698 int end = 0;
699 while (FindStyleChars(sLine, '*', start, end))
701 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
702 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
703 CHARFORMAT2 format;
704 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
705 format.cbSize = sizeof(CHARFORMAT2);
706 format.dwMask = CFM_BOLD;
707 format.dwEffects = CFE_BOLD;
708 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
709 bStyled = true;
710 start = end;
712 start = 0;
713 end = 0;
714 while (FindStyleChars(sLine, '^', start, end))
716 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
717 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
718 CHARFORMAT2 format;
719 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
720 format.cbSize = sizeof(CHARFORMAT2);
721 format.dwMask = CFM_ITALIC;
722 format.dwEffects = CFE_ITALIC;
723 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
724 bStyled = true;
725 start = end;
727 start = 0;
728 end = 0;
729 while (FindStyleChars(sLine, '_', start, end))
731 CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};
732 pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);
733 CHARFORMAT2 format;
734 SecureZeroMemory(&format, sizeof(CHARFORMAT2));
735 format.cbSize = sizeof(CHARFORMAT2);
736 format.dwMask = CFM_UNDERLINE;
737 format.dwEffects = CFE_UNDERLINE;
738 pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
739 bStyled = true;
740 start = end;
742 offset = nNewlinePos+1;
743 } while(nNewlinePos>=0);
744 return bStyled;
747 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)
749 int i=start;
750 bool bFoundMarker = false;
751 // find a starting marker
752 while (sText[i] != 0)
754 if (sText[i] == stylechar)
756 if (((i+1)<sText.GetLength())&&(IsCharAlphaNumeric(sText[i+1])) &&
757 (((i>0)&&(!IsCharAlphaNumeric(sText[i-1])))||(i==0)))
759 start = i+1;
760 i++;
761 bFoundMarker = true;
762 break;
765 i++;
767 if (!bFoundMarker)
768 return false;
769 // find ending marker
770 bFoundMarker = false;
771 while (sText[i] != 0)
773 if (sText[i] == stylechar)
775 if ((IsCharAlphaNumeric(sText[i-1])) &&
776 ((((i+1)<sText.GetLength())&&(!IsCharAlphaNumeric(sText[i+1])))||(i+1)==sText.GetLength()))
778 end = i;
779 i++;
780 bFoundMarker = true;
781 break;
784 i++;
786 return bFoundMarker;
789 bool CAppUtils::BrowseRepository(CHistoryCombo& combo, CWnd * pParent, GitRev& rev)
791 #if 0
792 CString strUrl;
793 combo.GetWindowText(strUrl);
794 strUrl.Replace('\\', '/');
795 strUrl.Replace(_T("%"), _T("%25"));
796 strUrl = CUnicodeUtils::GetUnicode(CPathUtils::PathEscape(CUnicodeUtils::GetUTF8(strUrl)));
797 if (strUrl.Left(7) == _T("file://"))
799 CString strFile(strUrl);
800 Git::UrlToPath(strFile);
802 Git svn;
803 if (svn.IsRepository(CTGitPath(strFile)))
805 // browse repository - show repository browser
806 Git::preparePath(strUrl);
807 CRepositoryBrowser browser(strUrl, rev, pParent);
808 if (browser.DoModal() == IDOK)
810 combo.SetCurSel(-1);
811 combo.SetWindowText(browser.GetPath());
812 rev = browser.GetRevision();
813 return true;
816 else
818 // browse local directories
819 CBrowseFolder folderBrowser;
820 folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
821 // remove the 'file:///' so the shell can recognize the local path
822 Git::UrlToPath(strUrl);
823 if (folderBrowser.Show(pParent->GetSafeHwnd(), strUrl) == CBrowseFolder::OK)
825 Git::PathToUrl(strUrl);
827 combo.SetCurSel(-1);
828 combo.SetWindowText(strUrl);
829 return true;
833 else if ((strUrl.Left(7) == _T("http://")
834 ||(strUrl.Left(8) == _T("https://"))
835 ||(strUrl.Left(6) == _T("svn://"))
836 ||(strUrl.Left(4) == _T("svn+"))) && strUrl.GetLength() > 6)
838 // browse repository - show repository browser
839 CRepositoryBrowser browser(strUrl, rev, pParent);
840 if (browser.DoModal() == IDOK)
842 combo.SetCurSel(-1);
843 combo.SetWindowText(browser.GetPath());
844 rev = browser.GetRevision();
845 return true;
848 else
850 // browse local directories
851 CBrowseFolder folderBrowser;
852 folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
853 if (folderBrowser.Show(pParent->GetSafeHwnd(), strUrl) == CBrowseFolder::OK)
855 Git::PathToUrl(strUrl);
857 combo.SetCurSel(-1);
858 combo.SetWindowText(strUrl);
859 return true;
862 #endif
863 return false;
866 bool CAppUtils::FileOpenSave(CString& path, int * filterindex, UINT title, UINT filter, bool bOpen, HWND hwndOwner)
868 OPENFILENAME ofn = {0}; // common dialog box structure
869 TCHAR szFile[MAX_PATH] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH.
870 ofn.lStructSize = sizeof(OPENFILENAME);
871 ofn.hwndOwner = hwndOwner;
872 _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path);
873 ofn.lpstrFile = szFile;
874 ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
875 CString sFilter;
876 TCHAR * pszFilters = NULL;
877 if (filter)
879 sFilter.LoadString(filter);
880 pszFilters = new TCHAR[sFilter.GetLength()+4];
881 _tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
882 // Replace '|' delimiters with '\0's
883 TCHAR *ptr = pszFilters + _tcslen(pszFilters); //set ptr at the NULL
884 while (ptr != pszFilters)
886 if (*ptr == '|')
887 *ptr = '\0';
888 ptr--;
890 ofn.lpstrFilter = pszFilters;
892 ofn.nFilterIndex = 1;
893 ofn.lpstrFileTitle = NULL;
894 ofn.nMaxFileTitle = 0;
895 ofn.lpstrInitialDir = NULL;
896 CString temp;
897 if (title)
899 temp.LoadString(title);
900 CStringUtils::RemoveAccelerators(temp);
902 ofn.lpstrTitle = temp;
903 if (bOpen)
904 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER;
905 else
906 ofn.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER;
909 // Display the Open dialog box.
910 bool bRet = false;
911 if (bOpen)
913 bRet = !!GetOpenFileName(&ofn);
915 else
917 bRet = !!GetSaveFileName(&ofn);
919 if (bRet)
921 if (pszFilters)
922 delete [] pszFilters;
923 path = CString(ofn.lpstrFile);
924 if (filterindex)
925 *filterindex = ofn.nFilterIndex;
926 return true;
928 if (pszFilters)
929 delete [] pszFilters;
930 return false;
933 bool CAppUtils::SetListCtrlBackgroundImage(HWND hListCtrl, UINT nID, int width /* = 128 */, int height /* = 128 */)
935 ListView_SetTextBkColor(hListCtrl, CLR_NONE);
936 COLORREF bkColor = ListView_GetBkColor(hListCtrl);
937 // create a bitmap from the icon
938 HICON hIcon = (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(nID), IMAGE_ICON, width, height, LR_DEFAULTCOLOR);
939 if (!hIcon)
940 return false;
942 RECT rect = {0};
943 rect.right = width;
944 rect.bottom = height;
945 HBITMAP bmp = NULL;
947 HWND desktop = ::GetDesktopWindow();
948 if (desktop)
950 HDC screen_dev = ::GetDC(desktop);
951 if (screen_dev)
953 // Create a compatible DC
954 HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
955 if (dst_hdc)
957 // Create a new bitmap of icon size
958 bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
959 if (bmp)
961 // Select it into the compatible DC
962 HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
963 // Fill the background of the compatible DC with the given color
964 ::SetBkColor(dst_hdc, bkColor);
965 ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
967 // Draw the icon into the compatible DC
968 ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);
969 ::SelectObject(dst_hdc, old_dst_bmp);
971 ::DeleteDC(dst_hdc);
974 ::ReleaseDC(desktop, screen_dev);
977 // Restore settings
978 DestroyIcon(hIcon);
980 if (bmp == NULL)
981 return false;
983 LVBKIMAGE lv;
984 lv.ulFlags = LVBKIF_TYPE_WATERMARK;
985 lv.hbm = bmp;
986 lv.xOffsetPercent = 100;
987 lv.yOffsetPercent = 100;
988 ListView_SetBkImage(hListCtrl, &lv);
989 return true;
992 CString CAppUtils::GetProjectNameFromURL(CString url)
994 CString name;
995 while (name.IsEmpty() || (name.CompareNoCase(_T("branches"))==0) ||
996 (name.CompareNoCase(_T("tags"))==0) ||
997 (name.CompareNoCase(_T("trunk"))==0))
999 name = url.Mid(url.ReverseFind('/')+1);
1000 url = url.Left(url.ReverseFind('/'));
1002 if ((name.Compare(_T("svn")) == 0)||(name.Compare(_T("svnroot")) == 0))
1004 // a name of svn or svnroot indicates that it's not really the project name. In that
1005 // case, we try the first part of the URL
1006 // of course, this won't work in all cases (but it works for Google project hosting)
1007 url.Replace(_T("http://"), _T(""));
1008 url.Replace(_T("https://"), _T(""));
1009 url.Replace(_T("svn://"), _T(""));
1010 url.Replace(_T("svn+ssh://"), _T(""));
1011 url.TrimLeft(_T("/"));
1012 name = url.Left(url.Find('.'));
1014 return name;
1017 bool CAppUtils::StartShowUnifiedDiff(HWND hWnd, const CTGitPath& url1, const git_revnum_t& rev1,
1018 const CTGitPath& url2, const git_revnum_t& rev2,
1019 //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
1020 bool bAlternateDiff /* = false */, bool bIgnoreAncestry /* = false */, bool /* blame = false */)
1023 CString tempfile=GetTempFile();
1024 CString cmd;
1025 if(rev1 == GitRev::GetWorkingCopy())
1027 cmd.Format(_T("git.exe diff --stat -p %s "),rev2);
1028 }else
1030 cmd.Format(_T("git.exe diff-tree -r -p --stat %s %s"),rev1,rev2);
1033 if( !url1.IsEmpty() )
1035 cmd+=_T(" \"");
1036 cmd+=url1.GetGitPathString();
1037 cmd+=_T("\" ");
1039 g_Git.RunLogFile(cmd,tempfile);
1040 CAppUtils::StartUnifiedDiffViewer(tempfile,rev1.Left(6)+_T(":")+rev2.Left(6));
1043 #if 0
1044 CString sCmd;
1045 sCmd.Format(_T("%s /command:showcompare /unified"),
1046 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));
1047 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
1048 if (rev1.IsValid())
1049 sCmd += _T(" /revision1:") + rev1.ToString();
1050 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
1051 if (rev2.IsValid())
1052 sCmd += _T(" /revision2:") + rev2.ToString();
1053 if (peg.IsValid())
1054 sCmd += _T(" /pegrevision:") + peg.ToString();
1055 if (headpeg.IsValid())
1056 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
1058 if (bAlternateDiff)
1059 sCmd += _T(" /alternatediff");
1061 if (bIgnoreAncestry)
1062 sCmd += _T(" /ignoreancestry");
1064 if (hWnd)
1066 sCmd += _T(" /hwnd:");
1067 TCHAR buf[30];
1068 _stprintf_s(buf, 30, _T("%d"), hWnd);
1069 sCmd += buf;
1072 return CAppUtils::LaunchApplication(sCmd, NULL, false);
1073 #endif
1074 return TRUE;
1077 bool CAppUtils::StartShowCompare(HWND hWnd, const CTGitPath& url1, const GitRev& rev1,
1078 const CTGitPath& url2, const GitRev& rev2,
1079 const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,
1080 bool bAlternateDiff /* = false */, bool bIgnoreAncestry /* = false */, bool blame /* = false */)
1082 #if 0
1083 CString sCmd;
1084 sCmd.Format(_T("%s /command:showcompare"),
1085 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));
1086 sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");
1087 if (rev1.IsValid())
1088 sCmd += _T(" /revision1:") + rev1.ToString();
1089 sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");
1090 if (rev2.IsValid())
1091 sCmd += _T(" /revision2:") + rev2.ToString();
1092 if (peg.IsValid())
1093 sCmd += _T(" /pegrevision:") + peg.ToString();
1094 if (headpeg.IsValid())
1095 sCmd += _T(" /headpegrevision:") + headpeg.ToString();
1096 if (bAlternateDiff)
1097 sCmd += _T(" /alternatediff");
1098 if (bIgnoreAncestry)
1099 sCmd += _T(" /ignoreancestry");
1100 if (blame)
1101 sCmd += _T(" /blame");
1103 if (hWnd)
1105 sCmd += _T(" /hwnd:");
1106 TCHAR buf[30];
1107 _stprintf_s(buf, 30, _T("%d"), hWnd);
1108 sCmd += buf;
1111 return CAppUtils::LaunchApplication(sCmd, NULL, false);
1112 #endif
1113 return true;
1116 bool CAppUtils::Export(CString *BashHash)
1118 bool bRet = false;
1120 // ask from where the export has to be done
1121 CExportDlg dlg;
1122 if(BashHash)
1123 dlg.m_Revision=*BashHash;
1125 if (dlg.DoModal() == IDOK)
1127 CString cmd;
1128 cmd.Format(_T("git.exe archive --format=zip --verbose %s"),
1129 dlg.m_VersionName);
1131 //g_Git.RunLogFile(cmd,dlg.m_strExportDirectory);
1132 CProgressDlg pro;
1133 pro.m_GitCmd=cmd;
1134 pro.m_LogFile=dlg.m_strExportDirectory;
1135 pro.DoModal();
1136 return TRUE;
1138 return bRet;
1141 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash)
1143 CCreateBranchTagDlg dlg;
1144 dlg.m_bIsTag=IsTag;
1145 if(CommitHash)
1146 dlg.m_Base = *CommitHash;
1148 if(dlg.DoModal()==IDOK)
1150 CString cmd;
1151 CString force;
1152 CString track;
1153 if(dlg.m_bTrack)
1154 track=_T(" --track ");
1156 if(dlg.m_bForce)
1157 force=_T(" -f ");
1159 if(IsTag)
1161 cmd.Format(_T("git.exe tag %s %s %s %s"),
1162 track,
1163 force,
1164 dlg.m_BranchTagName,
1165 dlg.m_VersionName
1169 }else
1171 cmd.Format(_T("git.exe branch %s %s %s %s"),
1172 track,
1173 force,
1174 dlg.m_BranchTagName,
1175 dlg.m_VersionName
1178 CString out;
1179 if(g_Git.Run(cmd,&out,CP_UTF8))
1181 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1183 if( !IsTag && dlg.m_bSwitch )
1185 // it is a new branch and the user has requested to switch to it
1186 cmd.Format(_T("git.exe checkout %s"), dlg.m_BranchTagName);
1187 g_Git.Run(cmd,&out,CP_UTF8);
1188 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1191 return TRUE;
1194 return FALSE;
1197 bool CAppUtils::Switch(CString *CommitHash)
1199 CGitSwitchDlg dlg;
1200 if(CommitHash)
1201 dlg.m_Base=*CommitHash;
1203 if (dlg.DoModal() == IDOK)
1205 CString cmd;
1206 CString track;
1207 CString base;
1208 CString force;
1209 CString branch;
1211 if(dlg.m_bBranch)
1212 branch.Format(_T("-b %s"),dlg.m_NewBranch);
1213 if(dlg.m_bForce)
1214 force=_T("-f");
1215 if(dlg.m_bTrack)
1216 track=_T("--track");
1218 cmd.Format(_T("git.exe checkout %s %s %s %s"),
1219 force,
1220 track,
1221 branch,
1222 dlg.m_VersionName);
1224 CProgressDlg progress;
1225 progress.m_GitCmd=cmd;
1226 if(progress.DoModal()==IDOK)
1227 return TRUE;
1230 return FALSE;
1233 bool CAppUtils::IgnoreFile(CTGitPathList &path,bool IsMask)
1235 CString ignorefile;
1236 ignorefile=g_Git.m_CurrentDir+_T("\\");
1238 if(IsMask)
1240 ignorefile+=path.GetCommonRoot().GetDirectory().GetWinPathString()+_T("\\.gitignore");
1242 }else
1244 ignorefile+=_T("\\.gitignore");
1247 CStdioFile file;
1248 if(!file.Open(ignorefile,CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate))
1250 CMessageBox::Show(NULL,ignorefile+_T(" Open Failure"),_T("TortoiseGit"),MB_OK);
1251 return FALSE;
1254 CString ignorelist;
1255 CString mask;
1258 //file.ReadString(ignorelist);
1259 file.SeekToEnd();
1260 for(int i=0;i<path.GetCount();i++)
1262 if(IsMask)
1264 mask=_T("*")+path[i].GetFileExtension();
1265 if(ignorelist.Find(mask)<0)
1266 ignorelist+=_T("\n")+mask;
1268 }else
1270 ignorelist+=_T("\n/")+path[i].GetGitPathString();
1273 file.WriteString(ignorelist);
1275 file.Close();
1277 }catch(...)
1279 file.Close();
1280 return FALSE;
1283 return TRUE;
1287 bool CAppUtils::GitReset(CString *CommitHash,int type)
1289 CResetDlg dlg;
1290 dlg.m_ResetType=type;
1291 if (dlg.DoModal() == IDOK)
1293 CString cmd;
1294 CString type;
1295 switch(dlg.m_ResetType)
1297 case 0:
1298 type=_T("--soft");
1299 break;
1300 case 1:
1301 type=_T("--mixed");
1302 break;
1303 case 2:
1304 type=_T("--hard");
1305 break;
1306 default:
1307 type=_T("--mixed");
1308 break;
1310 cmd.Format(_T("git.exe reset %s %s"),type, *CommitHash);
1312 CProgressDlg progress;
1313 progress.m_GitCmd=cmd;
1314 if(progress.DoModal()==IDOK)
1315 return TRUE;
1318 return FALSE;
1321 void CAppUtils::DescribeFile(bool mode, bool base,CString &descript)
1323 if(mode == FALSE)
1325 descript=_T("Deleted");
1326 return;
1328 if(base)
1330 descript=_T("Modified");
1331 return;
1333 descript=_T("Created");
1334 return;
1337 CString CAppUtils::GetMergeTempFile(CString type,CTGitPath &merge)
1339 CString file;
1340 file=g_Git.m_CurrentDir+_T("\\")+merge.GetDirectory().GetWinPathString()+_T("\\")+merge.GetFilename()+_T(".")+type+merge.GetFileExtension();
1342 return file;
1345 bool CAppUtils::ConflictEdit(CTGitPath &path,bool bAlternativeTool)
1347 bool bRet = false;
1349 CTGitPath merge=path;
1350 CTGitPath directory = merge.GetDirectory();
1354 // we have the conflicted file (%merged)
1355 // now look for the other required files
1356 //GitStatus stat;
1357 //stat.GetStatus(merge);
1358 //if (stat.status == NULL)
1359 // return false;
1361 BYTE_VECTOR vector;
1363 CString cmd;
1364 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),merge.GetGitPathString());
1366 if(g_Git.Run(cmd,&vector))
1368 return FALSE;
1371 CTGitPathList list;
1372 list.ParserFromLsFile(vector);
1374 if(list.GetCount() == 0)
1375 return FALSE;
1377 TCHAR szTempName[512];
1378 GetTempFileName(_T(""),_T(""),0,szTempName);
1379 CString temp(szTempName);
1380 temp=temp.Mid(1,temp.GetLength()-5);
1382 CTGitPath theirs;
1383 CTGitPath mine;
1384 CTGitPath base;
1387 mine.SetFromGit(GetMergeTempFile(_T("LOCAL"),merge));
1388 theirs.SetFromGit(GetMergeTempFile(_T("REMOTE"),merge));
1389 base.SetFromGit(GetMergeTempFile(_T("BASE"),merge));
1391 CString format;
1393 format=_T("git.exe cat-file blob \":%d:%s\"");
1394 CFile tempfile;
1395 //create a empty file, incase stage is not three
1396 tempfile.Open(mine.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1397 tempfile.Close();
1398 tempfile.Open(theirs.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1399 tempfile.Close();
1400 tempfile.Open(base.GetWinPathString(),CFile::modeCreate|CFile::modeReadWrite);
1401 tempfile.Close();
1403 bool b_base=false, b_local=false, b_remote=false;
1405 for(int i=0;i<list.GetCount();i++)
1407 CString cmd;
1408 CString outfile;
1409 cmd.Format(format,list[i].m_Stage,list[i].GetGitPathString());
1411 if( list[i].m_Stage == 1)
1413 b_base = true;
1414 outfile=base.GetWinPathString();
1416 if( list[i].m_Stage == 2 )
1418 b_local = true;
1419 outfile=mine.GetWinPathString();
1421 if( list[i].m_Stage == 3 )
1423 b_remote = true;
1424 outfile=theirs.GetWinPathString();
1426 g_Git.RunLogFile(cmd,outfile);
1429 if(b_local && b_remote )
1431 merge.SetFromWin(g_Git.m_CurrentDir+_T("\\")+merge.GetWinPathString());
1432 bRet = !!CAppUtils::StartExtMerge(base, theirs, mine, merge,_T("BASE"),_T("REMOTE"),_T("LOCAL"));
1434 }else
1436 CFile::Remove(mine.GetWinPathString());
1437 CFile::Remove(theirs.GetWinPathString());
1438 CFile::Remove(base.GetWinPathString());
1440 CDeleteConflictDlg dlg;
1441 DescribeFile(b_local, b_base,dlg.m_LocalStatus);
1442 DescribeFile(b_remote,b_base,dlg.m_RemoteStatus);
1443 dlg.m_bShowModifiedButton=b_base;
1444 dlg.m_File=merge.GetGitPathString();
1445 if(dlg.DoModal() == IDOK)
1447 CString cmd,out;
1448 if(dlg.m_bIsDelete)
1450 cmd.Format(_T("git.exe rm \"%s\""),merge.GetGitPathString());
1451 }else
1452 cmd.Format(_T("git.exe add \"%s\""),merge.GetGitPathString());
1454 if(g_Git.Run(cmd,&out,CP_ACP))
1456 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
1457 return FALSE;
1459 return TRUE;
1461 else
1462 return FALSE;
1468 #if 0
1470 CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1471 base, theirs, mine, merge);
1472 #endif
1473 #if 0
1474 if (stat.status->text_status == svn_wc_status_conflicted)
1476 // we have a text conflict, use our merge tool to resolve the conflict
1478 CTSVNPath theirs(directory);
1479 CTSVNPath mine(directory);
1480 CTSVNPath base(directory);
1481 bool bConflictData = false;
1483 if ((stat.status->entry)&&(stat.status->entry->conflict_new))
1485 theirs.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_new));
1486 bConflictData = true;
1488 if ((stat.status->entry)&&(stat.status->entry->conflict_old))
1490 base.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_old));
1491 bConflictData = true;
1493 if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
1495 mine.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->conflict_wrk));
1496 bConflictData = true;
1498 else
1500 mine = merge;
1502 if (bConflictData)
1503 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1504 base, theirs, mine, merge);
1507 if (stat.status->prop_status == svn_wc_status_conflicted)
1509 // we have a property conflict
1510 CTSVNPath prej(directory);
1511 if ((stat.status->entry)&&(stat.status->entry->prejfile))
1513 prej.AppendPathString(CUnicodeUtils::GetUnicode(stat.status->entry->prejfile));
1514 // there's a problem: the prej file contains a _description_ of the conflict, and
1515 // that description string might be translated. That means we have no way of parsing
1516 // the file to find out the conflicting values.
1517 // The only thing we can do: show a dialog with the conflict description, then
1518 // let the user either accept the existing property or open the property edit dialog
1519 // to manually change the properties and values. And a button to mark the conflict as
1520 // resolved.
1521 CEditPropConflictDlg dlg;
1522 dlg.SetPrejFile(prej);
1523 dlg.SetConflictedItem(merge);
1524 bRet = (dlg.DoModal() != IDCANCEL);
1528 if (stat.status->tree_conflict)
1530 // we have a tree conflict
1531 SVNInfo info;
1532 const SVNInfoData * pInfoData = info.GetFirstFileInfo(merge, SVNRev(), SVNRev());
1533 if (pInfoData)
1535 if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_text)
1537 CTSVNPath theirs(directory);
1538 CTSVNPath mine(directory);
1539 CTSVNPath base(directory);
1540 bool bConflictData = false;
1542 if (pInfoData->treeconflict_theirfile)
1544 theirs.AppendPathString(pInfoData->treeconflict_theirfile);
1545 bConflictData = true;
1547 if (pInfoData->treeconflict_basefile)
1549 base.AppendPathString(pInfoData->treeconflict_basefile);
1550 bConflictData = true;
1552 if (pInfoData->treeconflict_myfile)
1554 mine.AppendPathString(pInfoData->treeconflict_myfile);
1555 bConflictData = true;
1557 else
1559 mine = merge;
1561 if (bConflictData)
1562 bRet = !!CAppUtils::StartExtMerge(CAppUtils::MergeFlags().AlternativeTool(bAlternativeTool),
1563 base, theirs, mine, merge);
1565 else if (pInfoData->treeconflict_kind == svn_wc_conflict_kind_tree)
1567 CString sConflictAction;
1568 CString sConflictReason;
1569 CString sResolveTheirs;
1570 CString sResolveMine;
1571 CTSVNPath treeConflictPath = CTSVNPath(pInfoData->treeconflict_path);
1572 CString sItemName = treeConflictPath.GetUIFileOrDirectoryName();
1574 if (pInfoData->treeconflict_nodekind == svn_node_file)
1576 switch (pInfoData->treeconflict_operation)
1578 case svn_wc_operation_update:
1579 switch (pInfoData->treeconflict_action)
1581 case svn_wc_conflict_action_edit:
1582 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEEDIT, (LPCTSTR)sItemName);
1583 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1584 break;
1585 case svn_wc_conflict_action_add:
1586 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEADD, (LPCTSTR)sItemName);
1587 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1588 break;
1589 case svn_wc_conflict_action_delete:
1590 sConflictAction.Format(IDS_TREECONFLICT_FILEUPDATEDELETE, (LPCTSTR)sItemName);
1591 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1592 break;
1594 break;
1595 case svn_wc_operation_switch:
1596 switch (pInfoData->treeconflict_action)
1598 case svn_wc_conflict_action_edit:
1599 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHEDIT, (LPCTSTR)sItemName);
1600 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1601 break;
1602 case svn_wc_conflict_action_add:
1603 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHADD, (LPCTSTR)sItemName);
1604 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1605 break;
1606 case svn_wc_conflict_action_delete:
1607 sConflictAction.Format(IDS_TREECONFLICT_FILESWITCHDELETE, (LPCTSTR)sItemName);
1608 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1609 break;
1611 break;
1612 case svn_wc_operation_merge:
1613 switch (pInfoData->treeconflict_action)
1615 case svn_wc_conflict_action_edit:
1616 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEEDIT, (LPCTSTR)sItemName);
1617 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1618 break;
1619 case svn_wc_conflict_action_add:
1620 sResolveTheirs.Format(IDS_TREECONFLICT_FILEMERGEADD, (LPCTSTR)sItemName);
1621 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYFILE);
1622 break;
1623 case svn_wc_conflict_action_delete:
1624 sConflictAction.Format(IDS_TREECONFLICT_FILEMERGEDELETE, (LPCTSTR)sItemName);
1625 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1626 break;
1628 break;
1631 else if (pInfoData->treeconflict_nodekind == svn_node_dir)
1633 switch (pInfoData->treeconflict_operation)
1635 case svn_wc_operation_update:
1636 switch (pInfoData->treeconflict_action)
1638 case svn_wc_conflict_action_edit:
1639 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEEDIT, (LPCTSTR)sItemName);
1640 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1641 break;
1642 case svn_wc_conflict_action_add:
1643 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEADD, (LPCTSTR)sItemName);
1644 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1645 break;
1646 case svn_wc_conflict_action_delete:
1647 sConflictAction.Format(IDS_TREECONFLICT_DIRUPDATEDELETE, (LPCTSTR)sItemName);
1648 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1649 break;
1651 break;
1652 case svn_wc_operation_switch:
1653 switch (pInfoData->treeconflict_action)
1655 case svn_wc_conflict_action_edit:
1656 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHEDIT, (LPCTSTR)sItemName);
1657 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1658 break;
1659 case svn_wc_conflict_action_add:
1660 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHADD, (LPCTSTR)sItemName);
1661 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1662 break;
1663 case svn_wc_conflict_action_delete:
1664 sConflictAction.Format(IDS_TREECONFLICT_DIRSWITCHDELETE, (LPCTSTR)sItemName);
1665 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1666 break;
1668 break;
1669 case svn_wc_operation_merge:
1670 switch (pInfoData->treeconflict_action)
1672 case svn_wc_conflict_action_edit:
1673 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEEDIT, (LPCTSTR)sItemName);
1674 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1675 break;
1676 case svn_wc_conflict_action_add:
1677 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEADD, (LPCTSTR)sItemName);
1678 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_KEEPREPOSITORYDIR);
1679 break;
1680 case svn_wc_conflict_action_delete:
1681 sConflictAction.Format(IDS_TREECONFLICT_DIRMERGEDELETE, (LPCTSTR)sItemName);
1682 sResolveTheirs.LoadString(IDS_TREECONFLICT_RESOLVE_REMOVEDIR);
1683 break;
1685 break;
1689 UINT uReasonID = 0;
1690 switch (pInfoData->treeconflict_reason)
1692 case svn_wc_conflict_reason_edited:
1693 uReasonID = IDS_TREECONFLICT_REASON_EDITED;
1694 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1695 break;
1696 case svn_wc_conflict_reason_obstructed:
1697 uReasonID = IDS_TREECONFLICT_REASON_OBSTRUCTED;
1698 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1699 break;
1700 case svn_wc_conflict_reason_deleted:
1701 uReasonID = IDS_TREECONFLICT_REASON_DELETED;
1702 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1703 break;
1704 case svn_wc_conflict_reason_added:
1705 uReasonID = IDS_TREECONFLICT_REASON_ADDED;
1706 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1707 break;
1708 case svn_wc_conflict_reason_missing:
1709 uReasonID = IDS_TREECONFLICT_REASON_MISSING;
1710 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_REMOVEDIR : IDS_TREECONFLICT_RESOLVE_REMOVEFILE);
1711 break;
1712 case svn_wc_conflict_reason_unversioned:
1713 uReasonID = IDS_TREECONFLICT_REASON_UNVERSIONED;
1714 sResolveMine.LoadString(pInfoData->treeconflict_nodekind == svn_node_dir ? IDS_TREECONFLICT_RESOLVE_KEEPLOCALDIR : IDS_TREECONFLICT_RESOLVE_KEEPLOCALFILE);
1715 break;
1717 sConflictReason.Format(uReasonID, (LPCTSTR)sConflictAction);
1719 CTreeConflictEditorDlg dlg;
1720 dlg.SetConflictInfoText(sConflictReason);
1721 dlg.SetResolveTexts(sResolveTheirs, sResolveMine);
1722 dlg.SetPath(treeConflictPath);
1723 INT_PTR dlgRet = dlg.DoModal();
1724 bRet = (dlgRet != IDCANCEL);
1728 #endif
1729 return bRet;
1733 * FUNCTION : FormatDateAndTime
1734 * DESCRIPTION : Generates a displayable string from a CTime object in
1735 * system short or long format or as a relative value
1736 * cTime - the time
1737 * option - DATE_SHORTDATE or DATE_LONGDATE
1738 * bIncluedeTime - whether to show time as well as date
1739 * bRelative - if true then relative time is shown if reasonable
1740 * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format
1741 * rather than locale
1742 * RETURN : CString containing date/time
1744 CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/,
1745 bool bRelative /*=false*/)
1747 CString datetime;
1748 if ( bRelative )
1750 datetime = ToRelativeTimeString( cTime );
1752 else
1754 // should we use the locale settings for formatting the date/time?
1755 if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE))
1757 // yes
1758 SYSTEMTIME sysTime;
1759 cTime.GetAsSystemTime( sysTime );
1761 TCHAR buf[100];
1763 GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf,
1764 sizeof(buf)/sizeof(TCHAR)-1);
1765 datetime = buf;
1766 if ( bIncludeTime )
1768 datetime += _T(" ");
1769 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1);
1770 datetime += buf;
1773 else
1775 // no, so fixed format
1776 if ( bIncludeTime )
1778 datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S"));
1780 else
1782 datetime = cTime.Format(_T("%Y-%m-%d"));
1786 return datetime;
1790 * Converts a given time to a relative display string (relative to current time)
1791 * Given time must be in local timezone
1793 CString CAppUtils::ToRelativeTimeString(CTime time)
1795 CString answer;
1796 // convert to COleDateTime
1797 SYSTEMTIME sysTime;
1798 time.GetAsSystemTime( sysTime );
1799 COleDateTime oleTime( sysTime );
1800 answer = ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime());
1801 return answer;
1805 * Generates a display string showing the relative time between the two given times as COleDateTimes
1807 CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)
1809 CString answer;
1810 COleDateTimeSpan ts = RelativeTo - time;
1811 //years
1812 if(fabs(ts.GetTotalDays()) >= 3*365)
1814 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/365, IDS_YEAR_AGO, IDS_YEARS_AGO );
1816 //Months
1817 if(fabs(ts.GetTotalDays()) >= 60)
1819 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/30, IDS_MONTH_AGO, IDS_MONTHS_AGO );
1820 return answer;
1822 //Weeks
1823 if(fabs(ts.GetTotalDays()) >= 14)
1825 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/7, IDS_WEEK_AGO, IDS_WEEKS_AGO );
1826 return answer;
1828 //Days
1829 if(fabs(ts.GetTotalDays()) >= 2)
1831 answer = ExpandRelativeTime( (int)ts.GetTotalDays(), IDS_DAY_AGO, IDS_DAYS_AGO );
1832 return answer;
1834 //hours
1835 if(fabs(ts.GetTotalHours()) >= 2)
1837 answer = ExpandRelativeTime( (int)ts.GetTotalHours(), IDS_HOUR_AGO, IDS_HOURS_AGO );
1838 return answer;
1840 //minutes
1841 if(fabs(ts.GetTotalMinutes()) >= 2)
1843 answer = ExpandRelativeTime( (int)ts.GetTotalMinutes(), IDS_MINUTE_AGO, IDS_MINUTES_AGO );
1844 return answer;
1846 //seconds
1847 answer = ExpandRelativeTime( (int)ts.GetTotalSeconds(), IDS_SECOND_AGO, IDS_SECONDS_AGO );
1848 return answer;
1851 /**
1852 * Passed a value and two resource string ids
1853 * if count is 1 then FormatString is called with format_1 and the value
1854 * otherwise format_2 is used
1855 * the formatted string is returned
1857 CString CAppUtils::ExpandRelativeTime( int count, UINT format_1, UINT format_n )
1859 CString answer;
1860 if ( count == 1 )
1862 answer.FormatMessage( format_1, count );
1864 else
1866 answer.FormatMessage( format_n, count );
1868 return answer;
1871 bool CAppUtils::IsSSHPutty()
1873 CString sshclient=CRegString(_T("Software\\TortoiseGit\\SSH"));
1874 sshclient=sshclient.MakeLower();
1875 if(sshclient.Find(_T("plink.exe"),0)>=0)
1877 return true;
1879 return false;
1882 CString CAppUtils::GetClipboardLink()
1884 if (!OpenClipboard(NULL))
1885 return CString();
1887 CString sClipboardText;
1888 HGLOBAL hglb = GetClipboardData(CF_TEXT);
1889 if (hglb)
1891 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1892 sClipboardText = CString(lpstr);
1893 GlobalUnlock(hglb);
1895 hglb = GetClipboardData(CF_UNICODETEXT);
1896 if (hglb)
1898 LPCTSTR lpstr = (LPCTSTR)GlobalLock(hglb);
1899 sClipboardText = lpstr;
1900 GlobalUnlock(hglb);
1902 CloseClipboard();
1904 if(!sClipboardText.IsEmpty())
1906 if(sClipboardText[0] == _T('\"') && sClipboardText[sClipboardText.GetLength()-1] == _T('\"'))
1907 sClipboardText=sClipboardText.Mid(1,sClipboardText.GetLength()-2);
1909 if(sClipboardText.Find( _T("http://")) == 0)
1910 return sClipboardText;
1912 if(sClipboardText.Find( _T("https://")) == 0)
1913 return sClipboardText;
1915 if(sClipboardText.Find( _T("git://")) == 0)
1916 return sClipboardText;
1918 if(sClipboardText.Find( _T("ssh://")) == 0)
1919 return sClipboardText;
1921 if(sClipboardText.GetLength()>=2)
1922 if( sClipboardText[1] == _T(':') )
1923 if( (sClipboardText[0] >= 'A' && sClipboardText[0] <= 'Z')
1924 || (sClipboardText[0] >= 'a' && sClipboardText[0] <= 'z') )
1925 return sClipboardText;
1928 return CString(_T(""));
1931 CString CAppUtils::ChooseRepository(CString *path)
1933 CBrowseFolder browseFolder;
1934 browseFolder.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
1935 CString strCloneDirectory;
1936 if(path)
1937 strCloneDirectory=*path;
1939 CString title;
1940 title.LoadString(IDS_CHOOSE_REPOSITORY);
1942 browseFolder.SetInfo(title);
1944 if (browseFolder.Show(NULL, strCloneDirectory) == CBrowseFolder::OK)
1946 return strCloneDirectory;
1948 }else
1950 return CString();
1955 bool CAppUtils::SendPatchMail(CTGitPathList &list,bool autoclose)
1957 CSendMailDlg dlg;
1959 dlg.m_PathList = list;
1961 if(dlg.DoModal()==IDOK)
1963 if(dlg.m_PathList.GetCount() == 0)
1964 return FALSE;
1966 CGitProgressDlg progDlg;
1968 theApp.m_pMainWnd = &progDlg;
1969 progDlg.SetCommand(CGitProgressDlg::GitProgress_SendMail);
1971 progDlg.SetAutoClose(autoclose);
1973 progDlg.SetPathList(dlg.m_PathList);
1974 //ProjectProperties props;
1975 //props.ReadPropsPathList(dlg.m_pathList);
1976 //progDlg.SetProjectProperties(props);
1977 progDlg.SetItemCount(dlg.m_PathList.GetCount());
1979 DWORD flags =0;
1980 if(dlg.m_bAttachment)
1981 flags |= SENDMAIL_ATTACHMENT;
1982 if(dlg.m_bCombine)
1983 flags |= SENDMAIL_COMBINED;
1985 progDlg.SetSendMailOption(dlg.m_To,dlg.m_CC,dlg.m_Subject,flags);
1987 progDlg.DoModal();
1989 return true;
1991 return false;
1993 int CAppUtils::GetLogOutputEncode()
1995 CString cmd,output;
1996 int start=0;
1997 cmd=_T("git.exe config i18n.logOutputEncoding");
1998 if(g_Git.Run(cmd,&output,CP_ACP))
2000 cmd=_T("git.exe config i18n.commitencoding");
2001 if(g_Git.Run(cmd,&output,CP_ACP))
2002 return CP_UTF8;
2004 int start=0;
2005 output=output.Tokenize(_T("\n"),start);
2006 return CUnicodeUtils::GetCPCode(output);
2008 }else
2010 output=output.Tokenize(_T("\n"),start);
2011 return CUnicodeUtils::GetCPCode(output);
2014 int CAppUtils::SaveCommitUnicodeFile(CString &filename, CString &message)
2016 CFile file(filename,CFile::modeReadWrite|CFile::modeCreate );
2017 CString cmd,output;
2018 int cp=CP_UTF8;
2020 cmd=_T("git.exe config i18n.commitencoding");
2021 if(g_Git.Run(cmd,&output,CP_ACP))
2022 cp=CP_UTF8;
2024 int start=0;
2025 output=output.Tokenize(_T("\n"),start);
2026 cp=CUnicodeUtils::GetCPCode(output);
2028 int len=message.GetLength();
2030 char * buf;
2031 buf = new char[len*4 + 4];
2032 SecureZeroMemory(buf, (len*4 + 4));
2034 int lengthIncTerminator = WideCharToMultiByte(cp, 0, message, -1, buf, len*4, NULL, NULL);
2036 file.Write(buf,lengthIncTerminator-1);
2037 file.Close();
2038 delete buf;
2039 return 0;
2042 bool CAppUtils::Push()
2044 CPushDlg dlg;
2045 // dlg.m_Directory=this->orgCmdLinePath.GetWinPathString();
2046 if(dlg.DoModal()==IDOK)
2048 // CString dir=dlg.m_Directory;
2049 // CString url=dlg.m_URL;
2050 CString cmd;
2051 CString force;
2052 CString tags;
2053 CString thin;
2055 if(dlg.m_bAutoLoad)
2057 CAppUtils::LaunchPAgent(NULL,&dlg.m_URL);
2060 if(dlg.m_bPack)
2061 thin=_T("--thin");
2062 if(dlg.m_bTags)
2063 tags=_T("--tags");
2064 if(dlg.m_bForce)
2065 force=_T("--force");
2067 cmd.Format(_T("git.exe push %s %s %s \"%s\" %s"),
2068 thin,tags,force,
2069 dlg.m_URL,
2070 dlg.m_BranchSourceName);
2071 if (!dlg.m_BranchRemoteName.IsEmpty())
2073 cmd += _T(":") + dlg.m_BranchRemoteName;
2076 CProgressDlg progress;
2077 progress.m_GitCmd=cmd;
2078 if(progress.DoModal()==IDOK)
2079 return TRUE;
2082 return FALSE;
2085 bool CAppUtils::CreateMultipleDirectory(CString& szPath)
2087 CString strDir(szPath);
2088 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
2090 strDir.AppendChar(_T('\\'));
2092 std::vector<CString> vPath;
2093 CString strTemp;
2094 bool bSuccess = false;
2096 for (int i=0;i<strDir.GetLength();++i)
2098 if (strDir.GetAt(i) != _T('\\'))
2100 strTemp.AppendChar(strDir.GetAt(i));
2102 else
2104 vPath.push_back(strTemp);
2105 strTemp.AppendChar(_T('\\'));
2109 std::vector<CString>::const_iterator vIter;
2110 for (vIter = vPath.begin(); vIter != vPath.end(); vIter++)
2112 bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
2115 return bSuccess;