Merge pull request #614 from ysalmon/betterHistory

Referring repos with their git-dir instead of their work-tree.
This commit is contained in:
Murmele 2023-11-20 14:38:04 +01:00 committed by GitHub
commit 69d3e21a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 48 additions and 30 deletions

View File

@ -50,16 +50,20 @@ void RecentRepositories::remove(int index) {
emit repositoryRemoved();
}
void RecentRepositories::add(QString path) {
/*!
* gitpath: path to the git repository, does not neccesarly need to be the
* workdir
*/
void RecentRepositories::add(QString gitpath) {
emit repositoryAboutToBeAdded();
auto end = mRepos.end();
RecentRepository *repo = new RecentRepository(path, this);
RecentRepository *repo = new RecentRepository(gitpath, 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;
return repo->gitpath().compare(rhs->gitpath(), Qt::CaseInsensitive) == 0;
#else
return (repo->path() == rhs->path());
return (repo->gitpath() == rhs->gitpath());
#endif
});
@ -82,7 +86,7 @@ RecentRepositories *RecentRepositories::instance() {
void RecentRepositories::store() {
QStringList paths;
foreach (RecentRepository *repo, mRepos)
paths.append(repo->path());
paths.append(repo->gitpath());
QSettings().setValue(kRecentKey, paths);
@ -125,6 +129,13 @@ void RecentRepositories::load() {
qDeleteAll(mRepos);
mRepos.clear();
/* If two paths have the same name, increase the path segment so that they get
* unique For example: path1/anotherpath/repositoryname
* path2/anotherpath/repositoryname
*
* In this case the complete paths are shown and not only 'repositoryname',
* otherwise they are not distinguishable in the recent repository list:
*/
foreach (const QString &path, paths) {
RecentRepository *repo = new RecentRepository(path, this);
auto functor = [repo](RecentRepository *rhs) {

View File

@ -9,13 +9,13 @@
#include "RecentRepository.h"
RecentRepository::RecentRepository(const QString &path, QObject *parent)
: QObject(parent), mPath(path) {}
RecentRepository::RecentRepository(const QString &gitpath, QObject *parent)
: QObject(parent), mGitPath(gitpath) {}
QString RecentRepository::path() const { return mPath; }
QString RecentRepository::gitpath() const { return mGitPath; }
QString RecentRepository::name() const {
return mPath.section('/', -mSections);
return mGitPath.section('/', -mSections, -1);
}
void RecentRepository::increment() { ++mSections; }

View File

@ -16,15 +16,15 @@ class RecentRepository : public QObject {
Q_OBJECT
public:
RecentRepository(const QString &path, QObject *parent = nullptr);
RecentRepository(const QString &gitpath, QObject *parent = nullptr);
QString path() const;
QString gitpath() const;
QString name() const;
private:
void increment();
QString mPath;
QString mGitPath;
int mSections = 1;
friend class RecentRepositories;

View File

@ -117,10 +117,10 @@ public:
RecentRepository *repo = repos->repository(index.row());
switch (role) {
case Qt::DisplayRole:
return mShowFullPath ? repo->path() : repo->name();
return mShowFullPath ? repo->gitpath() : repo->name();
case Qt::UserRole:
return repo->path();
return repo->gitpath();
}
return QVariant();

View File

@ -152,7 +152,14 @@ Repository::Repository(git_repository *repo) : d(registerRepository(repo)) {}
Repository::operator git_repository *() const { return d->repo; }
QDir Repository::dir() const { return QDir(git_repository_path(d->repo)); }
QDir Repository::dir(bool includeGitFolder) const {
QDir dir(git_repository_path(d->repo));
if (!includeGitFolder) {
assert(dir.dirName() == ".git");
assert(dir.cdUp());
}
return dir;
}
QDir Repository::workdir() const {
return isBare() ? dir() : QDir(git_repository_workdir(d->repo));

View File

@ -71,7 +71,7 @@ public:
RepositoryNotifier *notifier() const { return d->notifier; }
QDir dir() const;
QDir dir(bool includeGitFolder = true) const;
QDir workdir() const;
QDir appDir() const;

View File

@ -218,7 +218,7 @@ RepoView *MainWindow::addTab(const QString &path) {
TabWidget *tabs = tabWidget();
for (int i = 0; i < tabs->count(); i++) {
RepoView *view = static_cast<RepoView *>(tabs->widget(i));
if (path == view->repo().workdir().path()) {
if (path == view->repo().dir(false).path()) {
tabs->setCurrentIndex(i);
return view;
}
@ -235,13 +235,13 @@ RepoView *MainWindow::addTab(const QString &path) {
RepoView *MainWindow::addTab(const git::Repository &repo) {
// Update recent repository settings.
QDir dir = repo.workdir();
QDir dir = repo.dir(false);
RecentRepositories::instance()->add(dir.path());
TabWidget *tabs = tabWidget();
for (int i = 0; i < tabs->count(); i++) {
RepoView *view = static_cast<RepoView *>(tabs->widget(i));
if (dir.path() == view->repo().workdir().path()) {
if (dir.path() == view->repo().dir(false).path()) {
tabs->setCurrentIndex(i);
return view;
}
@ -381,7 +381,7 @@ MainWindow *MainWindow::open(const QString &path, bool warnOnInvalid) {
MainWindow *MainWindow::open(const git::Repository &repo) {
// Update recent repository settings.
if (repo.isValid())
RecentRepositories::instance()->add(repo.workdir().path());
RecentRepositories::instance()->add(repo.dir(false).path());
// Create the window.
MainWindow *window = new MainWindow(repo);
@ -470,7 +470,7 @@ void MainWindow::updateTabNames() {
QList<TabName> fullNames;
for (int i = 0; i < count(); ++i) {
TabName name(view(i)->repo().workdir().path());
TabName name(view(i)->repo().dir(false).path());
names[name.name()].append(i);
fullNames.append(name);
}
@ -520,7 +520,7 @@ void MainWindow::updateWindowTitle(int ahead, int behind) {
}
git::Repository repo = view->repo();
QDir dir = repo.workdir();
QDir dir = repo.dir(false);
git::Reference head = repo.head();
QString path = mFullPath ? dir.path() : dir.dirName();
QString name = head.isValid() ? head.name() : repo.unbornHeadName();
@ -586,7 +586,7 @@ void MainWindow::updateWindowTitle(int ahead, int behind) {
QStringList MainWindow::paths() const {
QStringList paths;
for (int i = 0; i < count(); ++i)
paths.append(view(i)->repo().workdir().path());
paths.append(view(i)->repo().dir(false).path());
return paths;
}

View File

@ -308,7 +308,7 @@ MenuBar::MenuBar(QWidget *parent) : QMenuBar(parent) {
RecentRepository *repo = repos->repository(i);
QAction *action = openRecent->addAction(repo->name());
connect(action, &QAction::triggered,
[repo] { MainWindow::open(repo->path()); });
[repo] { MainWindow::open(repo->gitpath()); });
}
});

View File

@ -352,7 +352,7 @@ public:
return mTabs->tabText(row);
RepoView *view = static_cast<RepoView *>(mTabs->widget(row));
return view->repo().workdir().path();
return view->repo().dir(false).path();
}
return tr("none");
@ -361,7 +361,7 @@ public:
RecentRepositories *recent = RecentRepositories::instance();
if (recent->count()) {
RecentRepository *repo = repos->repository(row);
return mShowFullPath ? repo->path() : repo->name();
return mShowFullPath ? repo->gitpath() : repo->name();
}
return tr("none");
@ -446,13 +446,13 @@ public:
return QVariant();
QWidget *widget = mTabs->widget(row);
RepoView *view = static_cast<RepoView *>(widget);
return view->repo().workdir().path();
return view->repo().dir(false).path();
}
case Recent:
if (!repos->count())
return QVariant();
return repos->repository(row)->path();
return repos->repository(row)->gitpath();
default:
return QVariant();
@ -493,7 +493,7 @@ public:
case Repo:
if (mTabs->count()) {
RepoView *view = static_cast<RepoView *>(mTabs->widget(row));
return view->repo().workdir().path();
return view->repo().dir(false).path();
}
return "";
@ -502,7 +502,7 @@ public:
RecentRepositories *recent = RecentRepositories::instance();
if (recent->count()) {
RecentRepository *repo = repos->repository(row);
return repo->path();
return repo->gitpath();
}
return "";