Skip to content

Instantly share code, notes, and snippets.

@paulelms
Forked from mbostock/.block
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulelms/d24f19fd4cfdd3fcdbb8 to your computer and use it in GitHub Desktop.
Save paulelms/d24f19fd4cfdd3fcdbb8 to your computer and use it in GitHub Desktop.

Run like so:

node gist-clone-all.js username

You'll want to replace "username" with your own username.

This script clones using the push URL, so you should probably be the owner of the gists. You could also use this to clone someone else's gists, but in that case you may wish to edit the code to use gist_pull_url instead.

#!/usr/local/bin/node
var fs = require("fs"),
https = require("https"),
exec = require("child_process").exec;
// TODO --pull or --push
var user = process.argv[2];
fetchAndClone(1, function callback(error, nextPage) {
if (error) return console.error(error);
if (nextPage > 0) fetchAndClone(nextPage, callback);
});
function fetchAndClone(page, callback) {
fetch(page, function(error, gists) {
if (error) return callback(error);
if (gists.length) ++page; else page = -1;
cloneNext(gists.pop());
function cloneNext(gist) {
if (!gist) return callback(null, page);
if (directoryExists(gist.id)) return cloneNext(gists.pop());
console.log("cloning " + gist.id);
exec("git clone git@gist.github.com:" + gist.id + ".git", function(error, stdout, stderr) {
if (error) return callback(error);
cloneNext(gists.pop());
});
}
});
}
function fetch(page, callback) {
https.get({
host: "api.github.com",
path: "/users/" + encodeURIComponent(user) + "/gists?page=" + page,
headers: {"User-Agent": "node"}
}, function(response) {
var body = [];
response
.on("data", function(data) { body.push(data); })
.on("end", function() { callback(null, JSON.parse(body.join(""))); })
.setEncoding("utf8");
}).on("error", function(error) {
callback(error, null);
});
}
function directoryExists(path) {
try {
return fs.lstatSync(path).isDirectory();
} catch (ignored) {
return false;
}
}
#!/bin/bash
for i in *; do
if [ -d $i ]; then
pushd $i
git commit -am '' --allow-empty-message && git push
popd
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment