Parcourir la source

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: Jens Baumgart <jens.baumgart@sap.com>
tags/v0.8.1
Jens Baumgart il y a 14 ans
Parent
révision
cc905e7d4b

+ 12
- 0
org.eclipse.jgit/src/org/eclipse/jgit/lib/FileBasedConfig.java Voir le fichier

*/ */
public class FileBasedConfig extends Config { public class FileBasedConfig extends Config {
private final File configFile; private final File configFile;
private volatile long lastModified;


/** /**
* Create a configuration with no default fallback. * Create a configuration with no default fallback.
* the file is not a properly formatted configuration file. * the file is not a properly formatted configuration file.
*/ */
public void load() throws IOException, ConfigInvalidException { public void load() throws IOException, ConfigInvalidException {
lastModified = getFile().lastModified();
try { try {
fromText(RawParseUtils.decode(IO.readFully(getFile()))); fromText(RawParseUtils.decode(IO.readFully(getFile())));
} catch (FileNotFoundException noFile) { } catch (FileNotFoundException noFile) {
if (!lf.lock()) if (!lf.lock())
throw new IOException("Cannot lock " + getFile()); throw new IOException("Cannot lock " + getFile());
try { try {
lf.setNeedStatInformation(true);
lf.write(out); lf.write(out);
if (!lf.commit()) if (!lf.commit())
throw new IOException("Cannot commit write to " + getFile()); throw new IOException("Cannot commit write to " + getFile());
} finally { } finally {
lf.unlock(); lf.unlock();
} }
lastModified = lf.getCommitLastModified();
} }


@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName() + "[" + getFile().getPath() + "]"; 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;
}
} }

+ 41
- 17
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java Voir le fichier



private final File gitDir; private final File gitDir;


private final FileBasedConfig userConfig;

private final RepositoryConfig config; private final RepositoryConfig config;


private final RefDatabase refs; private final RefDatabase refs;
throw new IllegalArgumentException("Either GIT_DIR or GIT_WORK_TREE must be passed to Repository constructor"); throw new IllegalArgumentException("Either GIT_DIR or GIT_WORK_TREE must be passed to Repository constructor");
} }


final FileBasedConfig userConfig;
userConfig = SystemReader.getInstance().openUserConfig(); 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")); 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) { if (workDir == null) {
String workTreeConfig = getConfig().getString("core", null, "worktree"); String workTreeConfig = getConfig().getString("core", null, "worktree");
} }
} }


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 * Create a new Git repository initializing the necessary files and
* directories. Repository with working tree is created using this method. * directories. Repository with working tree is created using this method.
* @return the configuration of this repository * @return the configuration of this repository
*/ */
public RepositoryConfig getConfig() { 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; return config;
} }



Chargement…
Annuler
Enregistrer