From 510ce650a6cbd4268f216b5d640c0aedf3e6437c Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 30 May 2011 14:45:39 -0400 Subject: [PATCH] Commands - merge - Migrated handleMerge to CmdMerge. --- src/Cmd.cpp | 5 -- src/Context.cpp | 5 +- src/command.cpp | 56 ------------------- src/commands/CMakeLists.txt | 1 + src/commands/CmdHelp.cpp | 4 -- src/commands/CmdMerge.cpp | 107 ++++++++++++++++++++++++++++++++++++ src/commands/CmdMerge.h | 42 ++++++++++++++ src/commands/Command.cpp | 2 + src/main.h | 1 - 9 files changed, 153 insertions(+), 70 deletions(-) create mode 100644 src/commands/CmdMerge.cpp create mode 100644 src/commands/CmdMerge.h diff --git a/src/Cmd.cpp b/src/Cmd.cpp index b31d4d479..ae47da8e2 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -129,8 +129,6 @@ void Cmd::load () { if (commands.size () == 0) { - commands.push_back ("merge"); - // Now load the custom reports. std::vector all; context.config.all (all); @@ -193,9 +191,6 @@ bool Cmd::isReadOnlyCommand () // Commands that directly modify the data files. bool Cmd::isWriteCommand () { - if (command == "merge") - return true; - return false; } diff --git a/src/Context.cpp b/src/Context.cpp index 7519d842b..c220fc1c0 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -249,10 +249,7 @@ int Context::dispatch (std::string &out) Timer t ("Context::dispatch"); // TODO Chain-of-command pattern dispatch. - if (cmd.command == "merge") { tdb.gc (); - handleMerge (out); } - else if (cmd.command == "" && - sequence.size ()) { rc = handleModify (out); } + if (cmd.command == "" && sequence.size ()) { rc = handleModify (out); } // Commands that display IDs and therefore need TDB::gc first. // ... diff --git a/src/command.cpp b/src/command.cpp index f116519bc..ef814f20e 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -51,62 +51,6 @@ extern Context context; -//////////////////////////////////////////////////////////////////////////////// -void handleMerge (std::string&) -{ - std::string file = trim (context.task.get ("description")); - std::string pushfile = ""; - std::string tmpfile = ""; - - std::string sAutopush = lowerCase (context.config.get ("merge.autopush")); - bool bAutopush = context.config.getBoolean ("merge.autopush"); - - Uri uri (file, "merge"); - uri.parse(); - - if (uri.data.length ()) - { - Directory location (context.config.get ("data.location")); - - // be sure that uri points to a file - uri.append ("undo.data"); - - Transport* transport; - if ((transport = Transport::getTransport (uri)) != NULL ) - { - tmpfile = location.data + "/undo_remote.data"; - transport->recv (tmpfile); - delete transport; - - file = tmpfile; - } - else - file = uri.path; - - context.tdb.lock (context.config.getBoolean ("locking")); - context.tdb.merge (file); - context.tdb.unlock (); - - std::cout << "Merge complete.\n"; - - if (tmpfile != "") - remove (tmpfile.c_str ()); - - if ( ((sAutopush == "ask") && (confirm ("Would you like to push the merged changes to \'" + uri.data + "\'?")) ) - || (bAutopush) ) - { - context.task.set ("description", uri.data); - - std::string out; - context.commands["push"]->execute ("", out); - } - } - else - throw std::string ("No uri was specified for the merge. Either specify " - "the uri of a remote .task directory, or create a " - "'merge.default.uri' entry in your .taskrc file."); -} - //////////////////////////////////////////////////////////////////////////////// int handleModify (std::string& outs) { diff --git a/src/commands/CMakeLists.txt b/src/commands/CMakeLists.txt index 61eb46b8d..c849bad7c 100644 --- a/src/commands/CMakeLists.txt +++ b/src/commands/CMakeLists.txt @@ -31,6 +31,7 @@ set (commands_SRCS Command.cpp Command.h CmdInstall.cpp CmdInstall.h CmdLog.cpp CmdLog.h CmdLogo.cpp CmdLogo.h + CmdMerge.cpp CmdMerge.h CmdPrepend.cpp CmdPrepend.h CmdProjects.cpp CmdProjects.h CmdPull.cpp CmdPull.h diff --git a/src/commands/CmdHelp.cpp b/src/commands/CmdHelp.cpp index 481ad14bb..553a19bde 100644 --- a/src/commands/CmdHelp.cpp +++ b/src/commands/CmdHelp.cpp @@ -102,10 +102,6 @@ int CmdHelp::execute (const std::string&, std::string& output) row = view.addRow (); view.set (row, 1, "task ID"); view.set (row, 2, "Specifying an ID without a command invokes the 'info' command."); - - row = view.addRow (); - view.set (row, 1, "task merge URL"); - view.set (row, 2, "Merges the specified undo.data file with the local data files."); */ output = "\n" diff --git a/src/commands/CmdMerge.cpp b/src/commands/CmdMerge.cpp new file mode 100644 index 000000000..893318c3f --- /dev/null +++ b/src/commands/CmdMerge.cpp @@ -0,0 +1,107 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include +#include +#include + +extern Context context; + +//////////////////////////////////////////////////////////////////////////////// +CmdMerge::CmdMerge () +{ + _keyword = "merge"; + _usage = "task merge URL"; + _description = "Merges the specified undo.data file with the local data files."; + _read_only = false; + _displays_id = false; +} + +//////////////////////////////////////////////////////////////////////////////// +int CmdMerge::execute (const std::string&, std::string& output) +{ + std::string file = trim (context.task.get ("description")); + std::string pushfile = ""; + std::string tmpfile = ""; + + std::string sAutopush = lowerCase (context.config.get ("merge.autopush")); + bool bAutopush = context.config.getBoolean ("merge.autopush"); + + Uri uri (file, "merge"); + uri.parse(); + + if (uri.data.length ()) + { + Directory location (context.config.get ("data.location")); + + // be sure that uri points to a file + uri.append ("undo.data"); + + Transport* transport; + if ((transport = Transport::getTransport (uri)) != NULL ) + { + tmpfile = location.data + "/undo_remote.data"; + transport->recv (tmpfile); + delete transport; + + file = tmpfile; + } + else + file = uri.path; + + context.tdb.lock (context.config.getBoolean ("locking")); + context.tdb.merge (file); + context.tdb.unlock (); + + output += "Merge complete.\n"; + + if (tmpfile != "") + remove (tmpfile.c_str ()); + + if ( ((sAutopush == "ask") && (confirm ("Would you like to push the merged changes to \'" + uri.data + "\'?")) ) + || (bAutopush) ) + { + context.task.set ("description", uri.data); + + std::string out; + context.commands["push"]->execute ("", out); + } + } + else + throw std::string ("No uri was specified for the merge. Either specify " + "the uri of a remote .task directory, or create a " + "'merge.default.uri' entry in your .taskrc file."); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdMerge.h b/src/commands/CmdMerge.h new file mode 100644 index 000000000..c3720453f --- /dev/null +++ b/src/commands/CmdMerge.h @@ -0,0 +1,42 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_CMDMERGE +#define INCLUDED_CMDMERGE +#define L10N // Localization complete. + +#include +#include + +class CmdMerge : public Command +{ +public: + CmdMerge (); + int execute (const std::string&, std::string&); +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 1b0401e23..9ff28e6fb 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -114,6 +115,7 @@ void Command::factory (std::map & all) c = new CmdInstall (); all[c->keyword ()] = c; c = new CmdLog (); all[c->keyword ()] = c; c = new CmdLogo (); all[c->keyword ()] = c; + c = new CmdMerge (); all[c->keyword ()] = c; c = new CmdPrepend (); all[c->keyword ()] = c; c = new CmdProjects (); all[c->keyword ()] = c; c = new CmdPull (); all[c->keyword ()] = c; diff --git a/src/main.h b/src/main.h index 14493e7cd..67a8d8d37 100644 --- a/src/main.h +++ b/src/main.h @@ -51,7 +51,6 @@ bool nag (Task&); // command.cpp int handleModify (std::string&); -void handleMerge (std::string&); int deltaAppend (Task&); int deltaPrepend (Task&); int deltaDescription (Task&);