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

This should ease the pain of upgrading from v2.x to v3.x.
This commit is contained in:
Dustin J. Mitchell 2024-12-16 20:24:45 -05:00 committed by GitHub
parent 758ac8f850
commit cc505e4881
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 525 additions and 1 deletions

View file

@ -35,6 +35,7 @@ set (commands_SRCS Command.cpp Command.h
CmdHistory.cpp CmdHistory.h
CmdIDs.cpp CmdIDs.h
CmdImport.cpp CmdImport.h
CmdImportV2.cpp CmdImportV2.h
CmdInfo.cpp CmdInfo.h
CmdLog.cpp CmdLog.h
CmdLogo.cpp CmdLogo.h

View file

@ -241,7 +241,8 @@ int CmdCustom::execute(std::string& output) {
Color warning = Color(Context::getContext().config.get("color.warning"));
std::cerr << warning.colorize(format("Found existing '*.data' files in {1}", location)) << "\n";
std::cerr << " Taskwarrior's storage format changed in 3.0, requiring a manual migration.\n";
std::cerr << " See https://taskwarrior.org/docs/upgrade-3/\n";
std::cerr << " See https://taskwarrior.org/docs/upgrade-3/. Run `task import-v2` to import\n";
std::cerr << " the tasks into the Taskwarrior-3.x format\n";
}
feedback_backlog();

View file

@ -0,0 +1,135 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
// cmake.h include header must come first
#include <CmdImportV2.h>
#include <CmdModify.h>
#include <Context.h>
#include <TF2.h>
#include <format.h>
#include <shared.h>
#include <util.h>
#include <iostream>
#include <unordered_map>
////////////////////////////////////////////////////////////////////////////////
CmdImportV2::CmdImportV2() {
_keyword = "import-v2";
_usage = "task import-v2";
_description = "Imports Taskwarrior v2.x files";
_read_only = false;
_displays_id = false;
_needs_gc = false;
_uses_context = false;
_accepts_filter = false;
_accepts_modifications = false;
_accepts_miscellaneous = true;
_category = Command::Category::migration;
}
////////////////////////////////////////////////////////////////////////////////
int CmdImportV2::execute(std::string&) {
std::vector<std::map<std::string, std::string>> task_data;
std::string location = (Context::getContext().data_dir);
File pending_file = File(location + "/pending.data");
if (pending_file.exists()) {
TF2 pending_tf;
pending_tf.target(pending_file);
auto& pending_tasks = pending_tf.get_tasks();
task_data.insert(task_data.end(), pending_tasks.begin(), pending_tasks.end());
}
File completed_file = File(location + "/completed.data");
if (completed_file.exists()) {
TF2 completed_tf;
completed_tf.target(completed_file);
auto& completed_tasks = completed_tf.get_tasks();
task_data.insert(task_data.end(), completed_tasks.begin(), completed_tasks.end());
}
auto count = import(task_data);
Context::getContext().footnote(
format("Imported {1} tasks from `*.data` files. You may now delete these files.", count));
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int CmdImportV2::import(const std::vector<std::map<std::string, std::string>>& task_data) {
auto count = 0;
const std::string uuid_key = "uuid";
const std::string id_key = "id";
const std::string descr_key = "description";
auto& replica = Context::getContext().tdb2.replica();
rust::Vec<tc::Operation> ops;
tc::add_undo_point(ops);
for (auto& task : task_data) {
auto uuid_iter = task.find(uuid_key);
if (uuid_iter == task.end()) {
std::cout << " err - Task with no UUID\n";
continue;
}
auto uuid_str = uuid_iter->second;
auto uuid = tc::uuid_from_string(uuid_str);
bool added_task = false;
auto maybe_task_data = replica->get_task_data(uuid);
auto task_data = maybe_task_data.is_some() ? maybe_task_data.take() : [&]() {
added_task = true;
return tc::create_task(uuid, ops);
}();
for (auto& attr : task) {
if (attr.first == uuid_key || attr.first == id_key) {
continue;
}
task_data->update(attr.first, attr.second, ops);
}
count++;
if (added_task) {
std::cout << " add ";
} else {
std::cout << " mod ";
}
std::cout << uuid_str << ' ';
if (auto descr_iter = task.find(descr_key); descr_iter != task.end()) {
std::cout << descr_iter->second;
} else {
std::cout << "(no description)";
}
std::cout << "\n";
}
replica->commit_operations(std::move(ops));
return count;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,46 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_CMDIMPORTV2
#define INCLUDED_CMDIMPORTV2
#include <Command.h>
#include <JSON.h>
#include <string>
#include <unordered_map>
class CmdImportV2 : public Command {
public:
CmdImportV2();
int execute(std::string &);
private:
int import(const std::vector<std::map<std::string, std::string>> &task_data);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -66,6 +66,7 @@
#include <CmdHistory.h>
#include <CmdIDs.h>
#include <CmdImport.h>
#include <CmdImportV2.h>
#include <CmdInfo.h>
#include <CmdLog.h>
#include <CmdLogo.h>
@ -188,6 +189,8 @@ void Command::factory(std::map<std::string, Command*>& all) {
all[c->keyword()] = c;
c = new CmdImport();
all[c->keyword()] = c;
c = new CmdImportV2();
all[c->keyword()] = c;
c = new CmdInfo();
all[c->keyword()] = c;
c = new CmdLog();