Przeglądaj źródła

Added implementation of copyfile rule.

Change-Id: I83e8a3218be2984321342039fda507fdb1aa5f30
Signed-off-by: Yuxuan 'fishy' Wang <fishywang@google.com>
tags/v3.4.0.201405051725-m7
Yuxuan 'fishy' Wang 10 lat temu
rodzic
commit
51cccc9dae

+ 35
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java Wyświetl plik

assertTrue("\"all,-a\" has have b", file.exists()); assertTrue("\"all,-a\" has have b", file.exists());
} }


@Test
public void testRepoManifestCopyfile() throws Exception {
Repository localDb = createWorkRepository();
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
.append("<remote name=\"remote1\" fetch=\".\" />")
.append("<default revision=\"master\" remote=\"remote1\" />")
.append("<project path=\"foo\" name=\"")
.append(defaultUri)
.append("\">")
.append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
.append("</project>")
.append("</manifest>");
JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
RepoCommand command = new RepoCommand(localDb);
command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
.setURI(rootUri)
.call();
// The original file should exist
File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
assertTrue("The original file exists", hello.exists());
BufferedReader reader = new BufferedReader(new FileReader(hello));
String content = reader.readLine();
reader.close();
assertEquals("The original file has expected content", "world", content);
// The dest file should also exist
hello = new File(localDb.getWorkTree(), "Hello");
assertTrue("The destination file exists", hello.exists());
reader = new BufferedReader(new FileReader(hello));
content = reader.readLine();
reader.close();
assertEquals("The destination file has expected content", "world", content);
}

private void resolveRelativeUris() { private void resolveRelativeUris() {
// Find the longest common prefix ends with "/" as rootUri. // Find the longest common prefix ends with "/" as rootUri.
defaultUri = defaultDb.getDirectory().toURI().toString(); defaultUri = defaultDb.getDirectory().toURI().toString();

+ 1
- 0
org.eclipse.jgit/resources/org/eclipse/jgit/gitrepo/internal/RepoText.properties Wyświetl plik

copyFileFailed=Error occurred during execution of copyfile rule.
errorNoDefault=Error: no default remote in file {0}. errorNoDefault=Error: no default remote in file {0}.
errorParsingManifestFile=Error occurred during parsing manifest file {0}. errorParsingManifestFile=Error occurred during parsing manifest file {0}.
invalidManifest=Invalid manifest. invalidManifest=Invalid manifest.

+ 74
- 5
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java Wyświetl plik

package org.eclipse.jgit.gitrepo; package org.eclipse.jgit.gitrepo;


import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.channels.FileChannel;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;


import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.SubmoduleAddCommand; import org.eclipse.jgit.api.SubmoduleAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;


private ProgressMonitor monitor; private ProgressMonitor monitor;


private static class CopyFile {
final String src;
final String dest;
final String relativeDest;

CopyFile(Repository repo, String path, String src, String dest) {
this.src = repo.getWorkTree() + "/" + path + "/" + src; //$NON-NLS-1$ //$NON-NLS-2$
this.relativeDest = dest;
this.dest = repo.getWorkTree() + "/" + dest; //$NON-NLS-1$
}

void copy() throws IOException {
FileInputStream input = new FileInputStream(src);
try {
FileOutputStream output = new FileOutputStream(dest);
try {
FileChannel channel = input.getChannel();
output.getChannel().transferFrom(channel, 0, channel.size());
} finally {
output.close();
}
} finally {
input.close();
}
}
}

private static class Project { private static class Project {
final String name; final String name;
final String path; final String path;
final Set<String> groups; final Set<String> groups;
final List<CopyFile> copyfiles;


Project(String name, String path, String groups) { Project(String name, String path, String groups) {
this.name = name; this.name = name;
this.groups = new HashSet<String>(); this.groups = new HashSet<String>();
if (groups != null && groups.length() > 0) if (groups != null && groups.length() > 0)
this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$ this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$
copyfiles = new ArrayList<CopyFile>();
}

void addCopyFile(CopyFile copyfile) {
copyfiles.add(copyfile);
} }
} }


private final Set<String> plusGroups; private final Set<String> plusGroups;
private final Set<String> minusGroups; private final Set<String> minusGroups;
private String defaultRemote; private String defaultRemote;
private Project currentProject;


XmlManifest(RepoCommand command, String filename, String baseUrl, String groups) { XmlManifest(RepoCommand command, String filename, String baseUrl, String groups) {
this.command = command; this.command = command;
String qName, String qName,
Attributes attributes) throws SAXException { Attributes attributes) throws SAXException {
if ("project".equals(qName)) { //$NON-NLS-1$ if ("project".equals(qName)) { //$NON-NLS-1$
projects.add(new Project( //$NON-NLS-1$
attributes.getValue("name"), //$NON-NLS-1$
attributes.getValue("path"), //$NON-NLS-1$
attributes.getValue("groups"))); //$NON-NLS-1$
currentProject = new Project( //$NON-NLS-1$
attributes.getValue("name"), //$NON-NLS-1$
attributes.getValue("path"), //$NON-NLS-1$
attributes.getValue("groups")); //$NON-NLS-1$
} else if ("remote".equals(qName)) { //$NON-NLS-1$ } else if ("remote".equals(qName)) { //$NON-NLS-1$
remotes.put(attributes.getValue("name"), //$NON-NLS-1$ remotes.put(attributes.getValue("name"), //$NON-NLS-1$
attributes.getValue("fetch")); //$NON-NLS-1$ attributes.getValue("fetch")); //$NON-NLS-1$
} else if ("default".equals(qName)) { //$NON-NLS-1$ } else if ("default".equals(qName)) { //$NON-NLS-1$
defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$ defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
} else if ("copyfile".equals(qName)) { //$NON-NLS-1$ } else if ("copyfile".equals(qName)) { //$NON-NLS-1$
// TODO(fishywang): Handle copyfile. Do nothing for now.
if (currentProject == null)
throw new SAXException(RepoText.get().invalidManifest);
currentProject.addCopyFile(new CopyFile(
command.repo,
currentProject.path,
attributes.getValue("src"), //$NON-NLS-1$
attributes.getValue("dest"))); //$NON-NLS-1$
}
}

@Override
public void endElement(
String uri,
String localName,
String qName) throws SAXException {
if ("project".equals(qName)) { //$NON-NLS-1$
projects.add(currentProject);
currentProject = null;
} }
} }


if (inGroups(proj)) { if (inGroups(proj)) {
String url = remoteUrl + proj.name; String url = remoteUrl + proj.name;
command.addSubmodule(url, proj.path); command.addSubmodule(url, proj.path);
for (CopyFile copyfile : proj.copyfiles) {
try {
copyfile.copy();
} catch (IOException e) {
throw new SAXException(
RepoText.get().copyFileFailed, e);
}
AddCommand add = new AddCommand(command.repo)
.addFilepattern(copyfile.relativeDest);
try {
add.call();
} catch (GitAPIException e) {
throw new SAXException(e);
}
}
} }
} }
} }

+ 1
- 0
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/internal/RepoText.java Wyświetl plik

} }


// @formatter:off // @formatter:off
/***/ public String copyFileFailed;
/***/ public String errorNoDefault; /***/ public String errorNoDefault;
/***/ public String errorParsingManifestFile; /***/ public String errorParsingManifestFile;
/***/ public String invalidManifest; /***/ public String invalidManifest;

Ładowanie…
Anuluj
Zapisz