• 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

94.52
/src/commands/CmdImportV2.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 <CmdImportV2.h>
31
#include <CmdModify.h>
32
#include <Context.h>
33
#include <TF2.h>
34
#include <format.h>
35
#include <shared.h>
36
#include <util.h>
37

38
#include <iostream>
39
#include <unordered_map>
40

41
////////////////////////////////////////////////////////////////////////////////
42
CmdImportV2::CmdImportV2() {
4,499✔
43
  _keyword = "import-v2";
4,499✔
44
  _usage = "task          import-v2";
4,499✔
45
  _description = "Imports Taskwarrior v2.x files";
4,499✔
46
  _read_only = false;
4,499✔
47
  _displays_id = false;
4,499✔
48
  _needs_gc = false;
4,499✔
49
  _uses_context = false;
4,499✔
50
  _accepts_filter = false;
4,499✔
51
  _accepts_modifications = false;
4,499✔
52
  _accepts_miscellaneous = true;
4,499✔
53
  _category = Command::Category::migration;
4,499✔
54
}
4,499✔
55

56
////////////////////////////////////////////////////////////////////////////////
57
int CmdImportV2::execute(std::string&) {
1✔
58
  std::vector<std::map<std::string, std::string>> task_data;
1✔
59

60
  std::string location = (Context::getContext().data_dir);
1✔
61
  File pending_file = File(location + "/pending.data");
1✔
62
  if (pending_file.exists()) {
1✔
63
    TF2 pending_tf;
1✔
64
    pending_tf.target(pending_file);
1✔
65
    auto& pending_tasks = pending_tf.get_tasks();
1✔
66
    task_data.insert(task_data.end(), pending_tasks.begin(), pending_tasks.end());
1✔
67
  }
1✔
68
  File completed_file = File(location + "/completed.data");
1✔
69
  if (completed_file.exists()) {
1✔
70
    TF2 completed_tf;
1✔
71
    completed_tf.target(completed_file);
1✔
72
    auto& completed_tasks = completed_tf.get_tasks();
1✔
73
    task_data.insert(task_data.end(), completed_tasks.begin(), completed_tasks.end());
1✔
74
  }
1✔
75

76
  auto count = import(task_data);
1✔
77

78
  Context::getContext().footnote(
2✔
79
      format("Imported {1} tasks from `*.data` files. You may now delete these files.", count));
2✔
80
  return 0;
1✔
81
}
1✔
82

83
////////////////////////////////////////////////////////////////////////////////
84
int CmdImportV2::import(const std::vector<std::map<std::string, std::string>>& task_data) {
1✔
85
  auto count = 0;
1✔
86
  const std::string uuid_key = "uuid";
2✔
87
  const std::string id_key = "id";
2✔
88
  const std::string descr_key = "description";
1✔
89
  auto& replica = Context::getContext().tdb2.replica();
1✔
90
  rust::Vec<tc::Operation> ops;
1✔
91
  tc::add_undo_point(ops);
1✔
92

93
  for (auto& task : task_data) {
5✔
94
    auto uuid_iter = task.find(uuid_key);
4✔
95
    if (uuid_iter == task.end()) {
4✔
NEW
96
      std::cout << " err  - Task with no UUID\n";
×
NEW
97
      continue;
×
98
    }
99
    auto uuid_str = uuid_iter->second;
4✔
100
    auto uuid = tc::uuid_from_string(uuid_str);
4✔
101

102
    bool added_task = false;
4✔
103
    auto maybe_task_data = replica->get_task_data(uuid);
4✔
104
    auto task_data = maybe_task_data.is_some() ? maybe_task_data.take() : [&]() {
4✔
105
      added_task = true;
4✔
106
      return tc::create_task(uuid, ops);
4✔
107
    }();
4✔
108

109
    for (auto& attr : task) {
27✔
110
      if (attr.first == uuid_key || attr.first == id_key) {
23✔
111
        continue;
4✔
112
      }
113
      task_data->update(attr.first, attr.second, ops);
19✔
114
    }
115
    count++;
4✔
116

117
    if (added_task) {
4✔
118
      std::cout << " add ";
4✔
119
    } else {
NEW
120
      std::cout << " mod ";
×
121
    }
122
    std::cout << uuid_str << ' ';
4✔
123
    if (auto descr_iter = task.find(descr_key); descr_iter != task.end()) {
4✔
124
      std::cout << descr_iter->second;
4✔
125
    } else {
NEW
126
      std::cout << "(no description)";
×
127
    }
128
    std::cout << "\n";
4✔
129
  }
4✔
130

131
  replica->commit_operations(std::move(ops));
1✔
132
  return count;
1✔
133
}
1✔
134

135
////////////////////////////////////////////////////////////////////////////////
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