Skip to content
Snippets Groups Projects
Commit cc905e7d authored by Jens Baumgart's avatar Jens Baumgart Committed by Jens
Browse files

Make Repository.getConfig aware of changed config


In the current implementation Repository reads user and repository 
config only at creation point of time.
The new implementatiopn checks in Repository.getConfig if user or 
repository config have changed on disk and reload the config if 
required. 

Change-Id: Ibd97515919ef66c6f8aa1a4fe8a11a6711335dad
Signed-off-by: default avatarJens Baumgart <jens.baumgart@sap.com>
parent c80181c7
No related branches found
No related tags found
No related merge requests found
......@@ -62,6 +62,7 @@
*/
public class FileBasedConfig extends Config {
private final File configFile;
private volatile long lastModified;
/**
* Create a configuration with no default fallback.
......@@ -103,6 +104,7 @@ public final File getFile() {
* the file is not a properly formatted configuration file.
*/
public void load() throws IOException, ConfigInvalidException {
lastModified = getFile().lastModified();
try {
fromText(RawParseUtils.decode(IO.readFully(getFile())));
} catch (FileNotFoundException noFile) {
......@@ -134,16 +136,26 @@ public void save() throws IOException {
if (!lf.lock())
throw new IOException("Cannot lock " + getFile());
try {
lf.setNeedStatInformation(true);
lf.write(out);
if (!lf.commit())
throw new IOException("Cannot commit write to " + getFile());
} finally {
lf.unlock();
}
lastModified = lf.getCommitLastModified();
}
@Override
public String toString() {
return getClass().getSimpleName() + "[" + getFile().getPath() + "]";
}
/**
* @return returns true if the currently loaded configuration file is older
* than the file on disk
*/
public boolean isOutdated() {
return getFile().lastModified() != lastModified;
}
}
......@@ -96,6 +96,8 @@ public class Repository {
private final File gitDir;
private final FileBasedConfig userConfig;
private final RepositoryConfig config;
private final RefDatabase refs;
......@@ -191,26 +193,11 @@ public Repository(final File d, final File workTree, final File objectDir,
throw new IllegalArgumentException("Either GIT_DIR or GIT_WORK_TREE must be passed to Repository constructor");
}
final FileBasedConfig userConfig;
userConfig = SystemReader.getInstance().openUserConfig();
try {
userConfig.load();
} catch (ConfigInvalidException e1) {
IOException e2 = new IOException("User config file "
+ userConfig.getFile().getAbsolutePath() + " invalid: "
+ e1);
e2.initCause(e1);
throw e2;
}
config = new RepositoryConfig(userConfig, FS.resolve(gitDir, "config"));
try {
getConfig().load();
} catch (ConfigInvalidException e1) {
IOException e2 = new IOException("Unknown repository format");
e2.initCause(e1);
throw e2;
}
loadUserConfig();
loadConfig();
if (workDir == null) {
String workTreeConfig = getConfig().getString("core", null, "worktree");
......@@ -244,6 +231,29 @@ public Repository(final File d, final File workTree, final File objectDir,
}
}
private void loadUserConfig() throws IOException {
try {
userConfig.load();
} catch (ConfigInvalidException e1) {
IOException e2 = new IOException("User config file "
+ userConfig.getFile().getAbsolutePath() + " invalid: "
+ e1);
e2.initCause(e1);
throw e2;
}
}
private void loadConfig() throws IOException {
try {
config.load();
} catch (ConfigInvalidException e1) {
IOException e2 = new IOException("Unknown repository format");
e2.initCause(e1);
throw e2;
}
}
/**
* Create a new Git repository initializing the necessary files and
* directories. Repository with working tree is created using this method.
......@@ -320,6 +330,20 @@ public RefDatabase getRefDatabase() {
* @return the configuration of this repository
*/
public RepositoryConfig getConfig() {
if (userConfig.isOutdated()) {
try {
loadUserConfig();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (config.isOutdated()) {
try {
loadConfig();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return config;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment