Overview
TheGitEngine class is the heart of Learn Git Branching. It simulates all git operations in-memory, manages the commit graph structure, and coordinates with the visualization system to display changes.
Location: src/js/git/index.js
Architecture
Class Structure
Key Properties
| Property | Type | Description |
|---|---|---|
refs | Object | Map of all references (commits, branches, tags, HEAD) by ID |
rootCommit | Commit | The initial C0 commit |
HEAD | Ref | Points to current branch or commit (detached HEAD) |
origin | GitEngine | Separate GitEngine instance simulating remote repository |
mode | String | Either ‘git’ or ‘hg’ for Mercurial simulation |
commitCollection | Collection | Backbone collection of all commits |
branchCollection | Collection | Backbone collection of all branches |
tagCollection | Collection | Backbone collection of all tags |
gitVisuals | GitVisuals | Reference to visualization system |
eventBaton | EventBaton | Event handling system for commands |
Core Operations
Initialization
init()
Creates the initial repository state:Commit Operations
makeCommit(parents, id, options)
Creates a new commit with automatic ID generation:commit(options)
Performs a git commit operation:Branch Management
makeBranch(id, target)
Creates a new branch:validateBranchName(name)
Validates branch names according to git rules:Reference Resolution
resolveID(idOrTarget)
Resolves string references to actual objects:resolveStringRef(ref)
Handles complex reference resolution including relative refs:resolveRelativeRef(commit, relative)
Handles~ and ^ relative reference operators:
Remote Operations
Origin Simulation
Remote repositories are simulated by creating a separateGitEngine instance:
makeOrigin(treeString)
Creates a remote repository:Push Operation
push(options)
Implements git push with fast-forward checking:Fetch Operation
fetch(options)
Downloads commits from remote:Tree Management
Tree Export/Import
exportTree()
Serializes entire git graph to JSON:loadTree(tree)
Restores git state from exported tree:VCS Mode Support
GitEngine supports both Git and Mercurial:handleModeChange(vcs, callback)
Event System
GitEngine uses the EventBaton pattern for command processing:Key Algorithms
Unique ID Generation
Graph Difference Algorithm
Finds commits to transfer between local and remote:Related Components
- Visualization System - Renders git graph
- Command Processing - Parses and executes commands
- Level System - Uses GitEngine for challenges