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

GothenburgBitFactory / taskwarrior / 20752910333

06 Jan 2026 03:23PM UTC coverage: 85.247% (+0.09%) from 85.162%
20752910333

push

github

djmitche
Update to TaskChampion-3.0.1

19617 of 23012 relevant lines covered (85.25%)

23481.55 hits per line

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

97.86
/src/TDB2.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 <cmake.h>
28
// cmake.h include header must come first
29

30
#include <Color.h>
31
#include <Context.h>
32
#include <Datetime.h>
33
#include <TDB2.h>
34
#include <Table.h>
35
#include <format.h>
36
#include <shared.h>
37
#include <stdlib.h>
38
#include <util.h>
39

40
#include <algorithm>
41
#include <unordered_set>
42
#include <vector>
43

44
bool TDB2::debug_mode = false;
45
static void dependency_scan(std::vector<Task>&);
46

47
////////////////////////////////////////////////////////////////////////////////
48
void TDB2::open_replica(const std::string& location, bool create_if_missing, bool read_write) {
4,607✔
49
  _replica = tc::new_replica_on_disk(location, create_if_missing, read_write);
4,608✔
50
}
4,606✔
51

52
////////////////////////////////////////////////////////////////////////////////
53
// Add the new task to the replica.
54
void TDB2::add(Task& task) {
3,007✔
55
  // Ensure the task is consistent, and provide defaults if necessary.
56
  // bool argument to validate() is "applyDefault", to apply default values for
57
  // properties not otherwise given.
58
  task.validate(true);
3,007✔
59

60
  rust::Vec<tc::Operation> ops;
3,007✔
61
  maybe_add_undo_point(ops);
3,007✔
62

63
  auto uuid = task.get("uuid");
3,007✔
64
  changes[uuid] = task;
3,007✔
65
  tc::Uuid tcuuid = tc::uuid_from_string(uuid);
3,007✔
66

67
  // run hooks for this new task
68
  Context::getContext().hooks.onAdd(task);
3,007✔
69

70
  auto taskdata = tc::create_task(tcuuid, ops);
3,000✔
71

72
  // add the task attributes
73
  for (auto& attr : task.all()) {
19,933✔
74
    // TaskChampion does not store uuid or id in the task data
75
    if (attr == "uuid" || attr == "id") {
16,933✔
76
      continue;
3,000✔
77
    }
78

79
    taskdata->update(attr, task.get(attr), ops);
13,933✔
80
  }
3,000✔
81
  replica()->commit_operations(std::move(ops));
3,000✔
82

83
  invalidate_cached_info();
3,000✔
84

85
  // get the ID that was assigned to this task
86
  auto id = working_set()->by_uuid(tcuuid);
3,000✔
87
  if (id > 0) {
3,000✔
88
    task.id = id;
2,880✔
89
  }
90
}
3,014✔
91

92
////////////////////////////////////////////////////////////////////////////////
93
// Modify the task in storage to match the given task.
94
//
95
// Note that there are a few race conditions to consider here.  Taskwarrior
96
// loads the enitre task into memory and this method then essentially writes
97
// the entire task back to the database. So, if the task in the database
98
// changes between loading the task and this method being called, this method
99
// will "revert" those changes. In practice this would only occur when multiple
100
// `task` invocatoins run at the same time and try to modify the same task.
101
//
102
// There is also the possibility that another task process has deleted the task
103
// from the database between the time this process loaded the tsak and called
104
// this method. In this case, this method throws an error that will make sense
105
// to the user. This is especially unlikely since tasks are only deleted when
106
// they have been unmodified for a long time.
107
void TDB2::modify(Task& task) {
591✔
108
  // All locally modified tasks are timestamped, implicitly overwriting any
109
  // changes the user or hooks tried to apply to the "modified" attribute.
110
  task.setAsNow("modified");
591✔
111
  task.validate(false);
591✔
112
  auto uuid = task.get("uuid");
591✔
113

114
  rust::Vec<tc::Operation> ops;
591✔
115
  maybe_add_undo_point(ops);
591✔
116

117
  changes[uuid] = task;
591✔
118

119
  // invoke the hook and allow it to modify the task before updating
120
  Task original;
591✔
121
  get(uuid, original);
591✔
122
  Context::getContext().hooks.onModify(original, task);
591✔
123

124
  tc::Uuid tcuuid = tc::uuid_from_string(uuid);
585✔
125
  auto maybe_tctask = replica()->get_task_data(tcuuid);
585✔
126
  if (maybe_tctask.is_none()) {
585✔
127
    throw std::string("task no longer exists");
×
128
  }
129
  auto tctask = maybe_tctask.take();
585✔
130

131
  // Perform the necessary `update` operations to set all keys in `tctask`
132
  // equal to those in `task`.
133
  std::unordered_set<std::string> seen;
585✔
134
  for (auto k : task.all()) {
5,064✔
135
    // ignore task keys that aren't stored
136
    if (k == "uuid") {
4,479✔
137
      continue;
585✔
138
    }
139
    seen.insert(k);
3,894✔
140
    bool update = false;
3,894✔
141
    auto v_new = task.get(k);
3,894✔
142
    std::string v_tctask;
3,894✔
143
    if (tctask->get(k, v_tctask)) {
3,894✔
144
      update = v_tctask != v_new;
3,317✔
145
    } else {
146
      // tctask does not contain k, so update it
147
      update = true;
577✔
148
    }
149
    if (update) {
3,894✔
150
      // An empty string indicates the value should be removed.
151
      if (v_new == "") {
1,018✔
152
        tctask->update_remove(k, ops);
4✔
153
      } else {
154
        tctask->update(k, v_new, ops);
1,014✔
155
      }
156
    }
157
  }
5,064✔
158

159
  // we've now added and updated properties; but must find any deleted properties
160
  for (auto k : tctask->properties()) {
4,538✔
161
    auto kstr = static_cast<std::string>(k);
3,953✔
162
    if (seen.find(kstr) == seen.end()) {
3,953✔
163
      tctask->update_remove(kstr, ops);
63✔
164
    }
165
  }
4,538✔
166

167
  replica()->commit_operations(std::move(ops));
585✔
168

169
  invalidate_cached_info();
585✔
170
}
603✔
171

