Skip to content

Create packages diff between the current state and a revision #3911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libmamba/include/mamba/api/install.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <nlohmann/json.hpp>
#include <yaml-cpp/yaml.h>

#include "mamba/core/history.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/solver/request.hpp"

Expand Down Expand Up @@ -111,6 +112,16 @@ namespace mamba
inline void to_json(nlohmann::json&, const other_pkg_mgr_spec&)
{
}

struct PackageDiff
{
std::map<std::string, specs::PackageInfo> removed_pkg_diff;
std::map<std::string, specs::PackageInfo> installed_pkg_diff;
};

specs::PackageInfo pkg_info_builder(std::string s);
PackageDiff
get_revision_pkg_diff(std::vector<History::UserRequest> user_requests, int REVISION);
}

}
Expand Down
2 changes: 1 addition & 1 deletion libmamba/include/mamba/core/history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace mamba
struct ParseResult
{
std::string head_line;
std::set<std::string> diff;
std::vector<std::string> diff;
std::vector<std::string> comments;
};

Expand Down
177 changes: 177 additions & 0 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,5 +1010,182 @@
}
}
}

// The following function parses the different formats that can be found in the history
// file. conda/mamba1 format: installed: +conda-forge/linux-64::xtl-0.8.0-h84d6215_0
// removed: -conda-forge/linux-64::xtl-0.8.0-h84d6215_0 mamba2 broken format: installed:
// +conda-forge::xtl-0.8.0-h84d6215_0 removed:
// -https://conda.anaconda.org/conda-forge/linux-64::xtl-0.8.0-h84d6215_0 mamba2 new format:
// installed: +https://conda.anaconda.org/conda-forge/linux-64::xtl-0.8.0-h84d6215_0
// removed: -https://conda.anaconda.org/conda-forge/linux-64::xtl-0.8.0-h84d6215_0
specs::PackageInfo pkg_info_builder(std::string s)
{
std::string s_pkg;
std::string channel;
size_t pos_0 = s.rfind("/");
if (pos_0 != std::string::npos)
{
std::string s_begin = s.substr(0, pos_0);
std::string s_end = s.substr(pos_0 + 1, s.size());
size_t pos = s_begin.rfind("/");
if (pos != std::string::npos)
{
channel = s_begin.substr(pos + 1, s_begin.size());
}
else
{
channel = s_begin;
}
s_pkg = util::split(s_end, "::").back();
}
else
{
channel = util::split(s, "::").front();
s_pkg = util::split(s, "::").back();
}

size_t pos_1 = s_pkg.rfind("-");
std::string s_pkg_ = s_pkg.substr(0, pos_1);
std::string build_string = s_pkg.substr(pos_1 + 1, s_pkg.size());

size_t pos_2 = s_pkg_.rfind("-");
std::string name = s_pkg_.substr(0, pos_2);
std::string version = s_pkg_.substr(pos_2 + 1, s_pkg_.size());

specs::PackageInfo pkg_info{ name, version, build_string, channel };
return pkg_info;
}

