-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqgitworktreedialog.cpp
More file actions
107 lines (89 loc) · 2.72 KB
/
Copy pathqgitworktreedialog.cpp
File metadata and controls
107 lines (89 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "qgitworktreedialog.h"
#include "ui_qgitworktreedialog.h"
#include "qgitrepository.h"
#include "qgit.h"
#include <QFileDialog>
#include <QDir>
QGitWorktreeDialog::QGitWorktreeDialog(QGitRepository *parent)
: QDialog(parent)
, ui(new Ui::QGitWorktreeDialog)
{
ui->setupUi(this);
m_git.setPath(parent->git()->path());
// Populate branch combobox with local branches
auto branches = m_git.branches(GIT_BRANCH_LOCAL);
auto currentBranch = m_git.currentBranch();
for (const auto &branch : branches) {
// Extract short name from "refs/heads/..." format
QString name = branch.name();
QStringView nameView(name);
if (nameView.startsWith(QStringLiteral("refs/heads/")))
nameView = nameView.sliced(11);
ui->comboBox_branch->addItem(nameView.toString());
}
// Pre-select current branch
int idx = ui->comboBox_branch->findText(currentBranch);
if (idx >= 0)
ui->comboBox_branch->setCurrentIndex(idx);
ui->lineEdit_name->setFocus();
validate();
}
QGitWorktreeDialog::~QGitWorktreeDialog()
{
delete ui;
}
QString QGitWorktreeDialog::worktreeName() const
{
return ui->lineEdit_name->text().trimmed();
}
QString QGitWorktreeDialog::worktreePath() const
{
return ui->lineEdit_path->text().trimmed();
}
QString QGitWorktreeDialog::worktreeBranch() const
{
if (ui->checkBox_newBranch->isChecked())
return ui->lineEdit_name->text().trimmed(); // new branch = same name as worktree
return ui->comboBox_branch->currentText();
}
bool QGitWorktreeDialog::createNewBranch() const
{
return ui->checkBox_newBranch->isChecked();
}
void QGitWorktreeDialog::on_pushButton_browse_clicked()
{
QString dir = QFileDialog::getExistingDirectory(
this,
tr("Select Worktree Directory"),
m_git.path().absolutePath()
);
if (!dir.isEmpty()) {
ui->lineEdit_path->setText(dir);
// Auto-fill name from the last path component if name is empty
if (ui->lineEdit_name->text().trimmed().isEmpty()) {
ui->lineEdit_name->setText(QDir(dir).dirName());
}
}
}
void QGitWorktreeDialog::on_lineEdit_name_textChanged(const QString &text)
{
Q_UNUSED(text)
validate();
}
void QGitWorktreeDialog::on_lineEdit_path_textChanged(const QString &text)
{
Q_UNUSED(text)
validate();
}
void QGitWorktreeDialog::on_checkBox_newBranch_toggled(bool checked)
{
ui->comboBox_branch->setEnabled(!checked);
validate();
}
void QGitWorktreeDialog::validate()
{
bool ok = !ui->lineEdit_name->text().trimmed().isEmpty()
&& !ui->lineEdit_path->text().trimmed().isEmpty();
if (auto *btn = ui->buttonBox->button(QDialogButtonBox::Ok))
btn->setEnabled(ok);
}