From 82b9a917b2a0a70a1de3906c4276da8b07bafffc Mon Sep 17 00:00:00 2001 From: Sup Yut Sum Date: Tue, 10 Mar 2015 00:48:36 +0800 Subject: [PATCH] Deduplicate code to read .git file Signed-off-by: Sup Yut Sum Signed-off-by: Sven Strickroth --- src/Git/GitAdminDir.cpp | 60 +++++++++++++++++++++++++++---------------------- src/Git/GitAdminDir.h | 1 + src/Git/gitindex.h | 41 +++++++-------------------------- 3 files changed, 42 insertions(+), 60 deletions(-) diff --git a/src/Git/GitAdminDir.cpp b/src/Git/GitAdminDir.cpp index d39fd6cf7..2ff0daa4a 100644 --- a/src/Git/GitAdminDir.cpp +++ b/src/Git/GitAdminDir.cpp @@ -164,38 +164,44 @@ bool GitAdminDir::GetAdminDirPath(const CString &projectTopDir, CString& adminDi } else { - FILE *pFile; - _tfopen_s(&pFile, sDotGitPath, _T("r")); - - if (!pFile) - return false; - - int size = 65536; - std::unique_ptr buffer(new char[size]); - int length = (int)fread(buffer.get(), sizeof(char), size, pFile); - fclose(pFile); - CStringA gitPathA(buffer.get(), length); - if (length < 8 || gitPathA.Left(8) != "gitdir: ") + CString result = ReadGitLink(projectTopDir, sDotGitPath); + if (result.IsEmpty()) return false; - CString gitPath = CUnicodeUtils::GetUnicode(gitPathA); - // trim after converting to UTF-16, because CStringA trim does not work when having UTF-8 chars - gitPath = gitPath.Trim().Mid(8); // 8 = len("gitdir: ") - gitPath.Replace('/', '\\'); - gitPath.TrimRight('\\'); - gitPath.Append(_T("\\")); - if (gitPath.GetLength() > 0 && gitPath[0] == _T('.')) - { - gitPath = projectTopDir + _T("\\") + gitPath; - PathCanonicalize(adminDir.GetBuffer(MAX_PATH), gitPath.GetBuffer()); - adminDir.ReleaseBuffer(); - gitPath.ReleaseBuffer(); - return true; - } - adminDir = gitPath; + adminDir = result + _T("\\"); return true; } } +CString GitAdminDir::ReadGitLink(const CString& topDir, const CString& dotGitPath) +{ + FILE* pFile = _tfsopen(dotGitPath, _T("r"), SH_DENYWR); + + if (!pFile) + return _T(""); + + int size = 65536; + std::unique_ptr buffer(new char[size]); + int length = (int)fread(buffer.get(), sizeof(char), size, pFile); + fclose(pFile); + CStringA gitPathA(buffer.get(), length); + if (length < 8 || gitPathA.Left(8) != "gitdir: ") + return _T(""); + CString gitPath = CUnicodeUtils::GetUnicode(gitPathA); + // trim after converting to UTF-16, because CStringA trim does not work when having UTF-8 chars + gitPath = gitPath.Trim().Mid(8); // 8 = len("gitdir: ") + gitPath.Replace('/', '\\'); + gitPath.TrimRight('\\'); + if (!gitPath.IsEmpty() && gitPath[0] == _T('.')) + { + gitPath = topDir + _T("\\") + gitPath; + CString adminDir; + PathCanonicalize(adminDir.GetBuffer(MAX_PATH), gitPath); + adminDir.ReleaseBuffer(); + return adminDir; + } + return gitPath; +} + bool GitAdminDir::IsAdminDirPath(const CString& path) { if (path.IsEmpty()) diff --git a/src/Git/GitAdminDir.h b/src/Git/GitAdminDir.h index c9763b18a..6f82b31a3 100644 --- a/src/Git/GitAdminDir.h +++ b/src/Git/GitAdminDir.h @@ -42,6 +42,7 @@ public: static CString GetSuperProjectRoot(const CString& path); static bool GetAdminDirPath(const CString &projectTopDir, CString& adminDir); + static CString ReadGitLink(const CString& topDir, const CString& dotGitPath); static CString GetGitTopDir(const CString& path); diff --git a/src/Git/gitindex.h b/src/Git/gitindex.h index 36d3d94eb..718717647 100644 --- a/src/Git/gitindex.h +++ b/src/Git/gitindex.h @@ -1,6 +1,6 @@ // TortoiseGit - a Windows shell extension for easy version control -// Copyright (C) 2008-2014 - TortoiseGit +// Copyright (C) 2008-2015 - TortoiseGit // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -22,6 +22,7 @@ #include "GitStatus.h" #include "UnicodeUtils.h" #include "ReaderWriterLock.h" +#include "GitAdminDir.h" class CGitIndex { @@ -417,40 +418,14 @@ public: } else { - FILE * pFile = _tfsopen(path + _T("\\.git"), _T("r"), SH_DENYWR); - if (pFile) + CString result = GitAdminDir::ReadGitLink(path, path + _T("\\.git")); + if (!result.IsEmpty()) { - int size = 65536; - std::unique_ptr buffer(new char[size]); - int length = 0; - if ((length = (int)fread(buffer.get(), sizeof(char), size, pFile)) >= 8) - { - fclose(pFile); - CStringA strA(buffer.get(), length); - if (strA.Left(8) == "gitdir: ") - { - CString str = CUnicodeUtils::GetUnicode(strA); - // trim after converting to UTF-16, because CStringA trim does not work when having UTF-8 chars - str = str.Trim().Mid(8); // 8 = len("gitdir: ") - str.Replace(_T("/"), _T("\\")); - str.TrimRight(_T("\\")); - if (str.GetLength() > 0 && str[0] == _T('.')) - { - str = path + _T("\\") + str; - CString newPath; - PathCanonicalize(newPath.GetBuffer(MAX_PATH), str.GetBuffer()); - newPath.ReleaseBuffer(); - str.ReleaseBuffer(); - str = newPath; - } - (*this)[thePath] = str + _T("\\"); - m_reverseLookup[str.MakeLower()] = path; - return (*this)[thePath]; - } - } - else - fclose(pFile); + (*this)[thePath] = result + _T("\\"); + m_reverseLookup[result.MakeLower()] = path; + return (*this)[thePath]; } + return path + _T("\\.git\\"); // in case of an error stick to old behavior } } -- 2.11.4.GIT