PackageDiff
get_revision_pkg_diff(std::vector<History::UserRequest> user_requests, int REVISION)
{
struct revision
{
int key = -1;
std::map<std::string, specs::PackageInfo> removed_pkg = {};
std::map<std::string, specs::PackageInfo> installed_pkg = {};
};

std::vector<revision> revisions;
for (auto r : user_requests)
{
if ((r.link_dists.size() > 0) || (r.unlink_dists.size() > 0))
{
if (r.revision_num > REVISION)
{
revision rev{ /*.key = */ r.revision_num };
for (auto ud : r.unlink_dists)
{
auto pkg_info = pkg_info_builder(ud);
auto name = pkg_info.name;
rev.removed_pkg[name] = pkg_info;
}
for (auto ld : r.link_dists)
{
auto pkg_info = pkg_info_builder(ld);
auto name = pkg_info.name;
rev.installed_pkg[name] = pkg_info;
}
revisions.push_back(rev);
}
}
}

std::map<std::string, specs::PackageInfo> removed_pkg_diff;
std::map<std::string, specs::PackageInfo> installed_pkg_diff;

auto handle_install =
[&installed_pkg_diff, &removed_pkg_diff](revision& rev, const std::string& pkg_name)
{
bool res = false;
if (auto rev_iter = rev.installed_pkg.find(pkg_name);
rev_iter != rev.installed_pkg.end())
{
auto version = rev.installed_pkg[pkg_name].version;
auto iter = removed_pkg_diff.find(pkg_name);
if (iter != removed_pkg_diff.end() && iter->second.version == version)
{
removed_pkg_diff.erase(iter);

Check warning on line 1108 in libmamba/src/api/install.cpp

View check run for this annotation

Codecov / codecov/patch

libmamba/src/api/install.cpp#L1108

Added line #L1108 was not covered by tests
}
else
{
installed_pkg_diff[pkg_name] = rev_iter->second;
}
rev.installed_pkg.erase(rev_iter);
res = true;
}
return res;
};

auto handle_remove =
[&installed_pkg_diff, &removed_pkg_diff](revision& rev, const std::string& pkg_name)
{
bool res = false;
if (auto rev_iter = rev.removed_pkg.find(pkg_name); rev_iter != rev.removed_pkg.end())
{
auto version = rev.removed_pkg[pkg_name].version;
auto iter = installed_pkg_diff.find(pkg_name);
if (iter != installed_pkg_diff.end() && iter->second.version == version)
{
installed_pkg_diff.erase(iter);
}
else
{
removed_pkg_diff[pkg_name] = rev_iter->second;

Check warning on line 1134 in libmamba/src/api/install.cpp

View check run for this annotation

Codecov / codecov/patch

libmamba/src/api/install.cpp#L1134

Added line #L1134 was not covered by tests
}
rev.removed_pkg.erase(rev_iter);
res = true;
}
return res;
};

while (!revisions.empty())
{
auto& revision = *(revisions.begin());
while (!revision.removed_pkg.empty())
{
auto [pkg_name, pkg_info] = *(revision.removed_pkg.begin());
removed_pkg_diff[pkg_name] = pkg_info;
revision.removed_pkg.erase(pkg_name);
bool lastly_removed = true; // whether last operation on package was a removal
lastly_removed = !handle_install(revision, pkg_name);
for (auto rev = ++revisions.begin(); rev != revisions.end(); ++rev)
{
if (lastly_removed)
{
lastly_removed = !handle_install(*rev, pkg_name);
}
else
{
lastly_removed = handle_remove(*rev, pkg_name);

Check warning on line 1160 in libmamba/src/api/install.cpp

View check run for this annotation

Codecov / codecov/patch

libmamba/src/api/install.cpp#L1160

Added line #L1160 was not covered by tests
if (lastly_removed)
{
lastly_removed = !handle_install(*rev, pkg_name);

Check warning on line 1163 in libmamba/src/api/install.cpp

View check run for this annotation

Codecov / codecov/patch

libmamba/src/api/install.cpp#L1163

Added line #L1163 was not covered by tests
}
}
}
}
while (!revision.installed_pkg.empty())
{
auto [pkg_name, pkg_info] = *(revision.installed_pkg.begin());
installed_pkg_diff[pkg_name] = pkg_info;
revision.installed_pkg.erase(pkg_name);
bool lastly_removed = false;
for (auto rev = ++revisions.begin(); rev != revisions.end(); ++rev)
{
if (!lastly_removed)
{
lastly_removed = handle_remove(*rev, pkg_name);
if (lastly_removed)
{
lastly_removed = !handle_install(*rev, pkg_name);
}
}
}
}
revisions.erase(revisions.begin());
}
return { removed_pkg_diff, installed_pkg_diff };
}
} // detail
} // mamba
4 changes: 2 additions & 2 deletions libmamba/src/core/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
{
if (res.size() > 0)
{
res[res.size() - 1].diff.insert(line);
res[res.size() - 1].diff.push_back(line);
}
else
{
res.push_back(ParseResult());
res[0].diff.insert(line);
res[0].diff.push_back(line);

Check warning on line 89 in libmamba/src/core/history.cpp

View check run for this annotation

Codecov / codecov/patch

libmamba/src/core/history.cpp#L89

Added line #L89 was not covered by tests
}
}
}
Expand Down
96 changes: 73 additions & 23 deletions libmamba/tests/data/history/parse/conda-meta/aux_file
Original file line number Diff line number Diff line change
@@ -1,24 +1,74 @@
==> 2020-05-03 11:18:43 <==
# cmd: /usr/bin/conda create -n mamba
# conda version: 4.8.3
==> 2020-05-03 11:19:42 <==
# cmd: /usr/bin/conda install pybind11 libsolv libarchive libcurl nlohmann_json pip cpp-tabulate -c conda-forge
# conda version: 4.8.3
+conda-forge/linux-64::_libgcc_mutex-0.1-conda_forge
# update specs: ["cpp-tabulate", "libsolv", "nlohmann_json", "pybind11", "pip", "libarchive", "libcurl"]
==> 2020-05-04 08:58:01 <==
# cmd: /home/mariana/.local/bin/mamba remove cpp-tabulate pybind11 pip
# conda version: 4.8.3
-conda-forge/noarch::wheel-0.34.2-py_1
# remove specs: ["cpp-tabulate", "pybind11", "pip"]
==> 2020-05-04 08:59:15 <==
# cmd: /home/mariana/.local/bin/mamba install -c conda-forge cpp-tabulate==1.0.0
# conda version: 4.8.3
+conda-forge/linux-64::cpp-tabulate-1.0-hc9558a2_0
# update specs: ["cpp-tabulate==1.0.0"]
==> 2020-05-04 09:00:02 <==
# cmd: /home/mariana/.local/bin/mamba upgrade -c conda-forge cpp-tabulate
# conda version: 4.8.3
-conda-forge/linux-64::cpp-tabulate-1.0-hc9558a2_0
+conda-forge/linux-64::cpp-tabulate-1.2-hc9558a2_0
==> 2025-04-14 09:23:10 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba install nlohmann_json
# conda version: 3.8.0
+conda-forge::nlohmann_json-3.12.0-h3f2d84a_0
# update specs: ["nlohmann_json"]
==> 2025-04-14 09:23:50 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba install xtl=0.7.2
# conda version: 3.8.0
+conda-forge::libgomp-14.2.0-h767d61c_2
+conda-forge::_libgcc_mutex-0.1-conda_forge
+conda-forge::_openmp_mutex-4.5-2_gnu
+conda-forge::libgcc-14.2.0-h767d61c_2
+conda-forge::libstdcxx-14.2.0-h8f9b012_2
+conda-forge::libgcc-ng-14.2.0-h69a702a_2
+conda-forge::libstdcxx-ng-14.2.0-h4852527_2
+conda-forge::xtl-0.7.2-h4bd325d_1
# update specs: ["xtl=0.7.2"]
==> 2025-04-14 09:24:22 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba install cpp-tabulate=1.0
# conda version: 3.8.0
+conda-forge::cpp-tabulate-1.0-hc9558a2_0
# update specs: ["cpp-tabulate=1.0"]
==> 2025-04-14 09:24:49 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba update cpp-tabulate
# conda version: 3.8.0
-https://conda.anaconda.org/conda-forge/linux-64::cpp-tabulate-1.0-hc9558a2_0
+conda-forge::cpp-tabulate-1.5-hf52228f_1
# update specs: ["cpp-tabulate"]
# remove specs: ["cpp-tabulate"]
==> 2025-04-14 09:25:49 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba install wheel=0.34.2 openssl=3.5.0
# conda version: 3.8.0
+conda-forge::libexpat-2.7.0-h5888daf_0
+conda-forge::liblzma-5.8.1-hb9d3cd8_0
+conda-forge::libmpdec-4.0.0-h4bc722e_0
+conda-forge::libuuid-2.38.1-h0b41bf4_0
+conda-forge::bzip2-1.0.8-h4bc722e_7
+conda-forge::ld_impl_linux-64-2.43-h712a8e2_4
+conda-forge::libffi-3.4.6-h2dba641_1
+conda-forge::libzlib-1.3.1-hb9d3cd8_2
+conda-forge::ncurses-6.5-h2d0b736_3
+conda-forge::python_abi-3.13-6_cp313
+conda-forge::ca-certificates-2025.1.31-hbcca054_0
+conda-forge::tk-8.6.13-noxft_h4845f30_101
+conda-forge::libsqlite-3.49.1-hee588c1_2
+conda-forge::readline-8.2-h8c095d6_2
+conda-forge::openssl-3.5.0-h7b32b05_0
+conda-forge::tzdata-2025b-h78e105d_0
+conda-forge::python-3.13.3-hf636f53_100_cp313
+conda-forge::pip-25.0.1-pyh145f28c_0
+conda-forge::setuptools-78.1.0-pyhff2d567_0
+conda-forge::wheel-0.34.2-py_1
# update specs: ["wheel=0.34.2", "openssl=3.5.0"]
==> 2025-04-14 09:26:26 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba update wheel
# conda version: 3.8.0
-https://conda.anaconda.org/conda-forge/noarch::setuptools-78.1.0-pyhff2d567_0
-https://conda.anaconda.org/conda-forge/noarch::wheel-0.34.2-py_1
+conda-forge::wheel-0.45.1-pyhd8ed1ab_1
# update specs: ["wheel"]
# remove specs: ["wheel"]
==> 2025-04-14 09:27:06 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba remove wheel xtl nlohmann_json
# conda version: 3.8.0
-https://conda.anaconda.org/conda-forge/linux-64::nlohmann_json-3.12.0-h3f2d84a_0
-https://conda.anaconda.org/conda-forge/noarch::wheel-0.45.1-pyhd8ed1ab_1
-https://conda.anaconda.org/conda-forge/linux-64::xtl-0.7.2-h4bd325d_1
# remove specs: ["wheel", "xtl", "nlohmann_json"]
==> 2025-04-14 09:27:41 <==
# cmd: /home/sandrinepataut/.local/bin/micromamba install wheel=0.40.0 xtl=0.8.0
# conda version: 3.8.0
+conda-forge::xtl-0.8.0-h84d6215_0
+conda-forge::wheel-0.40.0-pyhd8ed1ab_1
# update specs: ["wheel=0.40.0", "xtl=0.8.0"]
Loading
Loading