Mercurial > hg > mercurial
annotate mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java @ 65:3cb4f95a4f6f
do not show credentials in the error messages
author | Pavel.Sher |
---|---|
date | Thu, 09 Jul 2009 21:21:36 +0400 |
parents | c9f14e3ef86d |
children | e6971dc6b17c |
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 /** | |
60 * Returns repository path | |
61 * @return repository path | |
62 */ | |
63 @NotNull | |
64 public String getRepository() { | |
65 return myRepository; | |
66 } | |
67 | |
68 /** | |
57 | 69 * Returns name of the branch to use (returns 'default' if no branch specified) |
70 * @return see above | |
71 */ | |
72 @NotNull | |
73 public String getBranchName() { | |
74 return StringUtil.isEmpty(myBranchName) ? DEFAULT_BRANCH_NAME : myBranchName; | |
75 } | |
76 | |
77 /** | |
78 * Returns true if current branch is default branch | |
79 * @return see above | |
80 */ | |
81 public boolean isDefaultBranch() { | |
82 return getBranchName().equals(DEFAULT_BRANCH_NAME); | |
83 } | |
84 | |
85 /** | |
27 | 86 * Returns path to hg command |
87 * @return path to hg command | |
88 */ | |
89 @NotNull | |
90 public String getHgCommandPath() { | |
91 return myHgCommandPath; | |
92 } | |
93 | |
65 | 94 public String getUsername() { |
95 return myUsername; | |
96 } | |
97 | |
98 public String getPassword() { | |
99 return myPassword; | |
100 } | |
101 | |
45 | 102 private final static Set<String> AUTH_PROTOS = new HashSet<String>(); |
103 static { | |
104 AUTH_PROTOS.add("http://"); | |
105 AUTH_PROTOS.add("https://"); | |
106 AUTH_PROTOS.add("ssh://"); | |
107 } | |
108 | |
44 | 109 /** |
45 | 110 * Returns URL to use for push command |
111 * @return URL to use for push command | |
44 | 112 */ |
113 public String getPushUrl() { | |
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
|
114 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
|
115 |
45 | 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 for (String proto: AUTH_PROTOS) { | |
126 if (myRepository.startsWith(proto)) { | |
127 return proto + cre + myRepository.substring(proto.length()); | |
128 } | |
129 } | |
130 | |
131 return myRepository; | |
44 | 132 } |
133 | |
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
|
134 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
|
135 for (String proto: AUTH_PROTOS) { |
64 | 136 if (repository.startsWith(proto)) { |
137 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
|
138 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
|
139 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
|
140 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
|
141 } |
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 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
|
144 } |
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
|
145 |
27 | 146 public void setHgCommandPath(@NotNull final String hgCommandPath) { |
147 myHgCommandPath = hgCommandPath; | |
148 } | |
149 | |
150 public void setWorkingDir(@NotNull final File workingDir) { | |
46 | 151 myWorkingDir = PathUtil.getCanonicalFile(workingDir); |
27 | 152 } |
153 | |
154 /** | |
57 | 155 * Returns directory where repository is supposed to be cloned, i.e. working directory of cloned repository |
27 | 156 * @return repository working directory |
157 */ | |
158 @NotNull | |
57 | 159 public File getLocalRepositoryDir() { |
27 | 160 if (myWorkingDir != null) { |
161 return myWorkingDir; | |
162 } | |
163 | |
164 return getDefaultWorkDir(myWorkFolderParentDir, myRepository); | |
165 } | |
166 | |
29 | 167 /** |
168 * Returns true if current working directory contains copy of repository (contains .hg directory) | |
169 * @return see above | |
170 */ | |
171 public boolean hasCopyOfRepository() { | |
172 // need better way to check that repository copy is ok | |
57 | 173 return getLocalRepositoryDir().isDirectory() && new File(getLocalRepositoryDir(), ".hg").isDirectory(); |
29 | 174 } |
175 | |
27 | 176 public static String DEFAULT_WORK_DIR_PREFIX = "hg_"; |
177 | |
178 private static File getDefaultWorkDir(@NotNull File workFolderParentDir, @NotNull String repPath) { | |
179 String workingDirname = DEFAULT_WORK_DIR_PREFIX + String.valueOf(Hash.calc(normalize(repPath))); | |
46 | 180 return PathUtil.getCanonicalFile(new File(workFolderParentDir, workingDirname)); |
27 | 181 } |
182 | |
183 private static String normalize(final String path) { | |
184 String normalized = PathUtil.normalizeSeparator(path); | |
185 if (path.endsWith("/")) { | |
186 return normalized.substring(0, normalized.length()-1); | |
187 } | |
188 return normalized; | |
189 } | |
190 } |