1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2013-2014 - 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"
35 #include "git2/threads.h"
41 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
43 BEGIN_MESSAGE_MAP(CTortoiseMergeApp
, CWinAppEx
)
44 ON_COMMAND(ID_APP_ABOUT
, OnAppAbout
)
47 class PatchOpenDlgEventHandler
: public CFileDlgEventHandler
50 PatchOpenDlgEventHandler() {}
51 ~PatchOpenDlgEventHandler() {}
53 virtual STDMETHODIMP
OnButtonClicked(IFileDialogCustomize
* pfdc
, DWORD dwIDCtl
)
57 CComQIPtr
<IFileOpenDialog
> pDlg
= pfdc
;
68 CTortoiseMergeApp::CTortoiseMergeApp()
72 m_bHiColorIcons
= TRUE
;
76 CTortoiseMergeApp::~CTortoiseMergeApp()
78 git_threads_shutdown();
81 // The one and only CTortoiseMergeApp object
82 CTortoiseMergeApp theApp
;
84 #if ENABLE_CRASHHANLDER
85 CCrashReportTGit
g_crasher(L
"TortoiseGitMerge " _T(APP_X64_STRING
), TGIT_VERMAJOR
, TGIT_VERMINOR
, TGIT_VERMICRO
, TGIT_VERBUILD
, TGIT_VERDATE
);
88 CString g_sGroupingUUID
;
89 CString g_sGroupingIcon
;
90 bool g_bGroupingRemoveIcon
= false;
92 // CTortoiseMergeApp initialization
93 BOOL
CTortoiseMergeApp::InitInstance()
97 CCrashReport::Instance().AddUserInfoToReport(L
"CommandLine", GetCommandLine());
100 DWORD len
= GetCurrentDirectory(0, NULL
);
103 std::unique_ptr
<TCHAR
[]> originalCurrentDirectory(new TCHAR
[len
]);
104 if (GetCurrentDirectory(len
, originalCurrentDirectory
.get()))
106 sOrigCWD
= originalCurrentDirectory
.get();
107 sOrigCWD
= CPathUtils::GetLongPathname(sOrigCWD
);
112 //set the resource dll for the required language
113 CRegDWORD loc
= CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
116 HINSTANCE hInst
= NULL
;
119 langDll
.Format(_T("%sLanguages\\TortoiseMerge%ld.dll"), (LPCTSTR
)CPathUtils::GetAppParentDirectory(), langId
);
121 hInst
= LoadLibrary(langDll
);
122 CString sVer
= _T(STRPRODUCTVER
);
123 CString sFileVer
= CPathUtils::GetVersionFromFile(langDll
);
124 if (sFileVer
.Compare(sVer
)!=0)
130 AfxSetResourceHandle(hInst
);
133 DWORD lid
= SUBLANGID(langId
);
137 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
142 } while ((hInst
== NULL
) && (langId
!= 0));
143 TCHAR buf
[6] = { 0 };
144 _tcscpy_s(buf
, _T("en"));
146 CString sHelppath
= CPathUtils::GetAppDirectory() + _T("TortoiseMerge_en.chm");
147 free((void*)m_pszHelpFilePath
);
148 m_pszHelpFilePath
=_tcsdup(sHelppath
);
149 sHelppath
= CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseMerge_en.chm");
152 GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, _countof(buf
));
153 CString sLang
= _T("_");
155 sHelppath
.Replace(_T("_en"), sLang
);
156 if (PathFileExists(sHelppath
))
158 free((void*)m_pszHelpFilePath
);
159 m_pszHelpFilePath
=_tcsdup(sHelppath
);
162 sHelppath
.Replace(sLang
, _T("_en"));
163 GetLocaleInfo(MAKELCID(langId
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, _countof(buf
));
166 sHelppath
.Replace(_T("_en"), sLang
);
167 if (PathFileExists(sHelppath
))
169 free((void*)m_pszHelpFilePath
);
170 m_pszHelpFilePath
=_tcsdup(sHelppath
);
173 sHelppath
.Replace(sLang
, _T("_en"));
175 DWORD lid
= SUBLANGID(langId
);
179 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
184 setlocale(LC_ALL
, "");
185 // We need to explicitly set the thread locale to the system default one to avoid possible problems with saving files in its original codepage
186 // The problems occures when the language of OS differs from the regional settings
187 // See the details here: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100887
188 SetThreadLocale(LOCALE_SYSTEM_DEFAULT
);
190 // InitCommonControls() is required on Windows XP if an application
191 // manifest specifies use of ComCtl32.dll version 6 or later to enable
192 // visual styles. Otherwise, any window creation will fail.
193 InitCommonControls();
195 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows
));
196 CMFCButton::EnableWindowsTheming();
197 EnableTaskbarInteraction(FALSE
);
199 // Initialize all Managers for usage. They are automatically constructed
200 // if not yet present
201 InitContextMenuManager();
202 InitKeyboardManager();
203 InitTooltipManager ();
204 CMFCToolTipInfo params
;
205 params
.m_bVislManagerTheme
= TRUE
;
207 GetTooltipManager ()->SetTooltipParams (
208 AFX_TOOLTIP_TYPE_ALL
,
209 RUNTIME_CLASS (CMFCToolTipCtrl
),
212 CCmdLineParser parser
= CCmdLineParser(this->m_lpCmdLine
);
214 g_sGroupingUUID
= parser
.GetVal(L
"groupuuid");
216 if (parser
.HasKey(_T("?")) || parser
.HasKey(_T("help")))
219 sHelpText
.LoadString(IDS_COMMANDLINEHELP
);
220 MessageBox(NULL
, sHelpText
, _T("TortoiseGitMerge"), MB_ICONINFORMATION
);
224 // Initialize OLE libraries
227 AfxMessageBox(IDP_OLE_INIT_FAILED
);
230 AfxEnableControlContainer();
231 // Standard initialization
232 // If you are not using these features and wish to reduce the size
233 // of your final executable, you should remove from the following
234 // the specific initialization routines you do not need
235 // Change the registry key under which our settings are stored
236 SetRegistryKey(_T("TortoiseGitMerge"));
238 if (CRegDWORD(_T("Software\\TortoiseGitMerge\\Debug"), FALSE
)==TRUE
)
239 AfxMessageBox(AfxGetApp()->m_lpCmdLine
, MB_OK
| MB_ICONINFORMATION
);
241 // To create the main window, this code creates a new frame window
242 // object and then sets it as the application's main window object
243 CMainFrame
* pFrame
= new CMainFrame
;
248 // create and load the frame with its resources
249 if (!pFrame
->LoadFrame(IDR_MAINFRAME
, WS_OVERLAPPEDWINDOW
| FWS_ADDTOTITLE
, NULL
, NULL
))
252 // Fill in the command line options
253 pFrame
->m_Data
.m_baseFile
.SetFileName(parser
.GetVal(_T("base")));
254 pFrame
->m_Data
.m_baseFile
.SetDescriptiveName(parser
.GetVal(_T("basename")));
255 pFrame
->m_Data
.m_baseFile
.SetReflectedName(parser
.GetVal(_T("basereflectedname")));
256 pFrame
->m_Data
.m_theirFile
.SetFileName(parser
.GetVal(_T("theirs")));
257 pFrame
->m_Data
.m_theirFile
.SetDescriptiveName(parser
.GetVal(_T("theirsname")));
258 pFrame
->m_Data
.m_theirFile
.SetReflectedName(parser
.GetVal(_T("theirsreflectedname")));
259 pFrame
->m_Data
.m_yourFile
.SetFileName(parser
.GetVal(_T("mine")));
260 pFrame
->m_Data
.m_yourFile
.SetDescriptiveName(parser
.GetVal(_T("minename")));
261 pFrame
->m_Data
.m_yourFile
.SetReflectedName(parser
.GetVal(_T("minereflectedname")));
262 pFrame
->m_Data
.m_mergedFile
.SetFileName(parser
.GetVal(_T("merged")));
263 pFrame
->m_Data
.m_mergedFile
.SetDescriptiveName(parser
.GetVal(_T("mergedname")));
264 pFrame
->m_Data
.m_mergedFile
.SetReflectedName(parser
.GetVal(_T("mergedreflectedname")));
265 pFrame
->m_Data
.m_sPatchPath
= parser
.HasVal(_T("patchpath")) ? parser
.GetVal(_T("patchpath")) : _T("");
266 pFrame
->m_Data
.m_sPatchPath
.Replace('/', '\\');
267 if (parser
.HasKey(_T("patchoriginal")))
268 pFrame
->m_Data
.m_sPatchOriginal
= parser
.GetVal(_T("patchoriginal"));
269 if (parser
.HasKey(_T("patchpatched")))
270 pFrame
->m_Data
.m_sPatchPatched
= parser
.GetVal(_T("patchpatched"));
271 pFrame
->m_Data
.m_sDiffFile
= parser
.GetVal(_T("diff"));
272 pFrame
->m_Data
.m_sDiffFile
.Replace('/', '\\');
273 if (parser
.HasKey(_T("oneway")))
274 pFrame
->m_bOneWay
= TRUE
;
275 if (parser
.HasKey(_T("diff")))
276 pFrame
->m_bOneWay
= FALSE
;
277 if (parser
.HasKey(_T("reversedpatch")))
278 pFrame
->m_bReversedPatch
= TRUE
;
279 if (parser
.HasKey(_T("saverequired")))
280 pFrame
->m_bSaveRequired
= true;
281 if (parser
.HasKey(_T("saverequiredonconflicts")))
282 pFrame
->m_bSaveRequiredOnConflicts
= true;
283 if (pFrame
->m_Data
.IsBaseFileInUse() && !pFrame
->m_Data
.IsYourFileInUse() && pFrame
->m_Data
.IsTheirFileInUse())
285 pFrame
->m_Data
.m_yourFile
.TransferDetailsFrom(pFrame
->m_Data
.m_theirFile
);
288 if ((!parser
.HasKey(_T("patchpath")))&&(parser
.HasVal(_T("diff"))))
290 // a patchfile was given, but not folder path to apply the patch to
291 // If the patchfile is located inside a working copy, then use the parent directory
292 // of the patchfile as the target directory, otherwise ask the user for a path.
293 if (parser
.HasKey(_T("wc")))
294 pFrame
->m_Data
.m_sPatchPath
= pFrame
->m_Data
.m_sDiffFile
.Left(pFrame
->m_Data
.m_sDiffFile
.ReverseFind('\\'));
297 CBrowseFolder fbrowser
;
298 fbrowser
.m_style
= BIF_EDITBOX
| BIF_NEWDIALOGSTYLE
| BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
;
299 if (fbrowser
.Show(NULL
, pFrame
->m_Data
.m_sPatchPath
)==CBrowseFolder::CANCEL
)
304 if ((parser
.HasKey(_T("patchpath")))&&(!parser
.HasVal(_T("diff"))))
306 // A path was given for applying a patchfile, but
307 // the patchfile itself was not.
308 // So ask the user for that patchfile
311 // Create a new common save file dialog
312 CComPtr
<IFileOpenDialog
> pfd
= NULL
;
313 hr
= pfd
.CoCreateInstance(CLSID_FileOpenDialog
, NULL
, CLSCTX_INPROC_SERVER
);
316 // Set the dialog options
318 if (SUCCEEDED(hr
= pfd
->GetOptions(&dwOptions
)))
320 hr
= pfd
->SetOptions(dwOptions
| FOS_FILEMUSTEXIST
| FOS_FORCEFILESYSTEM
| FOS_PATHMUSTEXIST
);
327 temp
.LoadString(IDS_OPENDIFFFILETITLE
);
330 CSelectFileFilter
fileFilter(IDS_PATCHFILEFILTER
);
331 hr
= pfd
->SetFileTypes(fileFilter
.GetCount(), fileFilter
);
332 bool bAdvised
= false;
334 CComObjectStackEx
<PatchOpenDlgEventHandler
> cbk
;
335 CComQIPtr
<IFileDialogEvents
> pEvents
= cbk
.GetUnknown();
338 CComPtr
<IFileDialogCustomize
> pfdCustomize
;
339 hr
= pfd
->QueryInterface(IID_PPV_ARGS(&pfdCustomize
));
342 // check if there's a unified diff on the clipboard and
343 // add a button to the fileopen dialog if there is.
344 UINT cFormat
= RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
345 if ((cFormat
)&&(OpenClipboard(NULL
)))
347 HGLOBAL hglb
= GetClipboardData(cFormat
);
350 pfdCustomize
->AddPushButton(101, CString(MAKEINTRESOURCE(IDS_PATCH_COPYFROMCLIPBOARD
)));
351 hr
= pfd
->Advise(pEvents
, &dwCookie
);
352 bAdvised
= SUCCEEDED(hr
);
359 // Show the save file dialog
360 if (SUCCEEDED(hr
) && SUCCEEDED(hr
= pfd
->Show(pFrame
->m_hWnd
)))
362 // Get the selection from the user
363 CComPtr
<IShellItem
> psiResult
= NULL
;
364 hr
= pfd
->GetResult(&psiResult
);
366 pfd
->Unadvise(dwCookie
);
369 PWSTR pszPath
= NULL
;
370 hr
= psiResult
->GetDisplayName(SIGDN_FILESYSPATH
, &pszPath
);
373 pFrame
->m_Data
.m_sDiffFile
= pszPath
;
374 CoTaskMemFree(pszPath
);
379 // no result, which means we closed the dialog in our button handler
380 std::wstring sTempFile
;
381 if (TrySavePatchFromClipboard(sTempFile
))
382 pFrame
->m_Data
.m_sDiffFile
= sTempFile
.c_str();
388 pfd
->Unadvise(dwCookie
);
394 OPENFILENAME ofn
= {0}; // common dialog box structure
395 TCHAR szFile
[MAX_PATH
] = {0}; // buffer for file name
396 // Initialize OPENFILENAME
397 ofn
.lStructSize
= sizeof(OPENFILENAME
);
398 ofn
.hwndOwner
= pFrame
->m_hWnd
;
399 ofn
.lpstrFile
= szFile
;
400 ofn
.nMaxFile
= _countof(szFile
);
402 temp
.LoadString(IDS_OPENDIFFFILETITLE
);
404 ofn
.lpstrTitle
= NULL
;
406 ofn
.lpstrTitle
= temp
;
408 ofn
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_EXPLORER
;
409 if( HasClipboardPatch() ) {
410 ofn
.Flags
|= ( OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
);
411 ofn
.hInstance
= AfxGetResourceHandle();
412 ofn
.lpTemplateName
= MAKEINTRESOURCE(IDD_PATCH_FILE_OPEN_CUSTOM
);
413 ofn
.lpfnHook
= CreatePatchFileOpenHook
;
416 CSelectFileFilter
fileFilter(IDS_PATCHFILEFILTER
);
417 ofn
.lpstrFilter
= fileFilter
;
418 ofn
.nFilterIndex
= 1;
420 // Display the Open dialog box.
421 if (GetOpenFileName(&ofn
)==FALSE
)
425 pFrame
->m_Data
.m_sDiffFile
= ofn
.lpstrFile
;
429 if ( pFrame
->m_Data
.m_baseFile
.GetFilename().IsEmpty() && pFrame
->m_Data
.m_yourFile
.GetFilename().IsEmpty() )
432 LPWSTR
*szArglist
= CommandLineToArgvW(GetCommandLineW(), &nArgs
);
433 if( NULL
== szArglist
)
435 TRACE("CommandLineToArgvW failed\n");
439 if ( nArgs
==3 || nArgs
==4 )
445 // [3]: THEIR file (optional)
446 // This is the same format CAppUtils::StartExtDiff
447 // uses if %base and %mine are not set and most
448 // other diff tools use it too.
449 if ( PathFileExists(szArglist
[1]) && PathFileExists(szArglist
[2]) )
451 pFrame
->m_Data
.m_baseFile
.SetFileName(szArglist
[1]);
452 pFrame
->m_Data
.m_yourFile
.SetFileName(szArglist
[2]);
453 if ( nArgs
== 4 && PathFileExists(szArglist
[3]) )
455 pFrame
->m_Data
.m_theirFile
.SetFileName(szArglist
[3]);
461 // only one path specified: use it to fill the "open" dialog
462 if (PathFileExists(szArglist
[1]))
464 pFrame
->m_Data
.m_yourFile
.SetFileName(szArglist
[1]);
465 pFrame
->m_Data
.m_yourFile
.StoreFileAttributes();
470 // Free memory allocated for CommandLineToArgvW arguments.
471 LocalFree(szArglist
);
474 pFrame
->m_bReadOnly
= !!parser
.HasKey(_T("readonly"));
475 if (GetFileAttributes(pFrame
->m_Data
.m_yourFile
.GetFilename()) & FILE_ATTRIBUTE_READONLY
)
476 pFrame
->m_bReadOnly
= true;
477 pFrame
->m_bBlame
= !!parser
.HasKey(_T("blame"));
478 // diffing a blame means no editing!
479 if (pFrame
->m_bBlame
)
480 pFrame
->m_bReadOnly
= true;
482 pFrame
->SetWindowTitle();
484 if (parser
.HasKey(_T("createunifieddiff")))
486 // user requested to create a unified diff file
487 CString origFile
= parser
.GetVal(_T("origfile"));
488 CString modifiedFile
= parser
.GetVal(_T("modifiedfile"));
489 if (!origFile
.IsEmpty() && !modifiedFile
.IsEmpty())
491 CString outfile
= parser
.GetVal(_T("outfile"));
492 if (outfile
.IsEmpty())
494 CCommonAppUtils::FileOpenSave(outfile
, NULL
, IDS_SAVEASTITLE
, IDS_COMMONFILEFILTER
, false, NULL
);
496 if (!outfile
.IsEmpty())
498 CRegStdDWORD
regContextLines(L
"Software\\TortoiseGitMerge\\ContextLines", (DWORD
)-1);
499 CAppUtils::CreateUnifiedDiff(origFile
, modifiedFile
, outfile
, regContextLines
, false);
505 pFrame
->resolveMsgWnd
= parser
.HasVal(L
"resolvemsghwnd") ? (HWND
)parser
.GetLongLongVal(L
"resolvemsghwnd") : 0;
506 pFrame
->resolveMsgWParam
= parser
.HasVal(L
"resolvemsgwparam") ? (WPARAM
)parser
.GetLongLongVal(L
"resolvemsgwparam") : 0;
507 pFrame
->resolveMsgLParam
= parser
.HasVal(L
"resolvemsglparam") ? (LPARAM
)parser
.GetLongLongVal(L
"resolvemsglparam") : 0;
509 // The one and only window has been initialized, so show and update it
510 pFrame
->ActivateFrame();
511 pFrame
->ShowWindow(SW_SHOW
);
512 pFrame
->UpdateWindow();
513 pFrame
->ShowDiffBar(!pFrame
->m_bOneWay
);
514 if (!pFrame
->m_Data
.IsBaseFileInUse() && pFrame
->m_Data
.m_sPatchPath
.IsEmpty() && pFrame
->m_Data
.m_sDiffFile
.IsEmpty())
516 pFrame
->OnFileOpen(pFrame
->m_Data
.m_yourFile
.InUse());
521 if (parser
.HasVal(_T("line")))
523 line
= parser
.GetLongVal(_T("line"));
524 line
--; // we need the index
527 return pFrame
->LoadViews(line
);
530 // CTortoiseMergeApp message handlers
532 void CTortoiseMergeApp::OnAppAbout()
539 CTortoiseMergeApp::CreatePatchFileOpenHook(HWND hDlg
, UINT uiMsg
, WPARAM wParam
, LPARAM
/*lParam*/)
541 if(uiMsg
== WM_COMMAND
&& LOWORD(wParam
) == IDC_PATCH_TO_CLIPBOARD
)
543 HWND hFileDialog
= GetParent(hDlg
);
545 // if there's a patchfile in the clipboard, we save it
546 // to a temporary file and tell TortoiseMerge to use that one
547 std::wstring sTempFile
;
548 if (TrySavePatchFromClipboard(sTempFile
))
550 CommDlg_OpenSave_SetControlText(hFileDialog
, edt1
, sTempFile
.c_str());
551 PostMessage(hFileDialog
, WM_COMMAND
, MAKEWPARAM(IDOK
, BM_CLICK
), (LPARAM
)(GetDlgItem(hDlg
, IDOK
)));
557 int CTortoiseMergeApp::ExitInstance()
559 // Look for temporary files left around by TortoiseMerge and
560 // remove them. But only delete 'old' files
561 CTempFiles::DeleteOldTempFiles(_T("*tsm*.*"));
563 return CWinAppEx::ExitInstance();
566 bool CTortoiseMergeApp::HasClipboardPatch()
568 // check if there's a patchfile in the clipboard
569 const UINT cFormat
= RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
573 if (OpenClipboard(NULL
) == 0)
576 bool containsPatch
= false;
580 if (enumFormat
== cFormat
)
582 containsPatch
= true; // yes, there's a patchfile in the clipboard
584 } while((enumFormat
= EnumClipboardFormats(enumFormat
))!=0);
587 return containsPatch
;
590 bool CTortoiseMergeApp::TrySavePatchFromClipboard(std::wstring
& resultFile
)
594 UINT cFormat
= RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
597 if (OpenClipboard(NULL
) == 0)
600 HGLOBAL hglb
= GetClipboardData(cFormat
);
601 LPCSTR lpstr
= (LPCSTR
)GlobalLock(hglb
);
603 DWORD len
= GetTempPath(0, NULL
);
604 std::unique_ptr
<TCHAR
[]> path(new TCHAR
[len
+1]);
605 std::unique_ptr
<TCHAR
[]> tempF(new TCHAR
[len
+100]);
606 GetTempPath (len
+1, path
.get());
607 GetTempFileName (path
.get(), _T("tsm"), 0, tempF
.get());
608 std::wstring sTempFile
= std::wstring(tempF
.get());
611 _tfopen_s(&outFile
, sTempFile
.c_str(), _T("wb"));
614 size_t patchlen
= strlen(lpstr
);
615 size_t size
= fwrite(lpstr
, sizeof(char), patchlen
, outFile
);
616 if (size
== patchlen
)
617 resultFile
= sTempFile
;
624 return !resultFile
.empty();