• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GothenburgBitFactory / taskwarrior / 12364510719

17 Dec 2024 01:24AM UTC coverage: 85.046% (+0.3%) from 84.789%
12364510719

push

github

web-flow
Support importing Taskwarrior v2.x data files (#3724)

This should ease the pain of upgrading from v2.x to v3.x.

126 of 144 new or added lines in 4 files covered. (87.5%)

2 existing lines in 1 file now uncovered.

19456 of 22877 relevant lines covered (85.05%)

23175.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.1
/src/TF2.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included
13
// in all copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
//
23
// https://www.opensource.org/licenses/mit-license.php
24
//
25
////////////////////////////////////////////////////////////////////////////////
26

27
#include <Color.h>
28
#include <Context.h>
29
#include <Datetime.h>
30
#include <TF2.h>
31
#include <Table.h>
32
#include <cmake.h>
33
#include <format.h>
34
#include <main.h>
35
#include <shared.h>
36
#include <signal.h>
37
#include <stdlib.h>
38
#include <util.h>
39

40
#include <algorithm>
41
#include <iostream>
42
#include <list>
43
#include <set>
44
#include <sstream>
45

46
#define STRING_TDB2_REVERTED "Modified task reverted."
47

48
////////////////////////////////////////////////////////////////////////////////
49
TF2::TF2() : _loaded_tasks(false), _loaded_lines(false) {}
2✔
50

51
////////////////////////////////////////////////////////////////////////////////
52
TF2::~TF2() {}
2✔
53

54
////////////////////////////////////////////////////////////////////////////////
55
void TF2::target(const std::string& f) { _file = File(f); }
2✔
56

57
////////////////////////////////////////////////////////////////////////////////
58
const std::vector<std::map<std::string, std::string>>& TF2::get_tasks() {
2✔
59
  if (!_loaded_tasks) load_tasks();
2✔
60

61
  return _tasks;
2✔
62
}
63

64
////////////////////////////////////////////////////////////////////////////////
65
// Attempt an FF4 parse.
66
//
67
// Note that FF1, FF2, FF3, and JSON are no longer supported.
68
//
69
// start --> [ --> Att --> ] --> end
70
//              ^       |
71
//              +-------+
72
//
73
std::map<std::string, std::string> TF2::load_task(const std::string& input) {
4✔
74
  std::map<std::string, std::string> data;
4✔
75

76
  // File format version 4, from 2009-5-16 - now, v1.7.1+
77
  // This is the parse format tried first, because it is most used.
78
  data.clear();
4✔
79

80
  if (input[0] == '[') {
4✔
81
    // Not using Pig to parse here (which would be idiomatic), because we
82
    // don't need to differentiate betwen utf-8 and normal characters.
83
    // Pig's scanning the string can be expensive.
84
    auto ending_bracket = input.find_last_of(']');
4✔
85
    if (ending_bracket != std::string::npos) {
4✔
86
      std::string line = input.substr(1, ending_bracket);
4✔
87

88
      if (line.length() == 0) throw std::string("Empty record in input.");
4✔
89

90
      Pig attLine(line);
4✔
91
      std::string name;
4✔
92
      std::string value;
4✔
93
      while (!attLine.eos()) {
31✔
94
        if (attLine.getUntilAscii(':', name) && attLine.skip(':') &&
50✔
95
            attLine.getQuoted('"', value)) {
23✔
96
#ifdef PRODUCT_TASKWARRIOR
97
          legacyAttributeMap(name);
23✔
98
#endif
99

100
          data[name] = decode(json::decode(value));
23✔
101
        }
102

103
        attLine.skip(' ');
27✔
104
      }
105

106
      std::string remainder;
4✔
107
      attLine.getRemainder(remainder);
4✔
108
      if (remainder.length()) throw std::string("Unrecognized characters at end of line.");
4✔
109
    }
4✔
110
  } else {
NEW
111
    throw std::string("Record not recognized as format 4.");
×
112
  }
113

114
  // for compatibility, include all tags in `tags` as `tag_..` attributes
115
  if (data.find("tags") != data.end()) {
12✔
NEW
116
    for (auto& tag : split(data["tags"], ',')) {
×
NEW
117
      data[Task::tag2Attr(tag)] = "x";
×
NEW
118
    }
×
119
  }
120

121
  // same for `depends` / `dep_..`
122
  if (data.find("depends") != data.end()) {
12✔
NEW
123
    for (auto& dep : split(data["depends"], ',')) {
×
NEW
124
      data[Task::dep2Attr(dep)] = "x";
×
NEW
125
    }
×
126
  }
127

128
  return data;
4✔
NEW
129
}
×
130

131
////////////////////////////////////////////////////////////////////////////////
132
// Decode values after parse.
133
//   [  <- &open;
134
//   ]  <- &close;
135
const std::string TF2::decode(const std::string& value) const {
23✔
136
  if (value.find('&') == std::string::npos) return value;
23✔
137

NEW
138
  auto modified = str_replace(value, "&open;", "[");
×
NEW
139
  return str_replace(modified, "&close;", "]");
×
NEW
140
}
×
141

142
////////////////////////////////////////////////////////////////////////////////
143
void TF2::load_tasks() {
2✔
144
  Timer timer;
2✔
145

146
  if (!_loaded_lines) {
2✔
147
    load_lines();
2✔
148
  }
149

150
  // Reduce unnecessary allocations/copies.
151
  // Calling it on _tasks is the right thing to do even when from_gc is set.
152
  _tasks.reserve(_lines.size());
2✔
153

154
  int line_number = 0;  // Used for error message in catch block.
2✔
155
  try {
156
    for (auto& line : _lines) {
6✔
157
      ++line_number;
4✔
158
      auto task = load_task(line);
4✔
159
      _tasks.push_back(task);
4✔
160
    }
4✔
161

162
    _loaded_tasks = true;
2✔
163
  }
164

NEW
165
  catch (const std::string& e) {
×
NEW
166
    throw e + format(" in {1} at line {2}", _file._data, line_number);
×
NEW
167
  }
×
168

169
  Context::getContext().time_load_us += timer.total_us();
2✔
170
}
2✔
171

172
////////////////////////////////////////////////////////////////////////////////
173
void TF2::load_lines() {
2✔
174
  if (_file.open()) {
2✔
175
    if (Context::getContext().config.getBoolean("locking")) _file.lock();
6✔
176

177
    _file.read(_lines);
2✔
178
    _file.close();
2✔
179
    _loaded_lines = true;
2✔
180
  }
181
}
2✔
182

183
////////////////////////////////////////////////////////////////////////////////
184
// vim: ts=2 et sw=2
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc