diff --git a/src/conf/RecentRepositories.cpp b/src/conf/RecentRepositories.cpp index e9e3623d..bfae9a74 100644 --- a/src/conf/RecentRepositories.cpp +++ b/src/conf/RecentRepositories.cpp @@ -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) { diff --git a/src/conf/RecentRepository.cpp b/src/conf/RecentRepository.cpp index 1248d26e..242886e1 100644 --- a/src/conf/RecentRepository.cpp +++ b/src/conf/RecentRepository.cpp @@ -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; } diff --git a/src/conf/RecentRepository.h b/src/conf/RecentRepository.h index 99fbec8d..2b48264d 100644 --- a/src/conf/RecentRepository.h +++ b/src/conf/RecentRepository.h @@ -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; diff --git a/src/dialogs/StartDialog.cpp b/src/dialogs/StartDialog.cpp index 51ac0ebf..88931b6a 100644 --- a/src/dialogs/StartDialog.cpp +++ b/src/dialogs/StartDialog.cpp @@ -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(); diff --git a/src/git/Repository.cpp b/src/git/Repository.cpp index e224b946..3004501d 100644 --- a/src/git/Repository.cpp +++ b/src/git/Repository.cpp @@ -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)); diff --git a/src/git/Repository.h b/src/git/Repository.h index 875359ed..0abb2779 100644 --- a/src/git/Repository.h +++ b/src/git/Repository.h @@ -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; diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index 2b469c8f..ed04212b 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -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(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(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 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; } diff --git a/src/ui/MenuBar.cpp b/src/ui/MenuBar.cpp index 39f8b386..77c76dae 100644 --- a/src/ui/MenuBar.cpp +++ b/src/ui/MenuBar.cpp @@ -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()); }); } }); diff --git a/src/ui/SideBar.cpp b/src/ui/SideBar.cpp index e64b94cb..39652bcf 100644 --- a/src/ui/SideBar.cpp +++ b/src/ui/SideBar.cpp @@ -352,7 +352,7 @@ public: return mTabs->tabText(row); RepoView *view = static_cast(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(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(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 "";