comparison src/mainwindow.cpp @ 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 69b2338c06e1
children 06e5c4f3dd7c
comparison
equal deleted inserted replaced
420:50920723dd16 421:653e9694a694
586 action.executable = editor; 586 action.executable = editor;
587 587
588 m_runner->requestAction(action); 588 m_runner->requestAction(action);
589 } 589 }
590 590
591 static QString regexEscape(QString filename)
592 {
593 return filename
594 .replace(".", "\\.")
595 .replace("[", "\\[")
596 .replace("]", "\\]")
597 .replace("(", "\\(")
598 .replace(")", "\\)")
599 .replace("?", "\\?");
600 }
601
591 void MainWindow::hgIgnoreFiles(QStringList files) 602 void MainWindow::hgIgnoreFiles(QStringList files)
592 { 603 {
593 if (!QDir(m_workFolderPath).exists() || files.empty()) return; 604 if (!QDir(m_workFolderPath).exists() || files.empty()) return;
594 605
595 // we should: 606 // we should:
624 if (d != directory) { 635 if (d != directory) {
625 ++dirCount; 636 ++dirCount;
626 directory = d; 637 directory = d;
627 } 638 }
628 } 639 }
629 if (dirCount != 1) directory = ""; 640 if (dirCount != 1 || directory == ".") directory = "";
630 641
631 HgIgnoreDialog::IgnoreType itype = 642 HgIgnoreDialog::IgnoreType itype =
632 HgIgnoreDialog::confirmIgnore 643 HgIgnoreDialog::confirmIgnore
633 (this, files, QStringList::fromSet(suffixes), directory); 644 (this, files, QStringList::fromSet(suffixes), directory);
634 645
635 DEBUG << "hgIgnoreFiles: Ignore type is " << itype << endl; 646 DEBUG << "hgIgnoreFiles: Ignore type is " << itype << endl;
647
648 if (itype == HgIgnoreDialog::IgnoreNothing) return;
636 649
637 // Now, .hgignore can be switched from regex to glob syntax 650 // Now, .hgignore can be switched from regex to glob syntax
638 // part-way through -- and glob is much simpler for us, so we 651 // part-way through -- and glob is much simpler for us, so we
639 // should do that if it's in regex mode at the end of the file. 652 // should do that if it's in regex mode at the end of the file.
640 653
671 684
672 if (!glob) { 685 if (!glob) {
673 out << "syntax: glob" << endl; 686 out << "syntax: glob" << endl;
674 } 687 }
675 688
676 //!!! should we be warning the user which files this will mean 689 QString info = "<qt><h3>" + tr("Ignored files") + "</h3><p>";
677 //!!! they ignore? -- probably 690 info += tr("The following lines have been added to the .hgignore file for this working copy:");
678 691 info += "</p><code>";
692
693 QStringList args;
679 if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenSuffixes) { 694 if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenSuffixes) {
680 foreach (QString s, suffixes) { 695 args = QStringList::fromSet(suffixes);
681 out << "*." << s << endl;
682 }
683 } else if (itype == HgIgnoreDialog::IgnoreGivenFilesOnly) { 696 } else if (itype == HgIgnoreDialog::IgnoreGivenFilesOnly) {
684 // The default for glob seems to be to ignore the file 697 args = files;
685 // anywhere -- anchor the path to / to make it specific to 698 } else if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenNames) {
686 // this file only 699 QSet<QString> names;
687 //!!! check this!
688 //!!! ... no, it doesn't work. does this mean we need regex syntax?
689 foreach (QString f, files) { 700 foreach (QString f, files) {
690 out << "/" + f << endl; 701 names << QFileInfo(f).fileName();
691 } 702 }
692 } else if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenNames) { 703 args = QStringList::fromSet(names);
693 foreach (QString f, files) {
694 out << QFileInfo(f).fileName() << endl;
695 }
696 } else if (itype == HgIgnoreDialog::IgnoreWholeDirectory) { 704 } else if (itype == HgIgnoreDialog::IgnoreWholeDirectory) {
697 out << directory + "/" << endl; 705 args << directory;
706 }
707
708 bool first = true;
709
710 foreach (QString a, args) {
711 QString line;
712 if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenSuffixes) {
713 line = "*." + a;
714 } else if (itype == HgIgnoreDialog::IgnoreGivenFilesOnly) {
715 // Doesn't seem to be possible to do this with a glob,
716 // because the glob is always unanchored and there is no
717 // equivalent of ^ to anchor it
718 line = "re:^" + regexEscape(a);
719 } else if (itype == HgIgnoreDialog::IgnoreAllFilesOfGivenNames) {
720 line = a;
721 } else if (itype == HgIgnoreDialog::IgnoreWholeDirectory) {
722 line = "re:^" + regexEscape(a) + "/";
723 }
724 if (line != "") {
725 out << line << endl;
726 if (!first) info += "<br>";
727 first = false;
728 info += xmlEncode(line);
729 }
698 } 730 }
699 731
700 f.close(); 732 f.close();
701 733
702 734 info += "</code></qt>";
703 //!!! report, and offer to edit .hgignore now 735
704 736 QMessageBox::information(this, tr("Ignored files"),
705 // (tell the user at least what lines have been added to the 737 info);
706 // hgignore file and how to edit it) 738
707
708 // (also, if the hgignore is not tracked, suggest that they add
709 // it)
710
711 hgRefresh(); 739 hgRefresh();
712 } 740 }
713 741
714 void MainWindow::hgUnIgnoreFiles(QStringList files) 742 void MainWindow::hgUnIgnoreFiles(QStringList files)
715 { 743 {
716 //!!! not implemented yet 744 // Not implemented: edit the .hgignore instead
717 DEBUG << "MainWindow::hgUnIgnoreFiles: Not implemented" << endl; 745 hgEditIgnore();
718 } 746 }
719 747
720 QString MainWindow::getDiffBinaryName() 748 QString MainWindow::getDiffBinaryName()
721 { 749 {
722 QSettings settings; 750 QSettings settings;