comparison src/filestates.cpp @ 370:b9c153e00e84

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