1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "TortoiseProc.h"
22 #include "SysImageList.h"
23 #include "..\Utils\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"
33 #include "GitAdminDir.h"
35 #include "SmartHandle.h"
36 #include "Commands\Command.h"
37 #include "..\version.h"
38 #include "JumpListHelpers.h"
39 #include "SinglePropSheetDlg.h"
40 #include "Settings\setmainpage.h"
41 #include "..\Settings\Settings.h"
43 #include "Libraries.h"
45 #define STRUCT_IOVEC_DEFINED
51 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
53 #define APPID (_T("TGIT.TGIT.1") _T(TGIT_PLATFORM))
55 BEGIN_MESSAGE_MAP(CTortoiseProcApp
, CWinAppEx
)
56 ON_COMMAND(ID_HELP
, CWinAppEx::OnHelp
)
60 //CString CGit::m_MsysGitPath;
61 //////////////////////////////////////////////////////////////////////////
63 CTortoiseProcApp::CTortoiseProcApp()
66 CCrashReport::Instance().AddUserInfoToReport(L
"CommandLine", GetCommandLine());
69 // const char* const * argv = NULL;
72 m_bLoadUserToolbars
= FALSE
;
78 CTortoiseProcApp::~CTortoiseProcApp()
81 SYS_IMAGE_LIST().Cleanup();
84 // The one and only CTortoiseProcApp object
85 CTortoiseProcApp theApp
;
89 BOOL
CTortoiseProcApp::CheckMsysGitDir()
91 //CGitIndexFileMap map;
94 //path.SetFromGit(_T("src/gpl.txt"));
95 //map.GetFileStatus(_T("D:\\TortoiseGit"),&path, &status);
96 return g_Git
.CheckMsysGitDir();
98 CCrashReportTGit
crasher(L
"TortoiseGit " _T(APP_X64_STRING
));
100 // CTortoiseProcApp initialization
102 BOOL
CTortoiseProcApp::InitInstance()
105 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows
));
106 CMFCButton::EnableWindowsTheming();
108 Gdiplus::GdiplusStartupInput gdiplusStartupInput
;
109 Gdiplus::GdiplusStartup(&m_gdiplusToken
,&gdiplusStartupInput
,NULL
);
111 //set the resource dll for the required language
112 CRegDWORD loc
= CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
115 CStringA langpath
= CStringA(CPathUtils::GetAppParentDirectory());
116 langpath
+= "Languages";
117 // bindtextdomain("subversion", (LPCSTR)langpath);
118 // bind_textdomain_codeset("subversion", "UTF-8");
119 HINSTANCE hInst
= NULL
;
122 langDll
.Format(_T("%sLanguages\\TortoiseProc%d.dll"), (LPCTSTR
)CPathUtils::GetAppParentDirectory(), langId
);
124 hInst
= LoadLibrary(langDll
);
126 CString sVer
= _T(STRPRODUCTVER
);
127 CString sFileVer
= CPathUtils::GetVersionFromFile(langDll
);
128 if (sFileVer
.Compare(sVer
)!=0)
135 AfxSetResourceHandle(hInst
);
139 DWORD lid
= SUBLANGID(langId
);
143 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
148 } while ((hInst
== NULL
) && (langId
!= 0));
150 _tcscpy_s(buf
, _T("en"));
152 // MFC uses a help file with the same name as the application by default,
153 // which means we have to change that default to our language specific help files
154 CString sHelppath
= CPathUtils::GetAppDirectory() + _T("TortoiseGit_en.chm");
155 free((void*)m_pszHelpFilePath
);
156 m_pszHelpFilePath
=_tcsdup(sHelppath
);
157 sHelppath
= CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseGit_en.chm");
160 CString sLang
= _T("_");
161 if (GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, _countof(buf
)))
164 sHelppath
.Replace(_T("_en"), sLang
);
165 if (PathFileExists(sHelppath
))
167 free((void*)m_pszHelpFilePath
);
168 m_pszHelpFilePath
=_tcsdup(sHelppath
);
172 sHelppath
.Replace(sLang
, _T("_en"));
173 if (GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, _countof(buf
)))
177 sHelppath
.Replace(_T("_en"), sLang
);
178 if (PathFileExists(sHelppath
))
180 free((void*)m_pszHelpFilePath
);
181 m_pszHelpFilePath
=_tcsdup(sHelppath
);
185 sHelppath
.Replace(sLang
, _T("_en"));
187 DWORD lid
= SUBLANGID(langId
);
191 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
196 setlocale(LC_ALL
, "");
198 if(!CheckMsysGitDir())
200 UINT ret
= CMessageBox::Show(NULL
, IDS_PROC_NOMSYSGIT
, IDS_APPNAME
, 3, IDI_HAND
, IDS_PROC_SETMSYSGITPATH
, IDS_PROC_GOTOMSYSGITWEBSITE
, IDS_ABORTBUTTON
);
203 ShellExecute(NULL
, NULL
, _T("http://code.google.com/p/msysgit/"), NULL
, NULL
, SW_SHOW
);
207 // open settings dialog
208 CSinglePropSheetDlg(CString(MAKEINTRESOURCE(IDS_PROC_SETTINGS_TITLE
)), new CSetMainPage(), this->GetMainWnd()).DoModal();
212 if (CAppUtils::GetMsysgitVersion() < 0x01070a00)
214 int ret
= CMessageBox::ShowCheck(NULL
, IDS_PROC_OLDMSYSGIT
, IDS_APPNAME
, 1, IDI_EXCLAMATION
, IDS_PROC_GOTOMSYSGITWEBSITE
, IDS_ABORTBUTTON
, IDS_IGNOREBUTTON
, _T("OldMsysgitVersionWarning"), IDS_PROC_NOTSHOWAGAINIGNORE
);
217 CRegStdDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\OldMsysgitVersionWarning")).removeValue(); // only store answer if it is "Ignore"
218 ShellExecute(NULL
, NULL
, _T("http://code.google.com/p/msysgit/"), NULL
, NULL
, SW_SHOW
);
223 CRegStdDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\OldMsysgitVersionWarning")).removeValue(); // only store answer if it is "Ignore"
228 // InitCommonControls() is required on Windows XP if an application
229 // manifest specifies use of ComCtl32.dll version 6 or later to enable
230 // visual styles. Otherwise, any window creation will fail.
232 INITCOMMONCONTROLSEX used
= {
233 sizeof(INITCOMMONCONTROLSEX
),
234 ICC_ANIMATE_CLASS
| ICC_BAR_CLASSES
| ICC_COOL_CLASSES
| ICC_DATE_CLASSES
|
235 ICC_HOTKEY_CLASS
| ICC_INTERNET_CLASSES
| ICC_LISTVIEW_CLASSES
|
236 ICC_NATIVEFNTCTL_CLASS
| ICC_PAGESCROLLER_CLASS
| ICC_PROGRESS_CLASS
|
237 ICC_TAB_CLASSES
| ICC_TREEVIEW_CLASSES
| ICC_UPDOWN_CLASS
|
238 ICC_USEREX_CLASSES
| ICC_WIN95_CLASSES
240 InitCommonControlsEx(&used
);
242 AfxEnableControlContainer();
244 CWinAppEx::InitInstance();
245 SetRegistryKey(_T("TortoiseGit"));
246 AfxGetApp()->m_pszProfileName
= _tcsdup(_T("TortoiseProc")); // w/o this ResizableLib will store data under TortoiseGitProc which is not compatible with older versions
248 CCmdLineParser
parser(AfxGetApp()->m_lpCmdLine
);
251 CString sVal
= parser
.GetVal(_T("hwnd"));
253 hWndExplorer
= (HWND
)_ttoi64(sVal
);
255 while (GetParent(hWndExplorer
)!=NULL
)
256 hWndExplorer
= GetParent(hWndExplorer
);
257 if (!IsWindow(hWndExplorer
))
262 // if HKCU\Software\TortoiseGit\Debug is not 0, show our command line
264 if (CRegDWORD(_T("Software\\TortoiseGit\\Debug"), FALSE
)==TRUE
)
265 AfxMessageBox(AfxGetApp()->m_lpCmdLine
, MB_OK
| MB_ICONINFORMATION
);
267 if ( parser
.HasKey(_T("path")) && parser
.HasKey(_T("pathfile")))
269 CMessageBox::Show(NULL
, IDS_ERR_INVALIDPATH
, IDS_APPNAME
, MB_ICONERROR
);
273 CTGitPath cmdLinePath
;
274 CTGitPathList pathList
;
275 if ( parser
.HasKey(_T("pathfile")) )
278 CString sPathfileArgument
= CPathUtils::GetLongPathname(parser
.GetVal(_T("pathfile")));
280 cmdLinePath
.SetFromUnknown(sPathfileArgument
);
281 if (pathList
.LoadFromFile(cmdLinePath
)==false)
282 return FALSE
; // no path specified!
283 if ( parser
.HasKey(_T("deletepathfile")) )
285 // We can delete the temporary path file, now that we've loaded it
286 ::DeleteFile(cmdLinePath
.GetWinPath());
288 // This was a path to a temporary file - it's got no meaning now, and
289 // anybody who uses it again is in for a problem...
296 CString sPathArgument
= CPathUtils::GetLongPathname(parser
.GetVal(_T("path")));
297 if (parser
.HasKey(_T("expaths")))
299 // an /expaths param means we're started via the buttons in our Win7 library
300 // and that means the value of /expaths is the current directory, and
301 // the selected paths are then added as additional parameters but without a key, only a value
303 // because of the "strange treatment of quotation marks and backslashes by CommandLineToArgvW"
304 // we have to escape the backslashes first. Since we're only dealing with paths here, that's
306 // Without this, a command line like:
307 // /command:commit /expaths:"D:\" "D:\Utils"
308 // would fail because the "D:\" is treated as the backslash being the escape char for the quotation
309 // mark and we'd end up with:
310 // argv[1] = /command:commit
311 // argv[2] = /expaths:D:" D:\Utils
312 // See here for more details: http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx
313 CString cmdLine
= GetCommandLineW();
314 cmdLine
.Replace(L
"\\", L
"\\\\");
316 LPWSTR
*szArglist
= CommandLineToArgvW(cmdLine
, &nArgs
);
319 // argument 0 is the process path, so start with 1
320 for (int i
=1; i
<nArgs
; i
++)
322 if (szArglist
[i
][0] != '/')
324 if (!sPathArgument
.IsEmpty())
325 sPathArgument
+= '*';
326 sPathArgument
+= szArglist
[i
];
329 sPathArgument
.Replace(L
"\\\\", L
"\\");
331 LocalFree(szArglist
);
333 if (sPathArgument
.IsEmpty() && parser
.HasKey(L
"path"))
335 CMessageBox::Show(hWndExplorer
, IDS_ERR_INVALIDPATH
, IDS_APPNAME
, MB_ICONERROR
);
338 int asterisk
= sPathArgument
.Find('*');
339 cmdLinePath
.SetFromUnknown(asterisk
>= 0 ? sPathArgument
.Left(asterisk
) : sPathArgument
);
340 pathList
.LoadFromAsteriskSeparatedString(sPathArgument
);
343 if (pathList
.GetCount() == 0) {
344 pathList
.AddPath(CTGitPath::CTGitPath(g_Git
.m_CurrentDir
));
347 InitializeJumpList();
348 EnsureGitLibrary(false);
350 // Subversion sometimes writes temp files to the current directory!
351 // Since TSVN doesn't need a specific CWD anyway, we just set it
352 // to the users temp folder: that way, Subversion is guaranteed to
353 // have write access to the CWD
355 DWORD len
= GetCurrentDirectory(0, NULL
);
358 auto_buffer
<TCHAR
> originalCurrentDirectory(len
);
359 if (GetCurrentDirectory(len
, originalCurrentDirectory
))
361 sOrigCWD
= originalCurrentDirectory
;
362 sOrigCWD
= CPathUtils::GetLongPathname(sOrigCWD
);
365 TCHAR pathbuf
[MAX_PATH
];
366 GetTortoiseGitTempPath(MAX_PATH
, pathbuf
);
367 SetCurrentDirectory(pathbuf
);
370 CheckForNewerVersion();
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);
379 CAutoGeneralHandle TGitMutex
= ::CreateMutex(NULL
, FALSE
, _T("TortoiseGitProc.exe"));
380 if (!g_Git
.SetCurrentDir(cmdLinePath
.GetWinPathString(), parser
.HasKey(_T("submodule")) == TRUE
))
383 for(i
=0;i
<pathList
.GetCount();i
++)
384 if(g_Git
.SetCurrentDir(pathList
[i
].GetWinPath()))
388 if(!g_Git
.m_CurrentDir
.IsEmpty())
390 sOrigCWD
= g_Git
.m_CurrentDir
;
391 SetCurrentDirectory(g_Git
.m_CurrentDir
);
398 // requires CWD to be set
399 CGit::m_LogEncode
= CAppUtils::GetLogOutputEncode();
408 UINT choice
= CMessageBox::Show(hWndExplorer
, err
, _T("TortoiseGit"), 1, IDI_ERROR
, CString(MAKEINTRESOURCE(IDS_PROC_EDITLOCALGITCONFIG
)), CString(MAKEINTRESOURCE(IDS_PROC_EDITGLOBALGITCONFIG
)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON
)));
411 // open the config file with alternative editor
412 CAppUtils::LaunchAlternativeEditor(g_Git
.GetGitLocalConfig());
414 else if (choice
== 2)
416 // open the global config file with alternative editor
417 CAppUtils::LaunchAlternativeEditor(g_Git
.GetGitGlobalConfig());
423 // execute the requested command
424 CommandServer server
;
425 Command
* cmd
= server
.GetCommand(parser
.GetVal(_T("command")));
428 cmd
->SetExplorerHwnd(hWndExplorer
);
430 cmd
->SetParser(parser
);
431 cmd
->SetPaths(pathList
, cmdLinePath
);
433 retSuccess
= cmd
->Execute();
437 // Look for temporary files left around by TortoiseSVN and
438 // remove them. But only delete 'old' files because some
439 // apps might still be needing the recent ones.
441 DWORD len
= GetTortoiseGitTempPath(0, NULL
);
442 TCHAR
* path
= new TCHAR
[len
+ 100];
443 len
= GetTortoiseGitTempPath (len
+100, path
);
446 CDirFileEnum
finder(path
);
448 ::GetSystemTimeAsFileTime(&systime_
);
449 __int64 systime
= (((_int64
)systime_
.dwHighDateTime
)<<32) | ((__int64
)systime_
.dwLowDateTime
);
452 while (finder
.NextFile(filepath
, &isDir
))
454 HANDLE hFile
= ::CreateFile(filepath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, isDir
? FILE_FLAG_BACKUP_SEMANTICS
: NULL
, NULL
);
455 if (hFile
!= INVALID_HANDLE_VALUE
)
457 FILETIME createtime_
;
458 if (::GetFileTime(hFile
, &createtime_
, NULL
, NULL
))
460 ::CloseHandle(hFile
);
461 __int64 createtime
= (((_int64
)createtime_
.dwHighDateTime
)<<32) | ((__int64
)createtime_
.dwLowDateTime
);
462 if ((createtime
+ 864000000000) < systime
) //only delete files older than a day
464 ::SetFileAttributes(filepath
, FILE_ATTRIBUTE_NORMAL
);
466 ::RemoveDirectory(filepath
);
468 ::DeleteFile(filepath
);
472 ::CloseHandle(hFile
);
479 // Since the dialog has been closed, return FALSE so that we exit the
480 // application, rather than start the application's message pump.
484 void CTortoiseProcApp::CheckUpgrade()
486 CRegString regVersion
= CRegString(_T("Software\\TortoiseGit\\CurrentVersion"));
487 CString sVersion
= regVersion
;
488 if (sVersion
.Compare(_T(STRPRODUCTVER
))==0)
490 // we're starting the first time with a new version!
493 int pos
= sVersion
.Find('.');
496 lVersion
= (_ttol(sVersion
.Left(pos
))<<24);
497 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<16);
498 pos
= sVersion
.Find('.', pos
+1);
499 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<8);
503 pos
= sVersion
.Find(',');
506 lVersion
= (_ttol(sVersion
.Left(pos
))<<24);
507 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<16);
508 pos
= sVersion
.Find(',', pos
+1);
509 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<8);
514 if (lVersion
<= 0x01010300)
516 CSoundUtils::RegisterTSVNSounds();
520 if (lVersion
<= 0x01040000)
522 CRegStdDWORD(_T("Software\\TortoiseGit\\OwnerdrawnMenus")).removeValue();
525 if (lVersion
<= 0x01070600)
530 CRegStdDWORD(_T("Software\\TortoiseGit\\ConvertBase")).removeValue();
531 CRegStdDWORD(_T("Software\\TortoiseGit\\DiffProps")).removeValue();
532 if (CRegStdDWORD(_T("Software\\TortoiseGit\\CheckNewer"), TRUE
) == FALSE
)
533 CRegStdDWORD(_T("Software\\TortoiseGit\\VersionCheck")) = FALSE
;
536 if (lVersion
<= 0x01070E00)
538 CRegStdDWORD(_T("Software\\TortoiseGit\\CheckNewer")).removeValue();
539 // upgrade to 1.7.15: force recreation of all diff scripts.
540 CAppUtils::SetupDiffScripts(true, CString());
542 CAppUtils::SetupDiffScripts(false, CString());
544 // set the current version so we don't come here again until the next update!
545 regVersion
= _T(STRPRODUCTVER
);
548 void CTortoiseProcApp::InitializeJumpList()
550 // for Win7 : use a custom jump list
553 DeleteJumpList(APPID
);
554 DoInitializeJumpList();
558 void CTortoiseProcApp::DoInitializeJumpList()
560 ATL::CComPtr
<ICustomDestinationList
> pcdl
;
561 HRESULT hr
= pcdl
.CoCreateInstance(CLSID_DestinationList
, NULL
, CLSCTX_INPROC_SERVER
);
565 hr
= pcdl
->SetAppID(APPID
);
570 ATL::CComPtr
<IObjectArray
> poaRemoved
;
571 hr
= pcdl
->BeginList(&uMaxSlots
, IID_PPV_ARGS(&poaRemoved
));
575 ATL::CComPtr
<IObjectCollection
> poc
;
576 hr
= poc
.CoCreateInstance(CLSID_EnumerableObjectCollection
, NULL
, CLSCTX_INPROC_SERVER
);
580 CString sTemp
= CString(MAKEINTRESOURCE(IDS_MENUSETTINGS
));
583 ATL::CComPtr
<IShellLink
> psl
;
584 hr
= CreateShellLink(_T("/command:settings"), (LPCTSTR
)sTemp
, 20, &psl
);
588 sTemp
= CString(MAKEINTRESOURCE(IDS_MENUHELP
));
590 psl
.Release(); // Need to release the object before calling operator&()
591 hr
= CreateShellLink(_T("/command:help"), (LPCTSTR
)sTemp
, 19, &psl
);
596 ATL::CComPtr
<IObjectArray
> poa
;
597 hr
= poc
.QueryInterface(&poa
);
599 pcdl
->AppendCategory((LPCTSTR
)CString(MAKEINTRESOURCE(IDS_PROC_TASKS
)), poa
);
604 int CTortoiseProcApp::ExitInstance()
606 Gdiplus::GdiplusShutdown(m_gdiplusToken
);
608 CWinAppEx::ExitInstance();
614 void CTortoiseProcApp::CheckForNewerVersion()
616 // check for newer versions
617 if (CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck"), TRUE
) != FALSE
)
623 if ((now
!= 0) && (localtime_s(&ptm
, &now
)==0))
626 // Check daily for new preview releases
627 CRegDWORD oldday
= CRegDWORD(_T("Software\\TortoiseGit\\CheckNewerDay"), (DWORD
)-1);
628 if (((DWORD
)oldday
) == -1)
629 oldday
= ptm
.tm_yday
;
632 if ((DWORD
)oldday
!= (DWORD
)ptm
.tm_yday
)
634 oldday
= ptm
.tm_yday
;
637 // we don't calculate the real 'week of the year' here
638 // because just to decide if we should check for an update
639 // that's not needed.
640 week
= ptm
.tm_yday
/ 7;
642 CRegDWORD oldweek
= CRegDWORD(_T("Software\\TortoiseGit\\CheckNewerWeek"), (DWORD
)-1);
643 if (((DWORD
)oldweek
) == -1)
644 oldweek
= week
; // first start of TortoiseProc, no update check needed
647 if ((DWORD
)week
!= oldweek
)
651 TCHAR com
[MAX_PATH
+100];
652 GetModuleFileName(NULL
, com
, MAX_PATH
);
653 _tcscat_s(com
, MAX_PATH
+100, _T(" /command:updatecheck"));
655 CAppUtils::LaunchApplication(com
, 0, false);