Skip to content

Commit e2b23c8

Browse files
committed
Correct detached head behavior
Commits made from a detached commit should have the non-HEAD commit as their parent.
1 parent 5e1abd4 commit e2b23c8

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

VersionControl/hero.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ void add(const std::vector<std::string>& files) {
287287
// This file will have its SHA256 as its filename, and will have formatting compatible with the format specified in commit-blob.txt
288288
void commit() {
289289
bool detached(false);
290+
std::string parent(getHeadHash());
291+
if (parent == "") {
292+
std::cerr << "Could not find repository head - have you run init?\n";
293+
exit(1);
294+
}
290295

291296
// Check for the commit lock
292297
if (std::ifstream lock = std::ifstream(".vcs/COMMIT_LOCK", std::ios::binary)) {
@@ -297,13 +302,16 @@ void commit() {
297302
std::getline(lock, hash);
298303

299304
std::cerr << "Warning: You are in detached head state.\n";
300-
std::cerr << "The head commit is " << getHeadHash() << ".\n";
305+
std::cerr << "The head commit is " << parent << ".\n";
301306
std::cerr << "The currently checked out commit is recorded as " << hash << ".\n";
302307
std::cerr << "Committing in this state will not update the HEAD marker, and consequently\n";
303308
std::cerr << " any commit you might make here will not appear in logs and will only be\n";
304309
std::cerr << " reachable if you know their hash.\n";
305310
std::cerr << "To avoid this, copy your work to another location, press Ctrl-C to stop commit,\n";
306311
std::cerr << " and run `checkout HEAD`. You can then copy your work back and commit.\n";
312+
313+
// Update parent hash to be the one we have checked out
314+
parent = hash;
307315
}
308316

309317
std::string title; // Commit title
@@ -313,11 +321,6 @@ void commit() {
313321
commit << "&&&\n";
314322

315323
// Write parent hash
316-
std::string parent(getHeadHash());
317-
if (parent=="") {
318-
std::cerr << "Could not find repository head - have you run init?\n";
319-
exit(1);
320-
}
321324
commit << "parent " << parent << "\n";
322325

323326
// Write datetime using date

0 commit comments

Comments
 (0)