Skip to content

Cloning git sub modules using "nodegit" npm module #560

Closed

Description

@shivakumarpm

While cloing any git modules (https://github.com/xyz.git) if that modules is dependent on sub modules to clone, then can i or is it possible to clone git submodules using "nodegit" modules (https://github.com/nodegit/nodegit).

Activity

  1. tbranyen commented on May 11, 2015

    @tbranyen
    Member

    Yes it is possible. See the OP here for clues on how to do it: libgit2/libgit2sharp#640

  2. shivakumarpm commented on May 13, 2015

    @shivakumarpm
    Author

    Hi @tbranyen, one thing i am not able to understand that "libgit2sharp" is a native module, i want to clone recursively without installing git on my system. Instead i want to use node module to clone recursively. i used nodegit module like " var clone = require("nodegit").Clone.clone;
    // Clone a given repository into a specific folder.
    clone("https://github.com/xyz.git", "bootplate-moonstone", null)
    .then(function(repo) {
    finish();
    })
    .catch(function(err) { finish(err, {msg : 'error while cloning'});});" but it wont clones sub modules.
    Could you please guide me how to clone sub modules using "nodegit"?

  3. maxkorp commented on May 13, 2015

    @maxkorp
    Collaborator

    Libgit2sharp is a wrapper for libgit2 in c#, the same way that nodegit is a wrapper for libgit2 for javascript (albeit largely written in C++). The same series of commands you would use in one shouldmostly apply in the other.

    What Tim was saying is that this is the algorithm for doing what you want to do, just use nodegit commands instead of libgit2sharp commands, since the structure carries over.

    If you need more help, talk to us in our gitter room https://gitter.im/nodegit/nodegit and we'll get you all sorted out 👍

  4. ggendre commented on Aug 4, 2015

    @ggendre

    Hi,

    I'm having the same need as @shivakumarpm : I want to clone my repository and init and update all submodules recursively.

    I understand from @maxkorp reply that I should use the same algorithm as used in the linked commit. Which is :

    foreach (Submodule module in this)
    {
        module.Init(options);
        module.Update(options);
        using (Repository subrepo = new Repository(module.WorkingDirectory))
        {
            subrepo.Submodules.InitAndUpdateRecursively(options);
        }
    }
    

    So, my problem is to get the submodule list with nodegit. I can't find any function that gives me the submodules list. Did I miss something ?

  5. tbranyen commented on Aug 4, 2015

    @tbranyen
    Member

    @ggendre nope, but we are: https://github.com/nodegit/nodegit/blob/master/generate/input/descriptor.json#L1931-L1933 I don't remember why we don't compile it, but that method is ignored atm

  6. johnhaley81 commented on Aug 4, 2015

    @johnhaley81
    Collaborator

    @ggendre A PR with that method enabled and some tests confirming the functionality would be amazing hint hint nudge nudge

  7. ggendre commented on Aug 5, 2015

    @ggendre

    Sorry, I have not enough time to dive into this.

    Instead, to ugly-fix my needs, I made a loop over the submodule list grabbed from the modules list in .gitmodules

    Ini = require('ini');
    var config = Ini.parse(fs.readFileSync(path.normalize( localPath + '/.gitmodules'), 'utf-8'));
    //if startswith submodule, push into modules
    modules = [];
    for (var i in config) {
        if (i.indexOf('submodule') === 0){
            modules.push(config[i]);
        }
    }
    for (var i in modules) {
        var modulePath = modules[i].path;
        Git.Submodule.lookup(repository, modulePath).then(function(submodule) {
            // Use submodule
            log.success("got submodule '" + submodule.path() + "'");
            try{
                submodule.init(1);
                log.success("submodule init worked");
            }catch(e){
                log.error("submodule init failed "+ e);
            }
            try{
                submodule.update(1,new Git.CheckoutOptions);
                log.success("submodule update worked");
            }catch(e){
                log.error("submodule update failed "+ e);
            }
        },function(err){
            log.error('cant get submodule : '+err)
        });
    }
    

    This doesn't need to be recursive, submodules of submodules are retrieved with the init or update function.

    This is quite ugly but I'm posting it here if it can help anybody. Maybe one day I will dive into the writing of tests you talked about, but not now :)

    have a good day,

  8. papiro commented on Sep 19, 2015

    @papiro

    @ggendre, I can't believe I didn't find your post sooner. I looked everywhere for an example of how to do this and couldn't find anything! I too had the same issue and decided to manually parse the .gitmodules file and I even was having trouble getting the right syntax for submodule.update (didn't know about new Git.CheckoutOptions). Thanks!

  9. ggendre commented on Jul 15, 2016

    @ggendre

    for the record, nodegit now has a foreach and lookupmethod to browse submodules

  10. felixfbecker commented on Dec 28, 2016

    @felixfbecker

    @ggendre Do you have a small snippet that essentially does git clone --recursive? Completely lost

  11. allomov commented on Dec 12, 2017

    @allomov

    @felixfbecker here is a recursive function I have come to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions