Merge pull request #262 from exactly-one-kas/windows-recent-list

Canonicalize repository paths on Windows
This commit is contained in:
Murmele 2022-09-02 17:59:58 +02:00 committed by GitHub
commit 68a7923241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 77 additions and 8 deletions

View File

@ -13,6 +13,7 @@ add_subdirectory(plugins)
add_subdirectory(tools)
add_subdirectory(ui)
add_subdirectory(update)
add_subdirectory(util)
add_subdirectory(watcher)
# Add executable last.

View File

@ -1,7 +1,7 @@
add_library(conf ConfFile.cpp Settings.cpp RecentRepositories.cpp
RecentRepository.cpp)
target_link_libraries(conf lua Qt5::Core)
target_link_libraries(conf lua Qt5::Core util)
target_compile_definitions(
conf PRIVATE SCINTILLUA_LEXERS_DIR="${SCINTILLUA_LEXERS_DIR}")

View File

@ -9,6 +9,7 @@
#include "RecentRepositories.h"
#include "RecentRepository.h"
#include "util/Path.h"
#include <QCoreApplication>
#include <QFileInfo>
#include <QSettings>
@ -49,13 +50,17 @@ void RecentRepositories::remove(int index) {
emit repositoryRemoved();
}
void RecentRepositories::add(const QString &path) {
void RecentRepositories::add(QString path) {
emit repositoryAboutToBeAdded();
auto end = mRepos.end();
RecentRepository *repo = new RecentRepository(path, this);
auto it = std::remove_if(mRepos.begin(), end, [repo](RecentRepository *rhs) {
#ifdef Q_OS_WIN
return repo->path().compare(rhs->path(), Qt::CaseInsensitive) == 0;
#else
return (repo->path() == rhs->path());
#endif
});
if (it != end)
@ -99,6 +104,14 @@ void RecentRepositories::load() {
if (it != end)
paths.erase(it, end);
#ifdef Q_OS_WIN
for (QString &path : paths) {
path = util::canonicalizePath(path);
}
paths.removeDuplicates();
#endif
}
// Store filtered list.

View File

@ -24,7 +24,7 @@ public:
void clear();
void remove(int index);
void add(const QString &path);
void add(QString path);
static RecentRepositories *instance();

View File

@ -27,6 +27,6 @@ add_library(
TagRef.cpp
Tree.cpp)
target_link_libraries(git git2 Qt5::Core Qt5::Network)
target_link_libraries(git git2 Qt5::Core Qt5::Network util)
set_target_properties(git PROPERTIES AUTOMOC ON)

View File

@ -26,6 +26,7 @@
#include "Submodule.h"
#include "TagRef.h"
#include "Tree.h"
#include "util/Path.h"
#include "git2/buffer.h"
#include "git2/branch.h"
#include "git2/checkout.h"
@ -1105,14 +1106,15 @@ QDir Repository::appDir(const QDir &dir) {
Repository Repository::init(const QString &path, bool bare) {
git_repository *repo = nullptr;
git_repository_init(&repo, path.toUtf8(), bare);
git_repository_init(&repo, util::canonicalizePath(path).toUtf8(), bare);
return Repository(repo);
}
Repository Repository::open(const QString &path, bool searchParents) {
git_repository *repo = nullptr;
int flags = searchParents ? 0 : GIT_REPOSITORY_OPEN_NO_SEARCH;
git_repository_open_ext(&repo, path.toUtf8(), flags, nullptr);
git_repository_open_ext(&repo, util::canonicalizePath(path).toUtf8(), flags,
nullptr);
return Repository(repo);
}

5
src/util/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
add_library(util Path.cpp)
target_link_libraries(util Qt5::Core)
set_target_properties(util PROPERTIES AUTOMOC ON)

25
src/util/Path.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "Path.h"
#ifdef Q_OS_WIN
#include <memory>
#include <windows.h>
#endif
namespace util {
QString canonicalizePath(QString path) {
#ifdef Q_OS_WIN
// Convert from potential 8.3 paths to full paths on Windows
{
auto len = GetLongPathNameW((LPCWSTR)path.utf16(), nullptr, 0);
// GetLongPathNameW() returns 0 if the given path doesn't exist (yet)
if (len != 0) {
std::unique_ptr<wchar_t[]> buf{new wchar_t[len]};
len = GetLongPathNameW((LPCWSTR)path.utf16(), buf.get(), len);
path = QString::fromWCharArray(buf.get(), len);
}
}
#endif
return path;
}
} // namespace util

19
src/util/Path.h Normal file
View File

@ -0,0 +1,19 @@
//
// Copyright (c) 2022, Gittyup authors
//
// This software is licensed under the MIT License. The LICENSE.md file
// describes the conditions under which this software may be distributed.
//
// Author: Kas
//
#ifndef UTIL_PATH_H
#define UTIL_PATH_H
#include <QString>
namespace util {
QString canonicalizePath(QString path);
}
#endif

View File

@ -95,7 +95,9 @@ void TestBareRepo::checkDir() {
}
void TestBareRepo::cleanupTestCase() {
mWindow->close();
if (mWindow) {
mWindow->close();
}
QDir dir = QDir::temp();
QVERIFY(dir.cd("test_bare_repo"));
QVERIFY(dir.removeRecursively());

View File

@ -192,7 +192,9 @@ void TestInitRepo::editFile() {
}
void TestInitRepo::cleanupTestCase() {
mWindow->close();
if (mWindow) {
mWindow->close();
}
QDir dir = QDir::temp();
QVERIFY(dir.cd("test_init_repo"));
QVERIFY(dir.removeRecursively());