summaryrefslogtreecommitdiffstats
path: root/gerrit-common/src/main/java/com/google/gerrit/common/data/GitWebType.java
blob: 3580774f2e3455bf1f15e4c8cdfc5cf196a21de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.common.data;

/** Class to store information about different gitweb types. */
public class GitWebType {
  /**
   * Get a GitWebType based on the given name.
   *
   * @param name Name to look for.
   * @return GitWebType from the given name, else null if not found.
   */
  public static GitWebType fromName(final String name) {
    final GitWebType type;

    if (name == null || name.isEmpty() || name.equalsIgnoreCase("gitweb")) {
      type = new GitWebType();
      type.setLinkName("gitweb");
      type.setProject("?p=${project}.git;a=summary");
      type.setRevision("?p=${project}.git;a=commit;h=${commit}");
      type.setBranch("?p=${project}.git;a=shortlog;h=${branch}");
      type.setFileHistory("?p=${project}.git;a=history;hb=${branch};f=${file}");

    } else if (name.equalsIgnoreCase("cgit")) {
      type = new GitWebType();
      type.setLinkName("cgit");
      type.setProject("${project}.git/summary");
      type.setRevision("${project}.git/commit/?id=${commit}");
      type.setBranch("${project}.git/log/?h=${branch}");
      type.setFileHistory("${project}.git/log/${file}?h=${branch}");

    } else if (name.equalsIgnoreCase("custom")) {
      type = new GitWebType();
      // The custom name is not defined, let's keep the old style of using GitWeb
      type.setLinkName("gitweb");

    } else if (name.equalsIgnoreCase("disabled")) {
      type = null;

    } else {
      type = null;
    }

    return type;
  }

  /** name of the type. */
  private String name;

  /** String for revision view url. */
  private String revision;

  /** ParamertizedString for project view url. */
  private String project;

  /** ParamertizedString for branch view url. */
  private String branch;

  /** ParamertizedString for file history view url. */
  private String fileHistory;

  /** Character to substitute the standard path separator '/' in branch and
    * project names */
  private char pathSeparator = '/';

  /** Private default constructor for gson. */
  protected GitWebType() {
  }

  /**
   * Get the String for branch view.
   *
   * @return The String for branch view
   */
  public String getBranch() {
    return branch;
  }

  /**
   * Get the String for link-name of the type.
   *
   * @return The String for link-name of the type
   */
  public String getLinkName() {
    return name;
  }

  /**
   * Get the String for project view.
   *
   * @return The String for project view
   */
  public String getProject() {
    return project;
  }

  /**
   * Get the String for revision view.
   *
   * @return The String for revision view
   */
  public String getRevision() {
    return revision;
  }

  /**
   * Get the String for file history view.
   *
   * @return The String for file history view
   */
  public String getFileHistory() {
    return fileHistory;
  }

  /**
   * Set the pattern for branch view.
   *
   * @param pattern The pattern for branch view
   */
  public void setBranch(final String pattern) {
    if (pattern != null && !pattern.isEmpty()) {
      branch = pattern;
    }
  }

  /**
   * Set the pattern for link-name type.
   *
   * @param pattern The pattern for link-name type
   */
  public void setLinkName(final String name) {
    if (name != null && !name.isEmpty()) {
      this.name = name;
    }
  }

  /**
   * Set the pattern for project view.
   *
   * @param pattern The pattern for project view
   */
  public void setProject(final String pattern) {
    if (pattern != null && !pattern.isEmpty()) {
      project = pattern;
    }
  }

  /**
   * Set the pattern for revision view.
   *
   * @param pattern The pattern for revision view
   */
  public void setRevision(final String pattern) {
    if (pattern != null && !pattern.isEmpty()) {
      revision = pattern;
    }
  }

  /**
   * Set the pattern for file history view.
   *
   * @param pattern The pattern for file history view
   */
  public void setFileHistory(final String pattern) {
    if (pattern != null && !pattern.isEmpty()) {
      fileHistory = pattern;
    }
  }

  /**
   * Replace the standard path separator ('/') in a branch name or project
   * name with a custom path separator configured by the property
   * gitweb.pathSeparator.
   * @param urlSegment The branch or project to replace the path separator in
   * @return the urlSegment with the standard path separator replaced by the
   * custom path separator
   */
  public String replacePathSeparator(String urlSegment) {
    if ('/' != pathSeparator) {
      return urlSegment.replace('/', pathSeparator);
    }
    return urlSegment;
  }

  /**
   * Set the custom path separator
   * @param separator The custom path separator
   */
  public void setPathSeparator(char separator) {
    this.pathSeparator = separator;
  }
}