changeset 421:653e9694a694 ignore

Use regexes for anchored filenames (i.e. "this file only" and directory); report outcome of ignore to user
author Chris Cannam
date Fri, 17 Jun 2011 11:07:30 +0100
parents 50920723dd16
children 2af4b5b0bf83
files .hgignore src/filestates.cpp src/filestates.h src/filestatuswidget.cpp src/mainwindow.cpp
diffstat 5 files changed, 68 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Jun 16 22:21:00 2011 +0100
+++ b/.hgignore	Fri Jun 17 11:07:30 2011 +0100
@@ -21,3 +21,5 @@
 .DS_Store
 *.pdb
 
+re:^EasyMercurial
+re:^kdiff3
--- a/src/filestates.cpp	Thu Jun 16 22:21:00 2011 +0100
+++ b/src/filestates.cpp	Fri Jun 17 11:07:30 2011 +0100
@@ -137,6 +137,11 @@
     return Unknown;
 }
 
+bool FileStates::isKnown(QString file) const
+{
+    return (m_stateMap.contains(file));
+}
+
 FileStates::Activities FileStates::activitiesSupportedBy(State s)
 {
     Activities a;
--- a/src/filestates.h	Thu Jun 16 22:21:00 2011 +0100
+++ b/src/filestates.h	Fri Jun 17 11:07:30 2011 +0100
@@ -50,6 +50,7 @@
     bool isInState(QString file, State s) const;
     QStringList filesInState(State s) const;
     State stateOf(QString file) const;
+    bool isKnown(QString file) const;
 
     enum Activity {
 
--- a/src/filestatuswidget.cpp	Thu Jun 16 22:21:00 2011 +0100
+++ b/src/filestatuswidget.cpp	Fri Jun 17 11:07:30 2011 +0100
@@ -68,7 +68,8 @@
     m_actionLabels[FileStates::RedoMerge] = tr("Redo merge");
     m_actionLabels[FileStates::MarkResolved] = tr("Mark conflict as resolved");
     m_actionLabels[FileStates::Ignore] = tr("Ignore...");
-    m_actionLabels[FileStates::UnIgnore] = tr("Stop ignoring...");
+    // Unignore is too difficult in fact, so we just offer to edit the hgignore
+    m_actionLabels[FileStates::UnIgnore] = tr("Edit .hgignore File");
 
     m_descriptions[FileStates::Clean] = tr("You have not changed these files.");
     m_descriptions[FileStates::Modified] = tr("You have changed these files since you last committed them.");
--- a/src/mainwindow.cpp	Thu Jun 16 22:21:00 2011 +0100
+++ b/src/mainwindow.cpp	Fri Jun 17 11:07:30 2011 +0100
@@ -588,6 +588,17 @@
     m_runner->requestAction(action);
 }
 
+static QString regexEscape(QString filename)
+{
+    return filename
+        .replace(".", "\\.")
+        .replace("[", "\\[")
+        .replace("]", "\\]")
+        .replace("(", "\\(")
+        .replace(")", "\\)")
+        .replace("?", "\\?");
+}
+
 void MainWindow::hgIgnoreFiles(QStringList files)
 {
     if (!QDir(m_workFolderPath).exists() || files.empty()) return;
@@ -626,7 +637,7 @@
             directory = d;
         }
     }
-    if (dirCount != 1) directory = "";
+    if (dirCount != 1 || directory == ".") directory = "";
 
     HgIgnoreDialog::IgnoreType itype =
         HgIgnoreDialog::confirmIgnore
@@ -634,6 +645,8 @@
 
     DEBUG << "hgIgnoreFiles: Ignore type is " << itype << endl;
 
+    if (itype == HgIgnoreDialog::IgnoreNothing) return;
+
     // Now, .hgignore can be switched from regex to glob syntax
     // part-way through -- and glob is much simpler for us, so we
     // should do that if it's in regex mode at the end of the file.
@@ -673,48 +686,63 @@
         out << "syntax: glob" << endl;
     }
 
-    //!!! should we be warning the user which files this will mean
-    //!!! they ignore? -- probably
-
+    QString info = "<qt><h3>" + tr("Ignored files") + "</h3><p>";
+    info += tr("The following lines have been added to the .hgignore file for this working copy:");
+    info += "</p><code>";
+
+    QStringList args;
     if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenSuffixes) {
-        foreach (QString s, suffixes) {
-            out << "*." << s << endl;
+        args = QStringList::fromSet(suffixes);
+    } else if (itype == HgIgnoreDialog::IgnoreGivenFilesOnly) {
+        args = files;
+    } else if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenNames) {
+        QSet<QString> names;
+        foreach (QString f, files) {
+            names << QFileInfo(f).fileName();
         }
-    } else if (itype == HgIgnoreDialog::IgnoreGivenFilesOnly) {
-        // The default for glob seems to be to ignore the file
-        // anywhere -- anchor the path to / to make it specific to
-        // this file only
-        //!!! check this!
-        //!!! ... no, it doesn't work. does this mean we need regex syntax?
-        foreach (QString f, files) {
-            out << "/" + f << endl;
+        args = QStringList::fromSet(names);
+    } else if (itype == HgIgnoreDialog::IgnoreWholeDirectory) {
+        args << directory;
+    }
+
+    bool first = true;
+
+    foreach (QString a, args) {
+        QString line;
+        if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenSuffixes) {
+            line = "*." + a;
+        } else if (itype == HgIgnoreDialog::IgnoreGivenFilesOnly) {
+            // Doesn't seem to be possible to do this with a glob,
+            // because the glob is always unanchored and there is no
+            // equivalent of ^ to anchor it
+            line = "re:^" + regexEscape(a);
+        } else if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenNames) {
+            line = a;
+        } else if (itype == HgIgnoreDialog::IgnoreWholeDirectory) {
+            line = "re:^" + regexEscape(a) + "/";
         }
-    } else if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenNames) {
-        foreach (QString f, files) {
-            out << QFileInfo(f).fileName() << endl;
+        if (line != "") {
+            out << line << endl;
+            if (!first) info += "<br>";
+            first = false;
+            info += xmlEncode(line);
         }
-    } else if (itype == HgIgnoreDialog::IgnoreWholeDirectory) {
-        out << directory + "/" << endl;
     }
 
     f.close();
     
-
-    //!!! report, and offer to edit .hgignore now
-
-    // (tell the user at least what lines have been added to the
-    // hgignore file and how to edit it)
-    
-    // (also, if the hgignore is not tracked, suggest that they add
-    // it)
-    
+    info += "</code></qt>";
+
+    QMessageBox::information(this, tr("Ignored files"),
+                             info);
+
     hgRefresh();
 }
 
 void MainWindow::hgUnIgnoreFiles(QStringList files)
 {
-    //!!! not implemented yet
-    DEBUG << "MainWindow::hgUnIgnoreFiles: Not implemented" << endl;
+    // Not implemented: edit the .hgignore instead
+    hgEditIgnore();
 }
 
 QString MainWindow::getDiffBinaryName()