From cad6f37576f02b1070534b8f1857300496934fb6 Mon Sep 17 00:00:00 2001 From: Sup Yut Sum Date: Sun, 27 Sep 2015 20:09:33 +0800 Subject: [PATCH] Also detect refs/stash in packed-refs Signed-off-by: Sup Yut Sum --- src/Git/TGitPath.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Git/TGitPath.cpp b/src/Git/TGitPath.cpp index 4f0aca530..58dda082a 100644 --- a/src/Git/TGitPath.cpp +++ b/src/Git/TGitPath.cpp @@ -26,6 +26,7 @@ #include "Git.h" #include "../TortoiseShell/Globals.h" #include "StringUtils.h" +#include "SmartHandle.h" #include "../Resources/LoglistCommonResource.h" #include @@ -862,7 +863,70 @@ bool CTGitPath::HasStashDir() const CString dotGitPath; GitAdminDir::GetAdminDirPath(topdir, dotGitPath); - return !!PathFileExists(dotGitPath + _T("\\refs\\stash")); + if (!!PathFileExists(dotGitPath + _T("refs\\stash"))) + return true; + + CAutoFile hfile = CreateFile(dotGitPath + _T("packed-refs"), GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (!hfile) + return false; + + DWORD filesize = ::GetFileSize(hfile, nullptr); + if (filesize == 0) + return false; + + DWORD size = 0; + std::unique_ptr buff(new char[filesize + 1]); + ReadFile(hfile, buff.get(), filesize, &size, nullptr); + buff.get()[filesize] = '\0'; + + if (size != filesize) + return false; + + for (DWORD i = 0; i < filesize;) + { + if (buff[i] == '#' || buff[i] == '^') + { + while (buff[i] != '\n') + { + ++i; + if (i == filesize) + break; + } + ++i; + } + + if (i >= filesize) + break; + + while (buff[i] != ' ') + { + ++i; + if (i == filesize) + break; + } + + ++i; + if (i >= filesize) + break; + + if (i <= filesize - 10 && (buff[i + 10] == '\n' || buff[i + 10] == '\0') && !strncmp("refs/stash", buff.get() + i, 10)) + return true; + while (buff[i] != '\n') + { + ++i; + if (i == filesize) + break; + } + + while (buff[i] == '\n') + { + ++i; + if (i == filesize) + break; + } + } + return false; } bool CTGitPath::HasGitSVNDir() const { -- 2.11.4.GIT