Skip to content

Commit b5386a3

Browse files
committed
Defining better traversal methods for apps that walk trees
1 parent 11651da commit b5386a3

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/main/java/org/kohsuke/github/GHBlob.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/**
1111
* @author Kanstantsin Shautsou
1212
* @author Kohsuke Kawaguchi
13+
* @see GHTreeEntry#asBlob()
1314
* @see GHRepository#getBlob(String)
1415
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
1516
*/

src/main/java/org/kohsuke/github/GHTree.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* @author Daniel Teixeira - https://github.com/ddtxra
1313
* @see GHRepository#getTree(String)
14+
* @see GHTreeEntry#asTree()
1415
*/
1516
public class GHTree {
1617
/* package almost final */GHRepository repo;

src/main/java/org/kohsuke/github/GHTreeEntry.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.kohsuke.github;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
35
import java.net.URL;
46

57
/**
@@ -70,4 +72,37 @@ public String getSha() {
7072
public URL getUrl() {
7173
return GitHub.parseURL(url);
7274
}
75+
76+
/**
77+
* If this tree entry represents a file, then return its information.
78+
* Otherwise null.
79+
*/
80+
public GHBlob asBlob() throws IOException {
81+
if (type.equals("blob"))
82+
return tree.repo.getBlob(sha);
83+
else
84+
return null;
85+
}
86+
87+
/**
88+
* If this tree entry represents a file, then return its content.
89+
* Otherwise null.
90+
*/
91+
public InputStream readAsBlob() throws IOException {
92+
if (type.equals("blob"))
93+
return tree.repo.readBlob(sha);
94+
else
95+
return null;
96+
}
97+
98+
/**
99+
* If this tree entry represents a directory, then return it.
100+
* Otherwise null.
101+
*/
102+
public GHTree asTree() throws IOException {
103+
if (type.equals("tree"))
104+
return tree.repo.getTree(sha);
105+
else
106+
return null;
107+
}
73108
}

0 commit comments

Comments
 (0)