From baf0fdecfe26a9533af8340e60b2ff9793feb7f8 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Sun, 10 Feb 2013 14:18:09 +0100 Subject: [PATCH] Deduplicate code for retrieving libgit2 errors Signed-off-by: Sven Strickroth --- src/Git/Git.cpp | 38 ++++++++++++++-------- src/Git/Git.h | 2 ++ .../Commands/CreateRepositoryCommand.cpp | 6 +--- src/TortoiseProc/GitProgressDlg.cpp | 6 +--- src/TortoiseProc/RepositoryBrowser.cpp | 6 ++-- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/Git/Git.cpp b/src/Git/Git.cpp index 65771a77a..185ccb8d1 100644 --- a/src/Git/Git.cpp +++ b/src/Git/Git.cpp @@ -1207,22 +1207,14 @@ int CGit::GetTagList(STRING_VECTOR &list) } } +/** +Use this method only if m_IsUseLibGit2 is used for fallbacks. +If you directly use libgit2 methods, use GetLibGit2LastErr instead. +*/ CString CGit::GetGitLastErr(CString msg) { if (this->m_IsUseLibGit2) - { - const git_error *libgit2err = giterr_last(); - if (libgit2err) - { - CString lastError = CUnicodeUtils::GetUnicode(CStringA(libgit2err->message)); - giterr_clear(); - return msg + _T("\nlibgit2 returned: ") + lastError; - } - else - { - return msg + _T("\nUnknown libgit2 error."); - } - } + return GetLibGit2LastErr(msg); else if (gitLastErr.IsEmpty()) return msg + _T("\nUnknown git.exe error."); else @@ -1233,6 +1225,26 @@ CString CGit::GetGitLastErr(CString msg) } } +CString CGit::GetLibGit2LastErr() +{ + const git_error *libgit2err = giterr_last(); + if (libgit2err) + { + CString lastError = CUnicodeUtils::GetUnicode(CStringA(libgit2err->message)); + giterr_clear(); + return _T("libgit2 returned: ") + lastError; + } + else + return _T("An error occoured in libgit2, but no message is available."); +} + +CString CGit::GetLibGit2LastErr(CString msg) +{ + if (!msg.IsEmpty()) + return msg + _T("\n") + GetLibGit2LastErr(); + return GetLibGit2LastErr(); +} + CString CGit::FixBranchName_Mod(CString& branchName) { if(branchName == "FETCH_HEAD") diff --git a/src/Git/Git.h b/src/Git/Git.h index 11acf16ab..953b92cd0 100644 --- a/src/Git/Git.h +++ b/src/Git/Git.h @@ -201,6 +201,8 @@ public: int Revert(CString commit, CTGitPathList &list, bool keep=true); int Revert(CString commit, CTGitPath &path); CString GetGitLastErr(CString msg); + static CString GetLibGit2LastErr(); + static CString GetLibGit2LastErr(CString msg); bool SetCurrentDir(CString path, bool submodule = false) { bool b = m_GitDir.HasAdminDir(path, submodule ? false : !!PathIsDirectory(path), &m_CurrentDir); diff --git a/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp b/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp index 160bd6ff0..5bedc324a 100644 --- a/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp +++ b/src/TortoiseProc/Commands/CreateRepositoryCommand.cpp @@ -45,11 +45,7 @@ bool CreateRepositoryCommand::Execute() if (git_repository_init(&repo, path.GetBuffer(), dlg.m_bBare)) { path.ReleaseBuffer(); - const git_error *err = giterr_last(); - if (err == nullptr) - CMessageBox::Show(hwndExplorer, _T("An error occoured in libgit2 without providing a message."), _T("TortoiseGit"), MB_OK | MB_ICONERROR); - else - CMessageBox::Show(hwndExplorer, CUnicodeUtils::GetUnicode(giterr_last()->message), _T("TortoiseGit"), MB_OK | MB_ICONERROR); + CMessageBox::Show(hwndExplorer, CGit::GetGitLastErr(_T("Could not initialize a new repository.")), _T("TortoiseGit"), MB_OK | MB_ICONERROR); return false; } path.ReleaseBuffer(); diff --git a/src/TortoiseProc/GitProgressDlg.cpp b/src/TortoiseProc/GitProgressDlg.cpp index 465bee424..776bb24cd 100644 --- a/src/TortoiseProc/GitProgressDlg.cpp +++ b/src/TortoiseProc/GitProgressDlg.cpp @@ -842,11 +842,7 @@ bool CGitProgressDlg::SetBackgroundImage(UINT nID) void CGitProgressDlg::ReportGitError() { - const git_error *err = giterr_last(); - if (err == nullptr) - ReportError(_T("An error occoured in libgit2, but no message is available.")); - else - ReportError(CUnicodeUtils::GetUnicode(giterr_last()->message)); + ReportError(CGit::GetLibGit2LastErr()); } void CGitProgressDlg::ReportError(const CString& sError) diff --git a/src/TortoiseProc/RepositoryBrowser.cpp b/src/TortoiseProc/RepositoryBrowser.cpp index df0f2639d..37d840881 100644 --- a/src/TortoiseProc/RepositoryBrowser.cpp +++ b/src/TortoiseProc/RepositoryBrowser.cpp @@ -373,7 +373,7 @@ int CRepositoryBrowser::ReadTree(CShadowFilesTree * treeroot) ret = git_repository_open(&repository, gitdir.GetBuffer()); if (ret) { - MessageBox(g_Git.GetGitLastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_ICONERROR); + MessageBox(CGit::GetLibGit2LastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_ICONERROR); break; } @@ -386,14 +386,14 @@ int CRepositoryBrowser::ReadTree(CShadowFilesTree * treeroot) ret = git_commit_lookup(&commit, repository, (git_oid *) hash.m_hash); if (ret) { - MessageBox(g_Git.GetGitLastErr(_T("Could not lookup commit.")), _T("TortoiseGit"), MB_ICONERROR); + MessageBox(CGit::GetLibGit2LastErr(_T("Could not lookup commit.")), _T("TortoiseGit"), MB_ICONERROR); break; } ret = git_commit_tree(&tree, commit); if (ret) { - MessageBox(g_Git.GetGitLastErr(_T("Could not get tree of commit.")), _T("TortoiseGit"), MB_ICONERROR); + MessageBox(CGit::GetLibGit2LastErr(_T("Could not get tree of commit.")), _T("TortoiseGit"), MB_ICONERROR); break; } -- 2.11.4.GIT