• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GothenburgBitFactory / taskwarrior / 10152339701

29 Jul 2024 09:45PM UTC coverage: 84.437% (+0.07%) from 84.372%
10152339701

push

github

web-flow
Merge pull request #3566 from felixschurk/add-clang-format

Add clang-format to enforce style guide

12359 of 13760 new or added lines in 147 files covered. (89.82%)

123 existing lines in 42 files now uncovered.

19070 of 22585 relevant lines covered (84.44%)

19724.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/src/commands/CmdPurge.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included
13
// in all copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
//
23
// https://www.opensource.org/licenses/mit-license.php
24
//
25
////////////////////////////////////////////////////////////////////////////////
26

27
#include <cmake.h>
28
// cmake.h include header must come first
29

30
#include <CmdPurge.h>
31
#include <Context.h>
32
#include <Filter.h>
33
#include <format.h>
34
#include <main.h>
35
#include <shared.h>
36

37
////////////////////////////////////////////////////////////////////////////////
38
CmdPurge::CmdPurge() {
4,389✔
39
  _keyword = "purge";
4,389✔
40
  _usage = "task <filter> purge";
4,389✔
41
  _description = "Removes the specified tasks from the data files. Causes permanent loss of data.";
4,389✔
42
  _read_only = false;
4,389✔
43
  _displays_id = false;
4,389✔
44
  _needs_confirm = true;
4,389✔
45
  _needs_gc = true;
4,389✔
46
  _uses_context = true;
4,389✔
47
  _accepts_filter = true;
4,389✔
48
  _accepts_modifications = false;
4,389✔
49
  _accepts_miscellaneous = false;
4,389✔
50
  _category = Command::Category::operation;
4,389✔
51
}
4,389✔
52

53
////////////////////////////////////////////////////////////////////////////////
54
// Purges the task, while taking care of:
55
// - dependencies on this task
56
// - child tasks
57
void CmdPurge::handleRelations(Task& task, std::vector<Task>& tasks) {
8✔
58
  handleDeps(task);
8✔
59
  handleChildren(task, tasks);
8✔
60
  tasks.push_back(task);
6✔
61
}
6✔
62

63
////////////////////////////////////////////////////////////////////////////////
64
// Makes sure that any task having the dependency on the task being purged
65
// has that dependency removed, to preserve referential integrity.
66
void CmdPurge::handleDeps(Task& task) {
8✔
67
  std::string uuid = task.get("uuid");
16✔
68

69
  for (auto& blockedConst : Context::getContext().tdb2.all_tasks()) {
39✔
70
    Task& blocked = const_cast<Task&>(blockedConst);
31✔
71
    if (blocked.hasDependency(uuid)) {
31✔
72
      blocked.removeDependency(uuid);
2✔
73
      Context::getContext().tdb2.modify(blocked);
2✔
74
    }
75
  }
8✔
76
}
8✔
77

78
////////////////////////////////////////////////////////////////////////////////
79
// Makes sure that with any recurrence parent are all the child tasks removed
80
// as well. If user chooses not to, the whole command is aborted.
81
void CmdPurge::handleChildren(Task& task, std::vector<Task>& tasks) {
8✔
82
  // If this is not a recurrence parent, we have no job here
83
  if (!task.has("mask")) return;
8✔
84

85
  std::string uuid = task.get("uuid");
6✔
86
  std::vector<Task> children;
3✔
87

88
  // Find all child tasks
89
  for (auto& childConst : Context::getContext().tdb2.all_tasks()) {
13✔
90
    Task& child = const_cast<Task&>(childConst);
11✔
91

92
    if (child.get("parent") == uuid) {
11✔
93
      if (child.getStatus() != Task::deleted)
7✔
94
        // In case any child task is not deleted, bail out
95
        throw format(
1✔
96
            "Task '{1}' is a recurrence template. Its child task {2} must be deleted before it can "
97
            "be purged.",
98
            task.get("description"), child.identifier(true));
2✔
99
      else
100
        children.push_back(child);
6✔
101
    }
102
  }
3✔
103

104
  // If there are no children, our job is done
105
  if (children.empty()) return;
2✔
106

107
  // Ask for confirmation to purge them, if needed
108
  std::string question = format(
109
      "Task '{1}' is a recurrence template. All its {2} deleted children tasks will be purged as "
110
      "well. Continue?",
111
      task.get("description"), children.size());
4✔
112

113
  if (Context::getContext().config.getBoolean("recurrence.confirmation") ||
8✔
114
      (Context::getContext().config.get("recurrence.confirmation") == "prompt" &&
4✔
115
       confirm(question))) {
2✔
116
    for (auto& child : children) handleRelations(child, tasks);
4✔
117
  } else
118
    throw std::string("Purge operation aborted.");
1✔
119
}
6✔
120

121
////////////////////////////////////////////////////////////////////////////////
122
int CmdPurge::execute(std::string&) {
5✔
123
  int rc = 0;
5✔
124
  std::vector<Task> tasks;
5✔
125
  bool matched_deleted = false;
5✔
126

127
  Filter filter;
5✔
128
  std::vector<Task> filtered;
5✔
129

130
  // Apply filter.
131
  filter.subset(filtered);
5✔
132
  if (filtered.size() == 0) {
5✔
NEW
133
    Context::getContext().footnote("No tasks specified.");
×
UNCOV
134
    return 1;
×
135
  }
136

137
  for (auto& task : filtered) {
8✔
138
    // Allow purging of deleted tasks only. Hence no need to deal with:
139
    // - unblocked tasks notifications (deleted tasks are not blocking)
140
    // - project changes (deleted tasks not included in progress)
141
    // It also has the nice property of being explicit - users need to
142
    // mark tasks as deleted before purging.
143
    if (task.getStatus() == Task::deleted) {
5✔
144
      // Mark that at least one deleted task matched the filter
145
      matched_deleted = true;
5✔
146

147
      std::string question;
5✔
148
      question = format("Permanently remove task {1} '{2}'?", task.identifier(true),
10✔
149
                        task.get("description"));
15✔
150

151
      if (permission(question, filtered.size())) handleRelations(task, tasks);
5✔
152
    }
5✔
153
  }
154

155
  // Now that any exceptions are handled, actually purge the tasks.
156
  for (auto& task : tasks) {
9✔
157
    Context::getContext().tdb2.purge(task);
6✔
158
  }
159

160
  if (filtered.size() > 0 and !matched_deleted)
3✔
NEW
161
    Context::getContext().footnote(
×
162
        "No deleted tasks specified. Maybe you forgot to delete tasks first?");
163

164
  feedback_affected(tasks.size() == 1 ? "Purged {1} task." : "Purged {1} tasks.", tasks.size());
3✔
165
  return rc;
3✔
166
}
7✔
167

168
////////////////////////////////////////////////////////////////////////////////
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc