-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGitWeb.java
More file actions
102 lines (88 loc) · 3.23 KB
/
GitWeb.java
File metadata and controls
102 lines (88 loc) · 3.23 KB
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
package hudson.plugins.git.browser;
import hudson.Extension;
import hudson.model.Descriptor;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import hudson.scm.browsers.QueryBuilder;
import hudson.Util;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest2;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.io.Serial;
import java.net.URL;
/**
* Git Browser URLs
*/
public class GitWeb extends GitRepositoryBrowser {
@Serial
private static final long serialVersionUID = 1L;
@DataBoundConstructor
public GitWeb(String repoUrl) {
super(repoUrl);
}
@Override
protected boolean getNormalizeUrl() {
return false;
}
@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return new URL(url, url.getPath()+ param(url).add("a=commit").add("h=" + changeSet.getId()));
}
private QueryBuilder param(URL url) {
return new QueryBuilder(url.getQuery());
}
/**
* Creates a link to the file diff.
* http://[GitWeb URL]?a=blobdiff;f=[path];fp=[path];h=[dst];hp=[src];hb=[commit];hpb=[parent commit]
*
* @param path affected file path
* @return diff link
* @throws IOException on input or output error
*/
@Override
public URL getDiffLink(Path path) throws IOException {
if (path.getEditType() != EditType.EDIT || path.getSrc() == null || path.getDst() == null
|| path.getChangeSet().getParentCommit() == null) {
return null;
}
GitChangeSet changeSet = path.getChangeSet();
URL url = getUrl();
String spec = param(url).add("a=blobdiff").add("f=" + Util.rawEncode(path.getPath())).add("fp=" + Util.rawEncode(path.getPath()))
.add("h=" + path.getSrc()).add("hp=" + path.getDst())
.add("hb=" + changeSet.getId()).add("hpb=" + changeSet.getParentCommit()).toString();
return new URL(url, url.getPath()+spec);
}
/**
* Creates a link to the file.
* http://[GitWeb URL]?a=blob;f=[path];h=[dst, or src for deleted files];hb=[commit]
* @param path file
* @return file link
* @throws IOException on input or output error
*/
@Override
public URL getFileLink(Path path) throws IOException {
URL url = getUrl();
String h = (path.getDst() != null) ? path.getDst() : path.getSrc();
String spec = param(url).add("a=blob").add("f=" + Util.rawEncode(path.getPath()))
.add("h=" + h).add("hb=" + path.getChangeSet().getId()).toString();
return encodeURL(new URL(url, url.getPath()+spec));
}
@Extension
@Symbol("gitWeb")
public static class GitWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
@NonNull
public String getDisplayName() {
return "gitweb";
}
@Override
public GitWeb newInstance(@NonNull StaplerRequest2 req, @NonNull JSONObject jsonObject) throws FormException {
return req.bindJSON(GitWeb.class, jsonObject);
}
}
}