Mercurial > hg > mercurial
annotate mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java @ 93:a0aea01c32b0
improve repository url parsing
author | Pavel.Sher |
---|---|
date | Tue, 19 Jan 2010 22:23:15 +0300 |
parents | e6971dc6b17c |
children | 6c1cff1f61cc |
rev | line source |
---|---|
27 | 1 /* |
2 * Copyright 2000-2007 JetBrains s.r.o. | |
3 * | |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 * you may not use this file except in compliance with the License. | |
6 * You may obtain a copy of the License at | |
7 * | |
8 * http://www.apache.org/licenses/LICENSE-2.0 | |
9 * | |
10 * Unless required by applicable law or agreed to in writing, software | |
11 * distributed under the License is distributed on an "AS IS" BASIS, | |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 * See the License for the specific language governing permissions and | |
14 * limitations under the License. | |
15 */ | |
16 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command; | |
17 | |
18 import jetbrains.buildServer.buildTriggers.vcs.mercurial.Constants; | |
19 import jetbrains.buildServer.buildTriggers.vcs.mercurial.PathUtil; | |
20 import jetbrains.buildServer.util.Hash; | |
44 | 21 import jetbrains.buildServer.util.StringUtil; |
45 | 22 import jetbrains.buildServer.vcs.VcsRoot; |
27 | 23 import org.jetbrains.annotations.NotNull; |
24 | |
25 import java.io.File; | |
46 | 26 import java.util.HashSet; |
45 | 27 import java.util.Set; |
27 | 28 |
29 /** | |
30 * Represents Mercurial repository settings | |
31 */ | |
32 public class Settings { | |
33 private String myRepository; | |
34 private String myHgCommandPath; | |
35 private File myWorkingDir; | |
36 private File myWorkFolderParentDir; | |
45 | 37 private String myUsername; |
38 private String myPassword; | |
57 | 39 private String myBranchName; |
40 private static final String DEFAULT_BRANCH_NAME = "default"; | |
27 | 41 |
42 public Settings(@NotNull File workFolderParentDir, @NotNull VcsRoot vcsRoot) { | |
43 myWorkFolderParentDir = workFolderParentDir; | |
44 setRepository(vcsRoot.getProperty(Constants.REPOSITORY_PROP)); | |
45 setHgCommandPath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP)); | |
57 | 46 myBranchName = vcsRoot.getProperty(Constants.BRANCH_NAME_PROP); |
45 | 47 |
48 myUsername = vcsRoot.getProperty(Constants.USERNAME); | |
49 myPassword = vcsRoot.getProperty(Constants.PASSWORD); | |
27 | 50 } |
51 | |
52 public Settings() { | |
53 } | |
54 | |
55 public void setRepository(@NotNull final String repository) { | |
56 myRepository = repository; | |
57 } | |
58 | |
59 /** | |
57 | 60 * Returns name of the branch to use (returns 'default' if no branch specified) |
61 * @return see above | |
62 */ | |
63 @NotNull | |
64 public String getBranchName() { | |
65 return StringUtil.isEmpty(myBranchName) ? DEFAULT_BRANCH_NAME : myBranchName; | |
66 } | |
67 | |
68 /** | |
69 * Returns true if current branch is default branch | |
70 * @return see above | |
71 */ | |
72 public boolean isDefaultBranch() { | |
73 return getBranchName().equals(DEFAULT_BRANCH_NAME); | |
74 } | |
75 | |
76 /** | |
27 | 77 * Returns path to hg command |
78 * @return path to hg command | |
79 */ | |
80 @NotNull | |
81 public String getHgCommandPath() { | |
82 return myHgCommandPath; | |
83 } | |
84 | |
65 | 85 public String getUsername() { |
86 return myUsername; | |
87 } | |
88 | |
89 public String getPassword() { | |
90 return myPassword; | |
91 } | |
92 | |
45 | 93 private final static Set<String> AUTH_PROTOS = new HashSet<String>(); |
94 static { | |
95 AUTH_PROTOS.add("http://"); | |
96 AUTH_PROTOS.add("https://"); | |
97 AUTH_PROTOS.add("ssh://"); | |
98 } | |
99 | |
44 | 100 /** |
45 | 101 * Returns URL to use for push command |
102 * @return URL to use for push command | |
44 | 103 */ |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
65
diff
changeset
|
104 public String getRepositoryUrl() { |
63
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
105 if (containsCredentials(myRepository)) return myRepository; |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
106 |
45 | 107 for (String proto: AUTH_PROTOS) { |
108 if (myRepository.startsWith(proto)) { | |
93 | 109 String repoUrl = myRepository.substring(proto.length()); |
110 int endIdx = repoUrl.indexOf('@'); | |
111 int slashIdx = repoUrl.indexOf('/'); | |
112 if (endIdx != -1 && slashIdx > endIdx) { | |
113 repoUrl = repoUrl.substring(endIdx+1); | |
114 } | |
115 | |
116 String cre = ""; | |
117 if (!StringUtil.isEmpty(myUsername)) { | |
118 cre += myUsername; | |
119 if (!StringUtil.isEmpty(myPassword)) { | |
120 cre += ":" + myPassword; | |
121 } | |
122 cre += "@"; | |
123 } | |
124 | |
125 return proto + cre + repoUrl; | |
45 | 126 } |
127 } | |
128 | |
129 return myRepository; | |
44 | 130 } |
131 | |
63
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
132 private boolean containsCredentials(final String repository) { |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
133 for (String proto: AUTH_PROTOS) { |
64 | 134 if (repository.startsWith(proto)) { |
135 String withoutProto = repository.substring(proto.length()); | |
63
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
136 int comma = withoutProto.indexOf(':'); |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
137 int at = withoutProto.indexOf('@'); |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
138 if (at != -1 && comma != -1 && at > comma) return true; |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
139 } |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
140 } |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
141 return false; |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
142 } |
cef5cfe52e3e
do not set credentials in the url if url already has them
C:\Documents and Settings\pavel.sher\Application Data\The Bat!
parents:
57
diff
changeset
|
143 |
27 | 144 public void setHgCommandPath(@NotNull final String hgCommandPath) { |
145 myHgCommandPath = hgCommandPath; | |
146 } | |
147 | |
148 public void setWorkingDir(@NotNull final File workingDir) { | |
46 | 149 myWorkingDir = PathUtil.getCanonicalFile(workingDir); |
27 | 150 } |
151 | |
152 /** | |
57 | 153 * Returns directory where repository is supposed to be cloned, i.e. working directory of cloned repository |
27 | 154 * @return repository working directory |
155 */ | |
156 @NotNull | |
57 | 157 public File getLocalRepositoryDir() { |
27 | 158 if (myWorkingDir != null) { |
159 return myWorkingDir; | |
160 } | |
161 | |
162 return getDefaultWorkDir(myWorkFolderParentDir, myRepository); | |
163 } | |
164 | |
29 | 165 /** |
166 * Returns true if current working directory contains copy of repository (contains .hg directory) | |
167 * @return see above | |
168 */ | |
169 public boolean hasCopyOfRepository() { | |
170 // need better way to check that repository copy is ok | |
57 | 171 return getLocalRepositoryDir().isDirectory() && new File(getLocalRepositoryDir(), ".hg").isDirectory(); |
29 | 172 } |
173 | |
27 | 174 public static String DEFAULT_WORK_DIR_PREFIX = "hg_"; |
175 | |
176 private static File getDefaultWorkDir(@NotNull File workFolderParentDir, @NotNull String repPath) { | |
177 String workingDirname = DEFAULT_WORK_DIR_PREFIX + String.valueOf(Hash.calc(normalize(repPath))); | |
46 | 178 return PathUtil.getCanonicalFile(new File(workFolderParentDir, workingDirname)); |
27 | 179 } |
180 | |
181 private static String normalize(final String path) { | |
182 String normalized = PathUtil.normalizeSeparator(path); | |
183 if (path.endsWith("/")) { | |
184 return normalized.substring(0, normalized.length()-1); | |
185 } | |
186 return normalized; | |
187 } | |
188 } |