Skip to content

Commit 968c6a8

Browse files
yamidarkeugenepeh
authored andcommitted
[#177] Add more default file types to analyze (#178)
RepoSense only tracks '.java' and '.adoc' file types for analyse. This may not be sufficient for visualization as not many other projects that are not mainly coded in Java or use asciidoctor for their documents. Let's include more file types by default in the analysis.
1 parent c247eeb commit 968c6a8

9 files changed

Lines changed: 64 additions & 12 deletions

File tree

src/functional/java/reposense/Entry.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
10+
import java.util.Arrays;
1011
import java.util.List;
1112
import java.util.stream.Collectors;
1213
import java.util.stream.Stream;
@@ -27,6 +28,7 @@
2728
public class Entry {
2829
private static final String FT_TEMP_DIR = "ft_temp";
2930
private static final String EXPECTED_FOLDER = "expected";
31+
private static final List<String> TESTING_FILE_FORMATS = Arrays.asList("java", "adoc");
3032

3133
@Before
3234
public void setUp() throws IOException {
@@ -61,6 +63,7 @@ private void generateReport(String inputDates) throws IOException, URISyntaxExce
6163

6264
CliArguments cliArguments = ArgsParser.parse(translateCommandline(input));
6365
List<RepoConfiguration> configs = CsvParser.parse(cliArguments.getConfigFilePath());
66+
RepoConfiguration.setFormatsToRepoConfigs(configs, TESTING_FILE_FORMATS);
6467
RepoConfiguration.setDatesToRepoConfigs(configs, cliArguments.getSinceDate(), cliArguments.getUntilDate());
6568

6669
ReportGenerator.generateReposReport(configs, FT_TEMP_DIR);

src/main/java/reposense/RepoSense.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package reposense;
22

33
import java.io.IOException;
4+
import java.util.Arrays;
45
import java.util.List;
56
import java.util.logging.Level;
67
import java.util.logging.Logger;
@@ -14,13 +15,17 @@
1415
import reposense.system.LogsManager;
1516

1617
public class RepoSense {
18+
public static final List<String> DEFAULT_FILE_FORMATS = Arrays.asList("java", "adoc", "js", "md", "css",
19+
"html", "cs", "json", "xml", "py", "fxml", "tag", "jsp", "gradle");
20+
1721
private static final Logger logger = LogsManager.getLogger(RepoSense.class);
1822

1923
public static void main(String[] args) {
2024
try {
2125
CliArguments cliArguments = ArgsParser.parse(args);
2226

2327
List<RepoConfiguration> configs = CsvParser.parse(cliArguments.getConfigFilePath());
28+
RepoConfiguration.setFormatsToRepoConfigs(configs, DEFAULT_FILE_FORMATS);
2429
RepoConfiguration.setDatesToRepoConfigs(configs, cliArguments.getSinceDate(), cliArguments.getUntilDate());
2530

2631
ReportGenerator.generateReposReport(

src/main/java/reposense/authorship/FileInfoExtractor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static void getAllFileInfo(
5858
getAllFileInfo(config, filePath, fileInfos);
5959
}
6060

61-
if (relativePath.endsWith(".java") || relativePath.endsWith(".adoc")) {
61+
if (isFileFormatInsideWhiteList(relativePath, config.getFileFormats())) {
6262
fileInfos.add(generateFileInfo(config.getRepoRoot(), relativePath.replace('\\', '/')));
6363
}
6464
}
@@ -92,4 +92,11 @@ public static FileInfo generateFileInfo(String repoRoot, String relativePath) {
9292
private static boolean shouldIgnore(String name, List<String> ignoreList) {
9393
return ignoreList.stream().anyMatch(name::contains);
9494
}
95+
96+
/**
97+
* Returns true if the {@code relativePath}'s file type is inside {@code fileFormatsWhiteList}.
98+
*/
99+
private static boolean isFileFormatInsideWhiteList(String relativePath, List<String> fileFormatsWhiteList) {
100+
return fileFormatsWhiteList.stream().anyMatch(fileFormat -> relativePath.endsWith("." + fileFormat));
101+
}
95102
}

src/main/java/reposense/commits/CommitInfoExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public static List<CommitInfo> extractCommitInfos(RepoConfiguration config) {
3232
* Returns the git log information for the repo for the date range in {@code config}.
3333
*/
3434
private static String getGitLogResult(RepoConfiguration config) {
35-
return CommandRunner.gitLog(config.getRepoRoot(), config.getSinceDate(), config.getUntilDate());
35+
return CommandRunner.gitLog(
36+
config.getRepoRoot(), config.getSinceDate(), config.getUntilDate(), config.getFileFormats());
3637
}
3738

3839
/**

src/main/java/reposense/model/RepoConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class RepoConfiguration {
2020
private Date untilDate;
2121

2222
private transient boolean needCheckStyle = false;
23+
private transient List<String> fileFormats;
2324
private transient int commitNum = 1;
2425
private transient List<String> ignoreDirectoryList = new ArrayList<>();
2526
private transient List<Author> authorList = new ArrayList<>();
@@ -42,6 +43,13 @@ public static void setDatesToRepoConfigs(
4243
}
4344
}
4445

46+
/**
47+
* Sets all {@code RepoConfiguration} in {@code configs} to have {@code fileFormats} set.
48+
*/
49+
public static void setFormatsToRepoConfigs(List<RepoConfiguration> configs, List<String> fileFormats) {
50+
configs.forEach(config -> config.setFileFormats(fileFormats));
51+
}
52+
4553
@Override
4654
public boolean equals(Object other) {
4755

@@ -165,6 +173,14 @@ public String getDisplayName() {
165173
return displayName;
166174
}
167175

176+
public List<String> getFileFormats() {
177+
return fileFormats;
178+
}
179+
180+
public void setFileFormats(List<String> fileFormats) {
181+
this.fileFormats = fileFormats;
182+
}
183+
168184
public void setAuthorDisplayName(Author author, String displayName) {
169185
authorDisplayNameMap.put(author, displayName);
170186
}

src/main/java/reposense/system/CommandRunner.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
import java.text.DateFormat;
88
import java.text.SimpleDateFormat;
99
import java.util.Date;
10+
import java.util.List;
1011

1112
import reposense.util.Constants;
1213

1314
public class CommandRunner {
14-
1515
private static final DateFormat GIT_LOG_SINCE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'00:00:00+08:00");
1616
private static final DateFormat GIT_LOG_UNTIL_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59+08:00");
1717

1818
private static boolean isWindows = isWindows();
1919

20-
public static String gitLog(String root, Date sinceDate, Date untilDate) {
20+
public static String gitLog(String root, Date sinceDate, Date untilDate, List<String> fileFormats) {
2121
Path rootPath = Paths.get(root);
2222

2323
String command = "git log --no-merges ";
24-
command += getGitDateRangeArgs(sinceDate, untilDate);
25-
command += " --pretty=format:\"%h|%aN|%ad|%s\" --date=iso --shortstat -- \"*.java\" -- \"*.adoc\"";
24+
command += convertToGitDateRangeArgs(sinceDate, untilDate);
25+
command += " --pretty=format:\"%h|%aN|%ad|%s\" --date=iso --shortstat";
26+
command += convertToGitFileFormatsArgs(fileFormats);
2627

2728
return runCommand(rootPath, command);
2829
}
@@ -59,7 +60,7 @@ public static String blameRaw(String root, String fileDirectory, Date sinceDate,
5960
Path rootPath = Paths.get(root);
6061

6162
String blameCommand = "git blame -w -C -C -M --line-porcelain";
62-
blameCommand += getGitDateRangeArgs(sinceDate, untilDate);
63+
blameCommand += convertToGitDateRangeArgs(sinceDate, untilDate);
6364
blameCommand += " " + addQuote(fileDirectory);
6465
blameCommand += getAuthorFilterCommand();
6566

@@ -139,7 +140,7 @@ private static String getAuthorFilterCommand() {
139140
/**
140141
* Returns the {@code String} command to specify the date range of commits to analyze for `git` commands.
141142
*/
142-
private static String getGitDateRangeArgs(Date sinceDate, Date untilDate) {
143+
private static String convertToGitDateRangeArgs(Date sinceDate, Date untilDate) {
143144
String gitDateRangeArgs = "";
144145

145146
if (sinceDate != null) {
@@ -151,4 +152,18 @@ private static String getGitDateRangeArgs(Date sinceDate, Date untilDate) {
151152

152153
return gitDateRangeArgs;
153154
}
155+
156+
/**
157+
* Returns the {@code String} command to specify the file formats to analyze for `git` commands.
158+
*/
159+
private static String convertToGitFileFormatsArgs(List<String> fileFormats) {
160+
StringBuilder gitFileFormatsArgsBuilder = new StringBuilder();
161+
162+
final String cmdFormat = " -- " + addQuote("*.%s");
163+
fileFormats.stream()
164+
.map(format -> String.format(cmdFormat, format))
165+
.forEach(gitFileFormatsArgsBuilder::append);
166+
167+
return gitFileFormatsArgsBuilder.toString();
168+
}
154169
}

src/test/java/reposense/authorship/FileInfoExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void extractFileInfosTest() {
2121
config.getAuthorAliasMap().put(TestConstants.FAKE_AUTHOR_NAME, new Author(TestConstants.FAKE_AUTHOR_NAME));
2222
GitChecker.checkout(config.getRepoRoot(), TestConstants.TEST_COMMIT_HASH);
2323
List<FileInfo> files = FileInfoExtractor.extractFileInfos(config);
24-
Assert.assertEquals(files.size(), 5);
24+
Assert.assertEquals(files.size(), 6);
2525
Assert.assertTrue(isFileExistence(Paths.get("annotationTest.java"), files));
2626
Assert.assertTrue(isFileExistence(Paths.get("blameTest.java"), files));
2727
Assert.assertTrue(isFileExistence(Paths.get("newPos/movedFile.java"), files));

src/test/java/reposense/system/CommandRunnerTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@ public void checkoutTest() {
3131

3232
@Test
3333
public void logWithContentTest() {
34-
String content = CommandRunner.gitLog(TestConstants.LOCAL_TEST_REPO_ADDRESS, null, null);
34+
String content = CommandRunner.gitLog(
35+
TestConstants.LOCAL_TEST_REPO_ADDRESS, null, null, config.getFileFormats());
3536
Assert.assertFalse(content.isEmpty());
3637
}
3738

3839
@Test
3940
public void logWithoutContentTest() {
4041
Date date = TestUtil.getDate(2050, Calendar.JANUARY, 1);
41-
String content = CommandRunner.gitLog(TestConstants.LOCAL_TEST_REPO_ADDRESS, date, null);
42+
String content = CommandRunner.gitLog(
43+
TestConstants.LOCAL_TEST_REPO_ADDRESS, date, null, config.getFileFormats());
4244
Assert.assertTrue(content.isEmpty());
4345

4446
date = TestUtil.getDate(1950, Calendar.JANUARY, 1);
45-
content = CommandRunner.gitLog(TestConstants.LOCAL_TEST_REPO_ADDRESS, null, date);
47+
content = CommandRunner.gitLog(
48+
TestConstants.LOCAL_TEST_REPO_ADDRESS, null, date, config.getFileFormats());
4649
Assert.assertTrue(content.isEmpty());
4750
}
4851

src/test/java/reposense/template/GitTestTemplate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Before;
1212
import org.junit.BeforeClass;
1313

14+
import reposense.RepoSense;
1415
import reposense.authorship.FileInfoAnalyzer;
1516
import reposense.authorship.FileInfoExtractor;
1617
import reposense.authorship.model.FileInfo;
@@ -32,6 +33,7 @@ public class GitTestTemplate {
3233
@Before
3334
public void before() {
3435
config = new RepoConfiguration(TEST_ORG, TEST_REPO, "master");
36+
config.setFileFormats(RepoSense.DEFAULT_FILE_FORMATS);
3537
}
3638

3739
@BeforeClass

0 commit comments

Comments
 (0)