Mercurial > hg > mercurial
annotate mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java @ 63:cef5cfe52e3e
do not set credentials in the url if url already has them
author | C:\Documents and Settings\pavel.sher\Application Data\The Bat! |
---|---|
date | Thu, 02 Jul 2009 20:14:58 +0400 |
parents | 99e757f2527b |
children | c9f14e3ef86d |
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 | |
45 | 94 private final static Set<String> AUTH_PROTOS = new HashSet<String>(); |
95 static { | |
96 AUTH_PROTOS.add("http://"); | |
97 AUTH_PROTOS.add("https://"); | |
98 AUTH_PROTOS.add("ssh://"); | |
99 } | |
100 | |
44 | 101 /** |
45 | 102 * Returns URL to use for push command |
103 * @return URL to use for push command | |
44 | 104 */ |
105 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
|
106 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
|
107 |
45 | 108 String cre = ""; |
109 if (!StringUtil.isEmpty(myUsername)) { | |
110 cre += myUsername; | |
111 if (!StringUtil.isEmpty(myPassword)) { | |
112 cre += ":" + myPassword; | |
113 } | |
114 cre += "@"; | |
115 } | |
116 | |
117 for (String proto: AUTH_PROTOS) { | |
118 if (myRepository.startsWith(proto)) { | |
119 return proto + cre + myRepository.substring(proto.length()); | |
120 } | |
121 } | |
122 | |
123 return myRepository; | |
44 | 124 } |
125 | |
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
|
126 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
|
127 for (String proto: AUTH_PROTOS) { |
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
|
128 if (myRepository.startsWith(proto)) { |
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
|
129 String withoutProto = myRepository.substring(proto.length()); |
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
|
130 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
|
131 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
|
132 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
|
133 } |
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 } |
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 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
|
136 } |
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 |
27 | 138 public void setHgCommandPath(@NotNull final String hgCommandPath) { |
139 myHgCommandPath = hgCommandPath; | |
140 } | |
141 | |
142 public void setWorkingDir(@NotNull final File workingDir) { | |
46 | 143 myWorkingDir = PathUtil.getCanonicalFile(workingDir); |
27 | 144 } |
145 | |
146 /** | |
57 | 147 * Returns directory where repository is supposed to be cloned, i.e. working directory of cloned repository |
27 | 148 * @return repository working directory |
149 */ | |
150 @NotNull | |
57 | 151 public File getLocalRepositoryDir() { |
27 | 152 if (myWorkingDir != null) { |
153 return myWorkingDir; | |
154 } | |
155 | |
156 return getDefaultWorkDir(myWorkFolderParentDir, myRepository); | |
157 } | |
158 | |
29 | 159 /** |
160 * Returns true if current working directory contains copy of repository (contains .hg directory) | |
161 * @return see above | |
162 */ | |
163 public boolean hasCopyOfRepository() { | |
164 // need better way to check that repository copy is ok | |
57 | 165 return getLocalRepositoryDir().isDirectory() && new File(getLocalRepositoryDir(), ".hg").isDirectory(); |
29 | 166 } |
167 | |
27 | 168 public static String DEFAULT_WORK_DIR_PREFIX = "hg_"; |
169 | |
170 private static File getDefaultWorkDir(@NotNull File workFolderParentDir, @NotNull String repPath) { | |
171 String workingDirname = DEFAULT_WORK_DIR_PREFIX + String.valueOf(Hash.calc(normalize(repPath))); | |
46 | 172 return PathUtil.getCanonicalFile(new File(workFolderParentDir, workingDirname)); |
27 | 173 } |
174 | |
175 private static String normalize(final String path) { | |
176 String normalized = PathUtil.normalizeSeparator(path); | |
177 if (path.endsWith("/")) { | |
178 return normalized.substring(0, normalized.length()-1); | |
179 } | |
180 return normalized; | |
181 } | |
182 } |