Write some git commands by hand

Git, as an indispensable part of enterprise development, can be said to be closely related to our future code life.

    git add fileRoute    // Add the specified file to the staging area
    git commit -m "What are you submitting?"    // Add the specified directory to the staging area, including subdirectories
    git push origin master       // Upload to remote cloud
    
    git log --oneline       // View the commit record
    git checkout -b "branchname"    // Create a branch and switch branches.
    git merge "branchname"      // Merge branches
Copy the code

The first three commands need to be typed several times almost every day, can be said to be a friend, but this friend is how to do it, today to study, a little rough, please forgive me.

Get ready to open

You need to download git software so you can use the commands. (The purpose of using commands is to compare with handwritten ones)

You need to download the Node. js software so that you can run the JS file from the command line.

Began to cough up

  1. To initialize git, use git init — this will appear in the next directory.
  2. Create a JS file with all the code in it, using a prototype chain

Git class

Create a Git function that represents a repository class that can be created using the new keyword.

Classes have repository names, version ids (with variable increment to ensure no duplication), branch arrays, primary branch instances…

function Git(name) {
    / / warehouse
    this.name = name;
    / / version id
    this.lastCommitId = -1;
    // Branch the array
    this.branches = [];
    // The default is master.
    this.master = new Branch('master'.null);
    this.branches.push(this.master);
    / / the head pointer
    this.HEAD = this.master;
}
// Create the test repository and assign it to the repo variable
var repo = new Git("test");
Copy the code

Branch class

Branch class, can create multiple branches, because the master branch can not be modified at will, so we need to create branches to modify and test.

function Branch(name, commit) {
    / / branch name
    this.name = name;
    this.commit = commit
}
Copy the code

Commit class

Create a Commit class with the current version ID, the previous version parent (version ID), and Commit instructions

function Commit(id, parent, message) {
    this.id = id;
    this.parent = parent;
    this.message = message;
}
Copy the code

Using prototypes, each repository can have a COMMIT capability.

Git.prototype.commit = function (message) {
    var commit = new Commit(++this.lastCommitId, this.HEAD.commit, message)
    // The header pointer points to the newly created commit class node
    this.HEAD.commit = commit;
    return commit;
}

repo.commit("hello1")// To print, use console.log(repo.com MIT ("hello1"))
Copy the code

That’s it

git log –oneline

The same usage of the prototype allows each repository to have a log view of historical commit records.

A linked list like operation is used to find the previous version step by step

Git.prototype.log = function () {
    var commit = this.HEAD.commit,
        history = [];
    while (commit) {
        history.push(commit);
        commit = commit.parent
    }
    return history
}
// Add more records
repo.commit("hello2")
repo.commit("hello3")
console.log(repo.log());
Copy the code

The above image returns all the contents of each commit record, which we normally use with Git log –oneline doesn’t need

So we can create a function that returns only the version ID

repo.commit("change1")
repo.commit("change2")
repo.commit("change3")
function historyToIdMapper(history) {
    var ids = history.map(commit= > commit.id)
    return ids.join("-")}console.log(historyToIdMapper(repo.log()));
Copy the code

Create a branch

The same usage of the prototype allows each repository to have other branches, ensuring that the Master branch does not change.

Git checkout -b “branchname” creates a branch using git checkout -b “branchname”. Use git Checkout “branchname” to create a new branch and move the head pointer to the head of the current branch.

Git.prototype.checkout = function (branchName) {
    for (var i = this.branches.length; i--;) {
        if (this.branches[i].name == branchName) {
            console.log('Switched to existing branch: ' + branchName);
            this.HEAD = this.branches[i];
            return this; }}var newBranch = new Branch(branchName, this.HEAD.commit);
    this.branches.push(newBranch);
    this.HEAD = newBranch;
    console.log(`Switched to new branch: ` + branchName);
    // Returns the current branch
    return this;
}


console.log(historyToIdMapper(repo.log()));
repo.checkout("testing")
repo.commit("change4")
console.log(historyToIdMapper(repo.log()));
repo.checkout("master")
console.log(historyToIdMapper(repo.log()));
Copy the code

Commit once after the branch is created, view the records separately, and then select the Master branch to see if the master branch is affected by the new branch

Merging branches

I just wrote merge master and one branch

Git.prototype.Merge = function (branchName){
    for (var i = this.branches.length; i--;) {
        if (this.branches[i].name == branchName) {
            var merge = [], commit = this.branches[i].commit,temp
            while(commit ! =this.master.commit) {
                merge.unshift(commit)
                commit = commit.parent
            }   
            merge.forEach(val= >{
                temp = this.master.commit
                this.master.commit = val
                this.master.commit.parent =temp
            })
            return this}}console.log(branchName +" branch is not find");
}


/ / test
console.log(historyToIdMapper(repo.log()));
repo.checkout("testing")
repo.commit("change4")
console.log(historyToIdMapper(repo.log()));
repo.checkout("master")
console.log(historyToIdMapper(repo.log()));
repo.Merge("testing")
console.log(historyToIdMapper(repo.log()));
Copy the code

After creating a new branch testing, go to the master branch and call the Merge function to print the results

conclusion

Handwriting is not easy, please give a thumbs-up to the article, thank you. I wish you a happy Mid-Autumn Festival in advance.