Skip to content

A guide on how to create a new branch, switch to it and delete it. #588

Closed

Description

@gabipetrovay

It would be useful to have a guide that shows us how to create a new branch, do something with it, and delete it.

I tried both repo.checkoutBranch(name); and Git.Branch.create(repo, name, commit, 0, Git.Signature.now("Gabriel Petrovay", "gabipetrovay@gmail.com|~ ", "")).then(function (branch) { ... }) but no success. The former has no effect and the latter also complains that a callback is missing (which one I have no idea).

Activity

  1. gabipetrovay commented on May 21, 2015

    @gabipetrovay
    Author

    This is one of my trials:

    var Git = require("nodegit");
    
    exports.create = function (repoPath, name, options, callback) {                                                   
    
        if (typeof options === "function") {                                                                          
            callback = options;
            options = {};
        }       
    
        options = Object(options);                                                                                    
        options.sourceBranch = options.sourceBranch || "master";                                                      
    
        // open the repo
        openRepo(repoPath, function (repo) {                                                                          
            // get the source branch most recent commit                                                               
            repo.getBranchCommit(options.sourceBranch).then(function (commit) {                                       
                // create the branch                                                                                  
                Git.Branch.create(repo, name, commit, 0, Git.Signature.now("Gabriel Petrovay", "gabipetrovay@gmail.com", "")).then(function (branch) {
                    callback(null, branch);                                                                           
                }, ec(callback));
            }, ec(callback));
        }, ec(callback));                                                                                             
    };  
    
    function openRepo (path, callback, errCb) {
        var repoPath = require("path").resolve(path);
        return Git.Repository.open(repoPath).then(callback, errCb);                                                   
    }           
    
    function ec (callback) {
        return function(err) {
            callback(err);
        }
    }
    

    then I call:

    branch.create(".", "master_1", function (err) {
        if (err) {
            console.error(err);
        }
    });
    

    The answer is:

    [Error: Callback is required and must be a Function.]
    

    which is thrown from the Git.Branch.create line.

  2. gabipetrovay commented on May 21, 2015

    @gabipetrovay
    Author

    The other trial is:

    exports.create_alt = function (repoPath, name, options, callback) {
    
        if (typeof options === "function") {
            callback = options;
            options = {};
        }
    
        options = Object(options);
        options.sourceBranch = options.sourceBranch || "master";
    
        // open the repo
        openRepo(repoPath, function (repo) {
            repo.checkoutBranch(name);
            callback(null);
        }, ec(callback));
    };
    

    and calling:

    branch.create_alt(".", "master_1", function (err) {
        if (err) {
            console.error(err);
        }
    });
    

    nothing happens. The repo remains on the master branch.

    I also did not find the equivalent of -b option in the checkout options

  3. maxkorp commented on May 21, 2015

    @maxkorp
    Collaborator

    it looks like logMessage argument (last arg) in the create is missing. Try adding that (empty string should be fine).
    Otherwise, we generally use the gitter channel for support questions. I'm going to close this issue in lieu of support there (unless this ends up being an actual bug, in which case I'll reopen it). I'll also add a line for a guide for this in our wanted guides/examples issue and reference this.

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