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

GothenburgBitFactory / taskwarrior / 26528171790

27 May 2026 05:40PM UTC coverage: 85.072% (-0.1%) from 85.167%
26528171790

Pull #4111

github

web-flow
Merge fe0d08ff5 into 8902b1e7a
Pull Request #4111: Add support for git sync

4 of 18 new or added lines in 2 files covered. (22.22%)

18 existing lines in 5 files now uncovered.

19626 of 23070 relevant lines covered (85.07%)

23495.82 hits per line

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

44.76
/src/commands/CmdSync.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 <CmdSync.h>
31
#include <Color.h>
32
#include <Context.h>
33
#include <Filter.h>
34
#include <format.h>
35
#include <shared.h>
36
#include <taskchampion-cpp/lib.h>
37
#include <util.h>
38

39
#include <regex>
40
#include <sstream>
41

42
////////////////////////////////////////////////////////////////////////////////
43
CmdSync::CmdSync() {
4,618✔
44
  _keyword = "synchronize";
4,618✔
45
  _usage = "task          synchronize [initialize]";
4,618✔
46
  _description = "Synchronizes data with the Taskserver";
4,618✔
47
  _read_only = false;
4,618✔
48
  _displays_id = false;
4,618✔
49
  _needs_gc = false;
4,618✔
50
  _needs_recur_update = false;
4,618✔
51
  _uses_context = false;
4,618✔
52
  _accepts_filter = false;
4,618✔
53
  _accepts_modifications = false;
4,618✔
54
  _accepts_miscellaneous = true;
4,618✔
55
  _category = Command::Category::migration;
4,618✔
56
}
4,618✔
57

