|
| 1 | +import * as path from "path"; |
| 2 | +import { Uri } from "vscode"; |
| 3 | +import { ISvnInfo } from "./common/types"; |
| 4 | +import { memoize } from "./decorators"; |
| 5 | +import { SvnRI } from "./svnRI"; |
| 6 | + |
| 7 | +enum ResourceKind { |
| 8 | + LocalRelative, |
| 9 | + LocalFull, |
| 10 | + RemoteFull |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * create from Repository class |
| 15 | + */ |
| 16 | +export class PathNormalizer { |
| 17 | + public readonly repoRoot: Uri; |
| 18 | + public readonly branchRoot: Uri; |
| 19 | + public readonly checkoutRoot?: Uri; |
| 20 | + |
| 21 | + constructor(public readonly repoInfo: ISvnInfo) { |
| 22 | + this.repoRoot = Uri.parse(repoInfo.repository.root); |
| 23 | + this.branchRoot = Uri.parse(repoInfo.url); |
| 24 | + if (repoInfo.wcInfo) { |
| 25 | + this.checkoutRoot = Uri.file(repoInfo.wcInfo.wcrootAbspath); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /** svn://foo.org/domain/trunk/x -> trunk/x */ |
| 30 | + private getFullRepoPathFromUrl(fpath: string): string { |
| 31 | + if (fpath.startsWith("/")) { |
| 32 | + return fpath.substr(1); |
| 33 | + } else if (fpath.startsWith("svn://") || fpath.startsWith("file://")) { |
| 34 | + const target = Uri.parse(fpath).path; |
| 35 | + return path.relative(this.repoRoot.path, target); |
| 36 | + } else { |
| 37 | + throw new Error("unknown path"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public parse( |
| 42 | + fpath: string, |
| 43 | + kind = ResourceKind.RemoteFull, |
| 44 | + rev?: string |
| 45 | + ): SvnRI { |
| 46 | + let target: string; |
| 47 | + if (kind === ResourceKind.RemoteFull) { |
| 48 | + target = this.getFullRepoPathFromUrl(fpath); |
| 49 | + } else if (kind === ResourceKind.LocalFull) { |
| 50 | + if (!path.isAbsolute(fpath)) { |
| 51 | + throw new Error("Path isn't absolute"); |
| 52 | + } |
| 53 | + if (this.checkoutRoot === undefined) { |
| 54 | + throw new Error("Local paths are not"); |
| 55 | + } |
| 56 | + target = path.join( |
| 57 | + this.fromRootToBranch(), |
| 58 | + path.relative(this.checkoutRoot.path, fpath) |
| 59 | + ); |
| 60 | + } else if (kind === ResourceKind.LocalRelative) { |
| 61 | + if (path.isAbsolute(fpath)) { |
| 62 | + throw new Error("Path is absolute"); |
| 63 | + } |
| 64 | + if (this.checkoutRoot === undefined) { |
| 65 | + throw new Error("Local paths are not"); |
| 66 | + } |
| 67 | + target = path.join(this.fromRootToBranch(), fpath); |
| 68 | + } else { |
| 69 | + throw new Error("unsupported kind"); |
| 70 | + } |
| 71 | + |
| 72 | + return new SvnRI( |
| 73 | + this.repoRoot, |
| 74 | + this.branchRoot, |
| 75 | + this.checkoutRoot, |
| 76 | + target, |
| 77 | + rev |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @memoize |
| 82 | + public fromRootToBranch(): string { |
| 83 | + return path.relative(this.repoRoot.path, this.branchRoot.path); |
| 84 | + } |
| 85 | + |
| 86 | + @memoize |
| 87 | + public fromBranchToRoot(): string { |
| 88 | + return path.relative(this.branchRoot.path, this.repoRoot.path); |
| 89 | + } |
| 90 | +} |
0 commit comments