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
//! Git interface

use self::super::{Github, Result};

/// reference to git operations associated with a github repo
pub struct Git<'a> {
    github: &'a Github,
    owner: String,
    repo: String,
}

impl<'a> Git<'a> {
    pub fn new<O, R>(github: &'a Github, owner: O, repo: R) -> Self
        where O: Into<String>,
              R: Into<String>
    {
        Git {
            github: github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

    /// list a git tree of files for this repo at a given sha
    pub fn tree<S>(&self, sha: S, recursive: bool) -> Result<TreeData>
        where S: Into<String>
    {
        self.github
            .get::<TreeData>(&format!("/repos/{}/{}/git/trees/{}?recursive={}",
                                      self.owner,
                                      self.repo,
                                      sha.into(),
                                      if recursive { "1" } else { "0" }))
    }

    /// get the blob contents of a given sha
    pub fn blob<S>(&self, sha: S) -> Result<Blob>
        where S: Into<String>
    {
        self.github
            .get::<Blob>(&format!("/repos/{}/{}/git/blobs/{}",
                                  self.owner,
                                  self.repo,
                                  sha.into()))
    }
}


// representations

#[derive(Debug, Deserialize)]
pub struct TreeData {
    pub sha: String,
    pub url: String,
    pub tree: Vec<GitFile>,
    pub truncated: bool,
}

#[derive(Debug, Deserialize)]
pub struct GitFile {
    pub path: String,
    pub mode: String,
    /// typically tree or blob
    #[serde(rename="type")]
    pub content_type: String,
    /// size will be None for directories
    pub size: Option<usize>,
    pub sha: String,
    /// url will be None for commits
    pub url: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Blob {
    pub content: String,
    pub encoding: String,
    pub url: String,
    pub sha: String,
    /// sizes will be None for directories
    pub size: Option<usize>,
}