1 package net.trajano.wagon.git;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7
8 import net.trajano.wagon.git.internal.AbstractGitWagon;
9 import net.trajano.wagon.git.internal.GitUri;
10
11 import org.apache.maven.wagon.ResourceDoesNotExistException;
12 import org.apache.maven.wagon.Wagon;
13 import org.codehaus.plexus.component.annotations.Component;
14 import org.eclipse.jgit.api.Git;
15 import org.eclipse.jgit.api.errors.GitAPIException;
16
17
18
19
20
21
22
23 @Component(role = Wagon.class, hint = "git", instantiationStrategy = "per-lookup")
24 public class GitWagon extends AbstractGitWagon {
25
26
27
28
29 @Override
30 public GitUri buildGitUri(final URI gitUri) {
31 final String branchName = gitUri.getQuery();
32 final String asciiUriString = gitUri.toASCIIString();
33 final String gitRepositoryUri = asciiUriString.substring(0,
34 asciiUriString.indexOf('?'));
35 final String resource = gitUri.getFragment();
36 return new GitUri(gitRepositoryUri, branchName, resource);
37 }
38
39 @Override
40 public File getFileForResource(final String resourceName)
41 throws GitAPIException, IOException, URISyntaxException {
42
43
44 final GitUri resolved = getGitUri().resolve(resourceName);
45 Git resourceGit;
46 try {
47 resourceGit = getGit(resolved.getGitRepositoryUri());
48 } catch (final ResourceDoesNotExistException e) {
49 return null;
50 }
51
52 final File workTree = resourceGit.getRepository().getWorkTree();
53 final File resolvedFile = new File(workTree, resolved.getResource());
54 if (!resolvedFile.getCanonicalPath().startsWith(
55 workTree.getCanonicalPath())) {
56 throw new IOException(String.format(
57 "The resolved file '%s' is not in work tree '%s'",
58 resolvedFile, workTree));
59 }
60 return resolvedFile;
61 }
62 }