From aeea1162482b7e073387747b32e95075f912f84d Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Wed, 18 Mar 2015 22:11:47 +0100 Subject: [PATCH] Fix a regression: Libgit2 changed to way to read string config values Regression introduced in revision 277e5282a9e9f020245a567b0c526083102d75d4. Signed-off-by: Sven Strickroth --- src/Utils/SmartLibgit2Ref.h | 122 +++++++++++++++---------------- src/libgit2/filter-filter.c | 25 +++---- test/UnitTests/UnitTests.vcxproj | 1 + test/UnitTests/UnitTests.vcxproj.filters | 3 + test/UnitTests/libgit2Test.cpp | 39 ++++++++++ 5 files changed, 113 insertions(+), 77 deletions(-) create mode 100644 test/UnitTests/libgit2Test.cpp diff --git a/src/Utils/SmartLibgit2Ref.h b/src/Utils/SmartLibgit2Ref.h index f8c6b2294..8b122174a 100644 --- a/src/Utils/SmartLibgit2Ref.h +++ b/src/Utils/SmartLibgit2Ref.h @@ -24,6 +24,64 @@ * \ingroup Utils * Helper classes for libgit2 references. */ +template +class CSmartBuffer : public FreeFunction +{ +public: + CSmartBuffer() + { + HandleType tmp = { 0 }; + m_Ref = tmp; + } + + operator HandleType*() + { + return &m_Ref; + } + + HandleType* operator->() + { + return &m_Ref; + } + + ~CSmartBuffer() + { + Free(&m_Ref); + } + +protected: + HandleType m_Ref; +}; + +struct CFreeBuf +{ +protected: + void Free(git_buf* ref) + { + git_buf_free(ref); + } + + ~CFreeBuf() + { + } +}; + +struct CFreeStrArray +{ +protected: + void Free(git_strarray* ref) + { + git_strarray_free(ref); + } + + ~CFreeStrArray() + { + } +}; + +typedef CSmartBuffer CAutoBuf; +typedef CSmartBuffer CAutoStrArray; + template class CSmartLibgit2Ref { @@ -247,15 +305,15 @@ public: if (!IsValid()) return -1; - const char* out = nullptr; + CAutoBuf buf; int ret = 0; - if ((ret = git_config_get_string(&out, m_Ref, CUnicodeUtils::GetUTF8(key)))) + if ((ret = git_config_get_string_buf(buf, m_Ref, CUnicodeUtils::GetUTF8(key)))) { value.Empty(); return ret; } - value = CUnicodeUtils::GetUnicode((CStringA)out); + value = CUnicodeUtils::GetUnicode((CStringA)buf->ptr); return ret; } @@ -475,61 +533,3 @@ protected: git_status_list_free(m_Ref); } }; - -template -class CSmartBuffer : public FreeFunction -{ -public: - CSmartBuffer() - { - HandleType tmp = { 0 }; - m_Ref = tmp; - } - - operator HandleType*() - { - return &m_Ref; - } - - HandleType* operator->() - { - return &m_Ref; - } - - ~CSmartBuffer() - { - Free(&m_Ref); - } - -protected: - HandleType m_Ref; -}; - -struct CFreeBuf -{ -protected: - void Free(git_buf* ref) - { - git_buf_free(ref); - } - - ~CFreeBuf() - { - } -}; - -struct CFreeStrArray -{ -protected: - void Free(git_strarray* ref) - { - git_strarray_free(ref); - } - - ~CFreeStrArray() - { - } -}; - -typedef CSmartBuffer CAutoBuf; -typedef CSmartBuffer CAutoStrArray; diff --git a/src/libgit2/filter-filter.c b/src/libgit2/filter-filter.c index e2bf927d8..be743c79e 100644 --- a/src/libgit2/filter-filter.c +++ b/src/libgit2/filter-filter.c @@ -128,8 +128,7 @@ static int filter_apply( git_buf configKey = GIT_BUF_INIT; int isRequired = FALSE; int error; - const char *cmd = NULL; - git_buf cmdBuf = GIT_BUF_INIT; + git_buf cmd = GIT_BUF_INIT; wchar_t *wide_cmd; COMMAND_HANDLE commandHandle; git_buf errBuf = GIT_BUF_INIT; @@ -163,7 +162,7 @@ static int filter_apply( return -1; } - error = git_config_get_string(&cmd, config, configKey.ptr); + error = git_config_get_string_buf(&cmd, config, configKey.ptr); git_buf_free(&configKey); if (error && error != GIT_ENOTFOUND) return -1; @@ -174,37 +173,31 @@ static int filter_apply( return GIT_PASSTHROUGH; } - git_buf_puts(&cmdBuf, cmd); - if (git_buf_oom(&cmdBuf)) { - giterr_set_oom(); - return -1; - } - - if (expandPerCentF(&cmdBuf, git_filter_source_path(src))) + if (expandPerCentF(&cmd, git_filter_source_path(src))) return -1; if (ffs->shexepath) { // build params for sh.exe git_buf shParams = GIT_BUF_INIT; git_buf_puts(&shParams, " -c \""); - git_buf_text_puts_escaped(&shParams, cmdBuf.ptr, "\"\\", "\\"); + git_buf_text_puts_escaped(&shParams, cmd.ptr, "\"\\", "\\"); git_buf_puts(&shParams, "\""); if (git_buf_oom(&shParams)) { - git_buf_free(&cmdBuf); + git_buf_free(&cmd); giterr_set_oom(); return -1; } - git_buf_swap(&shParams, &cmdBuf); + git_buf_swap(&shParams, &cmd); git_buf_free(&shParams); } - if (git__utf8_to_16_alloc(&wide_cmd, cmdBuf.ptr) < 0) + if (git__utf8_to_16_alloc(&wide_cmd, cmd.ptr) < 0) { - git_buf_free(&cmdBuf); + git_buf_free(&cmd); giterr_set_oom(); return -1; } - git_buf_free(&cmdBuf); + git_buf_free(&cmd); if (ffs->shexepath) { // build cmd, i.e. shexepath + params diff --git a/test/UnitTests/UnitTests.vcxproj b/test/UnitTests/UnitTests.vcxproj index 43828a821..8b282c367 100644 --- a/test/UnitTests/UnitTests.vcxproj +++ b/test/UnitTests/UnitTests.vcxproj @@ -116,6 +116,7 @@ + diff --git a/test/UnitTests/UnitTests.vcxproj.filters b/test/UnitTests/UnitTests.vcxproj.filters index 69339b986..28d33d882 100644 --- a/test/UnitTests/UnitTests.vcxproj.filters +++ b/test/UnitTests/UnitTests.vcxproj.filters @@ -193,5 +193,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/test/UnitTests/libgit2Test.cpp b/test/UnitTests/libgit2Test.cpp new file mode 100644 index 000000000..554095230 --- /dev/null +++ b/test/UnitTests/libgit2Test.cpp @@ -0,0 +1,39 @@ +// TortoiseGit - a Windows shell extension for easy version control + +// Copyright (C) 2015 - TortoiseGit + +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// + +#include "stdafx.h" +#include "StringUtils.h" + +TEST(libgit2, Config) +{ + CAutoTempDir tempdir; + CString testFile = tempdir.GetTempDir() + _T("\\config"); + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)testFile, L"[core]\nemail=dummy@example.com\ntrue=true\nfalse=false\n")); + CAutoConfig config(true); + EXPECT_EQ(0, git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(testFile), GIT_CONFIG_LEVEL_LOCAL, 1)); + bool ret = false; + EXPECT_EQ(0, config.GetBool(_T("core.true"), ret)); + EXPECT_EQ(true, ret); + EXPECT_EQ(0, config.GetBool(_T("core.false"), ret)); + EXPECT_EQ(false, ret); + EXPECT_EQ(-3, config.GetBool(_T("core.not-exist"), ret)); + CString value; + EXPECT_EQ(0, config.GetString(_T("core.email"), value)); + EXPECT_STREQ(_T("dummy@example.com"), value); +} -- 2.11.4.GIT