Commit ffdd3177 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh
Browse files

Git: Support remove/rename for tags



Change-Id: I737d2405ef1a10ec451c4baceaea9a0bc194ac1e
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 0ae3d29a
......@@ -104,10 +104,11 @@ void BranchDialog::enableButtons()
const bool currentSelected = hasSelection && idx == m_model->currentBranch();
const bool isLocal = m_model->isLocal(idx);
const bool isLeaf = m_model->isLeaf(idx);
const bool isTag = m_model->isTag(idx);
const bool hasActions = hasSelection && isLeaf;
m_ui->removeButton->setEnabled(hasActions && !currentSelected && isLocal);
m_ui->renameButton->setEnabled(hasActions && isLocal);
m_ui->removeButton->setEnabled(hasActions && !currentSelected && (isLocal || isTag));
m_ui->renameButton->setEnabled(hasActions && (isLocal || isTag));
m_ui->logButton->setEnabled(hasActions);
m_ui->diffButton->setEnabled(hasActions);
m_ui->checkoutButton->setEnabled(hasActions && !currentSelected);
......@@ -227,33 +228,47 @@ void BranchDialog::remove()
if (branchName.isEmpty())
return;
QString message = tr("Would you like to delete the branch '%1'?").arg(branchName);
bool wasMerged = m_model->branchIsMerged(selected);
if (!wasMerged)
message = tr("Would you like to delete the <b>unmerged</b> branch '%1'?").arg(branchName);
if (QMessageBox::question(this, tr("Delete Branch"), message, QMessageBox::Yes|QMessageBox::No,
wasMerged ? QMessageBox::Yes : QMessageBox::No) == QMessageBox::Yes)
m_model->removeBranch(selected);
const bool isTag = m_model->isTag(selected);
const bool wasMerged = isTag ? true : m_model->branchIsMerged(selected);
QString message = tr("Would you like to delete the %1 '%2'?");
if (isTag)
message = message.arg(tr("tag"));
else
message = message.arg(wasMerged ? tr("branch") : tr("<b>unmerged</b> branch"));
message = message.arg(branchName);
if (QMessageBox::question(this, isTag ? tr("Delete Tag") : tr("Delete Branch"),
message, QMessageBox::Yes | QMessageBox::No,
wasMerged ? QMessageBox::Yes : QMessageBox::No) == QMessageBox::Yes) {
if (isTag)
m_model->removeTag(selected);
else
m_model->removeBranch(selected);
}
}
void BranchDialog::rename()
{
QModelIndex selected = selectedIndex();
QTC_CHECK(selected != m_model->currentBranch()); // otherwise the button would not be enabled!
QTC_CHECK(m_model->isLocal(selected)); // otherwise the button would not be enabled!
const bool isTag = m_model->isTag(selected);
QTC_CHECK(m_model->isLocal(selected) || isTag);
QString oldBranchName = m_model->branchName(selected);
QStringList localNames = m_model->localBranchNames();
QString oldName = m_model->branchName(selected);
QStringList localNames;
if (!isTag)
localNames = m_model->localBranchNames();
BranchAddDialog branchAddDialog(false, this);
branchAddDialog.setBranchName(oldBranchName);
if (isTag)
branchAddDialog.setWindowTitle(tr("Rename Tag"));
branchAddDialog.setBranchName(oldName);
branchAddDialog.setTrackedBranchName(QString(), false);
branchAddDialog.exec();
if (branchAddDialog.result() == QDialog::Accepted && m_model) {
if (branchAddDialog.branchName() == oldBranchName)
if (branchAddDialog.branchName() == oldName)
return;
if (localNames.contains(branchAddDialog.branchName())) {
QMessageBox::critical(this, tr("Branch Exists"),
......@@ -261,7 +276,10 @@ void BranchDialog::rename()
.arg(branchAddDialog.branchName()));
return;
}
m_model->renameBranch(oldBranchName, branchAddDialog.branchName());
if (isTag)
m_model->renameTag(oldName, branchAddDialog.branchName());
else
m_model->renameBranch(oldName, branchAddDialog.branchName());
refresh();
}
enableButtons();
......
......@@ -387,6 +387,21 @@ void BranchModel::renameBranch(const QString &oldName, const QString &newName)
refresh(m_workingDirectory, &errorMessage);
}
void BranchModel::renameTag(const QString &oldName, const QString &newName)
{
QString errorMessage;
QString output;
if (!m_client->synchronousTagCmd(m_workingDirectory, QStringList() << newName << oldName,
&output, &errorMessage)
|| !m_client->synchronousTagCmd(m_workingDirectory,
QStringList() << QLatin1String("-d") << oldName,
&output, &errorMessage)) {
VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
} else {
refresh(m_workingDirectory, &errorMessage);
}
}
QString BranchModel::workingDirectory() const
{
return m_workingDirectory;
......@@ -412,6 +427,8 @@ QString BranchModel::branchName(const QModelIndex &idx) const
if (!node || !node->isLeaf())
return QString();
QStringList path = node->fullName();
if (node->isTag())
path.removeFirst();
return path.join(QString(QLatin1Char('/')));
}
......@@ -469,16 +486,25 @@ void BranchModel::removeBranch(const QModelIndex &idx)
VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
return;
}
removeNode(idx);
}
QModelIndex tmp = idx; // tmp is a leaf, so count must be 0.
while (indexToNode(tmp)->count() == 0) {
QModelIndex tmpParent = parent(tmp);
beginRemoveRows(tmpParent, tmp.row(), tmp.row());
indexToNode(tmpParent)->children.removeAt(tmp.row());
delete indexToNode(tmp);
endRemoveRows();
tmp = tmpParent;
void BranchModel::removeTag(const QModelIndex &idx)
{
QString tag = branchName(idx);
if (tag.isEmpty())
return;
QString errorMessage;
QString output;
QStringList args;
args << QLatin1String("-d") << tag;
if (!m_client->synchronousTagCmd(m_workingDirectory, args, &output, &errorMessage)) {
VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
return;
}
removeNode(idx);
}
void BranchModel::checkoutBranch(const QModelIndex &idx)
......@@ -647,6 +673,19 @@ QModelIndex BranchModel::nodeToIndex(BranchNode *node) const
return createIndex(node->parent->rowOf(node), 0, static_cast<void *>(node));
}
void BranchModel::removeNode(const QModelIndex &idx)
{
QModelIndex tmp = idx; // tmp is a leaf, so count must be 0.
while (indexToNode(tmp)->count() == 0) {
QModelIndex tmpParent = parent(tmp);
beginRemoveRows(tmpParent, tmp.row(), tmp.row());
indexToNode(tmpParent)->children.removeAt(tmp.row());
delete indexToNode(tmp);
endRemoveRows();
tmp = tmpParent;
}
}
QString BranchModel::toolTip(const QString &sha) const
{
// Show the sha description excluding diff as toolTip
......
......@@ -64,6 +64,7 @@ public:
bool refresh(const QString &workingDirectory, QString *errorMessage);
void renameBranch(const QString &oldName, const QString &newName);
void renameTag(const QString &oldName, const QString &newName);
QString workingDirectory() const;
GitClient *client() const;
......@@ -77,6 +78,7 @@ public:
bool isTag(const QModelIndex &idx) const;
void removeBranch(const QModelIndex &idx);
void removeTag(const QModelIndex &idx);
void checkoutBranch(const QModelIndex &idx);
bool branchIsMerged(const QModelIndex &idx);
QModelIndex addBranch(const QString &name, bool track, const QModelIndex &trackedBranch);
......@@ -86,6 +88,7 @@ private:
void setCurrentBranch();
BranchNode *indexToNode(const QModelIndex &index) const;
QModelIndex nodeToIndex(BranchNode *node) const;
void removeNode(const QModelIndex &idx);
QString toolTip(const QString &sha) const;
......
......@@ -1936,6 +1936,21 @@ bool GitClient::synchronousBranchCmd(const QString &workingDirectory, QStringLis
return true;
}
bool GitClient::synchronousTagCmd(const QString &workingDirectory, QStringList tagArgs, QString *output, QString *errorMessage)
{
tagArgs.push_front(QLatin1String("tag"));
QByteArray outputText;
QByteArray errorText;
const bool rc = fullySynchronousGit(workingDirectory, tagArgs, &outputText, &errorText);
*output = commandOutputFromLocal8Bit(outputText);
if (!rc) {
*errorMessage = msgCannotRun(QLatin1String("git tag"), workingDirectory,
commandOutputFromLocal8Bit(errorText));
return false;
}
return true;
}
bool GitClient::synchronousForEachRefCmd(const QString &workingDirectory, QStringList args,
QString *output, QString *errorMessage)
{
......
......@@ -200,6 +200,8 @@ public:
QString *errorMessage = 0);
bool synchronousBranchCmd(const QString &workingDirectory, QStringList branchArgs,
QString *output, QString *errorMessage);
bool synchronousTagCmd(const QString &workingDirectory, QStringList tagArgs,
QString *output, QString *errorMessage);
bool synchronousForEachRefCmd(const QString &workingDirectory, QStringList args,
QString *output, QString *errorMessage);
bool synchronousRemoteCmd(const QString &workingDirectory, QStringList remoteArgs,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment