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.vcs.VcsRoot;
|
|
21 import jetbrains.buildServer.util.Hash;
|
|
22 import jetbrains.buildServer.util.FileUtil;
|
|
23 import org.jetbrains.annotations.NotNull;
|
|
24
|
|
25 import java.io.File;
|
|
26
|
|
27 /**
|
|
28 * Represents Mercurial repository settings
|
|
29 */
|
|
30 public class Settings {
|
|
31 private String myRepository;
|
|
32 private String myHgCommandPath;
|
|
33 private File myWorkingDir;
|
|
34 private File myWorkFolderParentDir;
|
|
35
|
|
36 public Settings(@NotNull File workFolderParentDir, @NotNull VcsRoot vcsRoot) {
|
|
37 myWorkFolderParentDir = workFolderParentDir;
|
|
38 setRepository(vcsRoot.getProperty(Constants.REPOSITORY_PROP));
|
|
39 setHgCommandPath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP));
|
|
40 }
|
|
41
|
|
42 public Settings() {
|
|
43 }
|
|
44
|
|
45 public void setRepository(@NotNull final String repository) {
|
|
46 myRepository = repository;
|
|
47 }
|
|
48
|
|
49 /**
|
|
50 * Returns repository path
|
|
51 * @return repository path
|
|
52 */
|
|
53 @NotNull
|
|
54 public String getRepository() {
|
|
55 return myRepository;
|
|
56 }
|
|
57
|
|
58 /**
|
|
59 * Returns path to hg command
|
|
60 * @return path to hg command
|
|
61 */
|
|
62 @NotNull
|
|
63 public String getHgCommandPath() {
|
|
64 return myHgCommandPath;
|
|
65 }
|
|
66
|
|
67 public void setHgCommandPath(@NotNull final String hgCommandPath) {
|
|
68 myHgCommandPath = hgCommandPath;
|
|
69 }
|
|
70
|
|
71 public void setWorkingDir(@NotNull final File workingDir) {
|
|
72 myWorkingDir = FileUtil.getCanonicalFile(workingDir);
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Returns repository working directory where all mercurial commands should be invoked
|
|
77 * @return repository working directory
|
|
78 */
|
|
79 @NotNull
|
|
80 public File getWorkingDir() {
|
|
81 if (myWorkingDir != null) {
|
|
82 return myWorkingDir;
|
|
83 }
|
|
84
|
|
85 return getDefaultWorkDir(myWorkFolderParentDir, myRepository);
|
|
86 }
|
|
87
|
29
|
88 /**
|
|
89 * Returns true if current working directory contains copy of repository (contains .hg directory)
|
|
90 * @return see above
|
|
91 */
|
|
92 public boolean hasCopyOfRepository() {
|
|
93 // need better way to check that repository copy is ok
|
|
94 return getWorkingDir().isDirectory() && new File(getWorkingDir(), ".hg").isDirectory();
|
|
95 }
|
|
96
|
27
|
97 public static String DEFAULT_WORK_DIR_PREFIX = "hg_";
|
|
98
|
|
99 private static File getDefaultWorkDir(@NotNull File workFolderParentDir, @NotNull String repPath) {
|
|
100 String workingDirname = DEFAULT_WORK_DIR_PREFIX + String.valueOf(Hash.calc(normalize(repPath)));
|
|
101 return FileUtil.getCanonicalFile(new File(workFolderParentDir, workingDirname));
|
|
102 }
|
|
103
|
|
104 private static String normalize(final String path) {
|
|
105 String normalized = PathUtil.normalizeSeparator(path);
|
|
106 if (path.endsWith("/")) {
|
|
107 return normalized.substring(0, normalized.length()-1);
|
|
108 }
|
|
109 return normalized;
|
|
110 }
|
|
111 }
|