Skip to content

Commit

Permalink
Use applyingChangesProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Dec 2, 2024
1 parent bb04f7c commit 3249bc7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.function.Function;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -51,7 +52,7 @@
import com.linecorp.centraldogma.server.command.CommitResult;
import com.linecorp.centraldogma.server.storage.StorageException;

abstract class AbstractCommitExecutor {
final class CommitExecutor {

final GitRepository gitRepository;
private final long commitTimeMillis;
Expand All @@ -61,8 +62,8 @@ abstract class AbstractCommitExecutor {
private final Markup markup;
private final boolean allowEmptyCommit;

AbstractCommitExecutor(GitRepository gitRepository, long commitTimeMillis, Author author,
String summary, String detail, Markup markup, boolean allowEmptyCommit) {
CommitExecutor(GitRepository gitRepository, long commitTimeMillis, Author author,
String summary, String detail, Markup markup, boolean allowEmptyCommit) {
this.gitRepository = gitRepository;
this.commitTimeMillis = commitTimeMillis;
this.author = author;
Expand All @@ -80,7 +81,12 @@ String summary() {
return summary;
}

CommitResult execute(Revision baseRevision) {
void executeInitialCommit(Iterable<Change<?>> changes) {
commit(null, Revision.INIT, changes);
}

CommitResult execute(Revision baseRevision,
Function<Revision, Iterable<Change<?>>> applyingChangesProvider) {
final RevisionAndEntries res;
final Iterable<Change<?>> applyingChanges;
gitRepository.writeLock();
Expand All @@ -93,7 +99,7 @@ CommitResult execute(Revision baseRevision) {
" or equivalent)");
}

applyingChanges = getOrCreateApplyingChanges(normBaseRevision);
applyingChanges = applyingChangesProvider.apply(normBaseRevision);
res = commit(headRevision, headRevision.forward(1), applyingChanges);

gitRepository.setHeadRevision(res.revision);
Expand All @@ -106,8 +112,6 @@ CommitResult execute(Revision baseRevision) {
return CommitResult.of(res.revision, applyingChanges);
}

abstract Iterable<Change<?>> getOrCreateApplyingChanges(Revision normBaseRevision);

RevisionAndEntries commit(@Nullable Revision prevRevision, Revision nextRevision,
Iterable<Change<?>> changes) {
requireNonNull(nextRevision, "nextRevision");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -246,9 +247,9 @@ class GitRepository implements Repository {
// Initialize the commit ID database.
commitIdDatabase = new CommitIdDatabase(jGitRepository);

new DefaultCommitExecutor(this, creationTimeMillis, author, "Create a new repository", "",
Markup.PLAINTEXT, true, false, Collections.emptyList())
.executeInitialCommit();
new CommitExecutor(this, creationTimeMillis, author, "Create a new repository", "",
Markup.PLAINTEXT, true)
.executeInitialCommit(Collections.emptyList());

headRevision = Revision.INIT;
success = true;
Expand Down Expand Up @@ -853,10 +854,14 @@ public CompletableFuture<CommitResult> commit(
requireNonNull(detail, "detail");
requireNonNull(markup, "markup");
requireNonNull(changes, "changes");
final DefaultCommitExecutor commitExecutor =
new DefaultCommitExecutor(this, commitTimeMillis, author, summary, detail,
markup, false, directExecution, changes);
return commit(baseRevision, commitExecutor);
final CommitExecutor commitExecutor =
new CommitExecutor(this, commitTimeMillis, author, summary, detail, markup, false);
return commit(baseRevision, commitExecutor, normBaseRevision -> {
if (!directExecution) {
return changes;
}
return blockingPreviewDiff(normBaseRevision, new DefaultChangesApplier(changes)).values();
});
}

@Override
Expand All @@ -869,19 +874,22 @@ public CompletableFuture<CommitResult> commit(Revision baseRevision, long commit
requireNonNull(detail, "detail");
requireNonNull(markup, "markup");
requireNonNull(transformer, "transformer");
final TransformingCommitExecutor commitExecutor =
new TransformingCommitExecutor(this, commitTimeMillis, author, summary,
detail, markup, transformer);
return commit(baseRevision, commitExecutor);
final CommitExecutor commitExecutor =
new CommitExecutor(this, commitTimeMillis, author, summary, detail, markup, false);
return commit(baseRevision, commitExecutor,
normBaseRevision -> blockingPreviewDiff(
normBaseRevision, new TransformingChangesApplier(transformer)).values());
}

private CompletableFuture<CommitResult> commit(Revision baseRevision,
AbstractCommitExecutor commitExecutor) {
private CompletableFuture<CommitResult> commit(
Revision baseRevision,
CommitExecutor commitExecutor,
Function<Revision, Iterable<Change<?>>> applyingChangesProvider) {
final ServiceRequestContext ctx = context();
return CompletableFuture.supplyAsync(() -> {
failFastIfTimedOut(this, logger, ctx, "commit", baseRevision,
commitExecutor.author(), commitExecutor.summary());
return commitExecutor.execute(baseRevision);
return commitExecutor.execute(baseRevision, applyingChangesProvider);
}, repositoryWorker);
}

Expand Down Expand Up @@ -1306,16 +1314,18 @@ public void cloneTo(File newRepoDir, BiConsumer<Integer, Integer> progressListen
diff(previousNonEmptyRevision, revision, ALL_PATH).join().values();

try {
new DefaultCommitExecutor(newRepo, c.when(), c.author(), c.summary(),
c.detail(), c.markup(), false, false, changes)
.execute(baseRevision);
new CommitExecutor(newRepo, c.when(), c.author(), c.summary(),
c.detail(), c.markup(), false)
.execute(baseRevision, normBaseRevision -> blockingPreviewDiff(
normBaseRevision, new DefaultChangesApplier(changes)).values());
previousNonEmptyRevision = revision;
} catch (RedundantChangeException e) {
// NB: We allow an empty commit here because an old version of Central Dogma had a bug
// which allowed the creation of an empty commit.
new DefaultCommitExecutor(newRepo, c.when(), c.author(), c.summary(),
c.detail(), c.markup(), true, false, changes)
.execute(baseRevision);
new CommitExecutor(newRepo, c.when(), c.author(), c.summary(),
c.detail(), c.markup(), true)
.execute(baseRevision, normBaseRevision -> blockingPreviewDiff(
normBaseRevision, new DefaultChangesApplier(changes)).values());
}

progressListener.accept(i, endRevision.major());
Expand Down

This file was deleted.

0 comments on commit 3249bc7

Please sign in to comment.