172
////////////////////////////////////////////////////////////////////////////////
173
void TDB2::purge(Task& task) {
6✔
174
  auto uuid = tc::uuid_from_string(task.get("uuid"));
6✔
175
  rust::Vec<tc::Operation> ops;
6✔
176
  auto maybe_tctask = replica()->get_task_data(uuid);
6✔
177
  if (maybe_tctask.is_some()) {
6✔
178
    auto tctask = maybe_tctask.take();
6✔
179
    tctask->delete_task(ops);
6✔
180
    replica()->commit_operations(std::move(ops));
6✔
181
  }
6✔
182

183
  invalidate_cached_info();
6✔
184
}
6✔
185

186
////////////////////////////////////////////////////////////////////////////////
187
rust::Box<tc::Replica>& TDB2::replica() {
16,747✔
188
  // One of the open_replica_ methods must be called before this one.
189
  assert(_replica);
16,747✔
190
  return _replica.value();
16,747✔
191
}
192

193
////////////////////////////////////////////////////////////////////////////////
194
const rust::Box<tc::WorkingSet>& TDB2::working_set() {
750,805✔
195
  if (!_working_set.has_value()) {
750,805✔
196
    _working_set = replica()->working_set();
5,138✔
197
  }
198
  return _working_set.value();
750,805✔
199
}
200

201
////////////////////////////////////////////////////////////////////////////////
202
void TDB2::maybe_add_undo_point(rust::Vec<tc::Operation>& ops) {
3,598✔
203
  // Only add an UndoPoint if there are not yet any changes.
204
  if (changes.size() == 0) {
3,598✔
205
    tc::add_undo_point(ops);
1,806✔
206
  }
207
}
3,598✔
208

209
////////////////////////////////////////////////////////////////////////////////
210
void TDB2::get_changes(std::vector<Task>& changes) {
5✔
211
  std::map<std::string, Task>& changes_map = this->changes;
5✔
212
  changes.clear();
5✔
213
  std::transform(changes_map.begin(), changes_map.end(), std::back_inserter(changes),
5✔
214
                 [](const auto& kv) { return kv.second; });
1✔
215
}
5✔
216

217
////////////////////////////////////////////////////////////////////////////////
218
void TDB2::gc() {
931✔
219
  Timer timer;
931✔
220

221
  // Allowed as an override, but not recommended.
222
  if (Context::getContext().config.getBoolean("gc")) {
2,793✔
223
    replica()->rebuild_working_set(true);
926✔
224
  }
225

226
  Context::getContext().time_gc_us += timer.total_us();
931✔
227
}
931✔
228

229
////////////////////////////////////////////////////////////////////////////////
230
void TDB2::expire_tasks() { replica()->expire_tasks(); }
1✔
231

232
////////////////////////////////////////////////////////////////////////////////
233
// Latest ID is that of the last pending task.
234
int TDB2::latest_id() {
180✔
235
  auto& ws = working_set();
180✔
236
  return (int)ws->largest_index();
180✔
237
}
238

239
////////////////////////////////////////////////////////////////////////////////
240
const std::vector<Task> TDB2::all_tasks() {
443✔
241
  Timer timer;
443✔
242
  auto all_tctasks = replica()->all_task_data();
443✔
243
  std::vector<Task> all;
443✔
244
  for (auto& maybe_tctask : all_tctasks) {
1,639✔
245
    auto tctask = maybe_tctask.take();
1,196✔
246
    all.push_back(Task(std::move(tctask)));
1,196✔
247
  }
1,196✔
248

249
  dependency_scan(all);
443✔
250

251
  Context::getContext().time_load_us += timer.total_us();
443✔
252
  return all;
886✔
253
}
443✔
254

255
////////////////////////////////////////////////////////////////////////////////
256
const std::vector<Task> TDB2::pending_tasks() {
6,399✔
257
  if (!_pending_tasks) {
6,399✔
258
    Timer timer;
3,835✔
259

260
    auto pending_tctasks = replica()->pending_task_data();
3,835✔
261
    std::vector<Task> result;
3,835✔
262
    for (auto& maybe_tctask : pending_tctasks) {
749,218✔
263
      auto tctask = maybe_tctask.take();
745,383✔
264
      result.push_back(Task(std::move(tctask)));
745,383✔
265
    }
745,383✔
266

267
    dependency_scan(result);
3,835✔
268

269
    Context::getContext().time_load_us += timer.total_us();
3,835✔
270
    _pending_tasks = result;
3,835✔
271
  }
3,835✔
272

273
  return *_pending_tasks;
6,399✔
274
}
275

276
////////////////////////////////////////////////////////////////////////////////
277
const std::vector<Task> TDB2::completed_tasks() {
321✔
278
  if (!_completed_tasks) {
321✔
279
    auto all_tctasks = replica()->all_task_data();
321✔
280
    auto& ws = working_set();
321✔
281

282
    std::vector<Task> result;
321✔
283
    for (auto& maybe_tctask : all_tctasks) {
2,211✔
284
      auto tctask = maybe_tctask.take();
1,890✔
285
      // if this task is _not_ in the working set, return it.
286
      if (ws->by_uuid(tctask->get_uuid()) == 0) {
1,890✔
287
        result.push_back(Task(std::move(tctask)));
238✔
288
      }
289
    }
1,890✔
290
    _completed_tasks = result;
321✔
291
  }
321✔
292
  return *_completed_tasks;
321✔
293
}
294

295
////////////////////////////////////////////////////////////////////////////////
296
void TDB2::invalidate_cached_info() {
3,591✔
297
  _pending_tasks = std::nullopt;
3,591✔
298
  _completed_tasks = std::nullopt;
3,591✔
299
  _working_set = std::nullopt;
3,591✔
300
}
3,591✔
301

302
////////////////////////////////////////////////////////////////////////////////
303
// Locate task by ID, wherever it is.
304
bool TDB2::get(int id, Task& task) {
315✔
305
  auto& ws = working_set();
315✔
306
  const auto tcuuid = ws->by_index(id);
315✔
307
  if (!tcuuid.is_nil()) {
315✔
308
    std::string uuid = static_cast<std::string>(tcuuid.to_string());
303✔
309
    // Load all pending tasks in order to get dependency data, and in particular
310
    // `task.is_blocking` and `task.is_blocked`, set correctly.
311
    std::vector<Task> pending = pending_tasks();
303✔
312
    for (auto& pending_task : pending) {
1,548✔
313
      if (pending_task.get("uuid") == uuid) {
3,096✔
314
        task = pending_task;
303✔
315
        return true;
303✔
316
      }
317
    }
318
  }
606✔
319

320
  return false;
12✔
321
}
322

323
////////////////////////////////////////////////////////////////////////////////
324
// Locate task by UUID, including by partial ID, wherever it is.
325
bool TDB2::get(const std::string& uuid, Task& task) {
2,301✔
326
  // Load all pending tasks in order to get dependency data, and in particular
327
  // `task.is_blocking` and `task.is_blocked`, set correctly.
328
  std::vector<Task> pending = pending_tasks();
2,301✔
329

330
  // try by raw uuid, if the length is right
331
  for (auto& pending_task : pending) {
735,119✔
332
    if (closeEnough(pending_task.get("uuid"), uuid, uuid.length())) {
2,200,503✔
333
      task = pending_task;
683✔
334
      return true;
683✔
335
    }
336
  }
337

338
  // Nothing to do but iterate over all tasks and check whether it's closeEnough.
339
  for (auto& maybe_tctask : replica()->all_task_data()) {
733,504✔
340
    auto tctask = maybe_tctask.take();
731,914✔
341
    auto tctask_uuid = static_cast<std::string>(tctask->get_uuid().to_string());
731,914✔
342
    if (closeEnough(tctask_uuid, uuid, uuid.length())) {
731,914✔
343
      task = Task{std::move(tctask)};
28✔
344
      return true;
28✔
345
    }
346
  }
733,560✔
347

348
  return false;
1,590✔
349
}
2,301✔
350

