- Allow UUIDs and IDs range when modifying task dependencies.
- Update man page.
- Add unit tests.
- Fatorize code when adding and removing dependencies in Task.cpp.
This commit is contained in:
Louis-Claude Canon 2012-05-05 10:22:05 +02:00 committed by Paul Beckingham
parent 5ffb65b5ac
commit 1364202d30
5 changed files with 72 additions and 32 deletions

View file

@ -767,32 +767,16 @@ void Task::removeAnnotations ()
////////////////////////////////////////////////////////////////////////////////
void Task::addDependency (int id)
{
if (id == this->id)
throw std::string (STRING_TASK_DEPEND_ITSELF);
// Check that id is resolvable.
std::string uuid = context.tdb2.pending.uuid (id);
if (uuid == "")
throw format (STRING_TASK_DEPEND_MISSING, id);
throw format (STRING_TASK_DEPEND_MISS_CREA, id);
// Store the dependency.
std::string depends = get ("depends");
if (depends != "")
{
// Check for extant dependency.
if (depends.find (uuid) == std::string::npos)
set ("depends", depends + "," + uuid);
else
throw format (STRING_TASK_DEPEND_DUP, this->id, id);
}
else
set ("depends", uuid);
if (depends.find (uuid) != std::string::npos)
throw format (STRING_TASK_DEPEND_DUP, this->id, id);
// Prevent circular dependencies.
if (dependencyIsCircular (*this))
throw std::string (STRING_TASK_DEPEND_CIRCULAR);
recalc_urgency = true;
addDependency(uuid);
}
////////////////////////////////////////////////////////////////////////////////
@ -849,11 +833,12 @@ void Task::removeDependency (const std::string& uuid)
////////////////////////////////////////////////////////////////////////////////
void Task::removeDependency (int id)
{
std::string depends = get ("depends");
std::string uuid = context.tdb2.pending.uuid (id);
if (uuid != "")
if (uuid != "" && depends.find (uuid) != std::string::npos)
removeDependency (uuid);
else
throw std::string (STRING_TASK_DEPEND_NO_UUID);
throw format (STRING_TASK_DEPEND_MISS_DEL, id);
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -478,10 +478,10 @@ void Command::modify_task (
}
else
{
// Dependencies must be resolved to UUIDs.
// Dependencies are used as IDs.
if (name == "depends")
{
// Convert ID to UUID.
// Parse IDs
std::vector <std::string> deps;
split (deps, value, ',');
@ -489,11 +489,31 @@ void Command::modify_task (
std::vector <std::string>::iterator i;
for (i = deps.begin (); i != deps.end (); i++)
{
int id = strtol (i->c_str (), NULL, 10);
if (id < 0)
task.removeDependency (-id);
bool removal = false;
std::string& dep = *i;
if (dep[0] == '-')
{
removal = true;
dep = i->substr(1, std::string::npos);
}
std::vector <int> ids;
// Crude UUID check
if (dep.length () == 36)
{
int id = context.tdb2.pending.id (dep);
ids.push_back (id);
}
else
task.addDependency (id);
A3::extract_id (dep, ids);
std::vector <int>::iterator id;
for (id = ids.begin (); id != ids.end(); id++)
if (removal)
task.removeDependency (*id);
else
task.addDependency (*id);
}
}

View file

@ -755,10 +755,10 @@
#define STRING_TASK_PARSE_TOO_SHORT "Line too short."
#define STRING_TASK_PARSE_UNREC_FF "Unrecognized taskwarrior file format."
#define STRING_TASK_DEPEND_ITSELF "A task cannot be dependent on itself."
#define STRING_TASK_DEPEND_MISSING "Could not create a dependency on task {1} - not found."
#define STRING_TASK_DEPEND_MISS_CREA "Could not create a dependency on task {1} - not found."
#define STRING_TASK_DEPEND_MISS_DEL "Could not delete a dependency on task {1} - not found."
#define STRING_TASK_DEPEND_DUP "Task {1} already depends on task {2}."
#define STRING_TASK_DEPEND_CIRCULAR "Circular dependency detected and disallowed."
#define STRING_TASK_DEPEND_NO_UUID "Could not find a UUID for id {1}."
#define STRING_TASK_VALID_DESC "A task must have a description."
#define STRING_TASK_VALID_BLANK "Cannot add a task that is blank."
#define STRING_TASK_VALID_WAIT "Warning: You have specified a 'wait' date that is after the 'due' date."