This repository was archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathgithub.js
More file actions
173 lines (140 loc) · 3.62 KB
/
github.js
File metadata and controls
173 lines (140 loc) · 3.62 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var fs = require( "fs" ),
exec = require( "child_process" ).exec,
semver = require( "semver" ),
Step = require( "step" ),
mkdirp = require( "mkdirp" ),
service = require( "../service" );
var reGithubUrl = /^https?:\/\/github\.com\/([^\/]+)\/([^\/]+)(\/.*)?$/;
function dirname( path ) {
path = path.split( "/" );
path.pop();
return path.join( "/" );
}
function extend( a, b ) {
for ( var prop in b ) {
a[ prop ] = b[ prop ];
}
}
function repoFromHook( data ) {
var matches = reGithubUrl.exec( data.repository.url ),
repo = new GithubRepo( matches[ 1 ], matches[ 2 ] );
repo.forks = data.repository.forks;
repo.watchers = data.repository.watchers;
return repo;
}
function GithubRepo( userName, repoName ) {
if ( arguments.length === 1 ) {
return repoFromHook( userName );
}
var partialPath = "/" + userName + "/" + repoName;
this.userName = userName;
this.repoName = repoName;
this.siteUrl = "http://github.com" + partialPath;
this.sourceUrl = "git://github.com" + partialPath + ".git";
service.Repo.call( this );
}
GithubRepo.test = function( data ) {
return reGithubUrl.test( data.repository && data.repository.url );
};
// service interface
extend( GithubRepo.prototype, new service.Repo() );
extend( GithubRepo.prototype, {
downloadUrl: function( version ) {
return this.siteUrl + "/zipball/" + version;
},
getTags: function( fn ) {
var repo = this;
Step(
// fetch the repo
function() {
repo.fetch( this );
},
// get the tags
function( error ) {
if ( error ) {
return fn( error );
}
exec( "git tag", { cwd: repo.path }, this );
},
// parse the tags
function( error, stdout ) {
if ( error ) {
return fn( error );
}
var tags = stdout.split( "\n" );
tags.pop();
fn( null, tags );
}
);
},
getManifestFiles: function( tag, fn ) {
exec( "git ls-tree " + tag + " --name-only", { cwd: this.path }, function( error, stdout, stderr ) {
if ( error ) {
return fn( error );
}
// filter to *.jquery.json
fn( null, stdout.split( "\n" ).filter(function( file ) {
return file.indexOf( ".jquery.json" ) > 0;
}));
});
},
_getManifest: function( version, file, fn ) {
version = version || "master";
exec( "git show " + version + ":" + file, { cwd: this.path }, function( error, stdout, stderr ) {
// this will also result in an error being passed, so we check stderr first
if ( stderr && stderr.substring( 0, 11 ) === "fatal: Path" ) {
return fn( null, null );
}
if ( error ) {
return fn( error );
}
fn( null, stdout );
});
},
getReleaseDate: function( tag, fn ) {
exec( "git log --pretty='%cD' -1 " + tag, { cwd: this.path }, function( error, stdout ) {
if ( error ) {
return fn( error );
}
fn( null, new Date( stdout ) );
});
},
restore: function( fn ) {
this.fetch( fn );
}
});
// internals
extend( GithubRepo.prototype, {
fetch: function( fn ) {
var repo = this;
Step(
// make sure the user directory exists
function() {
mkdirp( dirname( repo.path ), "0755", this );
},
// check if the repo already exists
function( error ) {
if ( error ) {
return fn( error );
}
fs.stat( repo.path, this );
},
// create or update the repo
function( error ) {
// repo already exists
if ( !error ) {
return exec( "git fetch -t", { cwd: repo.path }, this );
}
// error other than repo not existing
if ( error.code !== "ENOENT" ) {
return fn( error );
}
exec( "git clone " + repo.sourceUrl + " " + repo.path, this );
},
function( error ) {
fn( error );
}
);
}
});
service.register( "github", GithubRepo );