Deduplicate code for retrieving libgit2 errors
authorSven Strickroth <email@cs-ware.de>
Sun, 10 Feb 2013 13:18:09 +0000 (10 14:18 +0100)
committerSven Strickroth <email@cs-ware.de>
Sun, 10 Feb 2013 13:22:12 +0000 (10 14:22 +0100)
Signed-off-by: Sven Strickroth <email@cs-ware.de>
src/Git/Git.cpp
src/Git/Git.h
src/TortoiseProc/Commands/CreateRepositoryCommand.cpp
src/TortoiseProc/GitProgressDlg.cpp
src/TortoiseProc/RepositoryBrowser.cpp

index 65771a7..185ccb8 100644 (file)
@@ -1207,22 +1207,14 @@ int CGit::GetTagList(STRING_VECTOR &list)
        }\r
 }\r
 \r
+/**\r
+Use this method only if m_IsUseLibGit2 is used for fallbacks.\r
+If you directly use libgit2 methods, use GetLibGit2LastErr instead.\r
+*/\r
 CString CGit::GetGitLastErr(CString msg)\r
 {\r
        if (this->m_IsUseLibGit2)\r
-       {\r
-               const git_error *libgit2err = giterr_last();\r
-               if (libgit2err)\r
-               {\r
-                       CString lastError = CUnicodeUtils::GetUnicode(CStringA(libgit2err->message));\r
-                       giterr_clear();\r
-                       return msg + _T("\nlibgit2 returned: ") + lastError;\r
-               }\r
-               else\r
-               {\r
-                       return msg + _T("\nUnknown libgit2 error.");\r
-               }\r
-       }\r
+               return GetLibGit2LastErr(msg);\r
        else if (gitLastErr.IsEmpty())\r
                return msg + _T("\nUnknown git.exe error.");\r
        else\r
@@ -1233,6 +1225,26 @@ CString CGit::GetGitLastErr(CString msg)
        }\r
 }\r
 \r
+CString CGit::GetLibGit2LastErr()\r
+{\r
+       const git_error *libgit2err = giterr_last();\r
+       if (libgit2err)\r
+       {\r
+               CString lastError = CUnicodeUtils::GetUnicode(CStringA(libgit2err->message));\r
+               giterr_clear();\r
+               return _T("libgit2 returned: ") + lastError;\r
+       }\r
+       else\r
+               return _T("An error occoured in libgit2, but no message is available.");\r
+}\r
+\r
+CString CGit::GetLibGit2LastErr(CString msg)\r
+{\r
+       if (!msg.IsEmpty())\r
+               return msg + _T("\n") + GetLibGit2LastErr();\r
+       return GetLibGit2LastErr();\r
+}\r
+\r
 CString CGit::FixBranchName_Mod(CString& branchName)\r
 {\r
        if(branchName == "FETCH_HEAD")\r
index 11acf16..953b92c 100644 (file)
@@ -201,6 +201,8 @@ public:
        int Revert(CString commit, CTGitPathList &list, bool keep=true);\r
        int Revert(CString commit, CTGitPath &path);\r
        CString GetGitLastErr(CString msg);\r
+       static CString GetLibGit2LastErr();\r
+       static CString GetLibGit2LastErr(CString msg);\r
        bool SetCurrentDir(CString path, bool submodule = false)\r
        {\r
                bool b = m_GitDir.HasAdminDir(path, submodule ? false : !!PathIsDirectory(path), &m_CurrentDir);\r
index 160bd6f..5bedc32 100644 (file)
@@ -45,11 +45,7 @@ bool CreateRepositoryCommand::Execute()
                if (git_repository_init(&repo, path.GetBuffer(), dlg.m_bBare))\r
                {\r
                        path.ReleaseBuffer();\r
-                       const git_error *err = giterr_last();\r
-                       if (err == nullptr)\r
-                               CMessageBox::Show(hwndExplorer, _T("An error occoured in libgit2 without providing a message."), _T("TortoiseGit"), MB_OK | MB_ICONERROR);\r
-                       else\r
-                               CMessageBox::Show(hwndExplorer, CUnicodeUtils::GetUnicode(giterr_last()->message), _T("TortoiseGit"), MB_OK | MB_ICONERROR);\r
+                       CMessageBox::Show(hwndExplorer, CGit::GetGitLastErr(_T("Could not initialize a new repository.")), _T("TortoiseGit"), MB_OK | MB_ICONERROR);\r
                        return false;\r
                }\r
                path.ReleaseBuffer();\r
index 465bee4..776bb24 100644 (file)
@@ -842,11 +842,7 @@ bool CGitProgressDlg::SetBackgroundImage(UINT nID)
 \r
 void CGitProgressDlg::ReportGitError()\r
 {\r
-       const git_error *err = giterr_last();\r
-       if (err == nullptr)\r
-               ReportError(_T("An error occoured in libgit2, but no message is available."));\r
-       else\r
-               ReportError(CUnicodeUtils::GetUnicode(giterr_last()->message));\r
+       ReportError(CGit::GetLibGit2LastErr());\r
 }\r
 \r
 void CGitProgressDlg::ReportError(const CString& sError)\r
index df0f263..37d8408 100644 (file)
@@ -373,7 +373,7 @@ int CRepositoryBrowser::ReadTree(CShadowFilesTree * treeroot)
                ret = git_repository_open(&repository, gitdir.GetBuffer());\r
                if (ret)\r
                {\r
-                       MessageBox(g_Git.GetGitLastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_ICONERROR);\r
+                       MessageBox(CGit::GetLibGit2LastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_ICONERROR);\r
                        break;\r
                }\r
 \r
@@ -386,14 +386,14 @@ int CRepositoryBrowser::ReadTree(CShadowFilesTree * treeroot)
                ret = git_commit_lookup(&commit, repository, (git_oid *) hash.m_hash);\r
                if (ret)\r
                {\r
-                       MessageBox(g_Git.GetGitLastErr(_T("Could not lookup commit.")), _T("TortoiseGit"), MB_ICONERROR);\r
+                       MessageBox(CGit::GetLibGit2LastErr(_T("Could not lookup commit.")), _T("TortoiseGit"), MB_ICONERROR);\r
                        break;\r
                }\r
 \r
                ret = git_commit_tree(&tree, commit);\r
                if (ret)\r
                {\r
-                       MessageBox(g_Git.GetGitLastErr(_T("Could not get tree of commit.")), _T("TortoiseGit"), MB_ICONERROR);\r
+                       MessageBox(CGit::GetLibGit2LastErr(_T("Could not get tree of commit.")), _T("TortoiseGit"), MB_ICONERROR);\r
                        break;\r
                }\r
 \r