Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 1 addition & 31 deletions src/main/java/jp/ac/titech/c/se/stein/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.app.Identity;
import jp.ac.titech.c.se.stein.rewriter.RewriterCommand;
import jp.ac.titech.c.se.stein.util.SettableHelpCommand;
Expand Down Expand Up @@ -292,7 +291,7 @@ public int execute(final ParseResult parseResult) throws ExecutionException, Par
.collect(Collectors.toList());
if (conf.useComposite) {
log.debug("Optimizing rewriters...");
commands = optimizeRewriters(commands);
commands = RewriterCommand.optimize(commands);
}
this.rewriters.addAll(prepareRewriters(commands));

Expand All @@ -307,35 +306,6 @@ public List<RepositoryRewriter> prepareRewriters(final List<RewriterCommand> com
return commands.stream().map(RewriterCommand::toRewriter).collect(Collectors.toList());
}

public List<RewriterCommand> optimizeRewriters(final List<RewriterCommand> commands) {
final List<RewriterCommand> result = new ArrayList<>();
final List<BlobTranslator> pending = new ArrayList<>();

for (final RewriterCommand cmd : commands) {
if (cmd instanceof BlobTranslator t) {
pending.add(t);
} else {
flushPendingTranslators(pending, result);
result.add(cmd);
}
}
flushPendingTranslators(pending, result);
return result;
}

private void flushPendingTranslators(final List<BlobTranslator> pending, final List<RewriterCommand> result) {
if (pending.isEmpty()) {
return;
}
if (pending.size() >= 2) {
log.info("Compose {} blob translators: {}", pending.size(), pending);
result.add(new BlobTranslator.Composite(pending));
} else {
result.add(new BlobTranslator.Single(pending.get(0)));
}
pending.clear();
}

