6542c65311ef2bc4e0b96b63d1cd3eb7e15a6697
[idea/community.git] / plugins / git4idea / src / git4idea / config / GitVcsApplicationSettings.java
1 /*
2  * Copyright 2000-2012 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 git4idea.config;
17
18 import com.intellij.openapi.components.*;
19 import com.intellij.openapi.util.SystemInfo;
20 import org.jetbrains.annotations.NonNls;
21 import org.jetbrains.annotations.NotNull;
22 import org.jetbrains.annotations.Nullable;
23
24 import java.io.File;
25
26 /**
27  * The application wide settings for the git
28  */
29 @State(
30   name = "Git.Application.Settings",
31   storages = {@Storage(file = StoragePathMacros.APP_CONFIG + "/vcs.xml")})
32 public class GitVcsApplicationSettings implements PersistentStateComponent<GitVcsApplicationSettings.State> {
33
34   @NonNls static final String[] DEFAULT_WINDOWS_PATHS = {"C:\\cygwin\\bin", "C:\\Program Files\\Git\\bin", "C:\\Program Files (x86)\\Git\\bin"};
35   @NonNls static final String[] DEFAULT_UNIX_PATHS = {"/usr/local/bin", "/usr/bin", "/opt/local/bin", "/opt/bin", "/usr/local/git/bin"};
36   @NonNls static final String DEFAULT_WINDOWS_GIT = "git.exe";
37   @NonNls static final String DEFAULT_UNIX_GIT = "git";
38   
39   private State myState = new State();
40
41   /**
42    * Kinds of SSH executable to be used with the git
43    */
44   public enum SshExecutable {
45     IDEA_SSH,
46     NATIVE_SSH,
47   }
48
49   public static class State {
50     public String myPathToGit = null;
51     public SshExecutable SSH_EXECUTABLE = null;
52   }
53
54   public static GitVcsApplicationSettings getInstance() {
55     return ServiceManager.getService(GitVcsApplicationSettings.class);
56   }
57
58   @Override
59   public State getState() {
60     return myState;
61   }
62
63   public void loadState(State state) {
64     myState = state;
65   }
66
67   /**
68    * @return the default executable name depending on the platform
69    */
70   @NotNull
71   public String defaultGit() {
72     if (myState.myPathToGit == null) {
73       String[] paths;
74       String program;
75       if (SystemInfo.isWindows) {
76         program = DEFAULT_WINDOWS_GIT;
77         paths = DEFAULT_WINDOWS_PATHS;
78       }
79       else {
80         program = DEFAULT_UNIX_GIT;
81         paths = DEFAULT_UNIX_PATHS;
82       }
83       for (String p : paths) {
84         File f = new File(p, program);
85         if (f.exists()) {
86           myState.myPathToGit = f.getAbsolutePath();
87           break;
88         }
89       }
90       if (myState.myPathToGit == null) { // otherwise, hope it's in $PATH
91         myState.myPathToGit = program;
92       }
93     }
94     return myState.myPathToGit;
95   }
96
97   @NotNull
98   public String getPathToGit() {
99     return myState.myPathToGit == null ? defaultGit() : myState.myPathToGit;
100   }
101
102   public void setPathToGit(String pathToGit) {
103     myState.myPathToGit = pathToGit;
104   }
105
106   public void setIdeaSsh(@NotNull SshExecutable executable) {
107     myState.SSH_EXECUTABLE = executable;
108   }
109
110   @Nullable
111   SshExecutable getIdeaSsh() {
112     return myState.SSH_EXECUTABLE;
113   }
114
115 }