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;
|
45
|
20 import jetbrains.buildServer.util.FileUtil;
|
27
|
21 import jetbrains.buildServer.util.Hash;
|
44
|
22 import jetbrains.buildServer.util.StringUtil;
|
45
|
23 import jetbrains.buildServer.vcs.VcsRoot;
|
27
|
24 import org.jetbrains.annotations.NotNull;
|
|
25
|
|
26 import java.io.File;
|
45
|
27 import java.util.Set;
|
|
28 import java.util.HashSet;
|
27
|
29
|
|
30 /**
|
|
31 * Represents Mercurial repository settings
|
|
32 */
|
|
33 public class Settings {
|
|
34 private String myRepository;
|
|
35 private String myHgCommandPath;
|
|
36 private File myWorkingDir;
|
|
37 private File myWorkFolderParentDir;
|
45
|
38 private String myUsername;
|
|
39 private String myPassword;
|
27
|
40
|
|
41 public Settings(@NotNull File workFolderParentDir, @NotNull VcsRoot vcsRoot) {
|
|
42 myWorkFolderParentDir = workFolderParentDir;
|
|
43 setRepository(vcsRoot.getProperty(Constants.REPOSITORY_PROP));
|
|
44 setHgCommandPath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP));
|
45
|
45
|
|
46 myUsername = vcsRoot.getProperty(Constants.USERNAME);
|
|
47 myPassword = vcsRoot.getProperty(Constants.PASSWORD);
|
27
|
48 }
|
|
49
|
|
50 public Settings() {
|
|
51 }
|
|
52
|
|
53 public void setRepository(@NotNull final String repository) {
|
|
54 myRepository = repository;
|
|
55 }
|
|
56
|
|
57 /**
|
|
58 * Returns repository path
|
|
59 * @return repository path
|
|
60 */
|
|
61 @NotNull
|
|
62 public String getRepository() {
|
|
63 return myRepository;
|
|
64 }
|
|
65
|
|
66 /**
|
|
67 * Returns path to hg command
|
|
68 * @return path to hg command
|
|
69 */
|
|
70 @NotNull
|
|
71 public String getHgCommandPath() {
|
|
72 return myHgCommandPath;
|
|
73 }
|
|
74
|
45
|
75 private final static Set<String> AUTH_PROTOS = new HashSet<String>();
|
|
76 static {
|
|
77 AUTH_PROTOS.add("http://");
|
|
78 AUTH_PROTOS.add("https://");
|
|
79 AUTH_PROTOS.add("ssh://");
|
|
80 }
|
|
81
|
44
|
82 /**
|
45
|
83 * Returns URL to use for push command
|
|
84 * @return URL to use for push command
|
44
|
85 */
|
|
86 public String getPushUrl() {
|
45
|
87 String cre = "";
|
|
88 if (!StringUtil.isEmpty(myUsername)) {
|
|
89 cre += myUsername;
|
|
90 if (!StringUtil.isEmpty(myPassword)) {
|
|
91 cre += ":" + myPassword;
|
|
92 }
|
|
93 cre += "@";
|
|
94 }
|
|
95
|
|
96 for (String proto: AUTH_PROTOS) {
|
|
97 if (myRepository.startsWith(proto)) {
|
|
98 return proto + cre + myRepository.substring(proto.length());
|
|
99 }
|
|
100 }
|
|
101
|
|
102 return myRepository;
|
44
|
103 }
|
|
104
|
27
|
105 public void setHgCommandPath(@NotNull final String hgCommandPath) {
|
|
106 myHgCommandPath = hgCommandPath;
|
|
107 }
|
|
108
|
|
109 public void setWorkingDir(@NotNull final File workingDir) {
|
|
110 myWorkingDir = FileUtil.getCanonicalFile(workingDir);
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Returns repository working directory where all mercurial commands should be invoked
|
|
115 * @return repository working directory
|
|
116 */
|
|
117 @NotNull
|
|
118 public File getWorkingDir() {
|
|
119 if (myWorkingDir != null) {
|
|
120 return myWorkingDir;
|
|
121 }
|
|
122
|
|
123 return getDefaultWorkDir(myWorkFolderParentDir, myRepository);
|
|
124 }
|
|
125
|
29
|
126 /**
|
|
127 * Returns true if current working directory contains copy of repository (contains .hg directory)
|
|
128 * @return see above
|
|
129 */
|
|
130 public boolean hasCopyOfRepository() {
|
|
131 // need better way to check that repository copy is ok
|
|
132 return getWorkingDir().isDirectory() && new File(getWorkingDir(), ".hg").isDirectory();
|
|
133 }
|
|
134
|
27
|
135 public static String DEFAULT_WORK_DIR_PREFIX = "hg_";
|
|
136
|
|
137 private static File getDefaultWorkDir(@NotNull File workFolderParentDir, @NotNull String repPath) {
|
|
138 String workingDirname = DEFAULT_WORK_DIR_PREFIX + String.valueOf(Hash.calc(normalize(repPath)));
|
|
139 return FileUtil.getCanonicalFile(new File(workFolderParentDir, workingDirname));
|
|
140 }
|
|
141
|
|
142 private static String normalize(final String path) {
|
|
143 String normalized = PathUtil.normalizeSeparator(path);
|
|
144 if (path.endsWith("/")) {
|
|
145 return normalized.substring(0, normalized.length()-1);
|
|
146 }
|
|
147 return normalized;
|
|
148 }
|
|
149 }
|