/**
* Add all the command classes found in the given package as subcommands to the given commandline.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/jp/ac/titech/c/se/stein/app/Anonymize.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.jgit.lib.PersonIdent;

import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.rewriter.EntryResolver;
import jp.ac.titech.c.se.stein.rewriter.RepositoryRewriter;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -124,8 +125,8 @@ public AnyHotEntry rewriteBlobEntry(BlobEntry entry, final Context c) {
}

@Override
protected AnyColdEntry rewriteTreeEntry(TreeEntry entry, Context c) {
final AnyColdEntry result = super.rewriteTreeEntry(entry, c);
protected AnyColdEntry rewriteTreeEntry(TreeEntry entry, EntryResolver resolver, Context c) {
final AnyColdEntry result = super.rewriteTreeEntry(entry, resolver, c);
if (isTreeNameEnabled && result instanceof Entry e) {
return Entry.of(e.mode, treeNameMap.convert(e.name), e.id, e.directory);
}
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/jp/ac/titech/c/se/stein/app/commit/NoteCommit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jp.ac.titech.c.se.stein.app.commit;

import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.core.RepositoryAccess;
import jp.ac.titech.c.se.stein.rewriter.CommitTranslator;
import jp.ac.titech.c.se.stein.rewriter.RepositoryRewriter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,25 +13,31 @@
import java.nio.charset.StandardCharsets;

/**
* Prepends the original commit ID (from Git notes) to each commit message.
* If no note exists, the zero ID is used instead.
* Prepends the original commit ID to each commit message.
* If the source has notes (chained transformation), the original ID is read from the note.
* Otherwise, the current commit ID itself is used as the original.
*/
@Slf4j
@ToString
@Command(name = "@note-commit", description = "Note original commit id on each commit message")
public class NoteCommit extends RepositoryRewriter {
public class NoteCommit implements CommitTranslator {
@Option(names = "--length", paramLabel = "<num>", description = "length of SHA1 hash (default: ${DEFAULT-VALUE})")
protected int length = 40;

@Override
public String rewriteCommitMessage(final String message, final Context c) {
final ObjectId originalId = resolveOriginalId(c);
return originalId.name().substring(0, length) + " " + message;
}

private ObjectId resolveOriginalId(final Context c) {
final ObjectId current = c.getRev().getId();
final byte[] note = source.readNote(source.getDefaultNotes(), current);
final RepositoryAccess source = c.getRewriter().getSource();
final byte[] note = source.readNote(source.getNotes(RepositoryRewriter.R_NOTES_ORIG), current);
if (note != null && note.length == 40) {
// use the commit note for the original commit id
return new String(note, StandardCharsets.UTF_8).substring(0, length) + " " + message;
}
// no note or no valid note: use the zero id
return RepositoryRewriter.ZERO.name().substring(0, length) + " " + message;
return ObjectId.fromString(new String(note, StandardCharsets.UTF_8));
} else {
return current;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.eclipse.jgit.revwalk.RevCommit;

import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.rewriter.RepositoryRewriter;
import jp.ac.titech.c.se.stein.rewriter.CommitTranslator;
import jp.ac.titech.c.se.stein.core.Try;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand All @@ -29,7 +29,7 @@
@Slf4j
@ToString
@Command(name = "@svn-metadata", description = "Attach metadata obtained from svn2git")
public class SvnMetadata extends RepositoryRewriter {
public class SvnMetadata implements CommitTranslator {
@Option(names = "--svn-mapping", paramLabel = "<log-git-repository>", description = "svn mapping",
required = true)
protected Path svnMappingFile;
Expand All @@ -41,12 +41,12 @@ public class SvnMetadata extends RepositoryRewriter {
protected Map<ObjectId, Integer> mapping;

@Override
protected void setUp(final Context c) {
public void setUp(final Context c) {
mapping = Try.io(() -> collectCommitMapping(svnMappingFile, objectMappingFile));
}

@Override
protected String rewriteCommitMessage(final String message, final Context c) {
public String rewriteCommitMessage(final String message, final Context c) {
final RevCommit commit = c.getCommit();
final Integer svnId = mapping.get(commit.getId());
if (svnId != null) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/jp/ac/titech/c/se/stein/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Stream;

import jp.ac.titech.c.se.stein.Application;
import jp.ac.titech.c.se.stein.rewriter.RepositoryRewriter;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
Expand All @@ -31,7 +32,7 @@ public class Context implements Map<Context.Key, Object> {
* The keys that can be stored in a context.
*/
public enum Key {
commit, path, entry, rev, tag, ref, conf, inserter;
commit, path, entry, rev, tag, ref, conf, inserter, rewriter;

public static final Key[] ALL = Key.values();
public static final int SIZE = ALL.length;
Expand Down Expand Up @@ -230,4 +231,11 @@ public Application.Config getConfig() {
public ObjectInserter getInserter() {
return (ObjectInserter) get(Key.inserter);
}

/**
* Returns the rewriter, or {@code null} if not set.
*/
public RepositoryRewriter getRewriter() {
return (RepositoryRewriter) get(Key.rewriter);
}
}
18 changes: 11 additions & 7 deletions src/main/java/jp/ac/titech/c/se/stein/core/RepositoryAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public class RepositoryAccess implements AutoCloseable {

public final Repository repo;

@Getter
protected final NoteMap defaultNotes;
private final Map<String, NoteMap> notesCache = new HashMap<>();

protected boolean isDryRunning = false;

Expand All @@ -51,7 +50,6 @@ public void setDryRunning(final boolean isDryRunning) {

public RepositoryAccess(final Repository repo) {
this.repo = repo;
this.defaultNotes = readNotes();
}

@Override
Expand Down Expand Up @@ -388,7 +386,6 @@ public void writeNotes(final NoteMap notes, final String ref, final Context writ
final ObjectId commit = writeCommit(NO_PARENTS, treeId, ident, ident, message, writingContext);

applyRefUpdate(new RefEntry(ref, commit));

}

/**
Expand All @@ -403,10 +400,17 @@ public void forEachNote(final NoteMap notes, final BiConsumer<ObjectId, byte[]>
}

/**
* Reads notes from the default notes ref ({@code refs/notes/commits}).
* Returns the notes for the default ref ({@code refs/notes/commits}), reading lazily.
*/
public NoteMap getDefaultNotes() {
return getNotes(Constants.R_NOTES_COMMITS);
}

/**
* Returns the notes for the specified ref, reading lazily and caching the result.
*/
public NoteMap readNotes() {
return readNotes(Constants.R_NOTES_COMMITS);
public NoteMap getNotes(final String noteRef) {
return notesCache.computeIfAbsent(noteRef, this::readNotes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ static BlobTranslator of(Function<String, String> f) {
return (entry, c) -> entry.update(f.apply(entry.getContent()));
}

static BlobTranslator composite(BlobTranslator... translators) {
return new Composite(translators);
}

static BlobTranslator composite(List<BlobTranslator> translators) {
return new Composite(translators);
}

default RepositoryRewriter toRewriter() {
return new Single(this);
}
Expand All @@ -44,7 +52,7 @@ public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
}

@ToString
class Composite extends RepositoryRewriter {
class Composite implements BlobTranslator {
BlobTranslator[] translators;

public Composite(BlobTranslator... translators) {
Expand Down
Loading
Loading