Skip to content

Commit ced5378

Browse files
committed
Keep branches with unpushed commits as they are
This is the best definition I could come up with for when I feel pkg should not touch my branch.
1 parent 81b1351 commit ced5378

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

include/pkg/load_deps.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace pkg {
66

77
void load_deps(boost::filesystem::path const& repo,
88
boost::filesystem::path const& deps_root, bool clone_https,
9-
bool force, bool recursive);
9+
bool force, bool recursive, const bool drop_wip);
1010

1111
} // namespace pkg

src/load_deps.cc

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace fs = boost::filesystem;
2727
namespace pkg {
2828

2929
void load_deps(fs::path const& repo, fs::path const& deps_root,
30-
bool const clone_https, bool const force, bool const recursive) {
30+
bool const clone_https, bool const force, bool const recursive,
31+
bool const drop_wip) {
3132
if (!boost::filesystem::is_directory(deps_root)) {
3233
boost::filesystem::create_directories(deps_root);
3334
}
@@ -137,6 +138,22 @@ void load_deps(fs::path const& repo, fs::path const& deps_root,
137138
}
138139
executor ex;
139140
try {
141+
if (!drop_wip) {
142+
auto const remote_refs =
143+
ex.exec(d->path_,
144+
"git branch -r --contains HEAD --format '%(refname)'")
145+
.out_;
146+
auto const num_refs = std::ranges::count(remote_refs, '\n');
147+
if (num_refs == 0) {
148+
fmt::println(
149+
"warning: {} has commits that have not been pushed yet. "
150+
"Use --drop-wip to check out the commit from .pkg "
151+
"nevertheless.",
152+
d->path_.string());
153+
continue;
154+
}
155+
}
156+
140157
git_attach(ex, d, force);
141158
fmt::print("{}: checkout {}\n", d->name(), git_shorten(d, d->commit_));
142159
repeat = true;

src/main.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ int main(int argc, char** argv) {
2121
printf(
2222
"Usage:\n"
2323
" pkg load | -l [clone dependencies]\n"
24-
" -h for https (default: ssh);\n"
25-
" -f for hard reset (default: checkout)]\n"
26-
" -r for recursive (default: false)]\n"
24+
" -h for https (default: ssh);\n"
25+
" -f for hard reset (default: checkout)]\n"
26+
" -r for recursive (default: false)]\n"
27+
" --drop-wip check out ref from .pkg even though there are\n"
28+
" unpushed changes (default: false)\n"
2729
" pkg status | -s [print status]\n"
2830
"\n"
2931
"Common flags - apply everywhere:\n"
@@ -38,7 +40,8 @@ int main(int argc, char** argv) {
3840
load_deps(fs::path{"."}, fs::path("deps"), //
3941
has_flag(argc - 1, argv + 1, "-h"), //
4042
has_flag(argc - 1, argv + 1, "-f"), //
41-
has_flag(argc - 1, argv + 1, "-r"));
43+
has_flag(argc - 1, argv + 1, "-r"), //
44+
has_flag(argc - 1, argv + 1, "--drop-wip"));
4245
} else if (mode == "status" || mode == "-s") {
4346
print_status(fs::path{"."}, fs::path("deps"));
4447
} else {

0 commit comments

Comments
 (0)