Skip to content

Commit 2a4bdab

Browse files
committed
Merge git://github.com/bigkraig/git-plugin into bigkraig-5706ede
* git://github.com/bigkraig/git-plugin: link directly to modified files in side by side view set the GIT_BRANCH in all cases Adding support for Gitorious repositories
2 parents 3425061 + 1b6ef63 commit 2a4bdab

6 files changed

Lines changed: 232 additions & 7 deletions

File tree

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,8 @@ public Revision invoke(File localWorkspace, VirtualChannel channel)
10511051
}
10521052
listener.getLogger().println("Commencing build of " + revToBuild);
10531053
environment.put(GIT_COMMIT, revToBuild.getSha1String());
1054+
Branch branch = revToBuild.getBranches().iterator().next();
1055+
environment.put(GIT_BRANCH, branch.getName());
10541056

10551057
if (mergeOptions.doMerge() && !revToBuild.containsBranchName(mergeOptions.getRemoteBranchName())) {
10561058
build.addAction(workingDirectory.act(new FileCallable<BuildData>() {
@@ -1237,13 +1239,16 @@ private void computeChangeLog(IGitAPI git, Revision revToBuild, BuildListener li
12371239

12381240
public void buildEnvVars(AbstractBuild<?, ?> build, java.util.Map<String, String> env) {
12391241
super.buildEnvVars(build, env);
1240-
String branch = getSingleBranch(build);
1241-
if (branch != null) {
1242-
env.put(GIT_BRANCH, branch);
1243-
}
1244-
BuildData bd = fixNull(getBuildData(build, false));
1245-
if ((bd != null) && (bd.getLastBuiltRevision() != null)) {
1246-
String commit = bd.getLastBuiltRevision().getSha1String();
1242+
Revision rev = fixNull(getBuildData(build, false)).getLastBuiltRevision();
1243+
String singleBranch = getSingleBranch(build);
1244+
if (singleBranch != null){
1245+
env.put(GIT_BRANCH, singleBranch);
1246+
} else if (rev != null) {
1247+
Branch branch = rev.getBranches().iterator().next();
1248+
env.put(GIT_BRANCH, branch.getName());
1249+
}
1250+
if (rev != null) {
1251+
String commit = rev.getSha1String();
12471252
if (commit != null) {
12481253
env.put(GIT_COMMIT, commit);
12491254
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package hudson.plugins.git.browser;
2+
3+
import hudson.Extension;
4+
import hudson.model.Descriptor;
5+
import hudson.plugins.git.GitChangeSet;
6+
import hudson.plugins.git.GitChangeSet.Path;
7+
import hudson.scm.EditType;
8+
import hudson.scm.RepositoryBrowser;
9+
10+
import java.io.IOException;
11+
import java.net.URL;
12+
import java.net.MalformedURLException;
13+
14+
import net.sf.json.JSONObject;
15+
import org.kohsuke.stapler.DataBoundConstructor;
16+
import org.kohsuke.stapler.StaplerRequest;
17+
18+
/**
19+
* Git Browser for Gitorious
20+
*/
21+
public class GitoriousWeb extends GitRepositoryBrowser {
22+
23+
private static final long serialVersionUID = 1L;
24+
private final URL url;
25+
26+
@DataBoundConstructor
27+
public GitoriousWeb(String url) throws MalformedURLException {
28+
this.url = normalizeToEndWithSlash(new URL(url));
29+
}
30+
31+
public URL getUrl() {
32+
return url;
33+
}
34+
35+
@Override
36+
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
37+
return new URL(url, "commit/" + changeSet.getId().toString());
38+
}
39+
40+
/**
41+
* Creates a link to the commit diff.
42+
*
43+
* https://[Gitorious URL]/commit/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/diffs?diffmode=sidebyside&fragment=1#[path to file]
44+
*
45+
* @param path
46+
* @return diff link
47+
* @throws IOException
48+
*/
49+
@Override
50+
public URL getDiffLink(Path path) throws IOException {
51+
final GitChangeSet changeSet = path.getChangeSet();
52+
return new URL(url, "commit/" + changeSet.getId().toString() + "/diffs?diffmode=sidebyside&fragment=1#" + path.getPath());
53+
}
54+
55+
/**
56+
* Creates a link to the file.
57+
* https://[Gitorious URL]/blobs/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/pom.xml
58+
*
59+
* @param path
60+
* @return file link
61+
* @throws IOException
62+
*/
63+
@Override
64+
public URL getFileLink(Path path) throws IOException {
65+
if (path.getEditType().equals(EditType.DELETE)) {
66+
return getDiffLink(path);
67+
} else {
68+
final String spec = "blobs/" + path.getChangeSet().getId() + "/" + path.getPath();
69+
return new URL(url, url.getPath() + spec);
70+
}
71+
}
72+
73+
@Extension
74+
public static class GitoriousWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
75+
public String getDisplayName() {
76+
return "gitoriousweb";
77+
}
78+
79+
@Override
80+
public GitoriousWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
81+
return req.bindParameters(GitoriousWeb.class, "Gitoriousweb.");
82+
}
83+
}
84+
85+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
2+
<f:entry field="url" title="URL">
3+
<f:textbox/>
4+
</f:entry>
5+
</j:jelly>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
Specify the root URL serving this repository (such as <a href="http://gitorious.org/gitorious/mainline">http://gitorious.org/gitorious/mainline</a>).
3+
</div>

src/test/java/hudson/plugins/git/browser/BrowserChooserTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public void testRedmineWeb() throws IOException {
4747
testExistingBrowser(RedmineWeb.class);
4848
}
4949

50+
public void testGitoriousWeb() throws IOException {
51+
testExistingBrowser(GitoriousWeb.class);
52+
}
53+
5054
public void testGithubWeb() throws IOException {
5155
testExistingBrowser(GithubWeb.class);
5256
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package hudson.plugins.git.browser;
2+
3+
import hudson.plugins.git.GitChangeLogParser;
4+
import hudson.plugins.git.GitChangeSet;
5+
import hudson.plugins.git.GitChangeSet.Path;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
11+
import java.util.Collection;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
15+
import junit.framework.TestCase;
16+
17+
import org.xml.sax.SAXException;
18+
19+
20+
public class GitoriousWebTest extends TestCase {
21+
22+
/**
23+
*
24+
*/
25+
private static final String GITORIOUS_URL = "https://SERVER/PROJECT";
26+
private final GitoriousWeb gitoriousWeb;
27+
28+
{
29+
try {
30+
gitoriousWeb = new GitoriousWeb(GITORIOUS_URL);
31+
} catch (MalformedURLException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
/**
37+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}.
38+
* @throws MalformedURLException
39+
*/
40+
public void testGetUrl() throws MalformedURLException {
41+
assertEquals(String.valueOf(gitoriousWeb.getUrl()), GITORIOUS_URL + "/");
42+
}
43+
44+
/**
45+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}.
46+
* @throws MalformedURLException
47+
*/
48+
public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException {
49+
assertEquals(String.valueOf(new GitoriousWeb(GITORIOUS_URL + "/").getUrl()), GITORIOUS_URL + "/");
50+
}
51+
52+
/**
53+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}.
54+
* @throws SAXException
55+
* @throws IOException
56+
*/
57+
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
58+
final URL changeSetLink = gitoriousWeb.getChangeSetLink(createChangeSet("rawchangelog"));
59+
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString());
60+
}
61+
62+
/**
63+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}.
64+
* @throws SAXException
65+
* @throws IOException
66+
*/
67+
public void testGetDiffLinkPath() throws IOException, SAXException {
68+
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
69+
final Path modified1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
70+
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d/diffs?diffmode=sidebyside&fragment=1#src/main/java/hudson/plugins/git/browser/GithubWeb.java", gitoriousWeb.getDiffLink(modified1).toString());
71+
// For added files returns a link to the commit.
72+
final Path added = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file");
73+
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d/diffs?diffmode=sidebyside&fragment=1#src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file", gitoriousWeb.getDiffLink(added).toString());
74+
}
75+
76+
/**
77+
* Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
78+
* @throws SAXException
79+
* @throws IOException
80+
*/
81+
public void testGetFileLinkPath() throws IOException, SAXException {
82+
final HashMap<String,Path> pathMap = createPathMap("rawchangelog");
83+
final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
84+
final URL fileLink = gitoriousWeb.getFileLink(path);
85+
assertEquals(GITORIOUS_URL + "/blobs/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java", String.valueOf(fileLink));
86+
}
87+
88+
/**
89+
* Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
90+
* @throws SAXException
91+
* @throws IOException
92+
*/
93+
public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException {
94+
final HashMap<String,Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
95+
final Path path = pathMap.get("bar");
96+
final URL fileLink = gitoriousWeb.getFileLink(path);
97+
assertEquals(GITORIOUS_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f/diffs?diffmode=sidebyside&fragment=1#bar", String.valueOf(fileLink));
98+
}
99+
100+
private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
101+
final File rawchangelog = new File(GitoriousWebTest.class.getResource(rawchangelogpath).getFile());
102+
final GitChangeLogParser logParser = new GitChangeLogParser(false);
103+
final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs();
104+
return changeSetList.get(0);
105+
}
106+
107+
/**
108+
* @param changelog
109+
* @return
110+
* @throws IOException
111+
* @throws SAXException
112+
*/
113+
private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException {
114+
final HashMap<String, Path> pathMap = new HashMap<String, Path>();
115+
final Collection<Path> changeSet = createChangeSet(changelog).getPaths();
116+
for (final Path path : changeSet) {
117+
pathMap.put(path.getPath(), path);
118+
}
119+
return pathMap;
120+
}
121+
122+
123+
}

0 commit comments

Comments
 (0)