annotate src/filestates.cpp @ 737:4f3a8aa8d384 tip

Markdown
author Chris Cannam
date Wed, 28 Aug 2019 17:40:54 +0100
parents ae67ea0af696
children
rev   line source
Chris@109 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@109 2
Chris@109 3 /*
Chris@109 4 EasyMercurial
Chris@109 5
Chris@109 6 Based on HgExplorer by Jari Korhonen
Chris@109 7 Copyright (c) 2010 Jari Korhonen
Chris@644 8 Copyright (c) 2013 Chris Cannam
Chris@644 9 Copyright (c) 2013 Queen Mary, University of London
Chris@109 10
Chris@109 11 This program is free software; you can redistribute it and/or
Chris@109 12 modify it under the terms of the GNU General Public License as
Chris@109 13 published by the Free Software Foundation; either version 2 of the
Chris@109 14 License, or (at your option) any later version. See the file
Chris@109 15 COPYING included with this distribution for more information.
Chris@109 16 */
Chris@109 17
Chris@92 18 #include "filestates.h"
Chris@74 19
Chris@86 20 #include "debug.h"
Chris@86 21
Chris@74 22 #include <QMap>
Chris@74 23
Chris@94 24 FileStates::FileStates()
Chris@94 25 {
Chris@94 26 }
Chris@94 27
Chris@94 28 void FileStates::clearBuckets()
Chris@94 29 {
Chris@199 30 m_clean.clear();
Chris@94 31 m_modified.clear();
Chris@94 32 m_added.clear();
Chris@94 33 m_removed.clear();
Chris@94 34 m_missing.clear();
Chris@159 35 m_inConflict.clear();
Chris@94 36 m_unknown.clear();
Chris@199 37 m_ignored.clear();
Chris@94 38 }
Chris@94 39
Chris@94 40 FileStates::State FileStates::charToState(QChar c, bool *ok)
Chris@94 41 {
Chris@159 42 // Note that InConflict does not correspond to a stat char -- it's
Chris@163 43 // reported separately, by resolve --list, which shows U for
Chris@163 44 // Unresolved -- stat reports files in conflict as M, which means
Chris@163 45 // they will appear in more than one bin if we handle them
Chris@163 46 // naively. 'u' is also used by stat as the command option for
Chris@163 47 // Unknown, but the stat output uses ? for these so there's no
Chris@163 48 // ambiguity in parsing.
Chris@159 49
Chris@94 50 if (ok) *ok = true;
Chris@94 51 if (c == 'M') return Modified;
Chris@94 52 if (c == 'A') return Added;
Chris@94 53 if (c == 'R') return Removed;
Chris@94 54 if (c == '!') return Missing;
Chris@163 55 if (c == 'U') return InConflict;
Chris@94 56 if (c == '?') return Unknown;
Chris@94 57 if (c == 'C') return Clean;
Chris@199 58 if (c == 'I') return Ignored;
Chris@94 59 if (ok) *ok = false;
Chris@94 60 return Unknown;
Chris@94 61 }
Chris@94 62
Chris@94 63 QStringList *FileStates::stateToBucket(State s)
Chris@94 64 {
Chris@94 65 switch (s) {
Chris@324 66 case Clean: return &m_clean;
Chris@94 67 case Modified: return &m_modified;
Chris@94 68 case Added: return &m_added;
Chris@94 69 case Unknown: return &m_unknown;
Chris@94 70 case Removed: return &m_removed;
Chris@94 71 case Missing: return &m_missing;
Chris@159 72 case InConflict: return &m_inConflict;
Chris@199 73 case Ignored: return &m_ignored;
Chris@205 74
Chris@205 75 default: return &m_clean;
Chris@94 76 }
Chris@94 77 }
Chris@94 78
Chris@94 79 void FileStates::parseStates(QString text)
Chris@74 80 {
Chris@74 81 text.replace("\r\n", "\n");
Chris@74 82
Chris@94 83 clearBuckets();
Chris@163 84 m_stateMap.clear();
Chris@74 85
Chris@74 86 QStringList lines = text.split("\n", QString::SkipEmptyParts);
Chris@94 87
Chris@74 88 foreach (QString line, lines) {
Chris@94 89
Chris@86 90 if (line.length() < 3 || line[1] != ' ') {
Chris@86 91 continue;
Chris@86 92 }
Chris@94 93
Chris@94 94 QChar c = line[0];
Chris@94 95 bool ok = false;
Chris@94 96 State s = charToState(c, &ok);
Chris@94 97 if (!ok) continue;
Chris@94 98
Chris@74 99 QString file = line.right(line.length() - 2);
Chris@163 100 m_stateMap[file] = s;
Chris@163 101 }
Chris@163 102
Chris@163 103 foreach (QString file, m_stateMap.keys()) {
Chris@163 104 QStringList *bucket = stateToBucket(m_stateMap[file]);
Chris@199 105 if (bucket) bucket->push_back(file);
Chris@74 106 }
Chris@86 107
Chris@199 108 DEBUG << "FileStates: "
Chris@199 109 << m_modified.size() << " modified, " << m_added.size()
Chris@159 110 << " added, " << m_removed.size() << " removed, " << m_missing.size()
Chris@159 111 << " missing, " << m_inConflict.size() << " in conflict, "
Chris@159 112 << m_unknown.size() << " unknown" << endl;
Chris@74 113 }
Chris@92 114
Chris@324 115 QStringList FileStates::filesInState(State s) const
Chris@92 116 {
Chris@94 117 QStringList *sl = const_cast<FileStates *>(this)->stateToBucket(s);
Chris@94 118 if (sl) return *sl;
Chris@94 119 else return QStringList();
Chris@92 120 }
Chris@92 121
Chris@324 122 bool FileStates::isInState(QString file, State s) const
Chris@324 123 {
Chris@324 124 return filesInState(s).contains(file);
Chris@324 125 }
Chris@324 126
Chris@324 127 FileStates::State FileStates::stateOf(QString file) const
Chris@92 128 {
Chris@94 129 if (m_stateMap.contains(file)) {
Chris@94 130 return m_stateMap[file];
Chris@92 131 }
Chris@92 132 DEBUG << "FileStates: WARNING: getStateOfFile: file "
Chris@92 133 << file << " is unknown to us: returning Unknown state, "
Chris@92 134 << "but unknown to us is not supposed to be the same "
Chris@92 135 << "thing as unknown state..."
Chris@92 136 << endl;
Chris@92 137 return Unknown;
Chris@92 138 }
Chris@324 139
Chris@421 140 bool FileStates::isKnown(QString file) const
Chris@421 141 {
Chris@421 142 return (m_stateMap.contains(file));
Chris@421 143 }
Chris@421 144
Chris@541 145 QStringList FileStates::trackedFiles() const
Chris@541 146 {
Chris@541 147 QStringList all;
Chris@541 148 all << filesInState(Modified);
Chris@541 149 all << filesInState(Added);
Chris@541 150 all << filesInState(Removed);
Chris@541 151 all << filesInState(InConflict);
Chris@541 152 all << filesInState(Missing);
Chris@541 153 all << filesInState(Clean);
Chris@541 154 return all;
Chris@541 155 }
Chris@541 156
Chris@324 157 FileStates::Activities FileStates::activitiesSupportedBy(State s)
Chris@324 158 {
Chris@324 159 Activities a;
Chris@324 160
Chris@324 161 switch (s) {
Chris@324 162
Chris@324 163 case Modified:
sam@624 164 a << Annotate << Diff << Commit << Revert << Rename << Copy << Remove << ShowIn;
Chris@324 165 break;
Chris@324 166
Chris@324 167 case Added:
sam@624 168 a << Commit << Revert << Rename << Copy << Remove << ShowIn;
Chris@324 169 break;
Chris@324 170
Chris@324 171 case Removed:
Chris@324 172 a << Commit << Revert << Add;
Chris@324 173 break;
Chris@324 174
Chris@324 175 case InConflict:
sam@624 176 a << Annotate << Diff << RedoMerge << MarkResolved << Revert << ShowIn;
Chris@324 177 break;
Chris@324 178
Chris@324 179 case Missing:
Chris@324 180 a << Revert << Remove;
Chris@324 181 break;
Chris@324 182
Chris@324 183 case Unknown:
sam@625 184 a << Add << Ignore << ShowIn;
Chris@324 185 break;
Chris@324 186
Chris@324 187 case Clean:
sam@624 188 a << Annotate << Rename << Copy << Remove << ShowIn;
Chris@324 189 break;
Chris@324 190
Chris@324 191 case Ignored:
sam@624 192 a << UnIgnore << ShowIn;
Chris@324 193 break;
Chris@324 194 }
Chris@324 195
Chris@324 196 return a;
Chris@324 197 }
Chris@324 198
Chris@324 199 bool FileStates::supportsActivity(State s, Activity a)
Chris@324 200 {
Chris@324 201 return activitiesSupportedBy(s).contains(a);
Chris@324 202 }
Chris@324 203
Chris@325 204 int FileStates::activityGroup(Activity a)
Chris@325 205 {
Chris@325 206 switch (a) {
Chris@325 207 case Annotate: case Diff: return 0;
Chris@325 208 case Commit: case Revert: return 1;
Chris@361 209 case Rename: case Copy: return 2;
Chris@361 210 case Add: case Remove: return 3;
Chris@361 211 case RedoMerge: case MarkResolved: return 4;
Chris@361 212 case Ignore: case UnIgnore: return 5;
Chris@639 213 case ShowIn: return 6;
Chris@325 214 }
Chris@326 215 return 0;
Chris@325 216 }
Chris@325 217
Chris@324 218 bool FileStates::supportsActivity(QString file, Activity a) const
Chris@324 219 {
Chris@324 220 return supportsActivity(stateOf(file), a);
Chris@324 221 }
Chris@324 222
Chris@324 223 QStringList FileStates::filesSupportingActivity(Activity a) const
Chris@324 224 {
Chris@324 225 QStringList f;
Chris@324 226 for (int i = int(FirstState); i <= int(LastState); ++i) {
Chris@324 227 State s = (State)i;
Chris@326 228 if (supportsActivity(s, a)) {
Chris@326 229 f << filesInState(s);
Chris@326 230 }
Chris@324 231 }
Chris@324 232 return f;
Chris@324 233 }
Chris@324 234
Chris@324 235 FileStates::Activities FileStates::activitiesSupportedBy(QString file) const
Chris@324 236 {
Chris@324 237 return activitiesSupportedBy(stateOf(file));
Chris@324 238 }
Chris@324 239