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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class MyTranslator implements BlobTranslator {
- `--stream-size-limit=<num>{,K,M,G}`: increase the stream size limit.
- `--no-notes`: Stop noting the source commit ID to the commits in the target repository.
- `--no-pack`: Stop packing objects after transformation finished.
- `--alternates`: Share source objects via Git alternates to skip writing unchanged objects. Significantly speeds up transformations where many objects are unchanged. The target repository will depend on the source's object store until repacked (`git repack -a -d`).
- `--alternates`: Share source objects via Git alternates to skip writing unchanged objects, which speeds up transformations where many objects are unchanged. The target repository will depend on the source's object store until repacked.
- `--no-composite`: Stop composing multiple blob translators.
- `--extra-attributes`: Allow opportunity to rewrite the encoding and the signature fields in commits.
- `--cache=<level>,...`: Specify the object types for caching (`commit`, `blob`, `tree`. See [Incremental transformation](#incremental-transformation) for the details). Default: none. `commit` is recommended.
Expand Down Expand Up @@ -266,6 +266,7 @@ Anonymizes filenames, blob content, commit messages, branch/tag names, and autho
Options:
- `--all`: Enable all anonymization options.
- `--tree`: Anonymize directory names.
- `--link`: Anonymize link names.
- `--blob`: Anonymize file names.
- `--content`: Anonymize file contents.
- `--message`: Anonymize commit/tag messages.
Expand Down
31 changes: 24 additions & 7 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 @@ -5,7 +5,9 @@

import jp.ac.titech.c.se.stein.entry.Entry;
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.entry.AnyColdEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.entry.TreeEntry;
import jp.ac.titech.c.se.stein.util.HashUtils;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -28,6 +30,9 @@ public class Anonymize extends RepositoryRewriter {
@Option(names = "--tree", description = "anonymize tree name")
protected boolean isTreeNameEnabled;

@Option(names = "--link", description = "anonymize link name")
protected boolean isLinkNameEnabled;

@Option(names = "--blob", description = "anonymize blob name")
protected boolean isBlobNameEnabled;

Expand All @@ -53,6 +58,7 @@ public class Anonymize extends RepositoryRewriter {
@Option(names = "--all", description = "anonymize all")
protected void setAllEnabled(boolean isEnabled) {
isTreeNameEnabled = isEnabled;
isLinkNameEnabled = isEnabled;
isBlobNameEnabled = isEnabled;
isBlobContentEnabled = isEnabled;
isMessageEnabled = isEnabled;
Expand Down Expand Up @@ -91,6 +97,8 @@ public String convert(final String name) {

private final NameMap treeNameMap = new NameMap("directory", "t");

private final NameMap linkNameMap = new NameMap("link", "l");

private final NameMap blobNameMap = new NameMap("file", "f");

private final NameMap branchNameMap = new NameMap("branch", "b");
Expand All @@ -105,7 +113,7 @@ public String rewriteMessage(final String message, final Context c) {
}

@Override
public AnyHotEntry rewriteBlobEntry(HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(BlobEntry entry, final Context c) {
if (isBlobContentEnabled) {
entry = entry.update(entry.getId().name());
}
Expand All @@ -116,12 +124,21 @@ public AnyHotEntry rewriteBlobEntry(HotEntry entry, final Context c) {
}

@Override
public String rewriteName(final String name, final Context c) {
final Entry entry = c.getEntry();
if (entry.isTree()) {
return isTreeNameEnabled ? treeNameMap.convert(name) : name;
protected AnyColdEntry rewriteTreeEntry(TreeEntry entry, Context c) {
final AnyColdEntry result = super.rewriteTreeEntry(entry, c);
if (isTreeNameEnabled && result instanceof Entry) {
final Entry e = (Entry) result;
return Entry.of(e.mode, treeNameMap.convert(e.name), e.id, e.directory);
}
return name;
return result;
}

@Override
protected AnyColdEntry rewriteLinkEntry(Entry entry, Context c) {
if (isLinkNameEnabled) {
return Entry.of(entry.mode, linkNameMap.convert(entry.name), entry.id, entry.directory);
}
return entry;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.core.Try;
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.rewriter.NameFilter;
Expand Down Expand Up @@ -77,7 +78,7 @@ protected String[] makeCommand(final String inputFile) {
}

@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
if (!filter.accept(entry)) {
return entry;
}
Expand All @@ -92,7 +93,7 @@ public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
}
}

protected HotEntry processCommandlineFilter(final HotEntry entry, final Context c) {
protected BlobEntry processCommandlineFilter(final BlobEntry entry, final Context c) {
try {
final Process proc = new ProcessBuilder()
.command(makeCommand(entry.getName()))
Expand Down Expand Up @@ -131,7 +132,7 @@ protected HotEntry processCommandlineFilter(final HotEntry entry, final Context
}
}

protected AnyHotEntry processCommandline(final HotEntry entry, final Context c) {
protected AnyHotEntry processCommandline(final BlobEntry entry, final Context c) {
try (final TemporaryFile tmp = TemporaryFile.directoryOf("_stein")) {
// write input
final Path inputPath = tmp.getPath().resolve(entry.getName());
Expand Down Expand Up @@ -163,7 +164,7 @@ protected AnyHotEntry processCommandline(final HotEntry entry, final Context c)
}
}

protected HotEntry processEndpoint(final HotEntry entry, final Context c) {
protected BlobEntry processEndpoint(final BlobEntry entry, final Context c) {
try {
final HttpURLConnection conn = (HttpURLConnection) options.endpoint.openConnection();
conn.setRequestMethod("POST");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/jp/ac/titech/c/se/stein/app/blob/Cregit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.rewriter.NameFilter;
Expand Down Expand Up @@ -81,7 +82,7 @@ protected void setLanguage(final String language) {
protected String language;

@Override
public AnyHotEntry rewriteBlobEntry(HotEntry entry, Context c) {
public AnyHotEntry rewriteBlobEntry(BlobEntry entry, Context c) {
if (!filter.accept(entry)) {
return entry;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jp.ac.titech.c.se.stein.app.blob;

import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.rewriter.NameFilter;
import lombok.ToString;
Expand Down Expand Up @@ -30,7 +30,7 @@ public class FilterBlob implements BlobTranslator {
protected long maxSize = -1L;

@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
// name
if (nameFilter.getPatterns() != null) {
if (!nameFilter.accept(entry)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.reflect.TypeToken;
import jp.ac.titech.c.se.stein.core.*;
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.rewriter.NameFilter;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class Historage implements BlobTranslator {
protected Set<String> moduleKinds;

@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
if (!filter.accept(entry)) {
return entry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.core.SourceText;
import jp.ac.titech.c.se.stein.core.SourceText.Fragment;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.rewriter.NameFilter;
Expand Down Expand Up @@ -86,7 +87,7 @@ public class HistorageViaJDT implements BlobTranslator {
protected boolean parsable = false;

@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
if (!JAVA.accept(entry)) {
return entry;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jp/ac/titech/c/se/stein/app/blob/Tokenize.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.core.SourceText;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import lombok.ToString;
import picocli.CommandLine.Command;
Expand All @@ -30,7 +30,7 @@ public class Tokenize implements BlobTranslator {
));

@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
final String text = SourceText.of(entry.getBlob()).getContent();
return entry.update(encode(text));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.core.SourceText;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,7 +27,7 @@
@Command(name = "@tokenize-jdt", description = "Encode Java source files to linetoken format via JDT")
public class TokenizeViaJDT implements BlobTranslator {
@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
if (!HistorageViaJDT.JAVA.accept(entry)) {
return entry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.entry.AnyHotEntry;
import jp.ac.titech.c.se.stein.core.SourceText;
import jp.ac.titech.c.se.stein.entry.HotEntry;
import jp.ac.titech.c.se.stein.entry.BlobEntry;
import jp.ac.titech.c.se.stein.rewriter.BlobTranslator;
import jp.ac.titech.c.se.stein.rewriter.NameFilter;
import lombok.ToString;
Expand All @@ -23,7 +23,7 @@ public class Untokenize implements BlobTranslator {
@Mixin
protected final NameFilter filter = new NameFilter();
@Override
public AnyHotEntry rewriteBlobEntry(final HotEntry entry, final Context c) {
public AnyHotEntry rewriteBlobEntry(final BlobEntry entry, final Context c) {
if (!filter.accept(entry)) {
return entry;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/jp/ac/titech/c/se/stein/entry/AnyColdEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface AnyColdEntry extends Serializable {
* Normalizes this entry. A {@link Set} of size 0 becomes {@link Empty},
* size 1 is unwrapped to its sole {@link Entry}, and others remain as-is.
*/
default AnyColdEntry pack() {
default AnyColdEntry normalize() {
return this;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ static Empty empty() {

/**
* A collection of multiple {@link Entry} instances.
* Use {@link #pack()} to normalize after construction.
* Use {@link #normalize()} to normalize after construction.
*/
@NoArgsConstructor
@EqualsAndHashCode
Expand Down Expand Up @@ -101,7 +101,7 @@ public String toString() {
}

@Override
public AnyColdEntry pack() {
public AnyColdEntry normalize() {
switch (size()) {
case 0:
return empty();
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/jp/ac/titech/c/se/stein/entry/AnyHotEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.core.RepositoryAccess;
import lombok.Getter;
import org.eclipse.jgit.lib.ObjectId;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -29,6 +30,20 @@ public interface AnyHotEntry {
*/
int size();

/**
* Returns the first entry as a {@link BlobEntry}.
*/
default BlobEntry asBlob() {
return (BlobEntry) stream().findFirst().orElseThrow();
}

/**
* Returns the first entry as a {@link TreeEntry}.
*/
default TreeEntry asTree() {
return (TreeEntry) stream().findFirst().orElseThrow();
}

/**
* Converts this Hot entry to a Cold entry by writing blob data to the target repository.
*/
Expand Down Expand Up @@ -98,8 +113,9 @@ public int size() {
public AnyColdEntry fold(RepositoryAccess target, Context c) {
return AnyColdEntry.set(stream()
.map(e -> e.fold(target, c))
.filter(e -> !e.getId().equals(ObjectId.zeroId()))
.collect(Collectors.toList()))
.pack();
.normalize();
}

@Override
Expand Down
Loading
Loading