Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade git-stein to v0.5.0 #141

Merged
merged 4 commits into from
Apr 18, 2020
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
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ dependencies {


// Use JUnit test framework
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13'

// https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit
compile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.4.0.201906121030-r'
compile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.7.0.202003110725-r'

// https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
Expand All @@ -47,19 +47,19 @@ dependencies {
compile group: 'commons-codec', name: 'commons-codec', version: '1.12'

// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'

// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

// https://github.com/sh5i/git-stein/
compile group: 'com.github.sh5i', name: 'git-stein', version: 'v0.5.0'

// https://mvnrepository.com/artifact/org.assertj/assertj-core
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.12.2'

// https://mvnrepository.com/artifact/com.github.stefanbirkner/system-rules
testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'

// https://github.com/sh5i/git-stein/
compile 'com.github.sh5i:git-stein:0.1.0'
}

jar {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/finergit/FinerGitConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class FinerGitConfig {
private boolean isMethodTokenIncluded = true;
private boolean isCheckCommit = false;
private boolean isParallel = true;
private int nthreads = 0; // 0: number of processors - 1
private boolean isPeripheralFileGenerated = false;
private boolean isClassFileGenerated = false;
private boolean isMethodFileGenerated = true;
Expand Down Expand Up @@ -209,6 +210,18 @@ public void setParallel(final String flag) {
this.isParallel = getBooleanValue(flag, errorMessage);
}

// ===== "--nthreads" =====

public int getNumberOfThreads() {
return this.nthreads;
}

@Option(name = "--nthreads", metaVar = "<num>", usage = "number of threads used for --parallel")
public void setNumberOfThreads(final String nthreads) {
final String errorMessage = "\"--nthreads\" option can take only an integer";
this.nthreads = getIntValue(nthreads, errorMessage);
}

// ===== "--max-file-name-length" =====

public int getMaxFileNameLength() {
Expand Down Expand Up @@ -342,4 +355,14 @@ private boolean getBooleanValue(final String flag, final String message) {
}
return false;
}

private int getIntValue(final String stringValue, final String message) {
try {
return Integer.valueOf(stringValue);
} catch (final NumberFormatException e) {
System.err.println(message);
exit(0);
return 0;
}
}
}
38 changes: 19 additions & 19 deletions src/main/java/finergit/FinerGitRewriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import finergit.ast.FinerJavaFileBuilder;
import finergit.ast.FinerJavaModule;
import finergit.util.RevCommitUtil;
import jp.ac.titech.c.se.stein.core.ConcurrentRepositoryRewriter;
import jp.ac.titech.c.se.stein.core.Context;
import jp.ac.titech.c.se.stein.core.EntrySet;
import jp.ac.titech.c.se.stein.core.EntrySet.Entry;
import jp.ac.titech.c.se.stein.core.EntrySet.EntryList;
import jp.ac.titech.c.se.stein.core.RepositoryRewriter;

public class FinerGitRewriter extends ConcurrentRepositoryRewriter {
public class FinerGitRewriter extends RepositoryRewriter {

private static final Logger log = LoggerFactory.getLogger(FinerGitRewriter.class);

Expand All @@ -25,46 +25,46 @@ public class FinerGitRewriter extends ConcurrentRepositoryRewriter {
public FinerGitRewriter(final FinerGitConfig config) {
this.config = config;
this.builder = new FinerJavaFileBuilder(config);
setConcurrent(config.isParallel());
setPathSensitive(true);
this.nthreads = config.isParallel() ? config.getNumberOfThreads() : 1;
this.isPathSensitive = true;
}

@Override
protected String rewriteCommitMessage(final String message, final RevCommit commit) {
return "<OriginalCommitID:" + RevCommitUtil.getAbbreviatedID(commit) + "> " + message;
protected String rewriteCommitMessage(final String message, final Context c) {
return "<OriginalCommitID:" + RevCommitUtil.getAbbreviatedID(c.getCommit()) + "> " + message;
}

@Override
public EntrySet rewriteEntry(final Entry entry) {
public EntrySet rewriteEntry(final Entry entry, final Context c) {
if (entry.isTree()) {
return super.rewriteEntry(entry);
return super.rewriteEntry(entry, c);
}

// Treats non-java files
if (!entry.name.endsWith(".java")) {
return config.isOtherFilesIncluded() ? super.rewriteEntry(entry) : Entry.EMPTY;
return config.isOtherFilesIncluded() ? super.rewriteEntry(entry, c) : Entry.EMPTY;
}

// Convert to finer modules
final EntryList result = new EntryList();
if (config.isOriginalJavaIncluded()) {
log.debug("Keep original file: {}", entry);
log.debug("Keep original file: {} {}", entry, c);
result.add(entry);
}
for (final FinerJavaModule m : extractFinerModules(entry)) {
for (final FinerJavaModule m : extractFinerModules(entry, c)) {
final String finerSource = // 最終行に改行を入れないと途中行とのマッチングが正しく行われない
String.join(System.lineSeparator(), m.getLines()) + System.lineSeparator();
final ObjectId newId = writeBlob(finerSource.getBytes(StandardCharsets.UTF_8));
final ObjectId newId = target.writeBlob(finerSource.getBytes(StandardCharsets.UTF_8), c);
final String name = m.getFileName();
log.debug("Generate finer module: {} -> {} {}", entry, name, newId.name());
result.add(new Entry(entry.mode, name, newId, entry.pathContext));
log.debug("Generate finer module: {} -> {} {} {}", entry, name, newId.name(), c);
result.add(new Entry(entry.mode, name, newId, entry.directory));
}
return result;
}

protected List<FinerJavaModule> extractFinerModules(final Entry entry) {
final String base = entry.pathContext + "/" + entry.name;
final String source = new String(readBlob(entry.id), StandardCharsets.UTF_8);
return builder.getFinerJavaModules(base, source);
protected List<FinerJavaModule> extractFinerModules(final Entry entry, final Context c) {
final String base = entry.directory + "/" + entry.name;
final String body = new String(source.readBlob(entry.id, c), StandardCharsets.UTF_8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パスの区切り文字が"/"決め打ちになっていますが,ここってjava.nio.Pathを使った方が安全じゃないでしょうか?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このパスはGitリポジトリ中のファイルの位置を示すもので、実行環境のOSにおけるファイルシステムに依存していません(チェックアウトしてファイルシステム上に展開することなく扱いますので)。Gitにおいては"/"が唯一のパス区切り文字で、むしろ"\"をファイル名に含められたりします。よって、本来は、ファイルシステム依存性を気にする必要はないはずです。

ただし、このbaseは後にすぐPathに入れられてFinerJavaFileBuilderに渡ります。Pathの機能でディレクトリ名やファイル名を取り出したりするためです。

final JavaFileVisitor visitor = new JavaFileVisitor(Paths.get(path), this.config);

よって、結局はPathになるので、この時点でPathにしてしまっても差し支えはありません。その場合、FinerJavaFileBuilder#getFinerJavaModulesがPathを受けるようにするべきですか?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows環境でもちゃんと動くのであれば,このままで結構です.

return builder.getFinerJavaModules(base, body);
}
}
5 changes: 3 additions & 2 deletions src/main/java/finergit/FinerRepoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.attribute.BasicFileAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jp.ac.titech.c.se.stein.core.Context;

/**
* Gitリポジトリから細粒度リポジトリの生成処理を行うクラス
Expand All @@ -35,8 +36,8 @@ public GitRepo exec() {
repo.setIgnoreCase(false);

final FinerGitRewriter rewriter = new FinerGitRewriter(config);
rewriter.initialize(repo.getRepository());
rewriter.rewrite();
rewriter.initialize(repo.getRepository(), repo.getRepository());
rewriter.rewrite(Context.init());

// clean up working copy
final boolean resetSucceeded = repo.resetHard();
Expand Down