Skip to content

Commit

Permalink
per project renamed file handling (oracle#2134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Kotal authored May 31, 2018
1 parent 38f7757 commit 0817a68
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 60 deletions.
2 changes: 1 addition & 1 deletion opengrok-web-nbproject/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bcel.version=6.2
bcel.jar=bcel-${bcel.version}.jar
file.reference.bcel.jar=../lib/${bcel.jar}
file.reference.json-simple-1.1.1.jar=../lib/json-simple-1.1.1.jar
j2ee.platform.classpath=${j2ee.server.home}/lib/annotations-api.jar:${j2ee.server.home}/lib/catalina-ant.jar:${j2ee.server.home}/lib/catalina-ha.jar:${j2ee.server.home}/lib/catalina-storeconfig.jar:${j2ee.server.home}/lib/catalina-tribes.jar:${j2ee.server.home}/lib/catalina.jar:${j2ee.server.home}/lib/ecj-4.6.3.jar:${j2ee.server.home}/lib/el-api.jar:${j2ee.server.home}/lib/jasper-el.jar:${j2ee.server.home}/lib/jasper.jar:${j2ee.server.home}/lib/jaspic-api.jar:${j2ee.server.home}/lib/jsp-api.jar:${j2ee.server.home}/lib/servlet-api.jar:${j2ee.server.home}/lib/tomcat-api.jar:${j2ee.server.home}/lib/tomcat-coyote.jar:${j2ee.server.home}/lib/tomcat-dbcp.jar:${j2ee.server.home}/lib/tomcat-i18n-es.jar:${j2ee.server.home}/lib/tomcat-i18n-fr.jar:${j2ee.server.home}/lib/tomcat-i18n-ja.jar:${j2ee.server.home}/lib/tomcat-jdbc.jar:${j2ee.server.home}/lib/tomcat-jni.jar:${j2ee.server.home}/lib/tomcat-util-scan.jar:${j2ee.server.home}/lib/tomcat-util.jar:${j2ee.server.home}/lib/tomcat-websocket.jar:${j2ee.server.home}/lib/websocket-api.jar
j2ee.platform.classpath=${j2ee.server.home}/lib/annotations-api.jar:${j2ee.server.home}/lib/catalina-ant.jar:${j2ee.server.home}/lib/catalina-ha.jar:${j2ee.server.home}/lib/catalina-storeconfig.jar:${j2ee.server.home}/lib/catalina-tribes.jar:${j2ee.server.home}/lib/catalina.jar:${j2ee.server.home}/lib/ecj-4.4.2.jar:${j2ee.server.home}/lib/el-api.jar:${j2ee.server.home}/lib/jasper-el.jar:${j2ee.server.home}/lib/jasper.jar:${j2ee.server.home}/lib/jsp-api.jar:${j2ee.server.home}/lib/servlet-api.jar:${j2ee.server.home}/lib/tomcat-api.jar:${j2ee.server.home}/lib/tomcat-coyote.jar:${j2ee.server.home}/lib/tomcat-dbcp.jar:${j2ee.server.home}/lib/tomcat-i18n-es.jar:${j2ee.server.home}/lib/tomcat-i18n-fr.jar:${j2ee.server.home}/lib/tomcat-i18n-ja.jar:${j2ee.server.home}/lib/tomcat-jdbc.jar:${j2ee.server.home}/lib/tomcat-jni.jar:${j2ee.server.home}/lib/tomcat-util-scan.jar:${j2ee.server.home}/lib/tomcat-util.jar:${j2ee.server.home}/lib/tomcat-websocket.jar:${j2ee.server.home}/lib/websocket-api.jar
lucene.version=7.3.1
lucene-core.jar=lucene-core-${lucene.version}.jar
lucene-analyzers-common.jar=lucene-analyzers-common-${lucene.version}.jar
Expand Down
30 changes: 27 additions & 3 deletions src/org/opensolaris/opengrok/configuration/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

/*
* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
*/
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.configuration;

import java.io.File;
Expand Down Expand Up @@ -68,6 +68,11 @@ public class Project implements Comparable<Project>, Nameable, Serializable {
*/
private boolean navigateWindowEnabled = false;

/**
* This flag sets per-project handling of renamed files.
*/
private Boolean handleRenamedFiles = null;

/**
* This marks the project as (not)ready before initial index is done. this
* is to avoid all/multi-project searches referencing this project from
Expand Down Expand Up @@ -229,6 +234,20 @@ public void setNavigateWindowEnabled(boolean navigateWindowEnabled) {
this.navigateWindowEnabled = navigateWindowEnabled;
}

/**
* @return true if this project handles renamed files.
*/
public boolean isHandleRenamedFiles() {
return handleRenamedFiles != null && handleRenamedFiles;
}

/**
* @param flag true if project should handle renamed files, false otherwise.
*/
public void setHandleRenamedFiles(boolean flag) {
this.handleRenamedFiles = flag;
}

/**
* Return groups where this project belongs
*
Expand Down Expand Up @@ -263,7 +282,7 @@ public void addGroup(Group group) {
final public void completeWithDefaults(Configuration cfg) {
Configuration defaultCfg = new Configuration();
/**
* Choosing strategy for properties (tabSize here):
* Choosing strategy for properties (tabSize used as example here):
* <pre>
* this cfg defaultCfg chosen value
* ===============================================
Expand All @@ -278,6 +297,11 @@ final public void completeWithDefaults(Configuration cfg) {
if (getTabSize() == defaultCfg.getTabSize()) {
setTabSize(cfg.getTabSize());
}

// Allow project to override global setting of renamed file handling.
if (handleRenamedFiles == null) {
setHandleRenamedFiles(cfg.isHandleHistoryOfRenamedFiles());
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/org/opensolaris/opengrok/configuration/RuntimeEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,18 @@ public boolean isHandleHistoryOfRenamedFiles() {
return threadConfig.get().isHandleHistoryOfRenamedFiles();
}

public boolean isHandleHistoryOfRenamedFilesForProject(Project project) {
if (hasProjects() && project != null) {
return project.isHandleRenamedFiles();
} else {
return isHandleHistoryOfRenamedFiles();
}
}

public boolean isHandleHistoryOfRenamedFilesFor(String path) {
return isHandleHistoryOfRenamedFilesForProject(Project.getProject(path));
}

public void setRevisionMessageCollapseThreshold(int threshold) {
threadConfig.get().setRevisionMessageCollapseThreshold(threshold);
}
Expand Down
8 changes: 5 additions & 3 deletions src/org/opensolaris/opengrok/history/FileHistoryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
*/

Expand Down Expand Up @@ -54,6 +54,7 @@
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.opensolaris.opengrok.configuration.Project;
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
import org.opensolaris.opengrok.logger.LoggerFactory;
import org.opensolaris.opengrok.util.ForbiddenSymlinkException;
Expand Down Expand Up @@ -406,6 +407,7 @@ private void finishStore(Repository repository, String latestRev) {
public void store(History history, Repository repository)
throws HistoryException {
final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
final boolean handleRenamedFiles = repository.isHandleRenamedFiles();

String latestRev = null;

Expand Down Expand Up @@ -475,7 +477,7 @@ public void store(History history, Repository repository)
int fileHistoryCount = 0;
for (Map.Entry<String, List<HistoryEntry>> map_entry : map.entrySet()) {
try {
if (env.isHandleHistoryOfRenamedFiles() &&
if (handleRenamedFiles &&
isRenamedFile(map_entry.getKey(), env, repository, history)) {
continue;
}
Expand All @@ -491,7 +493,7 @@ public void store(History history, Repository repository)

LOGGER.log(Level.FINE, "Stored history for {0} files", fileHistoryCount);

if (!env.isHandleHistoryOfRenamedFiles()) {
if (!handleRenamedFiles) {
finishStore(repository, latestRev);
return;
}
Expand Down
10 changes: 8 additions & 2 deletions src/org/opensolaris/opengrok/history/GitHistoryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
*/
package org.opensolaris.opengrok.history;
Expand Down Expand Up @@ -59,6 +59,12 @@ private enum ParseState {
private GitRepository repository = new GitRepository();
private List<HistoryEntry> entries = new ArrayList<>();

private final boolean handleRenamedFiles;

GitHistoryParser(boolean flag) {
handleRenamedFiles = flag;
}

/**
* Process the output from the log command and insert the HistoryEntries
* into the history field.
Expand Down Expand Up @@ -177,7 +183,7 @@ History parse(File file, Repository repos, String sinceRevision) throws HistoryE
status));
}

if (RuntimeEnvironment.getInstance().isHandleHistoryOfRenamedFiles()) {
if (handleRenamedFiles) {
executor = repository.getRenamedFilesExecutor(file, sinceRevision);
status = executor.exec(true, parser);

Expand Down
6 changes: 3 additions & 3 deletions src/org/opensolaris/opengrok/history/GitRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision)
cmd.add("--name-only");
cmd.add("--pretty=fuller");
cmd.add(GIT_DATE_OPT);

if (file.isFile() && RuntimeEnvironment.getInstance().isHandleHistoryOfRenamedFiles()) {
if (file.isFile() && isHandleRenamedFiles()) {
cmd.add("--follow");
}

Expand Down Expand Up @@ -481,7 +481,7 @@ History getHistory(File file) throws HistoryException {
History getHistory(File file, String sinceRevision)
throws HistoryException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
History result = new GitHistoryParser().parse(file, this, sinceRevision);
History result = new GitHistoryParser(isHandleRenamedFiles()).parse(file, this, sinceRevision);
// Assign tags to changesets they represent
// We don't need to check if this repository supports tags,
// because we know it :-)
Expand Down
8 changes: 4 additions & 4 deletions src/org/opensolaris/opengrok/history/MercurialRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
*/
package org.opensolaris.opengrok.history;
Expand Down Expand Up @@ -93,7 +93,7 @@ public class MercurialRepository extends Repository {
+ END_OF_ENTRY + "\\n";

/**
* Template for formatting hg log output for directories.
* Template for formatting {@code hg log} output for directories.
*/
private static final String DIR_TEMPLATE_RENAMED
= TEMPLATE_STUB + FILE_LIST
Expand All @@ -104,7 +104,7 @@ public class MercurialRepository extends Repository {

private static final Pattern LOG_COPIES_PATTERN
= Pattern.compile("^(\\d+):(.*)");

public MercurialRepository() {
type = "Mercurial";
datePatterns = new String[]{
Expand Down Expand Up @@ -188,7 +188,7 @@ Executor getHistoryLogExecutor(File file, String sinceRevision)

cmd.add("--template");
if (file.isDirectory()) {
cmd.add(env.isHandleHistoryOfRenamedFiles() ? DIR_TEMPLATE_RENAMED : DIR_TEMPLATE);
cmd.add(this.isHandleRenamedFiles() ? DIR_TEMPLATE_RENAMED : DIR_TEMPLATE);
} else {
cmd.add(FILE_TEMPLATE);
}
Expand Down
19 changes: 15 additions & 4 deletions src/org/opensolaris/opengrok/history/RepositoryInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class RepositoryInfo implements Serializable {
protected String branch;
protected String currentVersion;

private boolean handleRenamedFiles;

/**
* format used for printing the date in {@code currentVersion}
*/
Expand All @@ -89,6 +91,13 @@ public RepositoryInfo(RepositoryInfo orig) {
this.currentVersion = orig.currentVersion;
}

/**
* @return true if the repository handles renamed files, false otherwise.
*/
public boolean isHandleRenamedFiles() {
return this.handleRenamedFiles;
}

/**
* @return relative path to source root
*/
Expand All @@ -102,6 +111,9 @@ public String getDirectoryNameRelative() {
*/
public void setDirectoryNameRelative(String dir) {
this.directoryNameRelative = dir;

handleRenamedFiles = RuntimeEnvironment.getInstance().
isHandleHistoryOfRenamedFilesFor(dir);
}

/**
Expand All @@ -117,8 +129,7 @@ public String getDirectoryName() {
/**
* Specify the name of the root directory for this repository.
*
* @param dir the new name of the root directory. Can be absolute
* path or relative to source root.
* @param dir the new root directory
*/
public void setDirectoryName(File dir) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
Expand All @@ -141,9 +152,9 @@ public void setDirectoryName(File dir) {
}

if (path.startsWith(rootPath)) {
this.directoryNameRelative = path.substring(rootPath.length());
setDirectoryNameRelative(path.substring(rootPath.length()));
} else {
this.directoryNameRelative = path;
setDirectoryNameRelative(path);
}
}

Expand Down
68 changes: 34 additions & 34 deletions src/org/opensolaris/opengrok/index/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,40 @@ public void prepareIndexer(RuntimeEnvironment env,
throw new IndexerException("Internal error, zapCache shouldn't be null");
}

if (addProjects) {
File files[] = env.getSourceRootFile().listFiles();
Map<String,Project> projects = env.getProjects();

// Keep a copy of the old project list so that we can preserve
// the customization of existing projects.
Map<String, Project> oldProjects = new HashMap<>();
for (Project p : projects.values()) {
oldProjects.put(p.getName(), p);
}

projects.clear();

// Add a project for each top-level directory in source root.
for (File file : files) {
String name = file.getName();
String path = "/" + name;
if (oldProjects.containsKey(name)) {
// This is an existing object. Reuse the old project,
// possibly with customizations, instead of creating a
// new with default values.
Project p = oldProjects.get(name);
p.setPath(path);
p.setName(name);
p.completeWithDefaults(env.getConfiguration());
projects.put(name, p);
} else if (!name.startsWith(".") && file.isDirectory()) {
// Found a new directory with no matching project, so
// create a new project with default properties.
projects.put(name, new Project(name, path, env.getConfiguration()));
}
}
}

if (searchRepositories || listRepoPaths || !zapCache.isEmpty()) {
LOGGER.log(Level.INFO, "Scanning for repositories...");
long start = System.currentTimeMillis();
Expand Down Expand Up @@ -937,40 +971,6 @@ public void prepareIndexer(RuntimeEnvironment env,
}
}

if (addProjects) {
File files[] = env.getSourceRootFile().listFiles();
Map<String,Project> projects = env.getProjects();

// Keep a copy of the old project list so that we can preserve
// the customization of existing projects.
Map<String, Project> oldProjects = new HashMap<>();
for (Project p : projects.values()) {
oldProjects.put(p.getName(), p);
}

projects.clear();

// Add a project for each top-level directory in source root.
for (File file : files) {
String name = file.getName();
String path = "/" + name;
if (oldProjects.containsKey(name)) {
// This is an existing object. Reuse the old project,
// possibly with customizations, instead of creating a
// new with default values.
Project p = oldProjects.get(name);
p.setPath(path);
p.setName(name);
p.completeWithDefaults(env.getConfiguration());
projects.put(name, p);
} else if (!name.startsWith(".") && file.isDirectory()) {
// Found a new directory with no matching project, so
// create a new project with default properties.
projects.put(name, new Project(name, path, env.getConfiguration()));
}
}
}

if (defaultProjects != null && !defaultProjects.isEmpty()) {
Set<Project> projects = new TreeSet<>();
for (String projectPath : defaultProjects) {
Expand Down
12 changes: 7 additions & 5 deletions test/org/opensolaris/opengrok/configuration/ProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* CDDL HEADER END
*/

/*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
/*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.configuration;

Expand Down Expand Up @@ -142,12 +142,10 @@ public void testMergeProjects1() {
cfg.setTabSize(new Configuration().getTabSize() + 3731);

Project p1 = new Project();
p1.setNavigateWindowEnabled(true);


p1.completeWithDefaults(cfg);

assertNotNull(p1);
assertTrue("Navigate window should be turned on", p1.isNavigateWindowEnabled());
assertEquals(new Configuration().getTabSize() + 3731, p1.getTabSize());
}

Expand All @@ -161,10 +159,14 @@ public void testMergeProjects2() {

Project p1 = new Project();
p1.setTabSize(new Project().getTabSize() + 9737);
p1.setNavigateWindowEnabled(true);
p1.setHandleRenamedFiles(true);

p1.completeWithDefaults(cfg);

assertNotNull(p1);
assertTrue("Navigate window should be turned on", p1.isNavigateWindowEnabled());
assertTrue("Renamed file handling should be true", p1.isHandleRenamedFiles());
assertEquals(new Project().getTabSize() + 9737, p1.getTabSize());
}

Expand Down
Loading

0 comments on commit 0817a68

Please sign in to comment.