Skip to content

Commit 97a5db1

Browse files
committed
Add version update, add version vnew [vold].
1 parent d9b78b4 commit 97a5db1

4 files changed

Lines changed: 103 additions & 2 deletions

File tree

src/client/main.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,13 +669,82 @@ ApiResult api_call(const String &cmd, const Strings &args)
669669
return ApiResult::Error;
670670
}
671671

672-
Api().add_version(find_remote(remote), p, read_file(args[arg++]));
672+
auto f = args[arg++];
673+
if (fs::exists(f) && fs::is_regular_file(f))
674+
{
675+
Api().add_version(find_remote(remote), p, read_file(f));
676+
return ApiResult::Handled;
677+
}
678+
679+
if (args.size() < arg + 1)
680+
{
681+
Api().add_version(find_remote(remote), p, Version(f));
682+
return ApiResult::Handled;
683+
}
684+
685+
auto vold = args[arg++];
686+
Api().add_version(find_remote(remote), p, Version(f), vold);
687+
673688
return ApiResult::Handled;
674689
}
675690

676691
return ApiResult::Handled;
677692
}
678693

694+
if (cmd == "update")
695+
{
696+
if (args.size() < 3)
697+
{
698+
std::cout << "invalid number of arguments\n";
699+
std::cout << "usage: cppan update version [remote] name version\n";
700+
return ApiResult::Error;
701+
}
702+
703+
size_t arg = 2;
704+
String what = args[arg++];
705+
706+
if (what == "version")
707+
{
708+
auto proj_usage = []
709+
{
710+
std::cout << "invalid number of arguments\n";
711+
std::cout << "usage: cppan update version [remote] name version\n";
712+
};
713+
714+
if (args.size() < arg + 1)
715+
{
716+
proj_usage();
717+
return ApiResult::Error;
718+
}
719+
720+
String remote = DEFAULT_REMOTE_NAME;
721+
ProjectPath p(args[arg++]);
722+
if (has_remote(remote) && p.is_relative() && p.size() == 1)
723+
{
724+
remote = args[arg - 1];
725+
726+
if (args.size() < arg + 1)
727+
{
728+
proj_usage();
729+
return ApiResult::Error;
730+
}
731+
732+
p = ProjectPath(args[arg++]);
733+
}
734+
735+
if (args.size() < arg + 1)
736+
{
737+
proj_usage();
738+
return ApiResult::Error;
739+
}
740+
741+
Api().update_version(find_remote(remote), p, String(args[arg++]));
742+
return ApiResult::Handled;
743+
}
744+
745+
return ApiResult::NotHandled;
746+
}
747+
679748
if (cmd == "remove")
680749
{
681750
if (args.size() < 3)

src/common/api.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ void Api::add_version(const Remote &r, ProjectPath p, const String &cppan)
8080
api_call(r, "add_version", request);
8181
}
8282

83+
void Api::add_version(const Remote &r, ProjectPath p, const Version &vnew)
84+
{
85+
add_version(r, p, vnew, String());
86+
}
87+
88+
void Api::add_version(const Remote &r, ProjectPath p, const Version &vnew, const String &vold)
89+
{
90+
check_relative(r, p);
91+
ptree request;
92+
request.put("project", p.toString());
93+
request.put("new", vnew.toString());
94+
if (!vold.empty())
95+
request.put("old", vold);
96+
api_call(r, "add_version", request);
97+
}
98+
99+
void Api::update_version(const Remote &r, ProjectPath p, const Version &v)
100+
{
101+
if (!v.isBranch())
102+
throw std::runtime_error("Only branches can be updated");
103+
check_relative(r, p);
104+
ptree request;
105+
request.put("project", p.toString());
106+
request.put("version", v.toString());
107+
api_call(r, "update_version", request);
108+
}
109+
83110
void Api::remove_version(const Remote &r, ProjectPath p, const Version &v)
84111
{
85112
check_relative(r, p);

src/common/api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct Api
2828
void add_project(const Remote &r, ProjectPath p, ProjectType t);
2929
void remove_project(const Remote &r, ProjectPath p);
3030
void add_version(const Remote &r, ProjectPath p, const String &cppan);
31+
void add_version(const Remote &r, ProjectPath p, const Version &vnew);
32+
void add_version(const Remote &r, ProjectPath p, const Version &vnew, const String &vold);
33+
void update_version(const Remote &r, ProjectPath p, const Version &v);
3134
void remove_version(const Remote &r, ProjectPath p, const Version &v);
3235
void get_notifications(const Remote &r, int n = 10);
3336
void clear_notifications(const Remote &r);

src/common/property_tree.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ std::string ptree2string(const ptree &p)
2727

2828
ptree string2ptree(const std::string &s)
2929
{
30-
std::istringstream iss(s);
3130
ptree p;
31+
if (s.empty())
32+
return p;
33+
std::istringstream iss(s);
3234
pt::read_json(iss, p);
3335
return p;
3436
}

0 commit comments

Comments
 (0)