-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqgitflowdialog.cpp
More file actions
272 lines (231 loc) · 9.79 KB
/
Copy pathqgitflowdialog.cpp
File metadata and controls
272 lines (231 loc) · 9.79 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "qgitflowdialog.h"
#include "ui_qgitflowdialog.h"
#include "qgitrepository.h"
#include <QMessageBox>
QGitFlowDialog::QGitFlowDialog(QGitRepository *parent)
: QDialog(parent)
, ui(new Ui::QGitFlowDialog)
, m_repo(parent)
{
ui->setupUi(this);
m_git.setPath(parent->git()->path());
refreshBranches();
// Enable/Disable push buttons based on input
on_lineEdit_featureName_textChanged(ui->lineEdit_featureName->text());
on_lineEdit_releaseName_textChanged(ui->lineEdit_releaseName->text());
on_lineEdit_hotfixName_textChanged(ui->lineEdit_hotfixName->text());
}
QGitFlowDialog::~QGitFlowDialog()
{
delete ui;
}
void QGitFlowDialog::refreshBranches()
{
ui->comboBox_featureBranch->clear();
ui->comboBox_releaseBranch->clear();
ui->comboBox_hotfixBranch->clear();
auto localBranches = m_git.branches(GIT_BRANCH_LOCAL);
for (const auto &branch : localBranches) {
QString name = branch.name();
QStringView nameView(name);
if (nameView.startsWith(QStringLiteral("refs/heads/")))
nameView = nameView.sliced(11);
if (nameView.startsWith(QStringLiteral("feature/"))) {
ui->comboBox_featureBranch->addItem(nameView.toString());
} else if (nameView.startsWith(QStringLiteral("release/"))) {
ui->comboBox_releaseBranch->addItem(nameView.toString());
} else if (nameView.startsWith(QStringLiteral("hotfix/"))) {
ui->comboBox_hotfixBranch->addItem(nameView.toString());
}
}
ui->pushButton_finishFeature->setEnabled(ui->comboBox_featureBranch->count() > 0);
ui->pushButton_finishRelease->setEnabled(ui->comboBox_releaseBranch->count() > 0);
ui->pushButton_finishHotfix->setEnabled(ui->comboBox_hotfixBranch->count() > 0);
}
bool QGitFlowDialog::hasBranch(const QString &name) const
{
auto localBranches = m_git.branches(GIT_BRANCH_LOCAL);
for (const auto &branch : localBranches) {
QString bName = branch.name();
QStringView bNameView(bName);
if (bNameView.startsWith(QStringLiteral("refs/heads/")))
bNameView = bNameView.sliced(11);
if (bNameView == name)
return true;
}
return false;
}
QString QGitFlowDialog::detectMainBranch() const
{
if (hasBranch(QStringLiteral("main")))
return QStringLiteral("main");
if (hasBranch(QStringLiteral("master")))
return QStringLiteral("master");
return m_git.currentBranch();
}
bool QGitFlowDialog::ensureDevelopBranch()
{
if (!hasBranch(QStringLiteral("develop"))) {
QString mainBranch = detectMainBranch();
if (mainBranch.isEmpty()) {
QMessageBox::warning(this, tr("Git Flow Initialization"),
tr("Cannot initialize Git Flow in an empty repository. Please make at least one commit first."));
return false;
}
try {
m_git.createLocalBranch(QStringLiteral("develop"), mainBranch, false);
QMessageBox::information(this, tr("Git Flow Initialization"),
tr("Branch 'develop' was automatically created from '%1'.").arg(mainBranch));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Initialization Error"),
tr("Failed to create 'develop' branch:\n%1").arg(err.errorString()));
return false;
}
}
return true;
}
void QGitFlowDialog::on_pushButton_startFeature_clicked()
{
QString name = ui->lineEdit_featureName->text().trimmed();
if (name.isEmpty()) return;
QString branchName = QStringLiteral("feature/") + name;
if (hasBranch(branchName)) {
QMessageBox::warning(this, tr("Branch Exists"), tr("Branch '%1' already exists.").arg(branchName));
return;
}
try {
if (!ensureDevelopBranch()) return;
m_git.createLocalBranch(branchName, QStringLiteral("develop"), true);
ui->lineEdit_featureName->clear();
refreshBranches();
m_repo->refreshData();
QMessageBox::information(this, tr("Feature Started"), tr("Feature branch '%1' created and checked out.").arg(branchName));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Error"), tr("Failed to start feature:\n%1").arg(err.errorString()));
}
}
void QGitFlowDialog::on_pushButton_finishFeature_clicked()
{
QString branchName = ui->comboBox_featureBranch->currentText();
if (branchName.isEmpty()) return;
try {
if (!ensureDevelopBranch()) return;
m_git.checkoutBranch(QStringLiteral("develop"));
m_git.merge(branchName);
// Delete feature branch
QList<QGitBranch> toDelete;
toDelete.append(QGitBranch(branchName, "", 0, GIT_BRANCH_LOCAL));
m_git.deleteBranches(toDelete, true);
refreshBranches();
m_repo->refreshData();
QMessageBox::information(this, tr("Feature Finished"), tr("Feature branch '%1' merged into develop and deleted.").arg(branchName));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Error"), tr("Failed to finish feature:\n%1").arg(err.errorString()));
}
}
void QGitFlowDialog::on_pushButton_startRelease_clicked()
{
QString name = ui->lineEdit_releaseName->text().trimmed();
if (name.isEmpty()) return;
QString branchName = QStringLiteral("release/") + name;
if (hasBranch(branchName)) {
QMessageBox::warning(this, tr("Branch Exists"), tr("Branch '%1' already exists.").arg(branchName));
return;
}
try {
if (!ensureDevelopBranch()) return;
m_git.createLocalBranch(branchName, QStringLiteral("develop"), true);
ui->lineEdit_releaseName->clear();
refreshBranches();
m_repo->refreshData();
QMessageBox::information(this, tr("Release Started"), tr("Release branch '%1' created and checked out.").arg(branchName));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Error"), tr("Failed to start release:\n%1").arg(err.errorString()));
}
}
void QGitFlowDialog::on_pushButton_finishRelease_clicked()
{
QString branchName = ui->comboBox_releaseBranch->currentText();
if (branchName.isEmpty()) return;
QString releaseName = branchName.mid(8); // remove "release/"
QString mainBranch = detectMainBranch();
try {
// Merge into main
m_git.checkoutBranch(mainBranch);
m_git.merge(branchName);
// Tag the release
m_git.createTag(releaseName, mainBranch, QString("Release %1").arg(releaseName), true);
// Merge into develop
if (!ensureDevelopBranch()) return;
m_git.checkoutBranch(QStringLiteral("develop"));
m_git.merge(branchName);
// Delete release branch
QList<QGitBranch> toDelete;
toDelete.append(QGitBranch(branchName, "", 0, GIT_BRANCH_LOCAL));
m_git.deleteBranches(toDelete, true);
refreshBranches();
m_repo->refreshData();
QMessageBox::information(this, tr("Release Finished"), tr("Release branch '%1' merged into '%2' and 'develop', and tagged as '%3'.").arg(branchName, mainBranch, releaseName));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Error"), tr("Failed to finish release:\n%1").arg(err.errorString()));
}
}
void QGitFlowDialog::on_pushButton_startHotfix_clicked()
{
QString name = ui->lineEdit_hotfixName->text().trimmed();
if (name.isEmpty()) return;
QString branchName = QStringLiteral("hotfix/") + name;
if (hasBranch(branchName)) {
QMessageBox::warning(this, tr("Branch Exists"), tr("Branch '%1' already exists.").arg(branchName));
return;
}
try {
QString mainBranch = detectMainBranch();
m_git.createLocalBranch(branchName, mainBranch, true);
ui->lineEdit_hotfixName->clear();
refreshBranches();
m_repo->refreshData();
QMessageBox::information(this, tr("Hotfix Started"), tr("Hotfix branch '%1' created and checked out from '%2'.").arg(branchName, mainBranch));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Error"), tr("Failed to start hotfix:\n%1").arg(err.errorString()));
}
}
void QGitFlowDialog::on_pushButton_finishHotfix_clicked()
{
QString branchName = ui->comboBox_hotfixBranch->currentText();
if (branchName.isEmpty()) return;
QString hotfixName = branchName.mid(7); // remove "hotfix/"
QString mainBranch = detectMainBranch();
try {
// Merge into main
m_git.checkoutBranch(mainBranch);
m_git.merge(branchName);
// Tag the hotfix
m_git.createTag(hotfixName, mainBranch, QString("Hotfix %1").arg(hotfixName), true);
// Merge into develop
if (!ensureDevelopBranch()) return;
m_git.checkoutBranch(QStringLiteral("develop"));
m_git.merge(branchName);
// Delete hotfix branch
QList<QGitBranch> toDelete;
toDelete.append(QGitBranch(branchName, "", 0, GIT_BRANCH_LOCAL));
m_git.deleteBranches(toDelete, true);
refreshBranches();
m_repo->refreshData();
QMessageBox::information(this, tr("Hotfix Finished"), tr("Hotfix branch '%1' merged into '%2' and 'develop', and tagged as '%3'.").arg(branchName, mainBranch, hotfixName));
} catch (const QGitError &err) {
QMessageBox::critical(this, tr("Error"), tr("Failed to finish hotfix:\n%1").arg(err.errorString()));
}
}
void QGitFlowDialog::on_lineEdit_featureName_textChanged(const QString &text)
{
ui->pushButton_startFeature->setEnabled(!text.trimmed().isEmpty());
}
void QGitFlowDialog::on_lineEdit_releaseName_textChanged(const QString &text)
{
ui->pushButton_startRelease->setEnabled(!text.trimmed().isEmpty());
}
void QGitFlowDialog::on_lineEdit_hotfixName_textChanged(const QString &text)
{
ui->pushButton_startHotfix->setEnabled(!text.trimmed().isEmpty());
}