Skip to content

Commit

Permalink
Refactor GitObjectManager methods to use GitPtr g and update `Git…
Browse files Browse the repository at this point in the history
…CommitTree
  • Loading branch information
soramimi committed Jun 17, 2024
1 parent fbf3f2d commit 5fdf913
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 125 deletions.
35 changes: 22 additions & 13 deletions src/CommitExploreWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ CommitExploreWindow::CommitExploreWindow(QWidget *parent, MainWindow *mainwin, G
ui->lineEdit_date->setText(misc::makeDateTimeString(commit->commit_date));
ui->lineEdit_author->setText(commit->author);

GitPtr g = git();
{
GitCommit c;
GitCommit::parseCommit(objcache, m->commit->commit_id.toQString(), &c);
GitCommit::parseCommit(g, objcache, m->commit->commit_id.toQString(), &c);
m->root_tree_id = c.tree_id;
}

{
GitCommitTree tree(objcache);
tree.parseTree(m->root_tree_id);
tree.parseTree(g, m->root_tree_id);
}

{
Expand All @@ -77,7 +78,7 @@ CommitExploreWindow::CommitExploreWindow(QWidget *parent, MainWindow *mainwin, G
rootitem->setData(0, ObjectIdRole, m->root_tree_id);
ui->treeWidget->addTopLevelItem(rootitem);

loadTree(m->root_tree_id);
loadTree(g, m->root_tree_id);

rootitem->setExpanded(true);
}
Expand All @@ -94,12 +95,17 @@ MainWindow *CommitExploreWindow::mainwindow()
return global->mainwindow;
}

GitPtr CommitExploreWindow::git()
{
return mainwindow()->git();
}

void CommitExploreWindow::clearContent()
{
m->content = ObjectContent();
}

void CommitExploreWindow::expandTreeItem_(QTreeWidgetItem *item)
void CommitExploreWindow::expandTreeItem_(GitPtr g, QTreeWidgetItem *item)
{
if (item->childCount() == 1) {
if (item->child(0)->text(0).isEmpty()) {
Expand All @@ -114,7 +120,7 @@ void CommitExploreWindow::expandTreeItem_(QTreeWidgetItem *item)
QString path = item->data(0, FilePathRole).toString();

QString tree_id = item->data(0, ObjectIdRole).toString();
loadTree(tree_id);
loadTree(g, tree_id);

for (GitTreeItem const &ti : m->tree_item_list) {
if (ti.type == GitTreeItem::TREE) {
Expand All @@ -134,13 +140,13 @@ void CommitExploreWindow::expandTreeItem_(QTreeWidgetItem *item)

void CommitExploreWindow::on_treeWidget_itemExpanded(QTreeWidgetItem *item)
{
expandTreeItem_(item);
expandTreeItem_(git(), item);
}

void CommitExploreWindow::loadTree(QString const &tree_id)
void CommitExploreWindow::loadTree(GitPtr g, QString const &tree_id)
{
GitCommitTree tree(m->objcache);
tree.parseTree(tree_id);
tree.parseTree(g, tree_id);

m->tree_item_list = *tree.treelist();

Expand All @@ -152,15 +158,15 @@ void CommitExploreWindow::loadTree(QString const &tree_id)
});
}

void CommitExploreWindow::doTreeItemChanged_(QTreeWidgetItem *current)
void CommitExploreWindow::doTreeItemChanged_(GitPtr g, QTreeWidgetItem *current)
{
ui->listWidget->clear();

QString path = current->data(0, FilePathRole).toString();

QString tree_id = current->data(0, ObjectIdRole).toString();

loadTree(tree_id);
loadTree(g, tree_id);

QFileIconProvider icons;

Expand Down Expand Up @@ -195,7 +201,8 @@ void CommitExploreWindow::doTreeItemChanged_(QTreeWidgetItem *current)
void CommitExploreWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem * /*previous*/)
{
clearContent();
doTreeItemChanged_(current);
GitPtr g = git();
doTreeItemChanged_(g, current);
}

void CommitExploreWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
Expand All @@ -207,7 +214,8 @@ void CommitExploreWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
QString tree_id = item->data(ObjectIdRole).toString();
clearContent();
QTreeWidgetItem *parent = ui->treeWidget->currentItem();
expandTreeItem_(parent);
GitPtr g = git();
expandTreeItem_(g, parent);
parent->setExpanded(true);
int n = parent->childCount();
for (int i = 0; i < n; i++) {
Expand All @@ -231,7 +239,8 @@ void CommitExploreWindow::on_listWidget_currentItemChanged(QListWidgetItem *curr
GitTreeItem::Type type = (GitTreeItem::Type)current->data(ItemTypeRole).toInt();
if (type == GitTreeItem::BLOB) {
QString id = current->data(ObjectIdRole).toString();
m->content_object = m->objcache->catFile(id);
GitPtr g = git();
m->content_object = m->objcache->catFile(g, id);
QString path = current->data(FilePathRole).toString();
clearContent();
QString mimetype = mainwindow()->determinFileType(m->content_object.content);
Expand Down
7 changes: 4 additions & 3 deletions src/CommitExploreWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class CommitExploreWindow : public QDialog {
struct Private;
Private *m;

void loadTree(QString const &tree_id);
void doTreeItemChanged_(QTreeWidgetItem *current);
void expandTreeItem_(QTreeWidgetItem *item);
void loadTree(GitPtr g, QString const &tree_id);
void doTreeItemChanged_(GitPtr g, QTreeWidgetItem *current);
void expandTreeItem_(GitPtr g, QTreeWidgetItem *item);
MainWindow *mainwindow();
GitPtr git();
public:
explicit CommitExploreWindow(QWidget *parent, MainWindow *mainwin, GitObjectCache *objcache, Git::CommitItem const *commit);
~CommitExploreWindow() override;
Expand Down
73 changes: 37 additions & 36 deletions src/GitDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QDebug>
#include <QThread>
#include "ApplicationGlobal.h"
#include "MainWindow.h"

// PathToIdMap

Expand Down Expand Up @@ -41,14 +42,14 @@ class GitDiff::LookupTable {

// GitDiff

GitPtr GitDiff::git()
{
return objcache->git();
}
// GitPtr GitDiff::git()
// {
// return objcache->git();
// }

GitPtr GitDiff::git(Git::SubmoduleItem const &submod)
{
return objcache->git(submod);
return global->mainwindow->git(submod);
}

QString GitDiff::makeKey(QString const &a_id, QString const &b_id)
Expand Down Expand Up @@ -130,21 +131,21 @@ void GitDiff::parseDiff(std::string const &s, Git::Diff const *info, Git::Diff *
}
}

void GitDiff::retrieveCompleteTree(QString const &dir, GitTreeItemList const *files, std::map<QString, GitTreeItem> *out)
void GitDiff::retrieveCompleteTree(GitPtr g, QString const &dir, GitTreeItemList const *files, std::map<QString, GitTreeItem> *out)
{
for (GitTreeItem const &d : *files) {
QString path = misc::joinWithSlash(dir, d.name);
if (d.type == GitTreeItem::BLOB) {
(*out)[path] = d;
} else if (d.type == GitTreeItem::TREE) {
GitTreeItemList files2;
parseGitTreeObject(objcache, d.id, QString(), &files2);
retrieveCompleteTree(path, &files2, out);
parseGitTreeObject(g, objcache, d.id, QString(), &files2);
retrieveCompleteTree(g, path, &files2, out);
}
}
}

void GitDiff::retrieveCompleteTree(QString const &dir, GitTreeItemList const *files)
void GitDiff::retrieveCompleteTree(GitPtr g, QString const &dir, GitTreeItemList const *files)
{
for (GitTreeItem const &d : *files) {
QString path = misc::joinWithSlash(dir, d.name);
Expand All @@ -153,8 +154,8 @@ void GitDiff::retrieveCompleteTree(QString const &dir, GitTreeItemList const *fi
diffs.push_back(diff);
} else if (d.type == GitTreeItem::TREE) {
GitTreeItemList files2;
parseGitTreeObject(objcache, d.id, QString(), &files2);
retrieveCompleteTree(path, &files2);
parseGitTreeObject(g, objcache, d.id, QString(), &files2);
retrieveCompleteTree(g, path, &files2);
}
}
}
Expand All @@ -176,11 +177,11 @@ bool GitDiff::diff(GitPtr g, Git::CommitID const &id, const QList<Git::Submodule
{ // diff_raw
GitTreeItemList files;
GitCommit newer_commit;
GitCommit::parseCommit(objcache, id, &newer_commit);
parseGitTreeObject(objcache, newer_commit.tree_id, QString(), &files);
GitCommit::parseCommit(g, objcache, id, &newer_commit);
parseGitTreeObject(g, objcache, newer_commit.tree_id, QString(), &files);

if (newer_commit.parents.isEmpty()) { // 親がないなら最古のコミット
retrieveCompleteTree(QString(), &files); // ツリー全体を取得
retrieveCompleteTree(g, QString(), &files); // ツリー全体を取得
} else {
std::map<QString, Git::Diff> diffmap;

Expand All @@ -189,7 +190,7 @@ bool GitDiff::diff(GitPtr g, Git::CommitID const &id, const QList<Git::Submodule

QList<Git::DiffRaw> list;
for (QString const &parent : newer_commit.parents) {
QList<Git::DiffRaw> l = git()->diff_raw(parent, id);
QList<Git::DiffRaw> l = g->diff_raw(parent, id);
for (Git::DiffRaw const &item : l) {
if (item.state.startsWith('D')) {
deleted_set.insert(item.a.id);
Expand Down Expand Up @@ -276,7 +277,7 @@ bool GitDiff::diff(GitPtr g, Git::CommitID const &id, const QList<Git::Submodule
Git::FileStatusList stats = g->status_s(); // git status

GitCommitTree head_tree(objcache);
head_tree.parseCommit(head_id); // HEADが親
head_tree.parseCommit(g, head_id); // HEADが親

QString zeros(GIT_ID_LENGTH, '0');

Expand All @@ -285,7 +286,7 @@ bool GitDiff::diff(GitPtr g, Git::CommitID const &id, const QList<Git::Submodule
Git::Diff item;

GitTreeItem treeitem;
if (head_tree.lookup(path, &treeitem)) {
if (head_tree.lookup(g, path, &treeitem)) {
item.blob.a_id_or_path = treeitem.id; // HEADにおけるこのファイルのID
if (fs.isDeleted()) { // 削除されてる
item.blob.b_id_or_path = zeros; // 削除された
Expand Down Expand Up @@ -364,12 +365,12 @@ GitCommitTree::GitCommitTree(GitObjectCache *objcache)
{
}

GitPtr GitCommitTree::git()
{
return objcache->git();
}
// GitPtr GitCommitTree::git()
// {
// return objcache->git();
// }

QString GitCommitTree::lookup_(QString const &file, GitTreeItem *out)
QString GitCommitTree::lookup_(GitPtr g, QString const &file, GitTreeItem *out)
{
int i = file.lastIndexOf('/');
if (i >= 0) {
Expand All @@ -381,11 +382,11 @@ QString GitCommitTree::lookup_(QString const &file, GitTreeItem *out)
if (it != tree_id_map.end()) {
tree_id = it->second;
} else {
tree_id = lookup_(subdir, out);
tree_id = lookup_(g, subdir, out);
}
}
GitTreeItemList list;
if (parseGitTreeObject(objcache, tree_id, QString(), &list)) {
if (parseGitTreeObject(g, objcache, tree_id, QString(), &list)) {
QString return_id;
for (GitTreeItem const &d : list) {
if (d.name == name) {
Expand Down Expand Up @@ -423,36 +424,36 @@ QString GitCommitTree::lookup_(QString const &file, GitTreeItem *out)
return QString();
}

QString GitCommitTree::lookup(QString const &file)
QString GitCommitTree::lookup(GitPtr g, QString const &file)
{
auto it = blob_map.find(file);
if (it != blob_map.end()) {
return it->second.id;
}
return lookup_(file, nullptr);
return lookup_(g, file, nullptr);
}

bool GitCommitTree::lookup(QString const &file, GitTreeItem *out)
bool GitCommitTree::lookup(GitPtr g, QString const &file, GitTreeItem *out)
{
*out = GitTreeItem();
auto it = blob_map.find(file);
if (it != blob_map.end()) {
*out = it->second;
return true;
}
return !lookup_(file, out).isEmpty();
return !lookup_(g, file, out).isEmpty();
}

void GitCommitTree::parseTree(QString const &tree_id)
void GitCommitTree::parseTree(GitPtr g, QString const &tree_id)
{
parseGitTreeObject(objcache, tree_id, QString(), &root_item_list);
parseGitTreeObject(g, objcache, tree_id, QString(), &root_item_list);
}

QString GitCommitTree::parseCommit(QString const &commit_id)
QString GitCommitTree::parseCommit(GitPtr g, QString const &commit_id)
{
GitCommit commit;
GitCommit::parseCommit(objcache, commit_id, &commit);
parseTree(commit.tree_id);
GitCommit::parseCommit(g, objcache, commit_id, &commit);
parseTree(g, commit.tree_id);
return commit.tree_id;
}

Expand All @@ -465,10 +466,10 @@ QString GitCommitTree::parseCommit(QString const &commit_id)
* @param file
* @return
*/
QString lookupFileID(GitObjectCache *objcache, QString const &commit_id, QString const &file)
QString lookupFileID(GitPtr g, GitObjectCache *objcache, QString const &commit_id, QString const &file)
{
GitCommitTree commit_tree(objcache);
commit_tree.parseCommit(commit_id);
QString id = commit_tree.lookup(file);
commit_tree.parseCommit(g, commit_id);
QString id = commit_tree.lookup(g, file);
return id;
}
10 changes: 5 additions & 5 deletions src/GitDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class GitDiff {

using MapList = std::list<LookupTable>;

GitPtr git();
// GitPtr git();
GitPtr git(const Git::SubmoduleItem &submod);

static void AddItem(Git::Diff *item, QList<Git::Diff> *diffs);

void retrieveCompleteTree(QString const &dir, GitTreeItemList const *files, std::map<QString, GitTreeItem> *out);
void retrieveCompleteTree(QString const &dir, GitTreeItemList const *files);
void retrieveCompleteTree(GitPtr g, QString const &dir, GitTreeItemList const *files, std::map<QString, GitTreeItem> *out);
void retrieveCompleteTree(GitPtr g, QString const &dir, GitTreeItemList const *files);
public:
GitDiff(GitObjectCache *objcache)
{
Expand All @@ -57,8 +57,8 @@ class GitDiff {
static QString makeKey(const QString &a_id, const QString &b_id);
static QString makeKey(const Git::Diff &diff);
static QString prependPathPrefix(QString const &path);


};

QString lookupFileID(GitPtr g, GitObjectCache *objcache, QString const &commit_id, QString const &file);

#endif // GITDIFF_H
Loading

0 comments on commit 5fdf913

Please sign in to comment.