351
////////////////////////////////////////////////////////////////////////////////
352
// Locate task by UUID, wherever it is.
353
bool TDB2::has(const std::string& uuid) {
×
354
  return replica()->get_task_data(tc::uuid_from_string(uuid)).is_some();
×
355
}
356

357
////////////////////////////////////////////////////////////////////////////////
358
const std::vector<Task> TDB2::siblings(Task& task) {
8✔
359
  std::vector<Task> results;
8✔
360
  if (task.has("parent")) {
16✔
361
    std::string parent = task.get("parent");
8✔
362

363
    for (auto& i : this->pending_tasks()) {
35✔
364
      // Do not include self in results.
365
      if (i.id != task.id) {
27✔
366
        // Do not include completed or deleted tasks.
367
        if (i.getStatus() != Task::completed && i.getStatus() != Task::deleted) {
19✔
368
          // If task has the same parent, it is a sibling.
369
          if (i.has("parent") && i.get("parent") == parent) {
77✔
370
            results.push_back(i);
10✔
371
          }
372
        }
373
      }
374
    }
8✔
375
  }
8✔
376

377
  return results;
8✔
378
}
×
379

380
////////////////////////////////////////////////////////////////////////////////
381
const std::vector<Task> TDB2::children(Task& parent) {
71✔
382
  // scan _pending_ tasks for those with `parent` equal to this task
383
  std::vector<Task> results;
71✔
384
  std::string this_uuid = parent.get("uuid");
71✔
385

386
  auto& ws = working_set();
71✔
387
  size_t end_idx = ws->largest_index();
71✔
388

389
  for (size_t i = 0; i <= end_idx; i++) {
387✔
390
    auto uuid = ws->by_index(i);
316✔
391
    if (uuid.is_nil()) {
316✔
392
      continue;
302✔
393
    }
394

395
    // skip self-references
396
    if (uuid.to_string() == this_uuid) {
245✔
397
      continue;
70✔
398
    }
399

400
    auto task_opt = replica()->get_task_data(uuid);
175✔
401
    if (task_opt.is_none()) {
175✔
402
      continue;
×
403
    }
404
    auto task = task_opt.take();
175✔
405

406
    std::string parent_uuid;
175✔
407
    if (!task->get("parent", parent_uuid)) {
525✔
408
      continue;
161✔
409
    }
410

411
    if (parent_uuid == this_uuid) {
14✔
412
      results.push_back(Task(std::move(task)));
14✔
413
    }
414
  }
336✔
415
  return results;
142✔
416
}
71✔
417

418
////////////////////////////////////////////////////////////////////////////////
419
std::string TDB2::uuid(int id) {
59✔
420
  auto& ws = working_set();
59✔
421
  auto uuid = ws->by_index(id);
59✔
422
  if (uuid.is_nil()) {
59✔
423
    return "";
2✔
424
  }
425
  return static_cast<std::string>(uuid.to_string());
58✔
426
}
427

428
////////////////////////////////////////////////////////////////////////////////
429
int TDB2::id(const std::string& uuid) {
746,859✔
430
  auto& ws = working_set();
746,859✔
431
  return ws->by_uuid(tc::uuid_from_string(uuid));
746,859✔
432
}
433

434
////////////////////////////////////////////////////////////////////////////////
435
int TDB2::num_local_changes() { return (int)replica()->num_local_operations(); }
5✔
436

437
////////////////////////////////////////////////////////////////////////////////
438
int TDB2::num_reverts_possible() { return (int)replica()->num_undo_points(); }
3✔
439

440
////////////////////////////////////////////////////////////////////////////////
441
// For any task that has depenencies, follow the chain of dependencies until the
442
// end.  Along the way, update the Task::is_blocked and Task::is_blocking data
443
// cache.
444
static void dependency_scan(std::vector<Task>& tasks) {
4,278✔
445
  for (auto& left : tasks) {
750,857✔
446
    for (auto& dep : left.getDependencyUUIDs()) {
746,937✔
447
      for (auto& right : tasks) {
1,514✔
448
        if (right.get("uuid") == dep) {
3,016✔
449
          // GC hasn't run yet, check both tasks for their current status
450
          Task::status lstatus = left.getStatus();
352✔
451
          Task::status rstatus = right.getStatus();
352✔
452
          if (lstatus != Task::completed && lstatus != Task::deleted &&
352✔
453
              rstatus != Task::completed && rstatus != Task::deleted) {
329✔
454
            left.is_blocked = true;
321✔
455
            right.is_blocking = true;
321✔
456
          }
457

458
          // Only want to break out of the "right" loop.
459
          break;
352✔
460
        }
461
      }
462
    }
746,579✔
463
  }
464
}
4,278✔
465

466
////////////////////////////////////////////////////////////////////////////////
467
// 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

© 2026 Coveralls, Inc