Merge "RepoCommand: Move building the index for base repos to its own method"

This commit is contained in:
Ivan Frade 2021-09-14 15:58:08 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit db032edd8f
1 changed files with 120 additions and 112 deletions

View File

@ -577,12 +577,50 @@ public RevCommit call() throws GitAPIException {
List<RepoProject> renamedProjects = renameProjects(filteredProjects); List<RepoProject> renamedProjects = renameProjects(filteredProjects);
DirCache index = DirCache.newInCore(); DirCache index = DirCache.newInCore();
DirCacheBuilder builder = index.builder();
ObjectInserter inserter = repo.newObjectInserter(); ObjectInserter inserter = repo.newObjectInserter();
try (RevWalk rw = new RevWalk(repo)) { try (RevWalk rw = new RevWalk(repo)) {
prepareIndex(renamedProjects, index, inserter);
ObjectId treeId = index.writeTree(inserter);
long prevDelay = 0;
for (int i = 0; i < LOCK_FAILURE_MAX_RETRIES - 1; i++) {
try {
return commitTreeOnCurrentTip(
inserter, rw, treeId);
} catch (ConcurrentRefUpdateException e) {
prevDelay = FileUtils.delay(prevDelay,
LOCK_FAILURE_MIN_RETRY_DELAY_MILLIS,
LOCK_FAILURE_MAX_RETRY_DELAY_MILLIS);
Thread.sleep(prevDelay);
repo.getRefDatabase().refresh();
}
}
// In the last try, just propagate the exceptions
return commitTreeOnCurrentTip(inserter, rw, treeId);
} catch (GitAPIException | IOException | InterruptedException e) {
throw new ManifestErrorException(e);
}
}
try (Git git = new Git(repo)) {
for (RepoProject proj : filteredProjects) {
addSubmodule(proj.getName(), proj.getUrl(), proj.getPath(),
proj.getRevision(), proj.getCopyFiles(),
proj.getLinkFiles(), git);
}
return git.commit().setMessage(RepoText.get().repoCommitMessage)
.call();
} catch (GitAPIException | IOException e) {
throw new ManifestErrorException(e);
}
}
private void prepareIndex(List<RepoProject> projects, DirCache index,
ObjectInserter inserter) throws IOException, GitAPIException {
Config cfg = new Config(); Config cfg = new Config();
StringBuilder attributes = new StringBuilder(); StringBuilder attributes = new StringBuilder();
for (RepoProject proj : renamedProjects) { DirCacheBuilder builder = index.builder();
for (RepoProject proj : projects) {
String name = proj.getName(); String name = proj.getName();
String path = proj.getPath(); String path = proj.getPath();
String url = proj.getUrl(); String url = proj.getUrl();
@ -597,13 +635,14 @@ public RevCommit call() throws GitAPIException {
if (recordRemoteBranch) { if (recordRemoteBranch) {
// "branch" field is only for non-tag references. // "branch" field is only for non-tag references.
// Keep tags in "ref" field as hint for other tools. // Keep tags in "ref" field as hint for other tools.
String field = proj.getRevision().startsWith( String field = proj.getRevision().startsWith(R_TAGS) ? "ref" //$NON-NLS-1$
R_TAGS) ? "ref" : "branch"; //$NON-NLS-1$ //$NON-NLS-2$ : "branch"; //$NON-NLS-1$
cfg.setString("submodule", name, field, //$NON-NLS-1$ cfg.setString("submodule", name, field, //$NON-NLS-1$
proj.getRevision()); proj.getRevision());
} }
if (recordShallowSubmodules && proj.getRecommendShallow() != null) { if (recordShallowSubmodules
&& proj.getRecommendShallow() != null) {
// The shallow recommendation is losing information. // The shallow recommendation is losing information.
// As the repo manifests stores the recommended // As the repo manifests stores the recommended
// depth in the 'clone-depth' field, while // depth in the 'clone-depth' field, while
@ -641,8 +680,8 @@ public RevCommit call() throws GitAPIException {
builder.add(dcEntry); builder.add(dcEntry);
for (CopyFile copyfile : proj.getCopyFiles()) { for (CopyFile copyfile : proj.getCopyFiles()) {
RemoteFile rf = callback.readFileWithMode( RemoteFile rf = callback.readFileWithMode(url,
url, proj.getRevision(), copyfile.src); proj.getRevision(), copyfile.src);
objectId = inserter.insert(Constants.OBJ_BLOB, objectId = inserter.insert(Constants.OBJ_BLOB,
rf.getContents()); rf.getContents());
dcEntry = new DirCacheEntry(copyfile.dest); dcEntry = new DirCacheEntry(copyfile.dest);
@ -673,7 +712,8 @@ public RevCommit call() throws GitAPIException {
String content = cfg.toText(); String content = cfg.toText();
// create a new DirCacheEntry for .gitmodules file. // create a new DirCacheEntry for .gitmodules file.
final DirCacheEntry dcEntry = new DirCacheEntry(Constants.DOT_GIT_MODULES); DirCacheEntry dcEntry = new DirCacheEntry(
Constants.DOT_GIT_MODULES);
ObjectId objectId = inserter.insert(Constants.OBJ_BLOB, ObjectId objectId = inserter.insert(Constants.OBJ_BLOB,
content.getBytes(UTF_8)); content.getBytes(UTF_8));
dcEntry.setObjectId(objectId); dcEntry.setObjectId(objectId);
@ -682,7 +722,8 @@ public RevCommit call() throws GitAPIException {
if (recordSubmoduleLabels) { if (recordSubmoduleLabels) {
// create a new DirCacheEntry for .gitattributes file. // create a new DirCacheEntry for .gitattributes file.
final DirCacheEntry dcEntryAttr = new DirCacheEntry(Constants.DOT_GIT_ATTRIBUTES); DirCacheEntry dcEntryAttr = new DirCacheEntry(
Constants.DOT_GIT_ATTRIBUTES);
ObjectId attrId = inserter.insert(Constants.OBJ_BLOB, ObjectId attrId = inserter.insert(Constants.OBJ_BLOB,
attributes.toString().getBytes(UTF_8)); attributes.toString().getBytes(UTF_8));
dcEntryAttr.setObjectId(attrId); dcEntryAttr.setObjectId(attrId);
@ -691,40 +732,7 @@ public RevCommit call() throws GitAPIException {
} }
builder.finish(); builder.finish();
ObjectId treeId = index.writeTree(inserter);
long prevDelay = 0;
for (int i = 0; i < LOCK_FAILURE_MAX_RETRIES - 1; i++) {
try {
return commitTreeOnCurrentTip(
inserter, rw, treeId);
} catch (ConcurrentRefUpdateException e) {
prevDelay = FileUtils.delay(prevDelay,
LOCK_FAILURE_MIN_RETRY_DELAY_MILLIS,
LOCK_FAILURE_MAX_RETRY_DELAY_MILLIS);
Thread.sleep(prevDelay);
repo.getRefDatabase().refresh();
} }
}
// In the last try, just propagate the exceptions
return commitTreeOnCurrentTip(inserter, rw, treeId);
} catch (GitAPIException | IOException | InterruptedException e) {
throw new ManifestErrorException(e);
}
}
try (Git git = new Git(repo)) {
for (RepoProject proj : filteredProjects) {
addSubmodule(proj.getName(), proj.getUrl(), proj.getPath(),
proj.getRevision(), proj.getCopyFiles(),
proj.getLinkFiles(), git);
}
return git.commit().setMessage(RepoText.get().repoCommitMessage)
.call();
} catch (GitAPIException | IOException e) {
throw new ManifestErrorException(e);
}
}
private RevCommit commitTreeOnCurrentTip(ObjectInserter inserter, private RevCommit commitTreeOnCurrentTip(ObjectInserter inserter,
RevWalk rw, ObjectId treeId) RevWalk rw, ObjectId treeId)