Skip to content

Commit

Permalink
merge: Handle not having common ancesstors
Browse files Browse the repository at this point in the history
This is an important case in GitJournal.
  • Loading branch information
vHanda committed Aug 24, 2024
1 parent a46fdef commit 1aaeab5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
2 changes: 0 additions & 2 deletions lib/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ class GitMergeTooManyBases extends GitException {}

class GitMergeOnHashNotAllowed extends GitException {}

class GitMergeNoCommonAncestor extends GitException {}

class GitNotImplemented extends GitException {}

class GitRepoExists implements GitException {}
41 changes: 21 additions & 20 deletions lib/merge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ extension Merge on GitRepository {
if (bases.length > 1) {
throw GitMergeTooManyBases();
}
if (bases.isEmpty) {
throw GitMergeNoCommonAncestor();
}
var baseHash = bases.first.hash;
if (bases.isNotEmpty) {
var baseHash = bases.first.hash;

// up to date
if (baseHash == commitB.hash) {
return;
}
// up to date
if (baseHash == commitB.hash) {
return;
}

// fastforward
if (baseHash == headCommit.hash) {
var branchNameRef = headRef.target;
assert(branchNameRef.isBranch());
// fastforward
if (baseHash == headCommit.hash) {
var branchNameRef = headRef.target;
assert(branchNameRef.isBranch());

var newRef = HashReference(branchNameRef, commitB.hash);
refStorage.saveRef(newRef);
var newRef = HashReference(branchNameRef, commitB.hash);
refStorage.saveRef(newRef);

checkout('.');
return;
checkout('.');
return;
}
}

var baseTree = objStorage.readTree(bases.first.treeHash);
var baseTree =
bases.isNotEmpty ? objStorage.readTree(bases.first.treeHash) : null;
var headTree = objStorage.readTree(headCommit.treeHash);
var bTree = objStorage.readTree(commitB.treeHash);

Expand All @@ -79,13 +79,13 @@ extension Merge on GitRepository {
}

/// throws exceptions
GitHash _combineTrees(GitTree a, GitTree b, GitTree base) {
GitHash _combineTrees(GitTree a, GitTree b, GitTree? base) {
// Get all the paths
var names = a.entries.map((e) => e.name).toSet();
names.addAll(b.entries.map((e) => e.name));

var entries = <GitTreeEntry>[];
for (var baseEntry in base.entries) {
for (var baseEntry in base?.entries ?? <GitTreeEntry>[]) {
var name = baseEntry.name;
var aIndex = a.entries.indexWhere((e) => e.name == name);
var bIndex = b.entries.indexWhere((e) => e.name == name);
Expand Down Expand Up @@ -118,7 +118,8 @@ extension Merge on GitRepository {
var name = entry.name;

// If the entry was already in the base
var baseIndex = base.entries.indexWhere((e) => e.name == name);
var baseIndex =
base == null ? -1 : base.entries.indexWhere((e) => e.name == name);
if (baseIndex != -1) {
continue;
}
Expand Down

0 comments on commit 1aaeab5

Please sign in to comment.