-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqgitreflogdialog.cpp
More file actions
160 lines (139 loc) · 5.81 KB
/
Copy pathqgitreflogdialog.cpp
File metadata and controls
160 lines (139 loc) · 5.81 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
#include "qgitreflogdialog.h"
#include "ui_qgitreflogdialog.h"
#include "qgit.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QDateTime>
QGitReflogDialog::QGitReflogDialog(const QString &refName, QGitRepository *repository, QWidget *parent)
: QDialog(parent)
, ui(new Ui::QGitReflogDialog)
, m_refName(refName)
, m_repository(repository)
{
ui->setupUi(this);
ui->label_info->setText(tr("Reflog for reference: %1").arg(refName));
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
loadReflog();
on_tableWidget_itemSelectionChanged();
}
QGitReflogDialog::~QGitReflogDialog()
{
delete ui;
}
void QGitReflogDialog::loadReflog()
{
ui->tableWidget->setRowCount(0);
ui->tableWidget->setSortingEnabled(false);
QList<QGitReflogEntry> entries = m_repository->git()->getReflog(m_refName);
ui->tableWidget->setRowCount(entries.count());
for (int row = 0; row < entries.count(); ++row) {
const auto &entry = entries.at(row);
auto *itemHash = new QTableWidgetItem(entry.commitHash.left(8));
itemHash->setData(Qt::UserRole, entry.commitHash);
QString committerStr;
if (!entry.committerEmail.isEmpty()) {
committerStr = QStringLiteral("%1 <%2>").arg(entry.committerName, entry.committerEmail);
} else {
committerStr = entry.committerName;
}
auto *itemCommitter = new QTableWidgetItem(committerStr);
QString dateStr = QDateTime::fromSecsSinceEpoch(entry.time).toString();
auto *itemDate = new QTableWidgetItem(dateStr);
auto *itemMsg = new QTableWidgetItem(entry.message);
ui->tableWidget->setItem(row, 0, itemHash);
ui->tableWidget->setItem(row, 1, itemCommitter);
ui->tableWidget->setItem(row, 2, itemDate);
ui->tableWidget->setItem(row, 3, itemMsg);
}
}
QString QGitReflogDialog::selectedCommitHash() const
{
int row = ui->tableWidget->currentRow();
if (row >= 0) {
auto *item = ui->tableWidget->item(row, 0);
if (item) {
return item->data(Qt::UserRole).toString();
}
}
return QString();
}
void QGitReflogDialog::on_tableWidget_itemSelectionChanged()
{
bool hasSelection = !selectedCommitHash().isEmpty();
ui->checkoutButton->setEnabled(hasSelection);
ui->createBranchButton->setEnabled(hasSelection);
ui->resetButton->setEnabled(hasSelection);
}
void QGitReflogDialog::on_checkoutButton_clicked()
{
QString hash = selectedCommitHash();
if (hash.isEmpty()) return;
bool ok;
QString branchName = QInputDialog::getText(this, tr("Checkout Commit"),
tr("Create and checkout a new branch at commit %1:").arg(hash.left(7)),
QLineEdit::Normal,
QStringLiteral("reflog-recover"),
&ok);
if (ok && !branchName.isEmpty()) {
try {
m_repository->git()->createLocalBranch(branchName, hash, true);
m_repository->refreshData();
accept();
} catch (const QGitError &error) {
QMessageBox::warning(this, tr("Checkout Commit Error"), error.errorString());
}
}
}
void QGitReflogDialog::on_createBranchButton_clicked()
{
QString hash = selectedCommitHash();
if (hash.isEmpty()) return;
bool ok;
QString branchName = QInputDialog::getText(this, tr("Create Branch"),
tr("Create a new branch at commit %1:").arg(hash.left(7)),
QLineEdit::Normal,
QStringLiteral("reflog-branch"),
&ok);
if (ok && !branchName.isEmpty()) {
try {
m_repository->git()->createLocalBranch(branchName, hash, false);
m_repository->refreshData();
loadReflog();
QMessageBox::information(this, tr("Branch Created"), tr("Branch '%1' successfully created.").arg(branchName));
} catch (const QGitError &error) {
QMessageBox::warning(this, tr("Create Branch Error"), error.errorString());
}
}
}
void QGitReflogDialog::on_resetButton_clicked()
{
QString hash = selectedCommitHash();
if (hash.isEmpty()) return;
git_reset_t resetType = GIT_RESET_SOFT;
QString typeStr = tr("Soft");
int index = ui->resetType_comboBox->currentIndex();
if (index == 1) {
resetType = GIT_RESET_MIXED;
typeStr = tr("Mixed");
} else if (index == 2) {
resetType = GIT_RESET_HARD;
typeStr = tr("Hard");
}
auto confirm = QMessageBox::question(this, tr("Reset Current Branch"),
tr("Are you sure you want to perform a %1 Reset to commit %2?\n\nThis will move your current branch pointer.")
.arg(typeStr, hash.left(7)),
QMessageBox::Yes | QMessageBox::No);
if (confirm == QMessageBox::Yes) {
try {
m_repository->git()->reset(hash, resetType);
m_repository->refreshData();
QMessageBox::information(this, tr("Reset Successful"), tr("Branch successfully reset to commit %1.").arg(hash.left(7)));
accept();
} catch (const QGitError &error) {
QMessageBox::critical(this, tr("Reset Error"), error.errorString());
}
}
}