Commands - merge

- Migrated handleMerge to CmdMerge.
This commit is contained in:
Paul Beckingham 2011-05-30 14:45:39 -04:00
parent af90a14cb5
commit 510ce650a6
9 changed files with 153 additions and 70 deletions

View file

@ -129,8 +129,6 @@ void Cmd::load ()
{
if (commands.size () == 0)
{
commands.push_back ("merge");
// Now load the custom reports.
std::vector <std::string> 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;
}

View file

@ -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.
// ...

View file

@ -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)
{

View file

@ -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

View file

@ -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"

107
src/commands/CmdMerge.cpp Normal file
View file

@ -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 <fstream>
#include <sstream>
#include <Context.h>
#include <URI.h>
#include <Transport.h>
#include <text.h>
#include <util.h>
#include <CmdMerge.h>
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;
}
////////////////////////////////////////////////////////////////////////////////

42
src/commands/CmdMerge.h Normal file
View file

@ -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 <string>
#include <Command.h>
class CmdMerge : public Command
{
public:
CmdMerge ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -53,6 +53,7 @@
#include <CmdInstall.h>
#include <CmdLog.h>
#include <CmdLogo.h>
#include <CmdMerge.h>
#include <CmdPrepend.h>
#include <CmdProjects.h>
#include <CmdPull.h>
@ -114,6 +115,7 @@ void Command::factory (std::map <std::string, Command*>& 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;

View file

@ -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&);