Skip to content

Commit 016f61f

Browse files
committed
Refactored options normalization to be more generic
1 parent 424c18d commit 016f61f

4 files changed

Lines changed: 35 additions & 29 deletions

File tree

lib/clone.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
var NodeGit = require("../");
2+
var normalizeOptions = require("./util/normalize_options");
23

34
var Clone = NodeGit.Clone;
5+
var clone = Clone.clone;
6+
7+
/**
8+
* Patch repository cloning to automatically coerce objects.
9+
*
10+
* @param url
11+
* @param local_path
12+
* @param options
13+
*/
14+
Clone.clone = function(url, local_path, options) {
15+
options = normalizeOptions(options, NodeGit.CloneOptions);
16+
17+
return clone.apply(this, arguments);
18+
};
419

520
module.exports = Clone;

lib/clone_options.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/util/normalize_options.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Normalize an object to match a struct.
3+
*
4+
* @param {String, Object} oid - The oid string or instance.
5+
* @return {Object} An Oid instance.
6+
*/
7+
function normalizeOptions(options, Ctor) {
8+
var instance = new Ctor();
9+
10+
if (!options) {
11+
return instance;
12+
}
13+
14+
Object.keys(options).forEach(function(key) {
15+
instance[key] = options[key];
16+
});
17+
}
18+
19+
module.exports = normalizeOptions;

test/tests/clone.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ describe("Clone", function() {
1212

1313
var Repository = require("../../lib/repository");
1414
var Clone = require("../../lib/clone");
15-
var CloneOptions = require("../../lib/clone_options");
1615

1716
before(function() {
1817
return Promise.all([
@@ -25,7 +24,7 @@ describe("Clone", function() {
2524

2625
it("can clone with http", function() {
2726
var url = "http://github.com/nodegit/test.git";
28-
var opts = CloneOptions.fromObj({ ignoreCertErrors: 1});
27+
var opts = { ignoreCertErrors: 1 };
2928

3029
return Clone.clone(url, http, opts).then(function(repository) {
3130
assert.ok(repository instanceof Repository);

0 commit comments

Comments
 (0)