Skip to content

Commit

Permalink
instead of ignoring the .git folder at multiple places, ignore it dir…
Browse files Browse the repository at this point in the history
…ectly when requesting the path, otherwise the settings are stored inconsistently and might lead to an infinity loop in void RecentRepositories::load()
  • Loading branch information
Murmele committed Nov 12, 2023
1 parent f07f4ed commit 0fd2d55
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 36 deletions.
7 changes: 1 addition & 6 deletions src/conf/RecentRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ RecentRepository::RecentRepository(const QString &gitpath, QObject *parent)
QString RecentRepository::gitpath() const { return mGitPath; }

QString RecentRepository::name() const {
QString name;
if (mGitPath.endsWith("/.git"))
name = mGitPath.section('/', -mSections - 1, -2);
else
name = mGitPath.section('/', -mSections, -1);
return name;
return mGitPath.section('/', -mSections, -1);
}

void RecentRepository::increment() { ++mSections; }
9 changes: 8 additions & 1 deletion src/git/Repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/git/Repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Repository {

RepositoryNotifier *notifier() const { return d->notifier; }

QDir dir() const;
QDir dir(bool includeGitFolder = true) const;
QDir workdir() const;
QDir appDir() const;

Expand Down
35 changes: 10 additions & 25 deletions src/ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ class TabName {
public:
TabName(const QString &path) : mPath(path) {}

QString name() const {
return mPath.endsWith("/.git") ? mPath.section('/', -mSections - 1, -2)
: mPath.section('/', -mSections);
}
QString name() const { return mPath.section('/', -mSections); }

void increment() { ++mSections; }
int sections() const { return mSections; }
Expand Down Expand Up @@ -221,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().dir().path()) {
if (path == view->repo().dir(false).path()) {
tabs->setCurrentIndex(i);
return view;
}
Expand All @@ -238,13 +235,13 @@ RepoView *MainWindow::addTab(const QString &path) {

RepoView *MainWindow::addTab(const git::Repository &repo) {
// Update recent repository settings.
QDir dir = repo.dir();
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().dir().path()) {
if (dir.path() == view->repo().dir(false).path()) {
tabs->setCurrentIndex(i);
return view;
}
Expand All @@ -259,13 +256,7 @@ RepoView *MainWindow::addTab(const git::Repository &repo) {
[this] { updateWindowTitle(); });

emit tabs->tabAboutToBeInserted();

// NB : this seems to be useless, because overwritten by
// MainWindow::updateTabNames
QString tabTitle = dir.dirName();
if (tabTitle == ".git")
tabTitle = dir.path().section("/", -2, -2);
tabs->setCurrentIndex(tabs->addTab(view, tabTitle));
tabs->setCurrentIndex(tabs->addTab(view, dir.dirName()));

Settings *settings = Settings::instance();
bool enable =
Expand Down Expand Up @@ -390,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.dir().path());
RecentRepositories::instance()->add(repo.dir(false).path());

// Create the window.
MainWindow *window = new MainWindow(repo);
Expand Down Expand Up @@ -479,7 +470,7 @@ void MainWindow::updateTabNames() {
QList<TabName> fullNames;

for (int i = 0; i < count(); ++i) {
TabName name(view(i)->repo().dir().path());
TabName name(view(i)->repo().dir(false).path());
names[name.name()].append(i);
fullNames.append(name);
}
Expand Down Expand Up @@ -529,15 +520,9 @@ void MainWindow::updateWindowTitle(int ahead, int behind) {
}

git::Repository repo = view->repo();
QDir dir = repo.dir();
QDir dir = repo.dir(false);
git::Reference head = repo.head();
QString path;
if (mFullPath)
path = dir.path();
else if (dir.dirName() == ".git")
path = dir.path().section("/", -2, -2);
else
path = dir.dirName();
QString path = mFullPath ? dir.path() : dir.dirName();
QString name = head.isValid() ? head.name() : repo.unbornHeadName();
QString title = tr("%1 - %2").arg(path, name);

Expand Down Expand Up @@ -601,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().dir().path());
paths.append(view(i)->repo().dir(false).path());
return paths;
}

Expand Down
6 changes: 3 additions & 3 deletions src/ui/SideBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class RepoModel : public QAbstractItemModel {
return mTabs->tabText(row);

RepoView *view = static_cast<RepoView *>(mTabs->widget(row));
return view->repo().dir().path();
return view->repo().dir(false).path();
}

return tr("none");
Expand Down Expand Up @@ -446,7 +446,7 @@ class RepoModel : public QAbstractItemModel {
return QVariant();
QWidget *widget = mTabs->widget(row);
RepoView *view = static_cast<RepoView *>(widget);
return view->repo().dir().path();
return view->repo().dir(false).path();
}

case Recent:
Expand Down Expand Up @@ -493,7 +493,7 @@ class RepoModel : public QAbstractItemModel {
case Repo:
if (mTabs->count()) {
RepoView *view = static_cast<RepoView *>(mTabs->widget(row));
return view->repo().dir().path();
return view->repo().dir(false).path();
}

return "";
Expand Down

0 comments on commit 0fd2d55

Please sign in to comment.