1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2013 - TortoiseGit
4 // Copyright (C) 2006-2014 - 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.
22 #include "TortoiseMerge.h"
25 #include "CmdLineParser.h"
28 #include "PathUtils.h"
29 #include "BrowseFolder.h"
30 #include "DirFileEnum.h"
31 #include "SelectFileFilter.h"
32 #include "FileDlgEventHandler.h"
34 #include "TaskbarUUID.h"
40 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
42 BEGIN_MESSAGE_MAP(CTortoiseMergeApp
, CWinAppEx
)
43 ON_COMMAND(ID_APP_ABOUT
, OnAppAbout
)
46 class PatchOpenDlgEventHandler
: public CFileDlgEventHandler
49 PatchOpenDlgEventHandler() {}
50 ~PatchOpenDlgEventHandler() {}
52 virtual STDMETHODIMP
OnButtonClicked(IFileDialogCustomize
* pfdc
, DWORD dwIDCtl
)
56 CComQIPtr
<IFileOpenDialog
> pDlg
= pfdc
;
67 CTortoiseMergeApp::CTortoiseMergeApp()
71 m_bHiColorIcons
= TRUE
;
74 // The one and only CTortoiseMergeApp object
75 CTortoiseMergeApp theApp
;
77 #if ENABLE_CRASHHANLDER
78 CCrashReportTGit
g_crasher(L
"TortoiseGitMerge " _T(APP_X64_STRING
), TGIT_VERMAJOR
, TGIT_VERMINOR
, TGIT_VERMICRO
, TGIT_VERBUILD
, TGIT_VERDATE
);
81 CString g_sGroupingUUID
;
82 CString g_sGroupingIcon
;
83 bool g_bGroupingRemoveIcon
= false;
85 // CTortoiseMergeApp initialization
86 BOOL
CTortoiseMergeApp::InitInstance()
90 CCrashReport::Instance().AddUserInfoToReport(L
"CommandLine", GetCommandLine());
93 DWORD len
= GetCurrentDirectory(0, NULL
);
96 std::unique_ptr
<TCHAR
[]> originalCurrentDirectory(new TCHAR
[len
]);
97 if (GetCurrentDirectory(len
, originalCurrentDirectory
.get()))
99 sOrigCWD
= originalCurrentDirectory
.get();
100 sOrigCWD
= CPathUtils::GetLongPathname(sOrigCWD
);
105 //set the resource dll for the required language
106 CRegDWORD loc
= CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
109 HINSTANCE hInst
= NULL
;
112 langDll
.Format(_T("%sLanguages\\TortoiseMerge%ld.dll"), (LPCTSTR
)CPathUtils::GetAppParentDirectory(), langId
);
114 hInst
= LoadLibrary(langDll
);
115 CString sVer
= _T(STRPRODUCTVER
);
116 CString sFileVer
= CPathUtils::GetVersionFromFile(langDll
);
117 if (sFileVer
.Compare(sVer
)!=0)
123 AfxSetResourceHandle(hInst
);
126 DWORD lid
= SUBLANGID(langId
);
130 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
135 } while ((hInst
== NULL
) && (langId
!= 0));
136 TCHAR buf
[6] = { 0 };
137 _tcscpy_s(buf
, _T("en"));
139 CString sHelppath
= CPathUtils::GetAppDirectory() + _T("TortoiseMerge_en.chm");
140 free((void*)m_pszHelpFilePath
);
141 m_pszHelpFilePath
=_tcsdup(sHelppath
);
142 sHelppath
= CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseMerge_en.chm");
145 GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, _countof(buf
));
146 CString sLang
= _T("_");
148 sHelppath
.Replace(_T("_en"), sLang
);
149 if (PathFileExists(sHelppath
))
151 free((void*)m_pszHelpFilePath
);
152 m_pszHelpFilePath
=_tcsdup(sHelppath
);
155 sHelppath
.Replace(sLang
, _T("_en"));
156 GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, _countof(buf
));
159 sHelppath
.Replace(_T("_en"), sLang
);
160 if (PathFileExists(sHelppath
))
162 free((void*)m_pszHelpFilePath
);
163 m_pszHelpFilePath
=_tcsdup(sHelppath
);
166 sHelppath
.Replace(sLang
, _T("_en"));
168 DWORD lid
= SUBLANGID(langId
);
172 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
177 setlocale(LC_ALL
, "");
178 // We need to explicitly set the thread locale to the system default one to avoid possible problems with saving files in its original codepage
179 // The problems occures when the language of OS differs from the regional settings
180 // See the details here: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100887
181 SetThreadLocale(LOCALE_SYSTEM_DEFAULT
);
183 // InitCommonControls() is required on Windows XP if an application
184 // manifest specifies use of ComCtl32.dll version 6 or later to enable
185 // visual styles. Otherwise, any window creation will fail.
186 InitCommonControls();
188 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows
));
189 CMFCButton::EnableWindowsTheming();
190 EnableTaskbarInteraction(FALSE
);
192 // Initialize all Managers for usage. They are automatically constructed
193 // if not yet present
194 InitContextMenuManager();
195 InitKeyboardManager();
196 InitTooltipManager ();
197 CMFCToolTipInfo params
;
198 params
.m_bVislManagerTheme
= TRUE
;
200 GetTooltipManager ()->SetTooltipParams (
201 AFX_TOOLTIP_TYPE_ALL
,
202 RUNTIME_CLASS (CMFCToolTipCtrl
),
205 CCmdLineParser parser
= CCmdLineParser(this->m_lpCmdLine
);
207 g_sGroupingUUID
= parser
.GetVal(L
"groupuuid");
209 if (parser
.HasKey(_T("?")) || parser
.HasKey(_T("help")))
212 sHelpText
.LoadString(IDS_COMMANDLINEHELP
);
213 MessageBox(NULL
, sHelpText
, _T("TortoiseGitMerge"), MB_ICONINFORMATION
);
217 // Initialize OLE libraries
220 AfxMessageBox(IDP_OLE_INIT_FAILED
);
223 AfxEnableControlContainer();
224 // Standard initialization
225 // If you are not using these features and wish to reduce the size
226 // of your final executable, you should remove from the following
227 // the specific initialization routines you do not need
228 // Change the registry key under which our settings are stored
229 SetRegistryKey(_T("TortoiseGitMerge"));
231 if (CRegDWORD(_T("Software\\TortoiseGitMerge\\Debug"), FALSE
)==TRUE
)
232 AfxMessageBox(AfxGetApp()->m_lpCmdLine
, MB_OK
| MB_ICONINFORMATION
);
234 // To create the main window, this code creates a new frame window
235 // object and then sets it as the application's main window object
236 CMainFrame
* pFrame
= new CMainFrame
;
241 // create and load the frame with its resources
242 if (!pFrame
->LoadFrame(IDR_MAINFRAME
, WS_OVERLAPPEDWINDOW
| FWS_ADDTOTITLE
, NULL
, NULL
))
245 // Fill in the command line options
246 pFrame
->m_Data
.m_baseFile
.SetFileName(parser
.GetVal(_T("base")));
247 pFrame
->m_Data
.m_baseFile
.SetDescriptiveName(parser
.GetVal(_T("basename")));
248 pFrame
->m_Data
.m_theirFile
.SetFileName(parser
.GetVal(_T("theirs")));
249 pFrame
->m_Data
.m_theirFile
.SetDescriptiveName(parser
.GetVal(_T("theirsname")));
250 pFrame
->m_Data
.m_yourFile
.SetFileName(parser
.GetVal(_T("mine")));
251 pFrame
->m_Data
.m_yourFile
.SetDescriptiveName(parser
.GetVal(_T("minename")));
252 pFrame
->m_Data
.m_mergedFile
.SetFileName(parser
.GetVal(_T("merged")));
253 pFrame
->m_Data
.m_mergedFile
.SetDescriptiveName(parser
.GetVal(_T("mergedname")));
254 pFrame
->m_Data
.m_sPatchPath
= parser
.HasVal(_T("patchpath")) ? parser
.GetVal(_T("patchpath")) : _T("");
255 pFrame
->m_Data
.m_sPatchPath
.Replace('/', '\\');
256 if (parser
.HasKey(_T("patchoriginal")))
257 pFrame
->m_Data
.m_sPatchOriginal
= parser
.GetVal(_T("patchoriginal"));
258 if (parser
.HasKey(_T("patchpatched")))
259 pFrame
->m_Data
.m_sPatchPatched
= parser
.GetVal(_T("patchpatched"));
260 pFrame
->m_Data
.m_sDiffFile
= parser
.GetVal(_T("diff"));
261 pFrame
->m_Data
.m_sDiffFile
.Replace('/', '\\');
262 if (parser
.HasKey(_T("oneway")))
263 pFrame
->m_bOneWay
= TRUE
;
264 if (parser
.HasKey(_T("diff")))
265 pFrame
->m_bOneWay
= FALSE
;
266 if (parser
.HasKey(_T("reversedpatch")))
267 pFrame
->m_bReversedPatch
= TRUE
;
268 if (parser
.HasKey(_T("saverequired")))
269 pFrame
->m_bSaveRequired
= true;
270 if (parser
.HasKey(_T("saverequiredonconflicts")))
271 pFrame
->m_bSaveRequiredOnConflicts
= true;
272 if (pFrame
->m_Data
.IsBaseFileInUse() && !pFrame
->m_Data
.IsYourFileInUse() && pFrame
->m_Data
.IsTheirFileInUse())
274 pFrame
->m_Data
.m_yourFile
.TransferDetailsFrom(pFrame
->m_Data
.m_theirFile
);
277 if ((!parser
.HasKey(_T("patchpath")))&&(parser
.HasVal(_T("diff"))))
279 // a patchfile was given, but not folder path to apply the patch to
280 // If the patchfile is located inside a working copy, then use the parent directory
281 // of the patchfile as the target directory, otherwise ask the user for a path.
282 if (parser
.HasKey(_T("wc")))
283 pFrame
->m_Data
.m_sPatchPath
= pFrame
->m_Data
.m_sDiffFile
.Left(pFrame
->m_Data
.m_sDiffFile
.ReverseFind('\\'));
286 CBrowseFolder fbrowser
;
287 fbrowser
.m_style
= BIF_EDITBOX
| BIF_NEWDIALOGSTYLE
| BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
;
288 if (fbrowser
.Show(NULL
, pFrame
->m_Data
.m_sPatchPath
)==CBrowseFolder::CANCEL
)
293 if ((parser
.HasKey(_T("patchpath")))&&(!parser
.HasVal(_T("diff"))))
295 // A path was given for applying a patchfile, but
296 // the patchfile itself was not.
297 // So ask the user for that patchfile
300 // Create a new common save file dialog
301 CComPtr
<IFileOpenDialog
> pfd
= NULL
;
302 hr
= pfd
.CoCreateInstance(CLSID_FileOpenDialog
, NULL
, CLSCTX_INPROC_SERVER
);
305 // Set the dialog options
307 if (SUCCEEDED(hr
= pfd
->GetOptions(&dwOptions
)))
309 hr
= pfd
->SetOptions(dwOptions
| FOS_FILEMUSTEXIST
| FOS_FORCEFILESYSTEM
| FOS_PATHMUSTEXIST
);
316 temp
.LoadString(IDS_OPENDIFFFILETITLE
);
319 CSelectFileFilter
fileFilter(IDS_PATCHFILEFILTER
);
320 hr
= pfd
->SetFileTypes(fileFilter
.GetCount(), fileFilter
);
321 bool bAdvised
= false;
323 CComObjectStackEx
<PatchOpenDlgEventHandler
> cbk
;
324 CComQIPtr
<IFileDialogEvents
> pEvents
= cbk
.GetUnknown();
327 CComPtr
<IFileDialogCustomize
> pfdCustomize
;
328 hr
= pfd
->QueryInterface(IID_PPV_ARGS(&pfdCustomize
));
331 // check if there's a unified diff on the clipboard and
332 // add a button to the fileopen dialog if there is.
333 UINT cFormat
= RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
334 if ((cFormat
)&&(OpenClipboard(NULL
)))
336 HGLOBAL hglb
= GetClipboardData(cFormat
);
339 pfdCustomize
->AddPushButton(101, CString(MAKEINTRESOURCE(IDS_PATCH_COPYFROMCLIPBOARD
)));
340 hr
= pfd
->Advise(pEvents
, &dwCookie
);
341 bAdvised
= SUCCEEDED(hr
);
348 // Show the save file dialog
349 if (SUCCEEDED(hr
) && SUCCEEDED(hr
= pfd
->Show(pFrame
->m_hWnd
)))
351 // Get the selection from the user
352 CComPtr
<IShellItem
> psiResult
= NULL
;
353 hr
= pfd
->GetResult(&psiResult
);
355 pfd
->Unadvise(dwCookie
);
358 PWSTR pszPath
= NULL
;
359 hr
= psiResult
->GetDisplayName(SIGDN_FILESYSPATH
, &pszPath
);
362 pFrame
->m_Data
.m_sDiffFile
= pszPath
;
363 CoTaskMemFree(pszPath
);
368 // no result, which means we closed the dialog in our button handler
369 std::wstring sTempFile
;
370 if (TrySavePatchFromClipboard(sTempFile
))
371 pFrame
->m_Data
.m_sDiffFile
= sTempFile
.c_str();
377 pfd
->Unadvise(dwCookie
);
383 OPENFILENAME ofn
= {0}; // common dialog box structure
384 TCHAR szFile
[MAX_PATH
] = {0}; // buffer for file name
385 // Initialize OPENFILENAME
386 ofn
.lStructSize
= sizeof(OPENFILENAME
);
387 ofn
.hwndOwner
= pFrame
->m_hWnd
;
388 ofn
.lpstrFile
= szFile
;
389 ofn
.nMaxFile
= _countof(szFile
);
391 temp
.LoadString(IDS_OPENDIFFFILETITLE
);
393 ofn
.lpstrTitle
= NULL
;
395 ofn
.lpstrTitle
= temp
;
397 ofn
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_EXPLORER
;
398 if( HasClipboardPatch() ) {
399 ofn
.Flags
|= ( OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
);
400 ofn
.hInstance
= AfxGetResourceHandle();
401 ofn
.lpTemplateName
= MAKEINTRESOURCE(IDD_PATCH_FILE_OPEN_CUSTOM
);
402 ofn
.lpfnHook
= CreatePatchFileOpenHook
;
405 CSelectFileFilter
fileFilter(IDS_PATCHFILEFILTER
);
406 ofn
.lpstrFilter
= fileFilter
;
407 ofn
.nFilterIndex
= 1;
409 // Display the Open dialog box.
410 if (GetOpenFileName(&ofn
)==FALSE
)
414 pFrame
->m_Data
.m_sDiffFile
= ofn
.lpstrFile
;
418 if ( pFrame
->m_Data
.m_baseFile
.GetFilename().IsEmpty() && pFrame
->m_Data
.m_yourFile
.GetFilename().IsEmpty() )
421 LPWSTR
*szArglist
= CommandLineToArgvW(GetCommandLineW(), &nArgs
);
422 if( NULL
== szArglist
)
424 TRACE("CommandLineToArgvW failed\n");
428 if ( nArgs
==3 || nArgs
==4 )
434 // [3]: THEIR file (optional)
435 // This is the same format CAppUtils::StartExtDiff
436 // uses if %base and %mine are not set and most
437 // other diff tools use it too.
438 if ( PathFileExists(szArglist
[1]) && PathFileExists(szArglist
[2]) )
440 pFrame
->m_Data
.m_baseFile
.SetFileName(szArglist
[1]);
441 pFrame
->m_Data
.m_yourFile
.SetFileName(szArglist
[2]);
442 if ( nArgs
== 4 && PathFileExists(szArglist
[3]) )
444 pFrame
->m_Data
.m_theirFile
.SetFileName(szArglist
[3]);
450 // Free memory allocated for CommandLineToArgvW arguments.
451 LocalFree(szArglist
);
454 pFrame
->m_bReadOnly
= !!parser
.HasKey(_T("readonly"));
455 if (GetFileAttributes(pFrame
->m_Data
.m_yourFile
.GetFilename()) & FILE_ATTRIBUTE_READONLY
)
456 pFrame
->m_bReadOnly
= true;
457 pFrame
->m_bBlame
= !!parser
.HasKey(_T("blame"));
458 // diffing a blame means no editing!
459 if (pFrame
->m_bBlame
)
460 pFrame
->m_bReadOnly
= true;
462 pFrame
->SetWindowTitle();
464 if (parser
.HasKey(_T("createunifieddiff")))
466 // user requested to create a unified diff file
467 CString origFile
= parser
.GetVal(_T("origfile"));
468 CString modifiedFile
= parser
.GetVal(_T("modifiedfile"));
469 if (!origFile
.IsEmpty() && !modifiedFile
.IsEmpty())
471 CString outfile
= parser
.GetVal(_T("outfile"));
472 if (outfile
.IsEmpty())
474 CCommonAppUtils::FileOpenSave(outfile
, NULL
, IDS_SAVEASTITLE
, IDS_COMMONFILEFILTER
, false, NULL
);
476 if (!outfile
.IsEmpty())
478 CAppUtils::CreateUnifiedDiff(origFile
, modifiedFile
, outfile
, false);
485 pFrame
->resolveMsgWnd
= parser
.HasVal(L
"resolvemsghwnd") ? (HWND
)parser
.GetLongLongVal(L
"resolvemsghwnd") : 0;
486 pFrame
->resolveMsgWParam
= parser
.HasVal(L
"resolvemsgwparam") ? (WPARAM
)parser
.GetLongLongVal(L
"resolvemsgwparam") : 0;
487 pFrame
->resolveMsgLParam
= parser
.HasVal(L
"resolvemsglparam") ? (LPARAM
)parser
.GetLongLongVal(L
"resolvemsglparam") : 0;
490 // The one and only window has been initialized, so show and update it
491 pFrame
->ActivateFrame();
492 pFrame
->ShowWindow(SW_SHOW
);
493 pFrame
->UpdateWindow();
494 pFrame
->ShowDiffBar(!pFrame
->m_bOneWay
);
495 if (!pFrame
->m_Data
.IsBaseFileInUse() && pFrame
->m_Data
.m_sPatchPath
.IsEmpty() && pFrame
->m_Data
.m_sDiffFile
.IsEmpty())
497 pFrame
->OnFileOpen();
502 if (parser
.HasVal(_T("line")))
504 line
= parser
.GetLongVal(_T("line"));
505 line
--; // we need the index
508 return pFrame
->LoadViews(line
);
511 // CTortoiseMergeApp message handlers
513 void CTortoiseMergeApp::OnAppAbout()
520 CTortoiseMergeApp::CreatePatchFileOpenHook(HWND hDlg
, UINT uiMsg
, WPARAM wParam
, LPARAM
/*lParam*/)
522 if(uiMsg
== WM_COMMAND
&& LOWORD(wParam
) == IDC_PATCH_TO_CLIPBOARD
)
524 HWND hFileDialog
= GetParent(hDlg
);
526 // if there's a patchfile in the clipboard, we save it
527 // to a temporary file and tell TortoiseMerge to use that one
528 std::wstring sTempFile
;
529 if (TrySavePatchFromClipboard(sTempFile
))
531 CommDlg_OpenSave_SetControlText(hFileDialog
, edt1
, sTempFile
.c_str());
532 PostMessage(hFileDialog
, WM_COMMAND
, MAKEWPARAM(IDOK
, BM_CLICK
), (LPARAM
)(GetDlgItem(hDlg
, IDOK
)));
538 int CTortoiseMergeApp::ExitInstance()
540 // Look for temporary files left around by TortoiseMerge and
541 // remove them. But only delete 'old' files
542 CTempFiles::DeleteOldTempFiles(_T("*tsm*.*"));
544 return CWinAppEx::ExitInstance();
547 bool CTortoiseMergeApp::HasClipboardPatch()
549 // check if there's a patchfile in the clipboard
550 const UINT cFormat
= RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
554 if (OpenClipboard(NULL
) == 0)
557 bool containsPatch
= false;
561 if (enumFormat
== cFormat
)
563 containsPatch
= true; // yes, there's a patchfile in the clipboard
565 } while((enumFormat
= EnumClipboardFormats(enumFormat
))!=0);
568 return containsPatch
;
571 bool CTortoiseMergeApp::TrySavePatchFromClipboard(std::wstring
& resultFile
)
575 UINT cFormat
= RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
578 if (OpenClipboard(NULL
) == 0)
581 HGLOBAL hglb
= GetClipboardData(cFormat
);
582 LPCSTR lpstr
= (LPCSTR
)GlobalLock(hglb
);
584 DWORD len
= GetTempPath(0, NULL
);
585 std::unique_ptr
<TCHAR
[]> path(new TCHAR
[len
+1]);
586 std::unique_ptr
<TCHAR
[]> tempF(new TCHAR
[len
+100]);
587 GetTempPath (len
+1, path
.get());
588 GetTempFileName (path
.get(), _T("tsm"), 0, tempF
.get());
589 std::wstring sTempFile
= std::wstring(tempF
.get());
592 _tfopen_s(&outFile
, sTempFile
.c_str(), _T("wb"));
595 size_t patchlen
= strlen(lpstr
);
596 size_t size
= fwrite(lpstr
, sizeof(char), patchlen
, outFile
);
597 if (size
== patchlen
)
598 resultFile
= sTempFile
;
605 return !resultFile
.empty();