From 68d5e3f296130fceb6bf419fd7a0e934e98f137f Mon Sep 17 00:00:00 2001 From: Johannes Schlatow Date: Sun, 3 Oct 2010 02:22:36 +0200 Subject: [PATCH] Pull command - Added pull command for cloning databases --- NEWS | 6 ++++ doc/man/task-faq.5 | 14 ++++---- doc/man/task.1 | 28 ++++++++++++++-- src/Cmd.cpp | 4 ++- src/Config.cpp | 6 +++- src/Context.cpp | 1 + src/Hooks.cpp | 2 ++ src/command.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++ src/i18n.h | 1 + src/main.h | 1 + src/report.cpp | 6 +++- 11 files changed, 137 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 4014cdf50..1fb47c513 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,7 @@ New Features in taskwarrior 1.9.3 detection of duplicate imports. - New merge capability for syncing task data files. - New push capability for distributing merged changes. + - New pull capability for copying data files from a remote location. - When completing or modifying a task, the project status is displayed. - The 'info' report is now colorized. - Certain characters (#, $, @) are now supported for use in tags. @@ -36,6 +37,7 @@ New commands in taskwarrior 1.9.3 - New 'task merge ' command that can merge the local and an undo.data file from another taskwarrior user, to sync across machines, for example. - New 'task push ' command to distribute merged changes. + - New 'task pull ' command to copy data files from a remote location. New configuration options in taskwarrior 1.9.3 @@ -45,6 +47,10 @@ New configuration options in taskwarrior 1.9.3 variable rule.precedence.color. Try "task show rule.pre" to show the default settings. - merge.autopush to control whether pushing after merging is automated. + - merge.*.uri to configure source locations for the merge command + (e.g. merge.default.uri). + - push.*.uri to configure target locations for the push command. + - pull.*.uri to configure source locations for the pull command. Newly deprecated features in taskwarrior 1.9.3 diff --git a/doc/man/task-faq.5 b/doc/man/task-faq.5 index b4c54ec89..327c9d088 100644 --- a/doc/man/task-faq.5 +++ b/doc/man/task-faq.5 @@ -170,14 +170,14 @@ a second database. Here is a basic example of the procedure: - $ rsync myremotehost:.task/undo.data /tmp/undo_remote.data - $ task merge /tmp/undo_remote.data - $ rsync ${HOME}/.task/*.data myremotehost:.task/ + $ task merge ssh://user@myremotehost/.task/ + $ task push ssh://user@myremotehost/.task/ -First you need to get the undo.data file from the remote system, or removable -media. When the merge command completes, you should copy all the local .data -files to the remote system. This way you ensure that both systems are fully -synchronized. +The first command fetches the undo.data file from the remote system, reads the +changes made and updates the local database. When this merge command completes, +you should copy all the local .data files to the remote system either by using +the push command explicitly or by activating the merge.autopush feature in the +~/.taskrc file. This way you ensure that both systems are fully synchronized. .TP .B Q: The undo.data file gets very large - do I need it? diff --git a/doc/man/task.1 b/doc/man/task.1 index 8968c198e..2f19d820b 100644 --- a/doc/man/task.1 +++ b/doc/man/task.1 @@ -143,9 +143,33 @@ Exports all tasks in YAML 1.1 format. Redirect the output to a file, if you wish to save it, or pipe it to another command. .TP -.B merge path/to/second/undo.data +.B merge URL Merges two task databases by comparing the modifications that are stored in the -undo.data files. The location of the second undo.data file must be passed on as argument. +undo.data files. The location of the second undo.data file must be passed on as argument. URL may have the following syntaxes: + + + ssh://[user@]host.xz[:port]/path/to/undo.data + + rsync://[user@]host.xz[:port]/path/to/undo.data + + [user@]host.xz:path/to/undo.data + + /path/to/local/undo.data + +You can set aliases for frequently used URLs in the .taskrc. + +.TP +.B push URL +Pushes the task database to a remote another location for distributing the +changes made by the merge command. + +(See annotations above for valid URL syntaxes.) + +.TP +.B pull URL +Overwrites the task database with those files found at the URL. + +(See annotations above for valid URL syntaxes.) .TP .B color [sample | legend] diff --git a/src/Cmd.cpp b/src/Cmd.cpp index aa257927d..ed18d4757 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -173,7 +173,8 @@ void Cmd::load () commands.push_back (context.stringtable.get (CMD_UNDO, "undo")); commands.push_back (context.stringtable.get (CMD_VERSION, "version")); commands.push_back (context.stringtable.get (CMD_MERGE, "merge")); - commands.push_back (context.stringtable.get (CMD_PUSH, "push")); + commands.push_back (context.stringtable.get (CMD_PUSH, "push")); + commands.push_back (context.stringtable.get (CMD_PULL, "pull")); // Now load the custom reports. std::vector all; @@ -277,6 +278,7 @@ bool Cmd::isWriteCommand () command == context.stringtable.get (CMD_IMPORT, "import") || command == context.stringtable.get (CMD_LOG, "log") || command == context.stringtable.get (CMD_PREPEND, "prepend") || + command == context.stringtable.get (CMD_PULL, "pull") || command == context.stringtable.get (CMD_START, "start") || command == context.stringtable.get (CMD_STOP, "stop") || command == context.stringtable.get (CMD_UNDO, "undo")) diff --git a/src/Config.cpp b/src/Config.cpp index d497a940b..76aa9bf38 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -80,7 +80,6 @@ std::string Config::defaults = "recurrence.indicator=R # What to show as a task recurrence indicator\n" "recurrence.limit=1 # Number of future recurring pending tasks\n" "undo.style=side # Undo style - can be 'side', or 'diff'\n" - "merge.autopush=ask # Push database to remote origin after merge: yes, no, ask\n" "\n" "# Dates\n" "dateformat=m/d/Y # Preferred input and display date format\n" @@ -232,6 +231,11 @@ std::string Config::defaults = "fontunderline=yes # Uses underlines rather than -------\n" "shell.prompt=task> # Prompt used by the shell command\n" "\n" + "# Merge options\n" + "merge.autopush=ask # Push database to remote origin after merge: yes, no, ask\n" + "#merge.default.uri=user@host.xz:.task/\n" + "#pull.default.uri=rsync://host.xz/task-backup/\n" + "\n" "# Import heuristics - alternate names for fields (comma-separated list of names)\n" "#import.synonym.bg=?\n" "#import.synonym.description=?\n" diff --git a/src/Context.cpp b/src/Context.cpp index c4d10c9b8..e07657cdc 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -244,6 +244,7 @@ int Context::dispatch (std::string &out) else if (cmd.command == "merge") { tdb.gc (); handleMerge (out); } else if (cmd.command == "push") { handlePush (out); } + else if (cmd.command == "pull") { handlePull (out); } else if (cmd.command == "_projects") { rc = handleCompletionProjects (out); } else if (cmd.command == "_tags") { rc = handleCompletionTags (out); } else if (cmd.command == "_commands") { rc = handleCompletionCommands (out); } diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 03366ab84..e9e9d28f2 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -167,6 +167,8 @@ Hooks::Hooks () validProgramEvents.push_back ("post-prepend-command"); validProgramEvents.push_back ("pre-projects-command"); validProgramEvents.push_back ("post-projects-command"); + validProgramEvents.push_back ("pre-pull-command"); + validProgramEvents.push_back ("post-pull-command"); validProgramEvents.push_back ("pre-push-command"); validProgramEvents.push_back ("post-push-command"); validProgramEvents.push_back ("pre-shell-command"); diff --git a/src/command.cpp b/src/command.cpp index b4f2cecc3..54dc4c5fa 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -598,6 +598,20 @@ void handleMerge (std::string& outs) { std::string file = trim (context.task.get ("description")); std::string tmpfile = ""; + + if (file.length () == 0) + { + // get default target from config + file = context.config.get ("merge.default.uri"); + } + else + { + // replace argument with uri from config + std::string tmp = context.config.get ("merge." + file + ".uri"); + + if (tmp != "") + file = tmp; + } if (file.length () > 0) { @@ -654,6 +668,20 @@ void handlePush (std::string& outs) if (context.hooks.trigger ("pre-push-command")) { std::string file = trim (context.task.get ("description")); + + if (file.length () == 0) + { + // get default target from config + file = context.config.get ("push.default.uri"); + } + else + { + // replace argument with uri from config + std::string tmp = context.config.get ("push." + file + ".uri"); + + if (tmp != "") + file = tmp; + } if (file.length () > 0) { @@ -672,6 +700,58 @@ void handlePush (std::string& outs) context.hooks.trigger ("post-push-command"); } + else // TODO : get default target from config file + throw std::string ("You must specify a target."); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void handlePull (std::string& outs) +{ + if (context.hooks.trigger ("pre-pull-command")) + { + std::string file = trim (context.task.get ("description")); + + if (file.length () == 0) + { + // get default target from config + file = context.config.get ("pull.default.uri"); + } + else + { + // replace argument with uri from config + std::string tmp = context.config.get ("pull." + file + ".uri"); + + if (tmp != "") + file = tmp; + } + + if (file.length () > 0) + { + Directory location (context.config.get ("data.location")); + + // add *.data to path if necessary + if (file.find ("*.data") == std::string::npos) + { + if (file[file.length()-1] != '/') + file += "/"; + + file += "*.data"; + } + + Transport* transport; + if ((transport = Transport::getTransport (file)) != NULL ) + { + transport->recv (location.data + "/"); + delete transport; + } + else + { + throw std::string ("Pull failed"); + } + + context.hooks.trigger ("post-pull-command"); + } else // TODO : get default target from config file throw std::string ("You must specify a target."); } diff --git a/src/i18n.h b/src/i18n.h index cfad6777f..8a05e3083 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -100,6 +100,7 @@ #define CMD_SHOW 231 #define CMD_MERGE 232 #define CMD_PUSH 233 +#define CMD_PULL 234 // 3xx Attributes #define ATT_PROJECT 300 diff --git a/src/main.h b/src/main.h index 3862a43f6..1f2042fbd 100644 --- a/src/main.h +++ b/src/main.h @@ -79,6 +79,7 @@ int handleDuplicate (std::string &); void handleUndo (); void handleMerge (std::string&); void handlePush (std::string&); +void handlePull (std::string&); #ifdef FEATURE_SHELL void handleShell (); #endif diff --git a/src/report.cpp b/src/report.cpp index 2f2a00f7e..2ffafd2eb 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -209,7 +209,11 @@ int shortUsage (std::string &outs) row = table.addRow (); table.addCell (row, 1, "task push URL"); - table.addCell (row, 2, "Pushes the local undo.data files to the URL."); + table.addCell (row, 2, "Pushes the local *.data files to the URL."); + + row = table.addRow (); + table.addCell (row, 1, "task pull URL"); + table.addCell (row, 2, "Overwrites the local *.data files with those found at the URL."); row = table.addRow (); table.addCell (row, 1, "task export.ical");