1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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 // prevent from inheriting %GIT_DIR% from parent process by resetting it,
67 // use MSVC function instead of Windows API because MSVC runtime caches environment variables
68 _tputenv(_T("GIT_DIR="));
69 CCrashReport::Instance().AddUserInfoToReport(L
"CommandLine", GetCommandLine());
72 // const char* const * argv = NULL;
75 m_bLoadUserToolbars
= FALSE
;
78 m_gdiplusToken
= NULL
;
82 CTortoiseProcApp::~CTortoiseProcApp()
85 SYS_IMAGE_LIST().Cleanup();
88 // The one and only CTortoiseProcApp object
89 CTortoiseProcApp theApp
;
93 BOOL
CTortoiseProcApp::CheckMsysGitDir()
95 //CGitIndexFileMap map;
98 //path.SetFromGit(_T("src/gpl.txt"));
99 //map.GetFileStatus(_T("D:\\TortoiseGit"),&path, &status);
100 return g_Git
.CheckMsysGitDir();
102 CCrashReportTGit
crasher(L
"TortoiseGit " _T(APP_X64_STRING
), TGIT_VERMAJOR
, TGIT_VERMINOR
, TGIT_VERMICRO
, TGIT_VERBUILD
, TGIT_VERDATE
);
104 // CTortoiseProcApp initialization
106 BOOL
CTortoiseProcApp::InitInstance()
109 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows
));
110 CMFCButton::EnableWindowsTheming();
112 Gdiplus::GdiplusStartupInput gdiplusStartupInput
;
113 Gdiplus::GdiplusStartup(&m_gdiplusToken
,&gdiplusStartupInput
,NULL
);
115 //set the resource dll for the required language
116 CRegDWORD loc
= CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
119 CStringA langpath
= CStringA(CPathUtils::GetAppParentDirectory());
120 langpath
+= "Languages";
121 // bindtextdomain("subversion", (LPCSTR)langpath);
122 // bind_textdomain_codeset("subversion", "UTF-8");
123 HINSTANCE hInst
= NULL
;
126 langDll
.Format(_T("%sLanguages\\TortoiseProc%d.dll"), (LPCTSTR
)CPathUtils::GetAppParentDirectory(), langId
);
128 hInst
= LoadLibrary(langDll
);
130 CString sVer
= _T(STRPRODUCTVER
);
131 CString sFileVer
= CPathUtils::GetVersionFromFile(langDll
);
132 if (sFileVer
.Compare(sVer
)!=0)
139 AfxSetResourceHandle(hInst
);
143 DWORD lid
= SUBLANGID(langId
);
147 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
152 } while ((hInst
== NULL
) && (langId
!= 0));
154 _tcscpy_s(buf
, _T("en"));
156 // MFC uses a help file with the same name as the application by default,
157 // which means we have to change that default to our language specific help files
158 CString sHelppath
= CPathUtils::GetAppDirectory() + _T("TortoiseGit_en.chm");
159 free((void*)m_pszHelpFilePath
);
160 m_pszHelpFilePath
=_tcsdup(sHelppath
);
161 sHelppath
= CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseGit_en.chm");
164 CString sLang
= _T("_");
165 if (GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, _countof(buf
)))
168 sHelppath
.Replace(_T("_en"), sLang
);
169 if (PathFileExists(sHelppath
))
171 free((void*)m_pszHelpFilePath
);
172 m_pszHelpFilePath
=_tcsdup(sHelppath
);
176 sHelppath
.Replace(sLang
, _T("_en"));
177 if (GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, _countof(buf
)))
181 sHelppath
.Replace(_T("_en"), sLang
);
182 if (PathFileExists(sHelppath
))
184 free((void*)m_pszHelpFilePath
);
185 m_pszHelpFilePath
=_tcsdup(sHelppath
);
189 sHelppath
.Replace(sLang
, _T("_en"));
191 DWORD lid
= SUBLANGID(langId
);
195 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
200 setlocale(LC_ALL
, "");
202 if(!CheckMsysGitDir())
204 UINT ret
= CMessageBox::Show(NULL
, IDS_PROC_NOMSYSGIT
, IDS_APPNAME
, 3, IDI_HAND
, IDS_PROC_SETMSYSGITPATH
, IDS_PROC_GOTOMSYSGITWEBSITE
, IDS_ABORTBUTTON
);
207 ShellExecute(NULL
, NULL
, _T("http://code.google.com/p/msysgit/"), NULL
, NULL
, SW_SHOW
);
211 // open settings dialog
212 CSinglePropSheetDlg(CString(MAKEINTRESOURCE(IDS_PROC_SETTINGS_TITLE
)), new CSetMainPage(), this->GetMainWnd()).DoModal();
216 if (CAppUtils::GetMsysgitVersion() < 0x01070a00)
218 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
);
221 CRegStdDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\OldMsysgitVersionWarning")).removeValue(); // only store answer if it is "Ignore"
222 ShellExecute(NULL
, NULL
, _T("http://code.google.com/p/msysgit/"), NULL
, NULL
, SW_SHOW
);
227 CRegStdDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\OldMsysgitVersionWarning")).removeValue(); // only store answer if it is "Ignore"
232 // InitCommonControls() is required on Windows XP if an application
233 // manifest specifies use of ComCtl32.dll version 6 or later to enable
234 // visual styles. Otherwise, any window creation will fail.
236 INITCOMMONCONTROLSEX used
= {
237 sizeof(INITCOMMONCONTROLSEX
),
238 ICC_ANIMATE_CLASS
| ICC_BAR_CLASSES
| ICC_COOL_CLASSES
| ICC_DATE_CLASSES
|
239 ICC_HOTKEY_CLASS
| ICC_INTERNET_CLASSES
| ICC_LISTVIEW_CLASSES
|
240 ICC_NATIVEFNTCTL_CLASS
| ICC_PAGESCROLLER_CLASS
| ICC_PROGRESS_CLASS
|
241 ICC_TAB_CLASSES
| ICC_TREEVIEW_CLASSES
| ICC_UPDOWN_CLASS
|
242 ICC_USEREX_CLASSES
| ICC_WIN95_CLASSES
244 InitCommonControlsEx(&used
);
246 AfxEnableControlContainer();
248 CWinAppEx::InitInstance();
249 SetRegistryKey(_T("TortoiseGit"));
250 AfxGetApp()->m_pszProfileName
= _tcsdup(_T("TortoiseProc")); // w/o this ResizableLib will store data under TortoiseGitProc which is not compatible with older versions
252 CCmdLineParser
parser(AfxGetApp()->m_lpCmdLine
);
255 CString sVal
= parser
.GetVal(_T("hwnd"));
257 hWndExplorer
= (HWND
)_ttoi64(sVal
);
259 while (GetParent(hWndExplorer
)!=NULL
)
260 hWndExplorer
= GetParent(hWndExplorer
);
261 if (!IsWindow(hWndExplorer
))
266 // if HKCU\Software\TortoiseGit\Debug is not 0, show our command line
268 if (CRegDWORD(_T("Software\\TortoiseGit\\Debug"), FALSE
)==TRUE
)
269 AfxMessageBox(AfxGetApp()->m_lpCmdLine
, MB_OK
| MB_ICONINFORMATION
);
271 if ( parser
.HasKey(_T("path")) && parser
.HasKey(_T("pathfile")))
273 CMessageBox::Show(NULL
, IDS_ERR_INVALIDPATH
, IDS_APPNAME
, MB_ICONERROR
);
277 CTGitPath cmdLinePath
;
278 CTGitPathList pathList
;
279 if ( parser
.HasKey(_T("pathfile")) )
282 CString sPathfileArgument
= CPathUtils::GetLongPathname(parser
.GetVal(_T("pathfile")));
284 cmdLinePath
.SetFromUnknown(sPathfileArgument
);
285 if (pathList
.LoadFromFile(cmdLinePath
)==false)
286 return FALSE
; // no path specified!
287 if ( parser
.HasKey(_T("deletepathfile")) )
289 // We can delete the temporary path file, now that we've loaded it
290 ::DeleteFile(cmdLinePath
.GetWinPath());
292 // This was a path to a temporary file - it's got no meaning now, and
293 // anybody who uses it again is in for a problem...
300 CString sPathArgument
= CPathUtils::GetLongPathname(parser
.GetVal(_T("path")));
301 if (parser
.HasKey(_T("expaths")))
303 // an /expaths param means we're started via the buttons in our Win7 library
304 // and that means the value of /expaths is the current directory, and
305 // the selected paths are then added as additional parameters but without a key, only a value
307 // because of the "strange treatment of quotation marks and backslashes by CommandLineToArgvW"
308 // we have to escape the backslashes first. Since we're only dealing with paths here, that's
310 // Without this, a command line like:
311 // /command:commit /expaths:"D:\" "D:\Utils"
312 // would fail because the "D:\" is treated as the backslash being the escape char for the quotation
313 // mark and we'd end up with:
314 // argv[1] = /command:commit
315 // argv[2] = /expaths:D:" D:\Utils
316 // See here for more details: http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx
317 CString cmdLine
= GetCommandLineW();
318 cmdLine
.Replace(L
"\\", L
"\\\\");
320 LPWSTR
*szArglist
= CommandLineToArgvW(cmdLine
, &nArgs
);
323 // argument 0 is the process path, so start with 1
324 for (int i
= 1; i
< nArgs
; ++i
)
326 if (szArglist
[i
][0] != '/')
328 if (!sPathArgument
.IsEmpty())
329 sPathArgument
+= '*';
330 sPathArgument
+= szArglist
[i
];
333 sPathArgument
.Replace(L
"\\\\", L
"\\");
335 LocalFree(szArglist
);
337 if (sPathArgument
.IsEmpty() && parser
.HasKey(L
"path"))
339 CMessageBox::Show(hWndExplorer
, IDS_ERR_INVALIDPATH
, IDS_APPNAME
, MB_ICONERROR
);
342 int asterisk
= sPathArgument
.Find('*');
343 cmdLinePath
.SetFromUnknown(asterisk
>= 0 ? sPathArgument
.Left(asterisk
) : sPathArgument
);
344 pathList
.LoadFromAsteriskSeparatedString(sPathArgument
);
347 if (pathList
.GetCount() == 0) {
348 pathList
.AddPath(CTGitPath::CTGitPath(g_Git
.m_CurrentDir
));
351 InitializeJumpList();
352 EnsureGitLibrary(false);
354 // Subversion sometimes writes temp files to the current directory!
355 // Since TSVN doesn't need a specific CWD anyway, we just set it
356 // to the users temp folder: that way, Subversion is guaranteed to
357 // have write access to the CWD
359 DWORD len
= GetCurrentDirectory(0, NULL
);
362 std::unique_ptr
<TCHAR
[]> originalCurrentDirectory(new TCHAR
[len
]);
363 if (GetCurrentDirectory(len
, originalCurrentDirectory
.get()))
365 sOrigCWD
= originalCurrentDirectory
.get();
366 sOrigCWD
= CPathUtils::GetLongPathname(sOrigCWD
);
369 TCHAR pathbuf
[MAX_PATH
];
370 GetTortoiseGitTempPath(MAX_PATH
, pathbuf
);
371 SetCurrentDirectory(pathbuf
);
374 CheckForNewerVersion();
376 if (parser
.HasVal(_T("configdir")))
378 // the user can override the location of the Subversion config directory here
379 CString sConfigDir
= parser
.GetVal(_T("configdir"));
380 // g_GitGlobal.SetConfigDir(sConfigDir);
383 CAutoGeneralHandle TGitMutex
= ::CreateMutex(NULL
, FALSE
, _T("TortoiseGitProc.exe"));
384 if (!g_Git
.SetCurrentDir(cmdLinePath
.GetWinPathString(), parser
.HasKey(_T("submodule")) == TRUE
))
386 for (int i
= 0; i
< pathList
.GetCount(); ++i
)
387 if(g_Git
.SetCurrentDir(pathList
[i
].GetWinPath()))
391 if(!g_Git
.m_CurrentDir
.IsEmpty())
393 sOrigCWD
= g_Git
.m_CurrentDir
;
394 SetCurrentDirectory(g_Git
.m_CurrentDir
);
401 // requires CWD to be set
402 CGit::m_LogEncode
= CAppUtils::GetLogOutputEncode();
404 // make sure all config files are read in order to check that none contains an error
405 g_Git
.GetConfigValue(_T("doesnot.exist"));
414 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
)));
417 // open the config file with alternative editor
418 CAppUtils::LaunchAlternativeEditor(g_Git
.GetGitLocalConfig());
420 else if (choice
== 2)
422 // open the global config file with alternative editor
423 CAppUtils::LaunchAlternativeEditor(g_Git
.GetGitGlobalConfig());
429 // execute the requested command
430 CommandServer server
;
431 Command
* cmd
= server
.GetCommand(parser
.GetVal(_T("command")));
434 cmd
->SetExplorerHwnd(hWndExplorer
);
436 cmd
->SetParser(parser
);
437 cmd
->SetPaths(pathList
, cmdLinePath
);
439 retSuccess
= cmd
->Execute();
443 // Look for temporary files left around by TortoiseSVN and
444 // remove them. But only delete 'old' files because some
445 // apps might still be needing the recent ones.
447 DWORD len
= GetTortoiseGitTempPath(0, NULL
);
448 std::unique_ptr
<TCHAR
[]> path(new TCHAR
[len
+ 100]);
449 len
= GetTortoiseGitTempPath (len
+ 100, path
.get());
452 CDirFileEnum
finder(path
.get());
454 ::GetSystemTimeAsFileTime(&systime_
);
455 __int64 systime
= (((_int64
)systime_
.dwHighDateTime
)<<32) | ((__int64
)systime_
.dwLowDateTime
);
458 while (finder
.NextFile(filepath
, &isDir
))
460 HANDLE hFile
= ::CreateFile(filepath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, isDir
? FILE_FLAG_BACKUP_SEMANTICS
: NULL
, NULL
);
461 if (hFile
!= INVALID_HANDLE_VALUE
)
463 FILETIME createtime_
;
464 if (::GetFileTime(hFile
, &createtime_
, NULL
, NULL
))
466 ::CloseHandle(hFile
);
467 __int64 createtime
= (((_int64
)createtime_
.dwHighDateTime
)<<32) | ((__int64
)createtime_
.dwLowDateTime
);
468 if ((createtime
+ 864000000000) < systime
) //only delete files older than a day
470 ::SetFileAttributes(filepath
, FILE_ATTRIBUTE_NORMAL
);
472 ::RemoveDirectory(filepath
);
474 ::DeleteFile(filepath
);
478 ::CloseHandle(hFile
);
484 // Since the dialog has been closed, return FALSE so that we exit the
485 // application, rather than start the application's message pump.
489 void CTortoiseProcApp::CheckUpgrade()
491 CRegString regVersion
= CRegString(_T("Software\\TortoiseGit\\CurrentVersion"));
492 CString sVersion
= regVersion
;
493 if (sVersion
.Compare(_T(STRPRODUCTVER
))==0)
495 // we're starting the first time with a new version!
498 int pos
= sVersion
.Find('.');
501 lVersion
= (_ttol(sVersion
.Left(pos
))<<24);
502 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<16);
503 pos
= sVersion
.Find('.', pos
+1);
504 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<8);
508 pos
= sVersion
.Find(',');
511 lVersion
= (_ttol(sVersion
.Left(pos
))<<24);
512 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<16);
513 pos
= sVersion
.Find(',', pos
+1);
514 lVersion
|= (_ttol(sVersion
.Mid(pos
+1))<<8);
519 if (lVersion
<= 0x01010300)
521 CSoundUtils::RegisterTSVNSounds();
525 if (lVersion
<= 0x01080100)
527 if (CRegStdDWORD(_T("Software\\TortoiseGit\\LogTopoOrder"), TRUE
) == FALSE
)
528 CRegStdDWORD(_T("Software\\TortoiseGit\\LogOrderBy")) = 0;
531 if (lVersion
<= 0x01040000)
533 CRegStdDWORD(_T("Software\\TortoiseGit\\OwnerdrawnMenus")).removeValue();
536 if (lVersion
<= 0x01070600)
541 CRegStdDWORD(_T("Software\\TortoiseGit\\ConvertBase")).removeValue();
542 CRegStdDWORD(_T("Software\\TortoiseGit\\DiffProps")).removeValue();
543 if (CRegStdDWORD(_T("Software\\TortoiseGit\\CheckNewer"), TRUE
) == FALSE
)
544 CRegStdDWORD(_T("Software\\TortoiseGit\\VersionCheck")) = FALSE
;
547 if (lVersion
<= 0x01070E00)
549 CRegStdDWORD(_T("Software\\TortoiseGit\\CheckNewer")).removeValue();
550 // upgrade to 1.7.15: force recreation of all diff scripts.
551 CAppUtils::SetupDiffScripts(true, CString());
553 CAppUtils::SetupDiffScripts(false, CString());
555 // set the current version so we don't come here again until the next update!
556 regVersion
= _T(STRPRODUCTVER
);
559 void CTortoiseProcApp::InitializeJumpList()
561 // for Win7 : use a custom jump list
564 DeleteJumpList(APPID
);
565 DoInitializeJumpList();
569 void CTortoiseProcApp::DoInitializeJumpList()
571 ATL::CComPtr
<ICustomDestinationList
> pcdl
;
572 HRESULT hr
= pcdl
.CoCreateInstance(CLSID_DestinationList
, NULL
, CLSCTX_INPROC_SERVER
);
576 hr
= pcdl
->SetAppID(APPID
);
581 ATL::CComPtr
<IObjectArray
> poaRemoved
;
582 hr
= pcdl
->BeginList(&uMaxSlots
, IID_PPV_ARGS(&poaRemoved
));
586 ATL::CComPtr
<IObjectCollection
> poc
;
587 hr
= poc
.CoCreateInstance(CLSID_EnumerableObjectCollection
, NULL
, CLSCTX_INPROC_SERVER
);
591 CString sTemp
= CString(MAKEINTRESOURCE(IDS_MENUSETTINGS
));
594 ATL::CComPtr
<IShellLink
> psl
;
595 hr
= CreateShellLink(_T("/command:settings"), (LPCTSTR
)sTemp
, 20, &psl
);
599 sTemp
= CString(MAKEINTRESOURCE(IDS_MENUHELP
));
601 psl
.Release(); // Need to release the object before calling operator&()
602 hr
= CreateShellLink(_T("/command:help"), (LPCTSTR
)sTemp
, 19, &psl
);
607 ATL::CComPtr
<IObjectArray
> poa
;
608 hr
= poc
.QueryInterface(&poa
);
610 pcdl
->AppendCategory((LPCTSTR
)CString(MAKEINTRESOURCE(IDS_PROC_TASKS
)), poa
);
615 int CTortoiseProcApp::ExitInstance()
617 Gdiplus::GdiplusShutdown(m_gdiplusToken
);
619 CWinAppEx::ExitInstance();
625 void CTortoiseProcApp::CheckForNewerVersion()
627 // check for newer versions
628 if (CRegDWORD(_T("Software\\TortoiseGit\\VersionCheck"), TRUE
) != FALSE
)
634 if ((now
!= 0) && (localtime_s(&ptm
, &now
)==0))
637 // Check daily for new preview releases
638 CRegDWORD oldday
= CRegDWORD(_T("Software\\TortoiseGit\\CheckNewerDay"), (DWORD
)-1);
639 if (((DWORD
)oldday
) == -1)
640 oldday
= ptm
.tm_yday
;
643 if ((DWORD
)oldday
!= (DWORD
)ptm
.tm_yday
)
645 oldday
= ptm
.tm_yday
;
648 // we don't calculate the real 'week of the year' here
649 // because just to decide if we should check for an update
650 // that's not needed.
651 week
= ptm
.tm_yday
/ 7;
653 CRegDWORD oldweek
= CRegDWORD(_T("Software\\TortoiseGit\\CheckNewerWeek"), (DWORD
)-1);
654 if (((DWORD
)oldweek
) == -1)
655 oldweek
= week
; // first start of TortoiseProc, no update check needed
658 if ((DWORD
)week
!= oldweek
)
662 TCHAR com
[MAX_PATH
+100];
663 GetModuleFileName(NULL
, com
, MAX_PATH
);
664 _tcscat_s(com
, MAX_PATH
+100, _T(" /command:updatecheck"));
666 CAppUtils::LaunchApplication(com
, 0, false);