58
////////////////////////////////////////////////////////////////////////////////
59
int CmdSync::execute(std::string& output) {
2✔
60
  int status = 0;
2✔
61

62
  Context& context = Context::getContext();
2✔
63
  auto& replica = context.tdb2.replica();
2✔
64
  std::stringstream out;
2✔
65
  bool avoid_snapshots = false;
2✔
66
  bool verbose = Context::getContext().verbose("sync");
4✔
67

68
  std::string origin = Context::getContext().config.get("sync.server.origin");
4✔
69
  std::string url = Context::getContext().config.get("sync.server.url");
4✔
70
  std::string server_dir = Context::getContext().config.get("sync.local.server_dir");
4✔
71
  std::string client_id = Context::getContext().config.get("sync.server.client_id");
4✔
72
  std::string aws_bucket = Context::getContext().config.get("sync.aws.bucket");
4✔
73
  std::string gcp_bucket = Context::getContext().config.get("sync.gcp.bucket");
4✔
74
  std::string git_local_path = Context::getContext().config.get("sync.git.local_path");
4✔
75
  std::string encryption_secret = Context::getContext().config.get("sync.encryption_secret");
4✔
76

77
  // sync.server.origin is a deprecated synonym for sync.server.url
78
  std::string server_url = url == "" ? origin : url;
2✔
79
  if (origin != "") {
2✔
80
    out << "sync.server.origin is deprecated. Use sync.server.url instead.\n";
×
81
  }
82

83
  // redact credentials from `server_url`, if present
84
  std::regex remove_creds_regex("^(https?://.+):(.+)@(.+)");
2✔
85
  std::string safe_server_url = std::regex_replace(server_url, remove_creds_regex, "$1:****@$3");
2✔
86

87
  auto num_local_operations = replica->num_local_operations();
2✔
88

89
  if (server_dir != "") {
2✔
90
    if (verbose) {
2✔
91
      out << format("Syncing with {1}", server_dir) << '\n';
6✔
92
    }
93
    replica->sync_to_local(server_dir, avoid_snapshots);
2✔
94
  } else if (aws_bucket != "") {
×
95
    std::string aws_region = Context::getContext().config.get("sync.aws.region");
×
96
    std::string aws_profile = Context::getContext().config.get("sync.aws.profile");
×
97
    std::string aws_access_key_id = Context::getContext().config.get("sync.aws.access_key_id");
×
98
    std::string aws_secret_access_key =
99
        Context::getContext().config.get("sync.aws.secret_access_key");
×
100
    std::string aws_default_credentials =
101
        Context::getContext().config.get("sync.aws.default_credentials");
×
102
    if (aws_region == "") {
×
103
      throw std::string("sync.aws.region is required");
×
104
    }
105
    if (encryption_secret == "") {
×
106
      throw std::string("sync.encryption_secret is required");
×
107
    }
108

109
    bool using_profile = false;
×
110
    bool using_creds = false;
×
111
    bool using_default = false;
×
112
    if (aws_profile != "") {
×
113
      using_profile = true;
×
114
    }
115
    if (aws_access_key_id != "" || aws_secret_access_key != "") {
×
116
      using_creds = true;
×
117
    }
118
    if (aws_default_credentials != "") {
×
119
      using_default = true;
×
120
    }
121

122
    if (using_profile + using_creds + using_default != 1) {
×
123
      throw std::string("exactly one method of specifying AWS credentials is required");
×
124
    }
125

126
    if (verbose) {
×
127
      out << format("Syncing with AWS bucket {1}", aws_bucket) << '\n';
×
128
    }
129

130
    if (using_profile) {
×
131
      replica->sync_to_aws_with_profile(aws_region, aws_bucket, aws_profile, encryption_secret,
×
132
                                        avoid_snapshots);
133
    } else if (using_creds) {
×
134
      replica->sync_to_aws_with_access_key(aws_region, aws_bucket, aws_access_key_id,
×
135
                                           aws_secret_access_key, encryption_secret,
136
                                           avoid_snapshots);
137
    } else {
138
      replica->sync_to_aws_with_default_creds(aws_region, aws_bucket, encryption_secret,
×
139
                                              avoid_snapshots);
140
    }
141

142
  } else if (gcp_bucket != "") {
×
143
    std::string gcp_credential_path = Context::getContext().config.get("sync.gcp.credential_path");
×
144
    if (encryption_secret == "") {
×
145
      throw std::string("sync.encryption_secret is required");
×
146
    }
147
    if (verbose) {
×
148
      out << format("Syncing with GCP bucket {1}", gcp_bucket) << '\n';
×
149
    }
150
    replica->sync_to_gcp(gcp_bucket, gcp_credential_path, encryption_secret, avoid_snapshots);
×
151

152
  } else if (server_url != "") {
×
153
    if (client_id == "" || encryption_secret == "") {
×
154
      throw std::string("sync.server.client_id and sync.encryption_secret are required");
×
155
    }
156
    if (verbose) {
×
157
      out << format("Syncing with sync server at {1}", safe_server_url) << '\n';
×
158
    }
159
    replica->sync_to_remote(server_url, tc::uuid_from_string(client_id), encryption_secret,
×
160
                            avoid_snapshots);
161

NEW
162
  } else if (git_local_path != "") {
×
NEW
163
    std::string git_branch = Context::getContext().config.get("sync.git.branch");
×
NEW
164
    std::string git_remote = Context::getContext().config.get("sync.git.remote");
×
NEW
165
    bool git_local_only = Context::getContext().config.getBoolean("sync.git.local_only");
×
NEW
166
    std::string git_git_path = Context::getContext().config.get("sync.git.git_path");
×
NEW
167
    if (git_branch == "") {
×
NEW
168
      throw std::string("sync.git.branch is required");
×
169
    }
NEW
170
    if (encryption_secret == "") {
×
NEW
171
      throw std::string("sync.encryption_secret is required");
×
172
    }
NEW
173
    if (verbose) {
×
NEW
174
      if (git_remote != "") {
×
NEW
175
        out << format("Syncing with git remote {1}", git_remote) << '\n';
×
176
      } else {
NEW
177
        out << format("Syncing with local git repository at {1}", git_local_path) << '\n';
×
178
      }
179
    }
NEW
180
    replica->sync_to_git(git_local_path, git_branch, git_remote, git_local_only, encryption_secret,
×
181
                         git_git_path, avoid_snapshots);
182

183
  } else {
×
184
    throw std::string("No sync.* settings are configured. See task-sync(5).");
×
185
  }
186

187
  if (context.config.getBoolean("purge.on-sync")) {
6✔
188
    context.tdb2.expire_tasks();
1✔
189
  }
190

191
  if (verbose) {
2✔
192
    out << "Success!\n";
2✔
193
    // Taskchampion does not provide a measure of the number of operations received from
194
    // the server, but we can give some indication of the number sent.
195
    if (num_local_operations) {
2✔
196
      out << format("Sent {1} local operations to the server", num_local_operations) << '\n';
2✔
197
    }
198
  }
199

200
  output = out.str();
2✔
201
  return status;
2✔
202
}
2✔
203

204
////////////////////////////////////////////////////////////////////////////////
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