1

I am trying to setup a central repository in my local, running on NodeJS, where others can access, create their own git repository, clone, commit, push, pull from their git repository. The idea is more like to setup a local Github running on NodeJS. I am currently using nodegit library.

I am able to create a new repository (git init --bare) by using Git.Repository.init(repopath, is_bare) and also set a custom clone url to that repo (git remote set-url) using Git.Remote.setUrl(repository, remote, cloneUrl)

But I am stuck at creating the clone URL like "http://example.com/repo/example.git". How can I create a repo URL in NodeJS which others can use to git clone? Where should it point? What kind of response should I provide for the repo URL?

Currently my code stands as:

var Git = require('nodegit');
var path = require('path');
var fs = require('fs');

exports.repo_create = function(req, res) {
  // Create repository folder if doesn't exist which will include all repos.
  const dir = path.join(__dirname, '/../../repository');
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
  }

  if (typeof req.body.reponame !== 'undefined' && req.body.reponame !== null) {
    
    // Creating req.body.reponame folder under repository folder.
    // repository/{req.body.reponame}
    const repopath = path.join(dir, req.body.reponame);
    if (!fs.existsSync(repopath)) {
      fs.mkdirSync(repopath);
    }
    
    var is_bare = 1;
    // Initialising bare repo.
    Git.Repository.init(repopath, is_bare).then(function(repository) {
      const remote = 'origin';
      var cloneUrl = req.protocol + '://' + req.get('host') + '/repos/' + req.body.reponame + '.git';
      
      // Setting http://www.example.com/repos/example.git as repo url for individual git repo under repositories/example folder.
      Git.Remote.setUrl(repository, remote, cloneUrl);
      
      // Sending JSON response from Node API.
      res.json({ status: 'SUCCESS', message: req.body.reponame + ' repository created' });
    }).catch(function(error) {
      // Sending JSON response from Node API.
      res.json({ status: 'FAILURE', message: error });
    });
  } else {
    // Sending JSON response from Node API.
    res.json({ status: 'FAILURE', message: 'Failed to create repository' });
  }
}

Any help is appreciated!!!

0 Answers0