1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
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.
21 #include "TortoiseProc.h"
22 #include "SysImageList.h"
23 #include "CrashReport.h"
24 #include "CmdLineParser.h"
27 #include "PathUtils.h"
28 #include "UnicodeUtils.h"
29 #include "MessageBox.h"
30 //#include "libintl.h"
31 #include "DirFileEnum.h"
32 //#include "SoundUtils.h"
34 #include "GitAdminDir.h"
36 //#include "SVNGlobal.h"
37 //#include "svn_types.h"
38 //#include "svn_dso.h"
39 //#include <openssl/ssl.h>
40 //#include <openssl/err.h>
42 #include "Commands\Command.h"
43 #include "CommonResource.h"
44 #include "..\version.h"
45 #include "..\Settings\Settings.h"
48 #define STRUCT_IOVEC_DEFINED
55 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
58 BEGIN_MESSAGE_MAP(CTortoiseProcApp
, CWinAppEx
)
59 ON_COMMAND(ID_HELP
, CWinAppEx::OnHelp
)
63 //CString CGit::m_MsysGitPath;
64 //////////////////////////////////////////////////////////////////////////
66 CTortoiseProcApp::CTortoiseProcApp()
70 // g_version=_T("abc");
71 // const char* const * argv = NULL;
72 // apr_app_initialize(&argc, &argv, NULL);
73 // svn_dso_initialize2();
77 m_bLoadUserToolbars
= FALSE
;
86 CTortoiseProcApp::~CTortoiseProcApp()
90 // global application exit cleanup (after all SSL activity is shutdown)
91 // we have to clean up SSL ourselves, since neon doesn't do that (can't do it)
92 // because those cleanup functions work globally per process.
95 //CRYPTO_cleanup_all_ex_data();
97 // since it is undefined *when* the global object SVNAdminDir is
98 // destroyed, we tell it to destroy the memory pools and terminate apr
99 // *now* instead of later when the object itself is destroyed.
100 g_GitAdminDir
.Close();
101 // CHooks::Destroy();
102 SYS_IMAGE_LIST().Cleanup();
106 // The one and only CTortoiseProcApp object
107 CTortoiseProcApp theApp
;
111 BOOL
CTortoiseProcApp::CheckMsysGitDir()
113 CGitIndexFileMap map
;
116 //path.SetFromGit(_T("src/gpl.txt"));
117 //map.GetFileStatus(_T("D:\\TortoiseGit"),&path, &status);
118 return CGit::CheckMsysGitDir();
120 CCrashReport
crasher("tortoisegit-bug@googlegroups.com", "Crash Report for TortoiseGit " APP_X64_STRING
" : " STRPRODUCTVER
, TRUE
);// crash
122 // CTortoiseProcApp initialization
124 BOOL
CTortoiseProcApp::InitInstance()
126 EnableCrashHandler();
128 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows
));
129 CMFCButton::EnableWindowsTheming();
131 Gdiplus::GdiplusStartupInput gdiplusStartupInput
;
132 Gdiplus::GdiplusStartup(&m_gdiplusToken
,&gdiplusStartupInput
,NULL
);
134 if(!CheckMsysGitDir())
136 if(CMessageBox::Show(NULL
,_T("MSysGit(http://code.google.com/p/msysgit) have not installed Correctly\n\
137 or MSysGit Path setting error\n\
138 Click Yes to open setting dialog to setup MSysGit Path"),
139 _T("TortoiseGit"),MB_YESNO
|MB_ICONERROR
)==IDYES
)
141 // open settings dialog
142 CSettings
dlg(IDS_PROC_SETTINGS_TITLE
);
143 dlg
.SetTreeViewMode(TRUE
, TRUE
, TRUE
);
144 dlg
.SetTreeWidth(220);
153 //set the resource dll for the required language
154 CRegDWORD loc
= CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
157 CStringA langpath
= CStringA(CPathUtils::GetAppParentDirectory());
158 langpath
+= "Languages";
159 // bindtextdomain("subversion", (LPCSTR)langpath);
160 // bind_textdomain_codeset("subversion", "UTF-8");
161 HINSTANCE hInst
= NULL
;
164 langDll
.Format(_T("..\\Languages\\TortoiseProc%d.dll"), langId
);
166 hInst
= LoadLibrary(langDll
);
168 CString sVer
= _T(STRPRODUCTVER
);
169 CString sFileVer
= CPathUtils::GetVersionFromFile(langDll
);
170 if (sFileVer
.Compare(sVer
)!=0)
177 AfxSetResourceHandle(hInst
);
181 DWORD lid
= SUBLANGID(langId
);
185 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
190 } while ((hInst
== NULL
) && (langId
!= 0));
192 _tcscpy_s(buf
, _T("en"));
195 sHelppath
= this->m_pszHelpFilePath
;
196 sHelppath
= sHelppath
.MakeLower();
197 // MFC uses a help file with the same name as the application by default,
198 // which means we have to change that default to our language specific help files
199 sHelppath
.Replace(_T("tortoiseproc.chm"), _T("TortoiseGit_en.chm"));
200 free((void*)m_pszHelpFilePath
);
201 m_pszHelpFilePath
=_tcsdup(sHelppath
);
202 sHelppath
= CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseGit_en.chm");
205 CString sLang
= _T("_");
206 if (GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, sizeof(buf
)))
209 sHelppath
.Replace(_T("_en"), sLang
);
210 if (PathFileExists(sHelppath
))
212 free((void*)m_pszHelpFilePath
);
213 m_pszHelpFilePath
=_tcsdup(sHelppath
);
217 sHelppath
.Replace(sLang
, _T("_en"));
218 if (GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, sizeof(buf
)))
222 sHelppath
.Replace(_T("_en"), sLang
);
223 if (PathFileExists(sHelppath
))
225 free((void*)m_pszHelpFilePath
);
226 m_pszHelpFilePath
=_tcsdup(sHelppath
);
230 sHelppath
.Replace(sLang
, _T("_en"));
232 DWORD lid
= SUBLANGID(langId
);
236 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
241 setlocale(LC_ALL
, "");
243 // InitCommonControls() is required on Windows XP if an application
244 // manifest specifies use of ComCtl32.dll version 6 or later to enable
245 // visual styles. Otherwise, any window creation will fail.
247 INITCOMMONCONTROLSEX used
= {
248 sizeof(INITCOMMONCONTROLSEX
),
249 ICC_ANIMATE_CLASS
| ICC_BAR_CLASSES
| ICC_COOL_CLASSES
| ICC_DATE_CLASSES
|
250 ICC_HOTKEY_CLASS
| ICC_INTERNET_CLASSES
| ICC_LISTVIEW_CLASSES
|
251 ICC_NATIVEFNTCTL_CLASS
| ICC_PAGESCROLLER_CLASS
| ICC_PROGRESS_CLASS
|
252 ICC_TAB_CLASSES
| ICC_TREEVIEW_CLASSES
| ICC_UPDOWN_CLASS
|
253 ICC_USEREX_CLASSES
| ICC_WIN95_CLASSES
255 InitCommonControlsEx(&used
);
257 AfxEnableControlContainer();
259 CWinAppEx::InitInstance();
260 SetRegistryKey(_T("TortoiseGit"));
262 CCmdLineParser
parser(AfxGetApp()->m_lpCmdLine
);
264 // if HKCU\Software\TortoiseSVN\Debug is not 0, show our command line
266 if (CRegDWORD(_T("Software\\TortoiseGit\\Debug"), FALSE
)==TRUE
)
267 AfxMessageBox(AfxGetApp()->m_lpCmdLine
, MB_OK
| MB_ICONINFORMATION
);
269 if ( parser
.HasKey(_T("path")) && parser
.HasKey(_T("pathfile")))
271 CMessageBox::Show(NULL
, IDS_ERR_INVALIDPATH
, IDS_APPNAME
, MB_ICONERROR
);
275 CTGitPath cmdLinePath
;
276 CTGitPathList pathList
;
277 if ( parser
.HasKey(_T("pathfile")) )
280 CString sPathfileArgument
= CPathUtils::GetLongPathname(parser
.GetVal(_T("pathfile")));
282 cmdLinePath
.SetFromUnknown(sPathfileArgument
);
283 if (pathList
.LoadFromFile(cmdLinePath
)==false)
284 return FALSE
; // no path specified!
285 if ( parser
.HasKey(_T("deletepathfile")) )
287 // We can delete the temporary path file, now that we've loaded it
288 ::DeleteFile(cmdLinePath
.GetWinPath());
290 // This was a path to a temporary file - it's got no meaning now, and
291 // anybody who uses it again is in for a problem...
298 CString sPathArgument
= CPathUtils::GetLongPathname(parser
.GetVal(_T("path")));
299 int asterisk
= sPathArgument
.Find('*');
300 cmdLinePath
.SetFromUnknown(asterisk
>= 0 ? sPathArgument
.Left(asterisk
) : sPathArgument
);
301 pathList
.LoadFromAsteriskSeparatedString(sPathArgument
);
306 CString sVal
= parser
.GetVal(_T("hwnd"));
308 hWndExplorer
= (HWND
)_ttoi64(sVal
);
310 while (GetParent(hWndExplorer
)!=NULL
)
311 hWndExplorer
= GetParent(hWndExplorer
);
312 if (!IsWindow(hWndExplorer
))
317 // Subversion sometimes writes temp files to the current directory!
318 // Since TSVN doesn't need a specific CWD anyway, we just set it
319 // to the users temp folder: that way, Subversion is guaranteed to
320 // have write access to the CWD
322 DWORD len
= GetCurrentDirectory(0, NULL
);
325 TCHAR
* originalCurrentDirectory
= new TCHAR
[len
];
326 if (GetCurrentDirectory(len
, originalCurrentDirectory
))
328 sOrigCWD
= originalCurrentDirectory
;
329 sOrigCWD
= CPathUtils::GetLongPathname(sOrigCWD
);
331 delete [] originalCurrentDirectory
;
333 TCHAR pathbuf
[MAX_PATH
];
334 GetTempPath(MAX_PATH
, pathbuf
);
335 SetCurrentDirectory(pathbuf
);
338 // check for newer versions
339 if (CRegDWORD(_T("Software\\TortoiseGit\\CheckNewer"), TRUE
) != FALSE
)
345 if ((now
!= 0) && (localtime_s(&ptm
, &now
)==0))
348 // we don't calculate the real 'week of the year' here
349 // because just to decide if we should check for an update
350 // that's not needed.
351 week
= ptm
.tm_yday
/ 7;
353 CRegDWORD oldweek
= CRegDWORD(_T("Software\\TortoiseGit\\CheckNewerWeek"), (DWORD
)-1);
354 if (((DWORD
)oldweek
) == -1)
355 oldweek
= week
; // first start of TortoiseProc, no update check needed
358 if ((DWORD
)week
!= oldweek
)
362 TCHAR com
[MAX_PATH
+100];
363 GetModuleFileName(NULL
, com
, MAX_PATH
);
364 _tcscat_s(com
, MAX_PATH
+100, _T(" /command:updatecheck"));
366 //CAppUtils::LaunchApplication(com, 0, false);
372 if (parser
.HasVal(_T("configdir")))
374 // the user can override the location of the Subversion config directory here
375 CString sConfigDir
= parser
.GetVal(_T("configdir"));
376 // g_GitGlobal.SetConfigDir(sConfigDir);
378 // to avoid that SASL will look for and load its plugin dlls all around the
379 // system, we set the path here.
380 // Note that SASL doesn't have to be initialized yet for this to work
381 // sasl_set_path(SASL_PATH_TYPE_PLUGIN, (LPSTR)(LPCSTR)CUnicodeUtils::GetUTF8(CPathUtils::GetAppDirectory().TrimRight('\\')));
383 HANDLE TSVNMutex
= ::CreateMutex(NULL
, FALSE
, _T("TortoiseGitProc.exe"));
386 CString err
= Git::CheckConfigFile();
389 CMessageBox::Show(hWndExplorer
, err
, _T("TortoiseGit"), MB_ICONERROR
);
390 // Normally, we give-up and exit at this point, but there is a trap here
391 // in that the user might need to use the settings dialog to edit the config file.
392 if (CString(parser
.GetVal(_T("command"))).Compare(_T("settings"))==0)
394 // just open the config file
396 SHGetFolderPath(NULL
, CSIDL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
, buf
);
398 path
+= _T("\\Git\\config");
399 CAppUtils::StartTextViewer(path
);
406 // execute the requested command
407 CommandServer server
;
408 Command
* cmd
= server
.GetCommand(parser
.GetVal(_T("command")));
411 cmd
->SetExplorerHwnd(hWndExplorer
);
413 if(!g_Git
.SetCurrentDir(cmdLinePath
.GetWinPathString()))
416 for(i
=0;i
<pathList
.GetCount();i
++)
417 if(g_Git
.SetCurrentDir(pathList
[i
].GetWinPath()))
421 if(!g_Git
.m_CurrentDir
.IsEmpty())
422 SetCurrentDirectory(g_Git
.m_CurrentDir
);
424 cmd
->SetParser(parser
);
425 cmd
->SetPaths(pathList
, cmdLinePath
);
427 CGit::m_LogEncode
= CAppUtils::GetLogOutputEncode();
429 retSuccess
= cmd
->Execute();
434 ::CloseHandle(TSVNMutex
);
436 // Look for temporary files left around by TortoiseSVN and
437 // remove them. But only delete 'old' files because some
438 // apps might still be needing the recent ones.
440 DWORD len
= ::GetTempPath(0, NULL
);
441 TCHAR
* path
= new TCHAR
[len
+ 100];
442 len
= ::GetTempPath (len
+100, path
);
445 CSimpleFileFind finder
= CSimpleFileFind(path
, _T("*svn*.*"));
447 ::GetSystemTimeAsFileTime(&systime_
);
448 __int64 systime
= (((_int64
)systime_
.dwHighDateTime
)<<32) | ((__int64
)systime_
.dwLowDateTime
);
449 while (finder
.FindNextFileNoDirectories())
451 CString filepath
= finder
.GetFilePath();
452 HANDLE hFile
= ::CreateFile(filepath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
453 if (hFile
!= INVALID_HANDLE_VALUE
)
455 FILETIME createtime_
;
456 if (::GetFileTime(hFile
, &createtime_
, NULL
, NULL
))
458 ::CloseHandle(hFile
);
459 __int64 createtime
= (((_int64
)createtime_
.dwHighDateTime
)<<32) | ((__int64
)createtime_
.dwLowDateTime
);
460 if ((createtime
+ 864000000000) < systime
) //only delete files older than a day
462 ::SetFileAttributes(filepath
, FILE_ATTRIBUTE_NORMAL
);
463 ::DeleteFile(filepath
);
467 ::CloseHandle(hFile
);
475 // Since the dialog has been closed, return FALSE so that we exit the
476 // application, rather than start the application's message pump.
482 void CTortoiseProcApp::CheckUpgrade()
484 CRegString regVersion
= CRegString(_T("Software\\TortoiseGit\\CurrentVersion"));
485 CString sVersion
= regVersion
;
486 if (sVersion
.Compare(_T(STRPRODUCTVER
))==0)
488 // we're starting the first time with a new version!
491 int pos
= sVersion
.Find(',');
494 lVersion
= (_ttol(sVersion
.Left(pos
))<<24);
495 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<16);
496 pos
= sVersion
.Find(',', pos
+1);
497 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<8);
500 CRegDWORD regval
= CRegDWORD(_T("Software\\TortoiseGit\\DontConvertBase"), 999);
501 if ((DWORD
)regval
!= 999)
503 // there's a leftover registry setting we have to convert and then delete it
504 CRegDWORD newregval
= CRegDWORD(_T("Software\\TortoiseGit\\ConvertBase"));
506 regval
.removeValue();
509 if (lVersion
<= 0x01010300)
511 CSoundUtils::RegisterTSVNSounds();
512 // remove all saved dialog positions
513 CRegString(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\")).removeKey();
514 CRegDWORD(_T("Software\\TortoiseGit\\RecursiveOverlay")).removeValue();
515 // remove the external cache key
516 CRegDWORD(_T("Software\\TortoiseGit\\ExternalCache")).removeValue();
519 if (lVersion
<= 0x01020200)
521 // upgrade to > 1.2.3 means the doc diff scripts changed from vbs to js
522 // so remove the diff/merge scripts if they're the defaults
523 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\.doc"));
524 CString sDiff
= diffreg
;
525 CString sCL
= _T("wscript.exe \"") + CPathUtils::GetAppParentDirectory()+_T("Diff-Scripts\\diff-doc.vbs\"");
526 if (sDiff
.Left(sCL
.GetLength()).CompareNoCase(sCL
)==0)
528 CRegString mergereg
= CRegString(_T("Software\\TortoiseGit\\MergeTools\\.doc"));
530 sCL
= _T("wscript.exe \"") + CPathUtils::GetAppParentDirectory()+_T("Diff-Scripts\\merge-doc.vbs\"");
531 if (sDiff
.Left(sCL
.GetLength()).CompareNoCase(sCL
)==0)
534 if (lVersion
<= 0x01040000)
536 CRegStdWORD(_T("Software\\TortoiseGit\\OwnerdrawnMenus")).removeValue();
539 // set the custom diff scripts for every user
540 CString scriptsdir
= CPathUtils::GetAppParentDirectory();
541 scriptsdir
+= _T("Diff-Scripts");
542 CSimpleFileFind
files(scriptsdir
);
543 while (files
.FindNextFileNoDirectories())
545 CString file
= files
.GetFilePath();
546 CString filename
= files
.GetFileName();
547 CString ext
= file
.Mid(file
.ReverseFind('-')+1);
548 ext
= _T(".")+ext
.Left(ext
.ReverseFind('.'));
550 if (file
.Right(3).CompareNoCase(_T("vbs"))==0)
552 kind
= _T(" //E:vbscript");
554 if (file
.Right(2).CompareNoCase(_T("js"))==0)
556 kind
= _T(" //E:javascript");
559 if (filename
.Left(5).CompareNoCase(_T("diff-"))==0)
561 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\DiffTools\\")+ext
);
562 CString diffregstring
= diffreg
;
563 if ((diffregstring
.IsEmpty()) || (diffregstring
.Find(filename
)>=0))
564 diffreg
= _T("wscript.exe \"") + file
+ _T("\" %base %mine") + kind
;
566 if (filename
.Left(6).CompareNoCase(_T("merge-"))==0)
568 CRegString diffreg
= CRegString(_T("Software\\TortoiseGit\\MergeTools\\")+ext
);
569 CString diffregstring
= diffreg
;
570 if ((diffregstring
.IsEmpty()) || (diffregstring
.Find(filename
)>=0))
571 diffreg
= _T("wscript.exe \"") + file
+ _T("\" %merged %theirs %mine %base") + kind
;
575 // Initialize "Software\\TortoiseGit\\DiffProps" once with the same value as "Software\\TortoiseGit\\Diff"
576 CRegString regDiffPropsPath
= CRegString(_T("Software\\TortoiseGit\\DiffProps"),_T("non-existant"));
577 CString strDiffPropsPath
= regDiffPropsPath
;
578 if ( strDiffPropsPath
==_T("non-existant") )
580 CString strDiffPath
= CRegString(_T("Software\\TortoiseGit\\Diff"));
581 regDiffPropsPath
= strDiffPath
;
584 // set the current version so we don't come here again until the next update!
585 regVersion
= _T(STRPRODUCTVER
);
588 void CTortoiseProcApp::EnableCrashHandler()
590 // the crash handler is enabled by default, but we disable it
591 // after 3 months after a release
593 #define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
594 + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))
596 #define MONTH (__DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 1 : 6) \
597 : __DATE__ [2] == 'b' ? 2 \
598 : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
599 : __DATE__ [2] == 'y' ? 5 \
600 : __DATE__ [2] == 'l' ? 7 \
601 : __DATE__ [2] == 'g' ? 8 \
602 : __DATE__ [2] == 'p' ? 9 \
603 : __DATE__ [2] == 't' ? 10 \
604 : __DATE__ [2] == 'v' ? 11 : 12)
606 #define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
607 + (__DATE__ [5] - '0'))
609 #define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)
611 CTime
compiletime(YEAR
, MONTH
, DAY
, 0, 0, 0);
612 CTime now
= CTime::GetCurrentTime();
614 CTimeSpan timediff
= now
-compiletime
;
615 if (timediff
.GetDays() > 3*31)
617 // crasher.Enable(FALSE);
621 int CTortoiseProcApp::ExitInstance()
623 Gdiplus::GdiplusShutdown(m_gdiplusToken
);
625 CWinAppEx::ExitInstance();