OSDN Git Service

Add Reset Command
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / AppUtils.cpp
1 // TortoiseGit - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "resource.h"\r
21 #include "TortoiseProc.h"\r
22 #include "PathUtils.h"\r
23 #include "AppUtils.h"\r
24 //#include "GitProperties.h"\r
25 #include "StringUtils.h"\r
26 #include "MessageBox.h"\r
27 #include "Registry.h"\r
28 #include "TGitPath.h"\r
29 #include "Git.h"\r
30 //#include "RepositoryBrowser.h"\r
31 //#include "BrowseFolder.h"\r
32 #include "UnicodeUtils.h"\r
33 #include "ExportDlg.h"\r
34 #include "ProgressDlg.h"\r
35 #include "GitAdminDir.h"\r
36 #include "ProgressDlg.h"\r
37 #include "BrowseFolder.h"\r
38 #include "DirFileEnum.h"\r
39 #include "MessageBox.h"\r
40 #include "GitStatus.h"\r
41 #include "CreateBranchTagDlg.h"\r
42 #include "GitSwitchDlg.h"\r
43 #include "ResetDlg.h"\r
44 \r
45 CAppUtils::CAppUtils(void)\r
46 {\r
47 }\r
48 \r
49 CAppUtils::~CAppUtils(void)\r
50 {\r
51 }\r
52 \r
53 bool CAppUtils::GetMimeType(const CTGitPath& file, CString& mimetype)\r
54 {\r
55 #if 0\r
56         GitProperties props(file, GitRev::REV_WC, false);\r
57         for (int i = 0; i < props.GetCount(); ++i)\r
58         {\r
59                 if (props.GetItemName(i).compare(_T("svn:mime-type"))==0)\r
60                 {\r
61                         mimetype = props.GetItemValue(i).c_str();\r
62                         return true;\r
63                 }\r
64         }\r
65 #endif\r
66         return false;\r
67 }\r
68 \r
69 BOOL CAppUtils::StartExtMerge(\r
70         const CTGitPath& basefile, const CTGitPath& theirfile, const CTGitPath& yourfile, const CTGitPath& mergedfile,\r
71         const CString& basename, const CString& theirname, const CString& yourname, const CString& mergedname, bool bReadOnly)\r
72 {\r
73 \r
74         CRegString regCom = CRegString(_T("Software\\TortoiseGit\\Merge"));\r
75         CString ext = mergedfile.GetFileExtension();\r
76         CString com = regCom;\r
77         bool bInternal = false;\r
78 \r
79         CString mimetype;\r
80         if (ext != "")\r
81         {\r
82                 // is there an extension specific merge tool?\r
83                 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + ext.MakeLower());\r
84                 if (CString(mergetool) != "")\r
85                 {\r
86                         com = mergetool;\r
87                 }\r
88         }\r
89         if (GetMimeType(yourfile, mimetype) || GetMimeType(theirfile, mimetype) || GetMimeType(basefile, mimetype))\r
90         {\r
91                 // is there a mime type specific merge tool?\r
92                 CRegString mergetool(_T("Software\\TortoiseGit\\MergeTools\\") + mimetype);\r
93                 if (CString(mergetool) != "")\r
94                 {\r
95                         com = mergetool;\r
96                 }\r
97         }\r
98         \r
99         if (com.IsEmpty()||(com.Left(1).Compare(_T("#"))==0))\r
100         {\r
101                 // use TortoiseMerge\r
102                 bInternal = true;\r
103                 CRegString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T(""), false, HKEY_LOCAL_MACHINE);\r
104                 com = tortoiseMergePath;\r
105                 if (com.IsEmpty())\r
106                 {\r
107                         com = CPathUtils::GetAppDirectory();\r
108                         com += _T("TortoiseMerge.exe");\r
109                 }\r
110                 com = _T("\"") + com + _T("\"");\r
111                 com = com + _T(" /base:%base /theirs:%theirs /mine:%mine /merged:%merged");\r
112                 com = com + _T(" /basename:%bname /theirsname:%tname /minename:%yname /mergedname:%mname");\r
113         }\r
114         // check if the params are set. If not, just add the files to the command line\r
115         if ((com.Find(_T("%merged"))<0)&&(com.Find(_T("%base"))<0)&&(com.Find(_T("%theirs"))<0)&&(com.Find(_T("%mine"))<0))\r
116         {\r
117                 com += _T(" \"")+basefile.GetWinPathString()+_T("\"");\r
118                 com += _T(" \"")+theirfile.GetWinPathString()+_T("\"");\r
119                 com += _T(" \"")+yourfile.GetWinPathString()+_T("\"");\r
120                 com += _T(" \"")+mergedfile.GetWinPathString()+_T("\"");\r
121         }\r
122         if (basefile.IsEmpty())\r
123         {\r
124                 com.Replace(_T("/base:%base"), _T(""));\r
125                 com.Replace(_T("%base"), _T(""));               \r
126         }\r
127         else\r
128                 com.Replace(_T("%base"), _T("\"") + basefile.GetWinPathString() + _T("\""));\r
129         if (theirfile.IsEmpty())\r
130         {\r
131                 com.Replace(_T("/theirs:%theirs"), _T(""));\r
132                 com.Replace(_T("%theirs"), _T(""));\r
133         }\r
134         else\r
135                 com.Replace(_T("%theirs"), _T("\"") + theirfile.GetWinPathString() + _T("\""));\r
136         if (yourfile.IsEmpty())\r
137         {\r
138                 com.Replace(_T("/mine:%mine"), _T(""));\r
139                 com.Replace(_T("%mine"), _T(""));\r
140         }\r
141         else\r
142                 com.Replace(_T("%mine"), _T("\"") + yourfile.GetWinPathString() + _T("\""));\r
143         if (mergedfile.IsEmpty())\r
144         {\r
145                 com.Replace(_T("/merged:%merged"), _T(""));\r
146                 com.Replace(_T("%merged"), _T(""));\r
147         }\r
148         else\r
149                 com.Replace(_T("%merged"), _T("\"") + mergedfile.GetWinPathString() + _T("\""));\r
150         if (basename.IsEmpty())\r
151         {\r
152                 if (basefile.IsEmpty())\r
153                 {\r
154                         com.Replace(_T("/basename:%bname"), _T(""));\r
155                         com.Replace(_T("%bname"), _T(""));\r
156                 }\r
157                 else\r
158                 {\r
159                         com.Replace(_T("%bname"), _T("\"") + basefile.GetUIFileOrDirectoryName() + _T("\""));\r
160                 }\r
161         }\r
162         else\r
163                 com.Replace(_T("%bname"), _T("\"") + basename + _T("\""));\r
164         if (theirname.IsEmpty())\r
165         {\r
166                 if (theirfile.IsEmpty())\r
167                 {\r
168                         com.Replace(_T("/theirsname:%tname"), _T(""));\r
169                         com.Replace(_T("%tname"), _T(""));\r
170                 }\r
171                 else\r
172                 {\r
173                         com.Replace(_T("%tname"), _T("\"") + theirfile.GetUIFileOrDirectoryName() + _T("\""));\r
174                 }\r
175         }\r
176         else\r
177                 com.Replace(_T("%tname"), _T("\"") + theirname + _T("\""));\r
178         if (yourname.IsEmpty())\r
179         {\r
180                 if (yourfile.IsEmpty())\r
181                 {\r
182                         com.Replace(_T("/minename:%yname"), _T(""));\r
183                         com.Replace(_T("%yname"), _T(""));\r
184                 }\r
185                 else\r
186                 {\r
187                         com.Replace(_T("%yname"), _T("\"") + yourfile.GetUIFileOrDirectoryName() + _T("\""));\r
188                 }\r
189         }\r
190         else\r
191                 com.Replace(_T("%yname"), _T("\"") + yourname + _T("\""));\r
192         if (mergedname.IsEmpty())\r
193         {\r
194                 if (mergedfile.IsEmpty())\r
195                 {\r
196                         com.Replace(_T("/mergedname:%mname"), _T(""));\r
197                         com.Replace(_T("%mname"), _T(""));\r
198                 }\r
199                 else\r
200                 {\r
201                         com.Replace(_T("%mname"), _T("\"") + mergedfile.GetUIFileOrDirectoryName() + _T("\""));\r
202                 }\r
203         }\r
204         else\r
205                 com.Replace(_T("%mname"), _T("\"") + mergedname + _T("\""));\r
206 \r
207         if ((bReadOnly)&&(bInternal))\r
208                 com += _T(" /readonly");\r
209 \r
210         if(!LaunchApplication(com, IDS_ERR_EXTMERGESTART, false))\r
211         {\r
212                 return FALSE;\r
213         }\r
214 \r
215         return TRUE;\r
216 }\r
217 \r
218 BOOL CAppUtils::StartExtPatch(const CTGitPath& patchfile, const CTGitPath& dir, const CString& sOriginalDescription, const CString& sPatchedDescription, BOOL bReversed, BOOL bWait)\r
219 {\r
220         CString viewer;\r
221         // use TortoiseMerge\r
222         viewer = CPathUtils::GetAppDirectory();\r
223         viewer += _T("TortoiseMerge.exe");\r
224 \r
225         viewer = _T("\"") + viewer + _T("\"");\r
226         viewer = viewer + _T(" /diff:\"") + patchfile.GetWinPathString() + _T("\"");\r
227         viewer = viewer + _T(" /patchpath:\"") + dir.GetWinPathString() + _T("\"");\r
228         if (bReversed)\r
229                 viewer += _T(" /reversedpatch");\r
230         if (!sOriginalDescription.IsEmpty())\r
231                 viewer = viewer + _T(" /patchoriginal:\"") + sOriginalDescription + _T("\"");\r
232         if (!sPatchedDescription.IsEmpty())\r
233                 viewer = viewer + _T(" /patchpatched:\"") + sPatchedDescription + _T("\"");\r
234         if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))\r
235         {\r
236                 return FALSE;\r
237         }\r
238         return TRUE;\r
239 }\r
240 \r
241 CString CAppUtils::PickDiffTool(const CTGitPath& file1, const CTGitPath& file2)\r
242 {\r
243         // Is there a mime type specific diff tool?\r
244         CString mimetype;\r
245         if (GetMimeType(file1, mimetype) ||  GetMimeType(file2, mimetype))\r
246         {\r
247                 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + mimetype);\r
248                 if (!difftool.IsEmpty())\r
249                         return difftool;\r
250         }\r
251         \r
252         // Is there an extension specific diff tool?\r
253         CString ext = file2.GetFileExtension().MakeLower();\r
254         if (!ext.IsEmpty())\r
255         {\r
256                 CString difftool = CRegString(_T("Software\\TortoiseGit\\DiffTools\\") + ext);\r
257                 if (!difftool.IsEmpty())\r
258                         return difftool;\r
259                 // Maybe we should use TortoiseIDiff?\r
260                 if ((ext == _T(".jpg")) || (ext == _T(".jpeg")) ||\r
261                         (ext == _T(".bmp")) || (ext == _T(".gif"))  ||\r
262                         (ext == _T(".png")) || (ext == _T(".ico"))  ||\r
263                         (ext == _T(".dib")) || (ext == _T(".emf")))\r
264                 {\r
265                         return\r
266                                 _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseIDiff.exe") + _T("\"") +\r
267                                 _T(" /left:%base /right:%mine /lefttitle:%bname /righttitle:%yname");\r
268                 }\r
269         }\r
270         \r
271         // Finally, pick a generic external diff tool\r
272         CString difftool = CRegString(_T("Software\\TortoiseGit\\Diff"));\r
273         return difftool;\r
274 }\r
275 \r
276 bool CAppUtils::StartExtDiff(\r
277         const CString& file1,  const CString& file2,\r
278         const CString& sName1, const CString& sName2, \r
279         const DiffFlags& flags)\r
280 {\r
281         CString viewer;\r
282 \r
283         CRegDWORD blamediff(_T("Software\\TortoiseGit\\DiffBlamesWithTortoiseMerge"), FALSE);\r
284         if (!flags.bBlame || !(DWORD)blamediff)\r
285         {\r
286                 viewer = PickDiffTool(file1, file2);\r
287                 // If registry entry for a diff program is commented out, use TortoiseMerge.\r
288                 bool bCommentedOut = viewer.Left(1) == _T("#");\r
289                 if (flags.bAlternativeTool)\r
290                 {\r
291                         // Invert external vs. internal diff tool selection.\r
292                         if (bCommentedOut)\r
293                                 viewer.Delete(0); // uncomment\r
294                         else\r
295                                 viewer = "";\r
296                 }\r
297                 else if (bCommentedOut)\r
298                         viewer = "";\r
299         }\r
300 \r
301         bool bInternal = viewer.IsEmpty();\r
302         if (bInternal)\r
303         {\r
304                 viewer =\r
305                         _T("\"") + CPathUtils::GetAppDirectory() + _T("TortoiseMerge.exe") + _T("\"") +\r
306                         _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");\r
307                 if (flags.bBlame)\r
308                         viewer += _T(" /blame");\r
309         }\r
310         // check if the params are set. If not, just add the files to the command line\r
311         if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))\r
312         {\r
313                 viewer += _T(" \"")+file1+_T("\"");\r
314                 viewer += _T(" \"")+file2+_T("\"");\r
315         }\r
316         if (viewer.Find(_T("%base")) >= 0)\r
317         {\r
318                 viewer.Replace(_T("%base"),  _T("\"")+file1+_T("\""));\r
319         }\r
320         if (viewer.Find(_T("%mine")) >= 0)\r
321         {\r
322                 viewer.Replace(_T("%mine"),  _T("\"")+file2+_T("\""));\r
323         }\r
324 \r
325         if (sName1.IsEmpty())\r
326                 viewer.Replace(_T("%bname"), _T("\"") + file1 + _T("\""));\r
327         else\r
328                 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));\r
329 \r
330         if (sName2.IsEmpty())\r
331                 viewer.Replace(_T("%yname"), _T("\"") + file2 + _T("\""));\r
332         else\r
333                 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));\r
334 \r
335         if (flags.bReadOnly && bInternal)\r
336                 viewer += _T(" /readonly");\r
337 \r
338         return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, flags.bWait);\r
339 }\r
340 \r
341 BOOL CAppUtils::StartExtDiffProps(const CTGitPath& file1, const CTGitPath& file2, const CString& sName1, const CString& sName2, BOOL bWait, BOOL bReadOnly)\r
342 {\r
343         CRegString diffpropsexe(_T("Software\\TortoiseGit\\DiffProps"));\r
344         CString viewer = diffpropsexe;\r
345         bool bInternal = false;\r
346         if (viewer.IsEmpty()||(viewer.Left(1).Compare(_T("#"))==0))\r
347         {\r
348                 //no registry entry (or commented out) for a diff program\r
349                 //use TortoiseMerge\r
350                 bInternal = true;\r
351                 viewer = CPathUtils::GetAppDirectory();\r
352                 viewer += _T("TortoiseMerge.exe");\r
353                 viewer = _T("\"") + viewer + _T("\"");\r
354                 viewer = viewer + _T(" /base:%base /mine:%mine /basename:%bname /minename:%yname");\r
355         }\r
356         // check if the params are set. If not, just add the files to the command line\r
357         if ((viewer.Find(_T("%base"))<0)&&(viewer.Find(_T("%mine"))<0))\r
358         {\r
359                 viewer += _T(" \"")+file1.GetWinPathString()+_T("\"");\r
360                 viewer += _T(" \"")+file2.GetWinPathString()+_T("\"");\r
361         }\r
362         if (viewer.Find(_T("%base")) >= 0)\r
363         {\r
364                 viewer.Replace(_T("%base"),  _T("\"")+file1.GetWinPathString()+_T("\""));\r
365         }\r
366         if (viewer.Find(_T("%mine")) >= 0)\r
367         {\r
368                 viewer.Replace(_T("%mine"),  _T("\"")+file2.GetWinPathString()+_T("\""));\r
369         }\r
370 \r
371         if (sName1.IsEmpty())\r
372                 viewer.Replace(_T("%bname"), _T("\"") + file1.GetUIFileOrDirectoryName() + _T("\""));\r
373         else\r
374                 viewer.Replace(_T("%bname"), _T("\"") + sName1 + _T("\""));\r
375 \r
376         if (sName2.IsEmpty())\r
377                 viewer.Replace(_T("%yname"), _T("\"") + file2.GetUIFileOrDirectoryName() + _T("\""));\r
378         else\r
379                 viewer.Replace(_T("%yname"), _T("\"") + sName2 + _T("\""));\r
380 \r
381         if ((bReadOnly)&&(bInternal))\r
382                 viewer += _T(" /readonly");\r
383 \r
384         if(!LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, !!bWait))\r
385         {\r
386                 return FALSE;\r
387         }\r
388         return TRUE;\r
389 }\r
390 \r
391 BOOL CAppUtils::StartUnifiedDiffViewer(const CString& patchfile, const CString& title, BOOL bWait)\r
392 {\r
393         CString viewer;\r
394         CRegString v = CRegString(_T("Software\\TortoiseGit\\DiffViewer"));\r
395         viewer = v;\r
396         if (viewer.IsEmpty() || (viewer.Left(1).Compare(_T("#"))==0))\r
397         {\r
398                 // use TortoiseUDiff\r
399                 viewer = CPathUtils::GetAppDirectory();\r
400                 viewer += _T("TortoiseUDiff.exe");\r
401                 // enquote the path to TortoiseUDiff\r
402                 viewer = _T("\"") + viewer + _T("\"");\r
403                 // add the params\r
404                 viewer = viewer + _T(" /patchfile:%1 /title:\"%title\"");\r
405 \r
406         }\r
407         if (viewer.Find(_T("%1"))>=0)\r
408         {\r
409                 if (viewer.Find(_T("\"%1\"")) >= 0)\r
410                         viewer.Replace(_T("%1"), patchfile);\r
411                 else\r
412                         viewer.Replace(_T("%1"), _T("\"") + patchfile + _T("\""));\r
413         }\r
414         else\r
415                 viewer += _T(" \"") + patchfile + _T("\"");\r
416         if (viewer.Find(_T("%title")) >= 0)\r
417         {\r
418                 viewer.Replace(_T("%title"), title);\r
419         }\r
420 \r
421         if(!LaunchApplication(viewer, IDS_ERR_DIFFVIEWSTART, !!bWait))\r
422         {\r
423                 return FALSE;\r
424         }\r
425         return TRUE;\r
426 }\r
427 \r
428 BOOL CAppUtils::StartTextViewer(CString file)\r
429 {\r
430         CString viewer;\r
431         CRegString txt = CRegString(_T(".txt\\"), _T(""), FALSE, HKEY_CLASSES_ROOT);\r
432         viewer = txt;\r
433         viewer = viewer + _T("\\Shell\\Open\\Command\\");\r
434         CRegString txtexe = CRegString(viewer, _T(""), FALSE, HKEY_CLASSES_ROOT);\r
435         viewer = txtexe;\r
436 \r
437         DWORD len = ExpandEnvironmentStrings(viewer, NULL, 0);\r
438         TCHAR * buf = new TCHAR[len+1];\r
439         ExpandEnvironmentStrings(viewer, buf, len);\r
440         viewer = buf;\r
441         delete [] buf;\r
442         len = ExpandEnvironmentStrings(file, NULL, 0);\r
443         buf = new TCHAR[len+1];\r
444         ExpandEnvironmentStrings(file, buf, len);\r
445         file = buf;\r
446         delete [] buf;\r
447         file = _T("\"")+file+_T("\"");\r
448         if (viewer.IsEmpty())\r
449         {\r
450                 OPENFILENAME ofn = {0};                         // common dialog box structure\r
451                 TCHAR szFile[MAX_PATH] = {0};           // buffer for file name. Explorer can't handle paths longer than MAX_PATH.\r
452                 // Initialize OPENFILENAME\r
453                 ofn.lStructSize = sizeof(OPENFILENAME);\r
454                 ofn.hwndOwner = NULL;\r
455                 ofn.lpstrFile = szFile;\r
456                 ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);\r
457                 CString sFilter;\r
458                 sFilter.LoadString(IDS_PROGRAMSFILEFILTER);\r
459                 TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];\r
460                 _tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);\r
461                 // Replace '|' delimiters with '\0's\r
462                 TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL\r
463                 while (ptr != pszFilters)\r
464                 {\r
465                         if (*ptr == '|')\r
466                                 *ptr = '\0';\r
467                         ptr--;\r
468                 }\r
469                 ofn.lpstrFilter = pszFilters;\r
470                 ofn.nFilterIndex = 1;\r
471                 ofn.lpstrFileTitle = NULL;\r
472                 ofn.nMaxFileTitle = 0;\r
473                 ofn.lpstrInitialDir = NULL;\r
474                 CString temp;\r
475                 temp.LoadString(IDS_UTILS_SELECTTEXTVIEWER);\r
476                 CStringUtils::RemoveAccelerators(temp);\r
477                 ofn.lpstrTitle = temp;\r
478                 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;\r
479 \r
480                 // Display the Open dialog box. \r
481 \r
482                 if (GetOpenFileName(&ofn)==TRUE)\r
483                 {\r
484                         delete [] pszFilters;\r
485                         viewer = CString(ofn.lpstrFile);\r
486                 }\r
487                 else\r
488                 {\r
489                         delete [] pszFilters;\r
490                         return FALSE;\r
491                 }\r
492         }\r
493         if (viewer.Find(_T("\"%1\"")) >= 0)\r
494         {\r
495                 viewer.Replace(_T("\"%1\""), file);\r
496         }\r
497         else if (viewer.Find(_T("%1")) >= 0)\r
498         {\r
499                 viewer.Replace(_T("%1"),  file);\r
500         }\r
501         else\r
502         {\r
503                 viewer += _T(" ");\r
504                 viewer += file;\r
505         }\r
506 \r
507         if(!LaunchApplication(viewer, IDS_ERR_TEXTVIEWSTART, false))\r
508         {\r
509                 return FALSE;\r
510         }\r
511         return TRUE;\r
512 }\r
513 \r
514 BOOL CAppUtils::CheckForEmptyDiff(const CTGitPath& sDiffPath)\r
515 {\r
516         DWORD length = 0;\r
517         HANDLE hFile = ::CreateFile(sDiffPath.GetWinPath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);\r
518         if (hFile == INVALID_HANDLE_VALUE)\r
519                 return TRUE;\r
520         length = ::GetFileSize(hFile, NULL);\r
521         ::CloseHandle(hFile);\r
522         if (length < 4)\r
523                 return TRUE;\r
524         return FALSE;\r
525 \r
526 }\r
527 \r
528 void CAppUtils::CreateFontForLogs(CFont& fontToCreate)\r
529 {\r
530         LOGFONT logFont;\r
531         HDC hScreenDC = ::GetDC(NULL);\r
532         logFont.lfHeight         = -MulDiv((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8), GetDeviceCaps(hScreenDC, LOGPIXELSY), 72);\r
533         ::ReleaseDC(NULL, hScreenDC);\r
534         logFont.lfWidth          = 0;\r
535         logFont.lfEscapement     = 0;\r
536         logFont.lfOrientation    = 0;\r
537         logFont.lfWeight         = FW_NORMAL;\r
538         logFont.lfItalic         = 0;\r
539         logFont.lfUnderline      = 0;\r
540         logFont.lfStrikeOut      = 0;\r
541         logFont.lfCharSet        = DEFAULT_CHARSET;\r
542         logFont.lfOutPrecision   = OUT_DEFAULT_PRECIS;\r
543         logFont.lfClipPrecision  = CLIP_DEFAULT_PRECIS;\r
544         logFont.lfQuality        = DRAFT_QUALITY;\r
545         logFont.lfPitchAndFamily = FF_DONTCARE | FIXED_PITCH;\r
546         _tcscpy_s(logFont.lfFaceName, 32, (LPCTSTR)(CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")));\r
547         VERIFY(fontToCreate.CreateFontIndirect(&logFont));\r
548 }\r
549 \r
550 bool CAppUtils::LaunchApplication(const CString& sCommandLine, UINT idErrMessageFormat, bool bWaitForStartup)\r
551 {\r
552         STARTUPINFO startup;\r
553         PROCESS_INFORMATION process;\r
554         memset(&startup, 0, sizeof(startup));\r
555         startup.cb = sizeof(startup);\r
556         memset(&process, 0, sizeof(process));\r
557 \r
558         CString cleanCommandLine(sCommandLine);\r
559 \r
560         if (CreateProcess(NULL, const_cast<TCHAR*>((LPCTSTR)cleanCommandLine), NULL, NULL, FALSE, 0, 0, sOrigCWD, &startup, &process)==0)\r
561         {\r
562                 if(idErrMessageFormat != 0)\r
563                 {\r
564                         LPVOID lpMsgBuf;\r
565                         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
566                                 FORMAT_MESSAGE_FROM_SYSTEM | \r
567                                 FORMAT_MESSAGE_IGNORE_INSERTS,\r
568                                 NULL,\r
569                                 GetLastError(),\r
570                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
571                                 (LPTSTR) &lpMsgBuf,\r
572                                 0,\r
573                                 NULL \r
574                                 );\r
575                         CString temp;\r
576                         temp.Format(idErrMessageFormat, lpMsgBuf);\r
577                         CMessageBox::Show(NULL, temp, _T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);\r
578                         LocalFree( lpMsgBuf );\r
579                 }\r
580                 return false;\r
581         }\r
582 \r
583         if (bWaitForStartup)\r
584         {\r
585                 WaitForInputIdle(process.hProcess, 10000);\r
586         }\r
587 \r
588         CloseHandle(process.hThread);\r
589         CloseHandle(process.hProcess);\r
590         return true;\r
591 }\r
592 \r
593 /**\r
594 * Launch the external blame viewer\r
595 */\r
596 bool CAppUtils::LaunchTortoiseBlame(const CString& sBlameFile,CString Rev,const CString& sParams)\r
597 {\r
598         CString viewer = CPathUtils::GetAppDirectory();\r
599         viewer += _T("TortoiseGitBlame.exe");\r
600         viewer += _T(" \"") + sBlameFile + _T("\"");\r
601         //viewer += _T(" \"") + sLogFile + _T("\"");\r
602         //viewer += _T(" \"") + sOriginalFile + _T("\"");\r
603         if(!Rev.IsEmpty())\r
604                 viewer += CString(_T(" /rev:"))+Rev;\r
605         viewer += _T(" ")+sParams;\r
606         \r
607         return LaunchApplication(viewer, IDS_ERR_EXTDIFFSTART, false);\r
608 }\r
609 \r
610 void CAppUtils::ResizeAllListCtrlCols(CListCtrl * pListCtrl)\r
611 {\r
612         int maxcol = ((CHeaderCtrl*)(pListCtrl->GetDlgItem(0)))->GetItemCount()-1;\r
613         int nItemCount = pListCtrl->GetItemCount();\r
614         TCHAR textbuf[MAX_PATH];\r
615         CHeaderCtrl * pHdrCtrl = (CHeaderCtrl*)(pListCtrl->GetDlgItem(0));\r
616         if (pHdrCtrl)\r
617         {\r
618                 for (int col = 0; col <= maxcol; col++)\r
619                 {\r
620                         HDITEM hdi = {0};\r
621                         hdi.mask = HDI_TEXT;\r
622                         hdi.pszText = textbuf;\r
623                         hdi.cchTextMax = sizeof(textbuf);\r
624                         pHdrCtrl->GetItem(col, &hdi);\r
625                         int cx = pListCtrl->GetStringWidth(hdi.pszText)+20; // 20 pixels for col separator and margin\r
626                         for (int index = 0; index<nItemCount; ++index)\r
627                         {\r
628                                 // get the width of the string and add 14 pixels for the column separator and margins\r
629                                 int linewidth = pListCtrl->GetStringWidth(pListCtrl->GetItemText(index, col)) + 14;\r
630                                 if (index == 0)\r
631                                 {\r
632                                         // add the image size\r
633                                         CImageList * pImgList = pListCtrl->GetImageList(LVSIL_SMALL);\r
634                                         if ((pImgList)&&(pImgList->GetImageCount()))\r
635                                         {\r
636                                                 IMAGEINFO imginfo;\r
637                                                 pImgList->GetImageInfo(0, &imginfo);\r
638                                                 linewidth += (imginfo.rcImage.right - imginfo.rcImage.left);\r
639                                                 linewidth += 3; // 3 pixels between icon and text\r
640                                         }\r
641                                 }\r
642                                 if (cx < linewidth)\r
643                                         cx = linewidth;\r
644                         }\r
645                         pListCtrl->SetColumnWidth(col, cx);\r
646 \r
647                 }\r
648         }\r
649 }\r
650 \r
651 bool CAppUtils::FormatTextInRichEditControl(CWnd * pWnd)\r
652 {\r
653         CString sText;\r
654         if (pWnd == NULL)\r
655                 return false;\r
656         bool bStyled = false;\r
657         pWnd->GetWindowText(sText);\r
658         // the rich edit control doesn't count the CR char!\r
659         // to be exact: CRLF is treated as one char.\r
660         sText.Replace(_T("\r"), _T(""));\r
661 \r
662         // style each line separately\r
663         int offset = 0;\r
664         int nNewlinePos;\r
665         do \r
666         {\r
667                 nNewlinePos = sText.Find('\n', offset);\r
668                 CString sLine = sText.Mid(offset);\r
669                 if (nNewlinePos>=0)\r
670                         sLine = sLine.Left(nNewlinePos-offset);\r
671                 int start = 0;\r
672                 int end = 0;\r
673                 while (FindStyleChars(sLine, '*', start, end))\r
674                 {\r
675                         CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};\r
676                         pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);\r
677                         CHARFORMAT2 format;\r
678                         SecureZeroMemory(&format, sizeof(CHARFORMAT2));\r
679                         format.cbSize = sizeof(CHARFORMAT2);\r
680                         format.dwMask = CFM_BOLD;\r
681                         format.dwEffects = CFE_BOLD;\r
682                         pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);\r
683                         bStyled = true;\r
684                         start = end;\r
685                 }\r
686                 start = 0;\r
687                 end = 0;\r
688                 while (FindStyleChars(sLine, '^', start, end))\r
689                 {\r
690                         CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};\r
691                         pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);\r
692                         CHARFORMAT2 format;\r
693                         SecureZeroMemory(&format, sizeof(CHARFORMAT2));\r
694                         format.cbSize = sizeof(CHARFORMAT2);\r
695                         format.dwMask = CFM_ITALIC;\r
696                         format.dwEffects = CFE_ITALIC;\r
697                         pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);\r
698                         bStyled = true;\r
699                         start = end;\r
700                 }\r
701                 start = 0;\r
702                 end = 0;\r
703                 while (FindStyleChars(sLine, '_', start, end))\r
704                 {\r
705                         CHARRANGE range = {(LONG)start+offset, (LONG)end+offset};\r
706                         pWnd->SendMessage(EM_EXSETSEL, NULL, (LPARAM)&range);\r
707                         CHARFORMAT2 format;\r
708                         SecureZeroMemory(&format, sizeof(CHARFORMAT2));\r
709                         format.cbSize = sizeof(CHARFORMAT2);\r
710                         format.dwMask = CFM_UNDERLINE;\r
711                         format.dwEffects = CFE_UNDERLINE;\r
712                         pWnd->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);\r
713                         bStyled = true;\r
714                         start = end;\r
715                 }\r
716                 offset = nNewlinePos+1;\r
717         } while(nNewlinePos>=0);\r
718         return bStyled; \r
719 }\r
720 \r
721 bool CAppUtils::FindStyleChars(const CString& sText, TCHAR stylechar, int& start, int& end)\r
722 {\r
723         int i=start;\r
724         bool bFoundMarker = false;\r
725         // find a starting marker\r
726         while (sText[i] != 0)\r
727         {\r
728                 if (sText[i] == stylechar)\r
729                 {\r
730                         if (((i+1)<sText.GetLength())&&(IsCharAlphaNumeric(sText[i+1])) &&\r
731                                 (((i>0)&&(!IsCharAlphaNumeric(sText[i-1])))||(i==0)))\r
732                         {\r
733                                 start = i+1;\r
734                                 i++;\r
735                                 bFoundMarker = true;\r
736                                 break;\r
737                         }\r
738                 }\r
739                 i++;\r
740         }\r
741         if (!bFoundMarker)\r
742                 return false;\r
743         // find ending marker\r
744         bFoundMarker = false;\r
745         while (sText[i] != 0)\r
746         {\r
747                 if (sText[i] == stylechar)\r
748                 {\r
749                         if ((IsCharAlphaNumeric(sText[i-1])) &&\r
750                                 ((((i+1)<sText.GetLength())&&(!IsCharAlphaNumeric(sText[i+1])))||(i+1)==sText.GetLength()))\r
751                         {\r
752                                 end = i;\r
753                                 i++;\r
754                                 bFoundMarker = true;\r
755                                 break;\r
756                         }\r
757                 }\r
758                 i++;\r
759         }\r
760         return bFoundMarker;\r
761 }\r
762 \r
763 bool CAppUtils::BrowseRepository(CHistoryCombo& combo, CWnd * pParent, GitRev& rev)\r
764 {\r
765 #if 0\r
766         CString strUrl;\r
767         combo.GetWindowText(strUrl);\r
768         strUrl.Replace('\\', '/');\r
769         strUrl.Replace(_T("%"), _T("%25"));\r
770         strUrl = CUnicodeUtils::GetUnicode(CPathUtils::PathEscape(CUnicodeUtils::GetUTF8(strUrl)));\r
771         if (strUrl.Left(7) == _T("file://"))\r
772         {\r
773                 CString strFile(strUrl);\r
774                 Git::UrlToPath(strFile);\r
775 \r
776                 Git svn;\r
777                 if (svn.IsRepository(CTGitPath(strFile)))\r
778                 {\r
779                         // browse repository - show repository browser\r
780                         Git::preparePath(strUrl);\r
781                         CRepositoryBrowser browser(strUrl, rev, pParent);\r
782                         if (browser.DoModal() == IDOK)\r
783                         {\r
784                                 combo.SetCurSel(-1);\r
785                                 combo.SetWindowText(browser.GetPath());\r
786                                 rev = browser.GetRevision();\r
787                                 return true;\r
788                         }\r
789                 }\r
790                 else\r
791                 {\r
792                         // browse local directories\r
793                         CBrowseFolder folderBrowser;\r
794                         folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;\r
795                         // remove the 'file:///' so the shell can recognize the local path\r
796                         Git::UrlToPath(strUrl);\r
797                         if (folderBrowser.Show(pParent->GetSafeHwnd(), strUrl) == CBrowseFolder::OK)\r
798                         {\r
799                                 Git::PathToUrl(strUrl);\r
800 \r
801                                 combo.SetCurSel(-1);\r
802                                 combo.SetWindowText(strUrl);\r
803                                 return true;\r
804                         }\r
805                 }\r
806         }\r
807         else if ((strUrl.Left(7) == _T("http://")\r
808                 ||(strUrl.Left(8) == _T("https://"))\r
809                 ||(strUrl.Left(6) == _T("svn://"))\r
810                 ||(strUrl.Left(4) == _T("svn+"))) && strUrl.GetLength() > 6)\r
811         {\r
812                 // browse repository - show repository browser\r
813                 CRepositoryBrowser browser(strUrl, rev, pParent);\r
814                 if (browser.DoModal() == IDOK)\r
815                 {\r
816                         combo.SetCurSel(-1);\r
817                         combo.SetWindowText(browser.GetPath());\r
818                         rev = browser.GetRevision();\r
819                         return true;\r
820                 }\r
821         }\r
822         else\r
823         {\r
824                 // browse local directories\r
825                 CBrowseFolder folderBrowser;\r
826                 folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;\r
827                 if (folderBrowser.Show(pParent->GetSafeHwnd(), strUrl) == CBrowseFolder::OK)\r
828                 {\r
829                         Git::PathToUrl(strUrl);\r
830 \r
831                         combo.SetCurSel(-1);\r
832                         combo.SetWindowText(strUrl);\r
833                         return true;\r
834                 }\r
835         }\r
836 #endif\r
837         return false;\r
838 }\r
839 \r
840 bool CAppUtils::FileOpenSave(CString& path, int * filterindex, UINT title, UINT filter, bool bOpen, HWND hwndOwner)\r
841 {\r
842         OPENFILENAME ofn = {0};                         // common dialog box structure\r
843         TCHAR szFile[MAX_PATH] = {0};           // buffer for file name. Explorer can't handle paths longer than MAX_PATH.\r
844         ofn.lStructSize = sizeof(OPENFILENAME);\r
845         ofn.hwndOwner = hwndOwner;\r
846         _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path);\r
847         ofn.lpstrFile = szFile;\r
848         ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);\r
849         CString sFilter;\r
850         TCHAR * pszFilters = NULL;\r
851         if (filter)\r
852         {\r
853                 sFilter.LoadString(filter);\r
854                 pszFilters = new TCHAR[sFilter.GetLength()+4];\r
855                 _tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);\r
856                 // Replace '|' delimiters with '\0's\r
857                 TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL\r
858                 while (ptr != pszFilters)\r
859                 {\r
860                         if (*ptr == '|')\r
861                                 *ptr = '\0';\r
862                         ptr--;\r
863                 }\r
864                 ofn.lpstrFilter = pszFilters;\r
865         }\r
866         ofn.nFilterIndex = 1;\r
867         ofn.lpstrFileTitle = NULL;\r
868         ofn.nMaxFileTitle = 0;\r
869         ofn.lpstrInitialDir = NULL;\r
870         CString temp;\r
871         if (title)\r
872         {\r
873                 temp.LoadString(title);\r
874                 CStringUtils::RemoveAccelerators(temp);\r
875         }\r
876         ofn.lpstrTitle = temp;\r
877         if (bOpen)\r
878                 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER;\r
879         else\r
880                 ofn.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER;\r
881 \r
882 \r
883         // Display the Open dialog box. \r
884         bool bRet = false;\r
885         if (bOpen)\r
886         {\r
887                 bRet = !!GetOpenFileName(&ofn);\r
888         }\r
889         else\r
890         {\r
891                 bRet = !!GetSaveFileName(&ofn);\r
892         }\r
893         if (bRet)\r
894         {\r
895                 if (pszFilters)\r
896                         delete [] pszFilters;\r
897                 path = CString(ofn.lpstrFile);\r
898                 if (filterindex)\r
899                         *filterindex = ofn.nFilterIndex;\r
900                 return true;\r
901         }\r
902         if (pszFilters)\r
903                 delete [] pszFilters;\r
904         return false;\r
905 }\r
906 \r
907 bool CAppUtils::SetListCtrlBackgroundImage(HWND hListCtrl, UINT nID, int width /* = 128 */, int height /* = 128 */)\r
908 {\r
909         ListView_SetTextBkColor(hListCtrl, CLR_NONE);\r
910         COLORREF bkColor = ListView_GetBkColor(hListCtrl);\r
911         // create a bitmap from the icon\r
912         HICON hIcon = (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(nID), IMAGE_ICON, width, height, LR_DEFAULTCOLOR);\r
913         if (!hIcon)\r
914                 return false;\r
915 \r
916         RECT rect = {0};\r
917         rect.right = width;\r
918         rect.bottom = height;\r
919         HBITMAP bmp = NULL;\r
920 \r
921         HWND desktop = ::GetDesktopWindow();\r
922         if (desktop)\r
923         {\r
924                 HDC screen_dev = ::GetDC(desktop);\r
925                 if (screen_dev)\r
926                 {\r
927                         // Create a compatible DC\r
928                         HDC dst_hdc = ::CreateCompatibleDC(screen_dev);\r
929                         if (dst_hdc)\r
930                         {\r
931                                 // Create a new bitmap of icon size\r
932                                 bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);\r
933                                 if (bmp)\r
934                                 {\r
935                                         // Select it into the compatible DC\r
936                                         HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);\r
937                                         // Fill the background of the compatible DC with the given color\r
938                                         ::SetBkColor(dst_hdc, bkColor);\r
939                                         ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);\r
940 \r
941                                         // Draw the icon into the compatible DC\r
942                                         ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);\r
943                                         ::SelectObject(dst_hdc, old_dst_bmp);\r
944                                 }\r
945                                 ::DeleteDC(dst_hdc);\r
946                         }\r
947                 }\r
948                 ::ReleaseDC(desktop, screen_dev); \r
949         }\r
950 \r
951         // Restore settings\r
952         DestroyIcon(hIcon);\r
953 \r
954         if (bmp == NULL)\r
955                 return false;\r
956 \r
957         LVBKIMAGE lv;\r
958         lv.ulFlags = LVBKIF_TYPE_WATERMARK;\r
959         lv.hbm = bmp;\r
960         lv.xOffsetPercent = 100;\r
961         lv.yOffsetPercent = 100;\r
962         ListView_SetBkImage(hListCtrl, &lv);\r
963         return true;\r
964 }\r
965 \r
966 CString CAppUtils::GetProjectNameFromURL(CString url)\r
967 {\r
968         CString name;\r
969         while (name.IsEmpty() || (name.CompareNoCase(_T("branches"))==0) ||\r
970                 (name.CompareNoCase(_T("tags"))==0) ||\r
971                 (name.CompareNoCase(_T("trunk"))==0))\r
972         {\r
973                 name = url.Mid(url.ReverseFind('/')+1);\r
974                 url = url.Left(url.ReverseFind('/'));\r
975         }\r
976         if ((name.Compare(_T("svn")) == 0)||(name.Compare(_T("svnroot")) == 0))\r
977         {\r
978                 // a name of svn or svnroot indicates that it's not really the project name. In that\r
979                 // case, we try the first part of the URL\r
980                 // of course, this won't work in all cases (but it works for Google project hosting)\r
981                 url.Replace(_T("http://"), _T(""));\r
982                 url.Replace(_T("https://"), _T(""));\r
983                 url.Replace(_T("svn://"), _T(""));\r
984                 url.Replace(_T("svn+ssh://"), _T(""));\r
985                 url.TrimLeft(_T("/"));\r
986                 name = url.Left(url.Find('.'));\r
987         }\r
988         return name;\r
989 }\r
990 \r
991 bool CAppUtils::StartShowUnifiedDiff(HWND hWnd, const CTGitPath& url1, const git_revnum_t& rev1, \r
992                                                                                                 const CTGitPath& url2, const git_revnum_t& rev2, \r
993                                                                          //const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */,  \r
994                                                                                                 bool bAlternateDiff /* = false */, bool bIgnoreAncestry /* = false */, bool /* blame = false */)\r
995 {\r
996 \r
997         CString tempfile=GetTempFile();\r
998         CString cmd;\r
999         if(rev1 == GitRev::GetWorkingCopy())\r
1000         {\r
1001                 cmd.Format(_T("git.exe diff --stat -p %s"),rev2);\r
1002         }else\r
1003         {       \r
1004                 cmd.Format(_T("git.exe diff-tree -r -p --stat %s %s"),rev1,rev2);\r
1005         }\r
1006         g_Git.RunLogFile(cmd,tempfile);\r
1007         CAppUtils::StartUnifiedDiffViewer(tempfile,rev1.Left(6)+_T(":")+rev2.Left(6));\r
1008 \r
1009 \r
1010 #if 0\r
1011         CString sCmd;\r
1012         sCmd.Format(_T("%s /command:showcompare /unified"),\r
1013                 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));\r
1014         sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");\r
1015         if (rev1.IsValid())\r
1016                 sCmd += _T(" /revision1:") + rev1.ToString();\r
1017         sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");\r
1018         if (rev2.IsValid())\r
1019                 sCmd += _T(" /revision2:") + rev2.ToString();\r
1020         if (peg.IsValid())\r
1021                 sCmd += _T(" /pegrevision:") + peg.ToString();\r
1022         if (headpeg.IsValid())\r
1023                 sCmd += _T(" /headpegrevision:") + headpeg.ToString();\r
1024 \r
1025         if (bAlternateDiff)\r
1026                 sCmd += _T(" /alternatediff");\r
1027 \r
1028         if (bIgnoreAncestry)\r
1029                 sCmd += _T(" /ignoreancestry");\r
1030 \r
1031         if (hWnd)\r
1032         {\r
1033                 sCmd += _T(" /hwnd:");\r
1034                 TCHAR buf[30];\r
1035                 _stprintf_s(buf, 30, _T("%d"), hWnd);\r
1036                 sCmd += buf;\r
1037         }\r
1038 \r
1039         return CAppUtils::LaunchApplication(sCmd, NULL, false);\r
1040 #endif\r
1041         return TRUE;\r
1042 }\r
1043 \r
1044 bool CAppUtils::StartShowCompare(HWND hWnd, const CTGitPath& url1, const GitRev& rev1, \r
1045                                                                  const CTGitPath& url2, const GitRev& rev2, \r
1046                                                                  const GitRev& peg /* = GitRev */, const GitRev& headpeg /* = GitRev */, \r
1047                                                                  bool bAlternateDiff /* = false */, bool bIgnoreAncestry /* = false */, bool blame /* = false */)\r
1048 {\r
1049 #if 0\r
1050         CString sCmd;\r
1051         sCmd.Format(_T("%s /command:showcompare"),\r
1052                 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")));\r
1053         sCmd += _T(" /url1:\"") + url1.GetGitPathString() + _T("\"");\r
1054         if (rev1.IsValid())\r
1055                 sCmd += _T(" /revision1:") + rev1.ToString();\r
1056         sCmd += _T(" /url2:\"") + url2.GetGitPathString() + _T("\"");\r
1057         if (rev2.IsValid())\r
1058                 sCmd += _T(" /revision2:") + rev2.ToString();\r
1059         if (peg.IsValid())\r
1060                 sCmd += _T(" /pegrevision:") + peg.ToString();\r
1061         if (headpeg.IsValid())\r
1062                 sCmd += _T(" /headpegrevision:") + headpeg.ToString();\r
1063         if (bAlternateDiff)\r
1064                 sCmd += _T(" /alternatediff");\r
1065         if (bIgnoreAncestry)\r
1066                 sCmd += _T(" /ignoreancestry");\r
1067         if (blame)\r
1068                 sCmd += _T(" /blame");\r
1069 \r
1070         if (hWnd)\r
1071         {\r
1072                 sCmd += _T(" /hwnd:");\r
1073                 TCHAR buf[30];\r
1074                 _stprintf_s(buf, 30, _T("%d"), hWnd);\r
1075                 sCmd += buf;\r
1076         }\r
1077 \r
1078         return CAppUtils::LaunchApplication(sCmd, NULL, false);\r
1079 #endif\r
1080         return true;\r
1081 }\r
1082 \r
1083 bool CAppUtils::Export(CString *BashHash)\r
1084 {\r
1085         bool bRet = false;\r
1086 \r
1087                 // ask from where the export has to be done\r
1088         CExportDlg dlg;\r
1089         if(BashHash)\r
1090                 dlg.m_Revision=*BashHash;\r
1091 \r
1092         if (dlg.DoModal() == IDOK)\r
1093         {\r
1094                 CString cmd;\r
1095                 cmd.Format(_T("git.exe archive --format=zip --verbose %s"),\r
1096                                         dlg.m_VersionName);\r
1097 \r
1098                 //g_Git.RunLogFile(cmd,dlg.m_strExportDirectory);\r
1099                 CProgressDlg pro;\r
1100                 pro.m_GitCmd=cmd;\r
1101                 pro.m_LogFile=dlg.m_strExportDirectory;\r
1102                 pro.DoModal();\r
1103                 return TRUE;\r
1104         }\r
1105         return bRet;\r
1106 }\r
1107 \r
1108 bool CAppUtils::CreateBranchTag(bool IsTag,CString *CommitHash)\r
1109 {\r
1110         CCreateBranchTagDlg dlg;\r
1111         dlg.m_bIsTag=IsTag;\r
1112         if(CommitHash)\r
1113                 dlg.m_Base = *CommitHash;\r
1114 \r
1115         if(dlg.DoModal()==IDOK)\r
1116         {\r
1117                 CString cmd;\r
1118                 CString force;\r
1119                 CString track;\r
1120                 if(dlg.m_bTrack)\r
1121                         track=_T("--track");\r
1122 \r
1123                 if(dlg.m_bForce)\r
1124                         force=_T("-f");\r
1125 \r
1126                 if(IsTag)\r
1127                 {\r
1128                         cmd.Format(_T("git.exe tag %s %s %s %s"),\r
1129                                 track,\r
1130                                 force,\r
1131                                 dlg.m_BranchTagName,\r
1132                                 dlg.m_VersionName\r
1133                                 );\r
1134 \r
1135         \r
1136                 }else\r
1137                 {\r
1138                         cmd.Format(_T("git.exe branch %s %s %s %s"),\r
1139                                 track,\r
1140                                 force,\r
1141                                 dlg.m_BranchTagName,\r
1142                                 dlg.m_VersionName\r
1143                                 );\r
1144                 }\r
1145                 CString out;\r
1146                 if(g_Git.Run(cmd,&out,CP_UTF8))\r
1147                 {\r
1148                         CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);\r
1149                 }\r
1150                 return TRUE;\r
1151                 \r
1152         }\r
1153         return FALSE;\r
1154 }\r
1155 \r
1156 bool CAppUtils::Switch(CString *CommitHash)\r
1157 {\r
1158         CGitSwitchDlg dlg;\r
1159         if(CommitHash)\r
1160                 dlg.m_Base=*CommitHash;\r
1161         \r
1162         if (dlg.DoModal() == IDOK)\r
1163         {\r
1164                 CString cmd;\r
1165                 CString track;\r
1166                 CString base;\r
1167                 CString force;\r
1168                 CString branch;\r
1169 \r
1170                 if(dlg.m_bBranch)\r
1171                         branch.Format(_T("-b %s"),dlg.m_NewBranch);\r
1172                 if(dlg.m_bForce)\r
1173                         force=_T("-f");\r
1174                 if(dlg.m_bTrack)\r
1175                         track=_T("--track");\r
1176 \r
1177                 cmd.Format(_T("git.exe checkout %s %s %s %s"),\r
1178                          force,\r
1179                          track,\r
1180                          branch,\r
1181                          dlg.m_VersionName);\r
1182 \r
1183                 CProgressDlg progress;\r
1184                 progress.m_GitCmd=cmd;\r
1185                 if(progress.DoModal()==IDOK)\r
1186                         return TRUE;\r
1187 \r
1188         }\r
1189         return FALSE;\r
1190 }\r
1191 \r
1192 bool CAppUtils::IgnoreFile(CTGitPath &path,bool IsMask)\r
1193 {\r
1194         CString ignorefile;\r
1195         ignorefile=g_Git.m_CurrentDir;\r
1196         ignorefile+=path.GetDirectory().GetWinPathString()+_T("\\.gitignore");\r
1197 \r
1198         CStdioFile file;\r
1199         if(!file.Open(ignorefile,CFile::modeCreate|CFile::modeWrite))\r
1200         {\r
1201                 CMessageBox::Show(NULL,ignorefile+_T(" Open Failure"),_T("TortoiseGit"),MB_OK);\r
1202                 return FALSE;\r
1203         }\r
1204 \r
1205         CString ignorelist;\r
1206         file.ReadString(ignorelist);\r
1207 \r
1208         if(IsMask)\r
1209         {\r
1210                 ignorelist+=_T("\n*.")+path.GetFileExtension();\r
1211         }else\r
1212         {\r
1213                 ignorelist+=_T("\n")+path.GetBaseFilename();\r
1214         }\r
1215         file.WriteString(ignorelist);\r
1216 \r
1217         file.Close();\r
1218         return TRUE;\r
1219 }\r
1220 \r
1221 bool CAppUtils::GitReset(CString *CommitHash,int type)\r
1222 {\r
1223         CResetDlg dlg;\r
1224         dlg.m_ResetType=type;\r
1225         if (dlg.DoModal() == IDOK)\r
1226         {\r
1227                 CString cmd;\r
1228                 CString type;\r
1229                 switch(dlg.m_ResetType)\r
1230                 {\r
1231                 case 0:\r
1232                         type=_T("--soft");\r
1233                         break;\r
1234                 case 1:\r
1235                         type=_T("--mixed");\r
1236                         break;\r
1237                 case 2:\r
1238                         type=_T("--hard");\r
1239                         break;\r
1240                 default:\r
1241                         type=_T("--mixed");\r
1242                         break;\r
1243                 }\r
1244                 cmd.Format(_T("git.exe reset %s %s"),type, *CommitHash);\r
1245 \r
1246                 CProgressDlg progress;\r
1247                 progress.m_GitCmd=cmd;\r
1248                 if(progress.DoModal()==IDOK)\r
1249                         return TRUE;\r
1250 \r
1251         }\r
1252         return FALSE;\r
1253 }