1
- // Github.js 0.1.2
1
+ // Github.js 0.2.0
2
2
// (c) 2012 Michael Aufreiter, Development Seed
3
3
// Github.js is freely distributable under the MIT license.
4
4
// For all details and documentation:
7
7
( function ( ) {
8
8
var Github ;
9
9
var API_URL = 'https://api.github.com' ;
10
-
11
- // Github API
12
- // -------
13
10
14
11
Github = window . Github = function ( options ) {
15
12
var username = options . username ;
16
13
var password = options . password ;
17
14
18
15
// Util
19
- // -------
16
+ // =======
20
17
21
18
function _request ( method , path , data , cb ) {
22
19
$ . ajax ( {
32
29
}
33
30
34
31
// USER API
35
- // -------
32
+ // =======
36
33
37
- Github . User = function ( options ) {
38
- this . username = options . username ;
39
- var userPath = "/users/" + options . username ;
34
+ Github . User = function ( ) {
40
35
this . repos = function ( cb ) {
41
- _request ( "GET" , userPath + " /repos?type=all", null , function ( err , res ) {
36
+ _request ( "GET" , "/user /repos?type=all", null , function ( err , res ) {
42
37
cb ( err , res ) ;
43
38
} ) ;
44
39
}
45
40
} ;
46
41
47
42
48
43
// Repository API
49
- // -------
44
+ // =======
50
45
51
46
Github . Repository = function ( options ) {
52
47
var repo = options . name ;
53
48
var branch = options . branch ;
49
+ var user = options . user ;
54
50
55
51
var that = this ;
56
- var repoPath = "/repos/" + username + "/" + repo ;
52
+ var repoPath = "/repos/" + user + "/" + repo ;
53
+
54
+ // Get a particular reference
55
+ // -------
57
56
58
- // Get latest commit from master
59
- function getLatestCommit ( cb ) {
60
- _request ( "GET" , repoPath + "/git/refs/heads/" + branch , null , function ( err , res ) {
57
+ this . getRef = function ( ref , cb ) {
58
+ _request ( "GET" , repoPath + "/git/refs/heads/" + ref , null , function ( err , res ) {
61
59
if ( err ) return cb ( err ) ;
62
60
cb ( null , res . object . sha ) ;
63
61
} ) ;
64
- }
62
+ } ;
63
+
64
+ // List all branches of a repository
65
+ // -------
66
+
67
+ this . listBranches = function ( cb ) {
68
+ _request ( "GET" , repoPath + "/git/refs/heads" , null , function ( err , heads ) {
69
+ if ( err ) return cb ( err ) ;
70
+ cb ( null , _ . map ( heads , function ( head ) { return _ . last ( head . ref . split ( '/' ) ) ; } ) ) ;
71
+ } ) ;
72
+ } ;
65
73
66
74
// Retrieve the contents of a blob
67
- function getBlob ( sha , cb ) {
75
+ // -------
76
+
77
+ this . getBlob = function ( sha , cb ) {
68
78
_request ( "GET" , repoPath + "/git/blobs/" + sha , null , function ( err , res ) {
69
79
cb ( err , res ) ;
70
80
} ) ;
71
- }
81
+ } ;
72
82
73
83
// Retrieve the tree a commit points to
84
+ // -------
74
85
75
- function getTree ( commit , cb ) {
86
+ this . getTree = function ( commit , cb ) {
76
87
_request ( "GET" , repoPath + "/git/trees/" + commit , null , function ( err , res ) {
77
88
if ( err ) return cb ( err ) ;
78
89
cb ( null , res . sha ) ;
79
90
} ) ;
80
- }
91
+ } ;
81
92
82
93
// Post a new blob object, getting a blob SHA back
94
+ // -------
83
95
84
- function postBlob ( content , cb ) {
96
+ this . postBlob = function ( content , cb ) {
85
97
var data = {
86
98
"content" : content ,
87
99
"encoding" : "utf-8"
90
102
if ( err ) return cb ( err ) ;
91
103
cb ( null , res . sha ) ;
92
104
} ) ;
93
- }
105
+ } ;
94
106
95
107
// Post a new tree object having a file path pointer replaced
96
108
// with a new blob SHA getting a tree SHA back
109
+ // -------
97
110
98
- function postTree ( baseTree , path , blob , cb ) {
111
+ this . postTree = function ( baseTree , path , blob , cb ) {
99
112
var data = {
100
113
"base_tree" : baseTree ,
101
114
"tree" : [
107
120
}
108
121
]
109
122
} ;
110
- _request ( "POST" , repoPath + "/git/trees" , data , function ( err , res ) {
123
+ _request ( "POST" , repoPath + "/git/trees" , data , function ( err , res ) {
111
124
if ( err ) return cb ( err ) ;
112
125
cb ( null , res . sha ) ;
113
126
} ) ;
114
127
} ;
115
128
116
129
// Create a new commit object with the current commit SHA as the parent
117
130
// and the new tree SHA, getting a commit SHA back
131
+ // -------
118
132
119
- function createCommit ( parent , tree , message , cb ) {
133
+ this . commit = function ( parent , tree , message , cb ) {
120
134
var data = {
121
135
"message" : message ,
122
136
"author" : {
132
146
if ( err ) return cb ( err ) ;
133
147
cb ( null , res . sha ) ;
134
148
} ) ;
135
- }
149
+ } ;
136
150
137
151
// Update the reference of your head to point to the new commit SHA
152
+ // -------
138
153
139
- function updateHead ( commit , cb ) {
140
- _request ( "PATCH" , repoPath + "/git/refs/heads/" + branch , { "sha" : commit } , function ( err , res ) {
154
+ this . updateHead = function ( head , commit , cb ) {
155
+ _request ( "PATCH" , repoPath + "/git/refs/heads/" + head , { "sha" : commit } , function ( err , res ) {
141
156
cb ( err ) ;
142
157
} ) ;
143
- }
144
-
145
-
158
+ } ;
146
159
147
160
// Show repository information
148
161
// -------
153
166
} ) ;
154
167
} ;
155
168
156
- // List all files
169
+ // List all files of a branch
157
170
// -------
158
171
159
- this . list = function ( cb ) {
172
+ this . list = function ( branch , cb ) {
160
173
_request ( "GET" , repoPath + "/git/trees/" + branch + "?recursive=1" , null , function ( err , res ) {
161
174
cb ( err , res ? res . tree : null ) ;
162
175
} ) ;
166
179
// Read file at given path
167
180
// -------
168
181
169
- this . read = function ( path , cb ) {
170
- that . list ( function ( err , tree ) {
182
+ this . read = function ( branch , path , cb ) {
183
+ that . list ( branch , function ( err , tree ) {
171
184
var file = _ . select ( tree , function ( file ) {
172
185
return file . path === path ;
173
186
} ) [ 0 ] ;
174
187
175
188
if ( ! file ) return cb ( "not found" , null ) ;
176
189
177
- getBlob ( file . sha , function ( err , blob ) {
190
+ that . getBlob ( file . sha , function ( err , blob ) {
178
191
function decode ( blob ) {
179
192
if ( blob . content ) {
180
193
var data = blob . encoding == 'base64' ?
192
205
} ) ;
193
206
} ;
194
207
195
- // List all commits
196
- // -------
197
-
198
- this . list_commits = function ( cb ) {
199
- _request ( "GET" , repoPath + "/commits" , null , function ( err , res ) {
200
- if ( err ) return cb ( err ) ;
201
- cb ( null , res ) ;
202
- } ) ;
203
- } ;
204
-
205
- // Write file contents on a given path
208
+ // Write file contents to a given branch and path
206
209
// -------
207
210
208
- this . write = function ( path , content , message , cb ) {
209
- getLatestCommit ( function ( err , latestCommit ) {
210
- getTree ( latestCommit , function ( err , tree ) {
211
- postBlob ( content , function ( err , blob ) {
212
- postTree ( tree , path , blob , function ( err , tree ) {
213
- createCommit ( latestCommit , tree , message , function ( err , commit ) {
214
- updateHead ( commit , function ( err ) {
211
+ this . write = function ( branch , path , content , message , cb ) {
212
+ that . getRef ( branch , function ( err , latestCommit ) {
213
+ that . getTree ( latestCommit , function ( err , tree ) {
214
+ that . postBlob ( content , function ( err , blob ) {
215
+ that . postTree ( tree , path , blob , function ( err , tree ) {
216
+ that . commit ( latestCommit , tree , message , function ( err , commit ) {
217
+ that . updateHead ( branch , commit , function ( err ) {
215
218
cb ( err ) ;
216
219
} ) ;
217
220
} ) ;
225
228
// Top Level API
226
229
// -------
227
230
228
- this . getRepo = function ( repo , branch ) {
229
- return new Github . Repository ( { name : repo , branch : branch || "master" } ) ;
231
+ this . getRepo = function ( user , repo , branch ) {
232
+ return new Github . Repository ( { user : user , name : repo , branch : branch || "master" } ) ;
230
233
} ;
231
234
232
- this . getUser = function ( user ) {
233
- return new Github . User ( { username : user } ) ;
235
+ this . getUser = function ( ) {
236
+ return new Github . User ( ) ;
234
237
} ;
235
238
} ;
236
239
} ) . call ( this ) ;
0 commit comments