renamed ITEMIS_SUBMODULE to ITEMIS_SUBMODULECONTAINER
[TortoiseGit.git] / src / TortoiseMerge / AppUtils.cpp
blob0da5c72275f0e9ad141305a8977832395699bb63
1 // TortoiseMerge - a Diff/Patch program
3 // Copyright (C) 2006-2009 - 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.
19 #include "StdAfx.h"
20 #include "Registry.h"
21 #include "AppUtils.h"
22 #include "PathUtils.h"
23 #include "UnicodeUtils.h"
24 #include "SysProgressDlg.h"
26 #include "svn_pools.h"
27 #include "svn_io.h"
28 #include "svn_path.h"
29 #include "svn_diff.h"
30 #include "svn_string.h"
31 #include "svn_utf.h"
32 #include "git.h"
33 #include "MessageBox.h"
35 CAppUtils::CAppUtils(void)
39 CAppUtils::~CAppUtils(void)
43 BOOL CAppUtils::GetVersionedFile(CString sPath, CString sVersion, CString sSavePath, CSysProgressDlg * progDlg, HWND hWnd /*=NULL*/)
45 CString sSCMPath = CRegString(_T("Software\\TortoiseMerge\\SCMPath"), _T(""));
46 if (sSCMPath.IsEmpty())
48 // no path set, so use TortoiseSVN as default
49 sSCMPath = CPathUtils::GetAppDirectory() + _T("TortoiseProc.exe");
50 sSCMPath += _T(" /command:cat /path:\"%1\" /revision:%2 /savepath:\"%3\" /hwnd:%4");
52 CString sTemp;
53 sTemp.Format(_T("%d"), hWnd);
54 sSCMPath.Replace(_T("%1"), sPath);
55 sSCMPath.Replace(_T("%2"), sVersion);
56 sSCMPath.Replace(_T("%3"), sSavePath);
57 sSCMPath.Replace(_T("%4"), sTemp);
58 // start the external SCM program to fetch the specific version of the file
59 STARTUPINFO startup;
60 PROCESS_INFORMATION process;
61 memset(&startup, 0, sizeof(startup));
62 startup.cb = sizeof(startup);
63 memset(&process, 0, sizeof(process));
64 if (CreateProcess(NULL, (LPTSTR)(LPCTSTR)sSCMPath, NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
66 LPVOID lpMsgBuf;
67 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
68 FORMAT_MESSAGE_FROM_SYSTEM |
69 FORMAT_MESSAGE_IGNORE_INSERTS,
70 NULL,
71 GetLastError(),
72 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
73 (LPTSTR) &lpMsgBuf,
75 NULL
77 MessageBox(NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseMerge"), MB_OK | MB_ICONERROR);
78 LocalFree( lpMsgBuf );
80 DWORD ret = 0;
83 ret = WaitForSingleObject(process.hProcess, 100);
84 } while ((ret == WAIT_TIMEOUT) && (!progDlg->HasUserCancelled()));
85 CloseHandle(process.hThread);
86 CloseHandle(process.hProcess);
88 if (progDlg->HasUserCancelled())
90 return FALSE;
92 if (!PathFileExists(sSavePath))
93 return FALSE;
94 return TRUE;
97 bool CAppUtils::CreateUnifiedDiff(const CString& orig, const CString& modified, const CString& output, bool bShowError)
99 CString cmd;
100 cmd.Format(_T("git.exe diff --no-index \"%s\" \"%s\""),orig, modified);
102 if(g_Git.RunLogFile(cmd,(CString&)output) && bShowError)
104 CMessageBox::Show(NULL, _T("Fail Create Patch"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
105 return false;
107 return true;
110 CString CAppUtils::GetErrorString(svn_error_t * Err)
112 CString msg;
113 CString temp;
114 char errbuf[256];
116 if (Err != NULL)
118 svn_error_t * ErrPtr = Err;
119 if (ErrPtr->message)
120 msg = CUnicodeUtils::GetUnicode(ErrPtr->message);
121 else
123 /* Is this a Subversion-specific error code? */
124 if ((ErrPtr->apr_err > APR_OS_START_USEERR)
125 && (ErrPtr->apr_err <= APR_OS_START_CANONERR))
126 msg = svn_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf));
127 /* Otherwise, this must be an APR error code. */
128 else
130 svn_error_t *temp_err = NULL;
131 const char * err_string = NULL;
132 temp_err = svn_utf_cstring_to_utf8(&err_string, apr_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf)-1), ErrPtr->pool);
133 if (temp_err)
135 svn_error_clear (temp_err);
136 msg = _T("Can't recode error string from APR");
138 else
140 msg = CUnicodeUtils::GetUnicode(err_string);
144 while (ErrPtr->child)
146 ErrPtr = ErrPtr->child;
147 msg += _T("\n");
148 if (ErrPtr->message)
149 temp = CUnicodeUtils::GetUnicode(ErrPtr->message);
150 else
152 /* Is this a Subversion-specific error code? */
153 if ((ErrPtr->apr_err > APR_OS_START_USEERR)
154 && (ErrPtr->apr_err <= APR_OS_START_CANONERR))
155 temp = svn_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf));
156 /* Otherwise, this must be an APR error code. */
157 else
159 svn_error_t *temp_err = NULL;
160 const char * err_string = NULL;
161 temp_err = svn_utf_cstring_to_utf8(&err_string, apr_strerror (ErrPtr->apr_err, errbuf, sizeof (errbuf)-1), ErrPtr->pool);
162 if (temp_err)
164 svn_error_clear (temp_err);
165 temp = _T("Can't recode error string from APR");
167 else
169 temp = CUnicodeUtils::GetUnicode(err_string);
173 msg += temp;
175 return msg;
177 return _T("");
180 bool CAppUtils::HasClipboardFormat(UINT format)
182 if (OpenClipboard(NULL))
184 UINT enumFormat = 0;
187 if (enumFormat == format)
189 CloseClipboard();
190 return true;
192 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
193 CloseClipboard();
195 return false;