Skip to content

Instantly share code, notes, and snippets.

@timabell
Last active August 7, 2018 07:46
Show Gist options
  • Select an option

  • Save timabell/fae531441e30d42f8b6c to your computer and use it in GitHub Desktop.

Select an option

Save timabell/fae531441e30d42f8b6c to your computer and use it in GitHub Desktop.
#abandonded# add remotes to a git repository for all the forks on github. not as good as https://github.com/frost-nzcr4/find_forks
#!/usr/bin/env node
console.log('use https://github.com/frost-nzcr4/find_forks instead')
process.exit()
// copy of clone-all-gists modified to get all forks of a repo
// https://gist.github.com/timabell/fae531441e30d42f8b6c
var fs = require("fs");
var https = require("https");
// var exec = require("child_process").exec;
// https://stackoverflow.com/a/50335035/10245
const { promisify } = require('util');
const exec = promisify(require('child_process').exec)
init();
async function init(){
var origin = await getOrigin();
console.log("asking api for fork list...");
apiFetch(user + '/' + repo, function callback(error, forks) {
if (error){
console.log(error);
return;
}
console.log("fork list received, adding remotes...");
addRemote(forks.pop(), addRemote);
console.log("all done.");
});
}
async function getGitUser () {
var cmd = 'git config user.name';
console.log(cmd)
const name = await exec(cmd)
const email = await exec('git config user.email')
console.log('hello');
console.log('name', name);
console.log('email', email);
return { name, email }
};
async function getOrigin(callback) {
var cmd = 'git config remote.origin.url';
console.log(cmd);
const url = await exec(cmd)
console.log(url);
}
function addRemote(fork, callback) {
console.log(fork.full_name);
if (directoryExists('.git/refs/remotes/' + fork.owner.login)){
console.log("... already configured");
return
}
cmd = "git remote add '" + fork.owner.login + "' '" + fork.clone_url + "'";
console.log(cmd);
exec(cmd, function(err, stdout, stderr) {
if (err){
console.log(err);
}
});
}
function apiFetch(repo, callback) {
// max 100 per page https://developer.github.com/v3/#pagination
var request = {
host: "api.github.com",
path: "/repos/" + repo + "/forks?per_page=100",
headers: {"User-Agent": "node"}
};
console.log("Fetching http://" + request.host + request.path);
https.get(request, 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment