Skip to content

Commit

Permalink
(wip) Add functionality to scan folder and register repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jun 3, 2024
1 parent 552bf61 commit d5b37be
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 41 deletions.
134 changes: 95 additions & 39 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,45 @@ bool MainWindow::execSetGlobalUserDialog()
return false;
}

QString MainWindow::preferredRepositoryGroup() const
{
return m->add_repository_into_group;
}

void MainWindow::setPreferredRepositoryGroup(QString const &group)
{
m->add_repository_into_group = group;
}

void MainWindow::saveRepositoryBookmark(RepositoryData item)
{
if (item.local_dir.isEmpty()) return;

if (item.name.isEmpty()) {
item.name = tr("Unnamed");
}

item.group = preferredRepositoryGroup();

QList<RepositoryData> repos = cRepositories();

bool done = false;
for (auto &repo : repos) {
RepositoryData *p = &repo;
if (item.local_dir == p->local_dir) {
*p = item;
done = true;
break;
}
}
if (!done) {
repos.push_back(item);
}
setRepositoryList(std::move(repos));
saveRepositoryBookmarks();
updateRepositoriesList();
}

/**
* @brief MainWindow::addExistingLocalRepository
* @param dir ディレクトリ
Expand All @@ -1061,7 +1100,7 @@ bool MainWindow::execSetGlobalUserDialog()
*
* 既存のリポジトリを追加する
*/
bool MainWindow::addExistingLocalRepository(QString dir, QString name, QString sshkey, bool open)
bool MainWindow::addExistingLocalRepository(QString dir, QString name, QString sshkey, bool open, bool save, bool msgbox_if_err)
{
if (dir.endsWith(".git")) {
auto i = dir.size();
Expand All @@ -1074,7 +1113,7 @@ bool MainWindow::addExistingLocalRepository(QString dir, QString name, QString s
}

if (!Git::isValidWorkingCopy(dir)) {
if (QFileInfo(dir).isDir()) {
if (msgbox_if_err && QFileInfo(dir).isDir()) {
QString text;
text += tr("The folder is not a valid git repository.") + '\n';
text += '\n';
Expand All @@ -1096,8 +1135,11 @@ bool MainWindow::addExistingLocalRepository(QString dir, QString name, QString s
RepositoryData item;
item.local_dir = dir;
item.name = name;
item.group = preferredRepositoryGroup();
item.ssh_key = sshkey;
saveRepositoryBookmark(item);
if (save) {
saveRepositoryBookmark(item);
}

if (open) {
setCurrentRepository(item, true);
Expand All @@ -1108,6 +1150,29 @@ bool MainWindow::addExistingLocalRepository(QString dir, QString name, QString s
return true;
}

void MainWindow::addExistingLocalRepositoryWithGroup(const QString &dir, const QString &group)
{
// setPreferredRepositoryGroup(group);
// addExistingLocalRepository(dir, {}, {}, false, false, false);
QFileInfo info1(dir);
if (info1.isDir()) {
QFileInfo info2(dir / ".git");
if (info2.isDir()) {
RepositoryData item;
item.local_dir = info1.absoluteFilePath();
item.name = makeRepositoryName(item.local_dir);
item.group = group;
// TODO: check duplicated
m->repos.append(item);
}
}
}

bool MainWindow::addExistingLocalRepository(const QString &dir, bool open)
{
return addExistingLocalRepository(dir, {}, {}, open);
}

/**
* @brief MainWindow::execWelcomeWizardDialog
*
Expand Down Expand Up @@ -2184,7 +2249,7 @@ void MainWindow::initRepository(QString const &path, QString const &reponame, Gi
*/
void MainWindow::addRepository(const QString &local_dir, const QString &group)
{
m->add_repository_into_group = group;
setPreferredRepositoryGroup(group);

AddRepositoryDialog dlg(this, local_dir); // リポジトリを追加するダイアログ
if (dlg.exec() == QDialog::Accepted) {
Expand All @@ -2207,6 +2272,22 @@ void MainWindow::addRepository(const QString &local_dir, const QString &group)
}
}

void MainWindow::scanFolderAndRegister(QString const &group)
{
QString path = QFileDialog::getExistingDirectory(this, tr("Select a folder"), QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!path.isEmpty()) {
QDirIterator it(path, QDir::Dirs | QDir::NoDotAndDotDot);
while (it.hasNext()) {
it.next();
QFileInfo info = it.fileInfo();
QString local_dir = info.absoluteFilePath();
addExistingLocalRepositoryWithGroup(local_dir, group);
}
saveRepositoryBookmarks2();
updateRepositoriesList();
}
}

void MainWindow::setLogEnabled(GitPtr g, bool f)
{
if (f) {
Expand Down Expand Up @@ -2636,35 +2717,15 @@ done:;
*out = submodules;
}

QString MainWindow::preferredRepositoryGroup() const
bool MainWindow::saveRepositoryBookmarks() const
{
return m->add_repository_into_group;
QString path = getBookmarksFilePath();
return RepositoryBookmark::save(path, &cRepositories());
}

void MainWindow::saveRepositoryBookmark(RepositoryData item)
void MainWindow::saveRepositoryBookmarks2() // TODO: rename
{
if (item.local_dir.isEmpty()) return;

if (item.name.isEmpty()) {
item.name = tr("Unnamed");
}

item.group = preferredRepositoryGroup();

QList<RepositoryData> repos = cRepositories();

bool done = false;
for (auto &repo : repos) {
RepositoryData *p = &repo;
if (item.local_dir == p->local_dir) {
*p = item;
done = true;
break;
}
}
if (!done) {
repos.push_back(item);
}
setRepositoryList(std::move(repos));
saveRepositoryBookmarks();
updateRepositoriesList();
Expand Down Expand Up @@ -3025,12 +3086,6 @@ void MainWindow::initNetworking()
global->webcx.set_https_proxy(https_proxy);
}

bool MainWindow::saveRepositoryBookmarks() const
{
QString path = getBookmarksFilePath();
return RepositoryBookmark::save(path, &cRepositories());
}

QString MainWindow::getBookmarksFilePath() const
{
return global->app_config_dir / "bookmarks.xml";
Expand Down Expand Up @@ -4556,6 +4611,7 @@ void MainWindow::on_treeWidget_repos_customContextMenuRequested(const QPoint &po
QAction *a_rename_group = menu.addAction(tr("&Rename group"));
menu.addSeparator();
QAction *a_add_repository = menu.addAction(tr("&Add repository"));
QAction *a_scan_folder_and_register = menu.addAction(tr("&Scan folder and register"));
QPoint pt = ui->treeWidget_repos->mapToGlobal(pos);
QAction *a = menu.exec(pt + QPoint(8, -8));
if (a) {
Expand Down Expand Up @@ -4587,6 +4643,11 @@ void MainWindow::on_treeWidget_repos_customContextMenuRequested(const QPoint &po
addRepository({}, group);
return;
}
if (a == a_scan_folder_and_register) {
QString group = treeItemGroup(treeitem) / treeItemName(treeitem);
scanFolderAndRegister(group);
return;
}
}
} else if (repo) { // repository item
QStringList strings;
Expand Down Expand Up @@ -5397,11 +5458,6 @@ void MainWindow::jumpToCommit(RepositoryWrapperFrame *frame, QString id)
}
}

bool MainWindow::addExistingLocalRepository(const QString &dir, bool open)
{
return addExistingLocalRepository(dir, {}, {}, open);
}

bool MainWindow::saveAs(RepositoryWrapperFrame *frame, const QString &id, const QString &dstpath)
{
if (id.startsWith(PATH_PREFIX)) {
Expand Down
9 changes: 7 additions & 2 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class MainWindow : public QMainWindow {
friend class FileHistoryWindow;
friend class FileDiffWidget;
friend class AboutDialog;
friend class RepositoriesTreeWidget; // TODO
public:
enum {
IndexRole = Qt::UserRole,
Expand Down Expand Up @@ -226,7 +227,6 @@ class MainWindow : public QMainWindow {
static bool git_log_callback(void *cookie, const char *ptr, int len);
bool execSetGlobalUserDialog();
void revertAllFiles();
bool addExistingLocalRepository(QString dir, QString name, QString sshkey, bool open);
bool execWelcomeWizardDialog();
void execRepositoryPropertyDialog(const RepositoryData &repo, bool open_repository_menu = false);
void execConfigUserDialog(const Git::User &global_user, const Git::User &local_user, bool enable_local_user, const QString &reponame);
Expand Down Expand Up @@ -268,6 +268,7 @@ class MainWindow : public QMainWindow {
bool internalAddTag(RepositoryWrapperFrame *frame, const QString &name);
void createRepository(const QString &dir);
void addRepository(const QString &local_dir, const QString &group = {});
void scanFolderAndRegister(const QString &group);
void setLogEnabled(GitPtr g, bool f);
void doGitCommand(const std::function<void (GitPtr)> &callback);
void setWindowTitle_(const Git::User &user);
Expand Down Expand Up @@ -334,6 +335,7 @@ class MainWindow : public QMainWindow {
void updateCommitGraph(RepositoryWrapperFrame *frame);
void initNetworking();
bool saveRepositoryBookmarks() const;
void saveRepositoryBookmarks2();
QString getBookmarksFilePath() const;
void stopPtyProcess();
void abortPtyProcess();
Expand Down Expand Up @@ -407,6 +409,10 @@ class MainWindow : public QMainWindow {
static QString treeItemName(QTreeWidgetItem *item);
static QString treeItemGroup(QTreeWidgetItem *item);
QString preferredRepositoryGroup() const;
void setPreferredRepositoryGroup(const QString &group);
bool addExistingLocalRepository(QString dir, QString name, QString sshkey, bool open, bool save = true, bool msgbox_if_err = true);
void addExistingLocalRepositoryWithGroup(const QString &dir, const QString &group);
bool addExistingLocalRepository(const QString &dir, bool open);
protected:
void closeEvent(QCloseEvent *event) override;
void customEvent(QEvent *) override;
Expand Down Expand Up @@ -463,7 +469,6 @@ class MainWindow : public QMainWindow {
void jumpToCommit(RepositoryWrapperFrame *frame, QString id);
Git::Object internalCatFile(RepositoryWrapperFrame *frame, const QString &id);
Git::Object catFile(const QString &id);
bool addExistingLocalRepository(const QString &dir, bool open);
bool saveAs(RepositoryWrapperFrame *frame, const QString &id, const QString &dstpath);
QString determinFileType(QByteArray in);
QList<Git::Tag> queryTagList(RepositoryWrapperFrame *frame);
Expand Down

0 comments on commit d5b37be

Please sign in to comment.