Core Philosophy
Learn Git Branching has no backend database or AJAX requests. Everything runs in the browser:- All git operations are simulated using in-memory data structures
- The visualization updates dynamically as commands are executed
- Educational levels are embedded in the JavaScript bundle
- State is transient - no persistence between sessions (unless using localStorage)
System Components
GitEngine
Location:src/js/git/index.js
The heart of the application. GitEngine simulates all git operations:
- Manages the commit graph (commits, branches, tags, refs, HEAD)
- Processes git commands and updates internal state
- Dispatches events to the visualization system
- Supports both git and Mercurial (hg) command modes
- Uses the EventBaton pattern for command handling
- Creating and manipulating commits
- Branch and tag management
- Simulating merge, rebase, cherry-pick, and other git operations
- HEAD pointer management
- Remote repository simulation (via separate GitEngine instances)
Visualization System
Location:src/js/visuals/
Renders the commit tree using Raphael.js (SVG graphics):
visualization.js
Main visualization controller that coordinates all visual updates
tree.js
Renders the commit tree structure using Raphael.js
visNode.js
Visual representation of individual commits
visBranch.js
Visual representation of branches
visTag.js
Visual representation of tags
visEdge.js
Visual representation of commit parent relationships
src/js/visuals/animation/):
- All visual updates go through an animation queue
- Uses Q promises to chain animations smoothly
- Can be disabled for testing (headless mode)
- Ensures visual consistency during complex operations
Command Processing
Location:src/js/commands/index.js, src/js/git/commands.js
Processes user input and executes commands:
- Commands are defined with regex patterns for parsing
- Each command has an execute function that manipulates GitEngine
- Supports command delegation (commands can invoke other commands)
- Handles both git and Mercurial command syntaxes
- Provides help text and command documentation
Levels System
Location:src/levels/
Educational challenges organized into sequences:
- Level Structure
- Level Sequences
Each level is a JavaScript module with:
- Name and description: What the level teaches
- Starting tree state: Initial commit graph configuration
- Goal description: What the user needs to achieve
- Solution validation: TreeCompare logic to verify completion
- Hint system: Optional hints for stuck users
src/levels/index.js):
- Imports all level modules
- Organizes them into sequences
- Provides level metadata and progression tracking
Flux Architecture
The app uses a unidirectional data flow pattern inspired by Facebook’s Flux:src/js/dispatcher/AppDispatcher.js):
- Central event bus using Facebook’s Flux Dispatcher
- Handles
VIEW_ACTIONandURI_ACTIONpayload sources
src/js/stores/):
CommandLineStore.js: Command line state and historyLevelStore.js: Current level and completion progressLocaleStore.js: Internationalization and languageGlobalStateStore.js: Global application state
src/js/actions/):
- Action creators for CommandLine, Level, Locale, and GlobalState
- Provide a clean API for triggering state changes
UI Components
Learn Git Branching uses a mix of legacy and modern UI frameworks: React Components (src/js/react_views/):
- Command line interface with syntax highlighting
- Toolbar and helper bars
- Command history view
- Modern, maintainable components
src/js/views/):
- Legacy modal dialogs and builders
gitDemonstrationView.js: Animated command demonstrations- Level builder and custom level import
- Gradually being migrated to React
Key Patterns
EventBaton
Location:src/js/util/eventBaton.js
A pattern for transferring event handling responsibility:
- Commands can “grab” the event baton to handle subsequent events
- Used extensively for multi-step operations (interactive rebase, level building)
- Prevents event conflicts during complex workflows
TreeCompare
Location:src/js/graph/treeCompare.js
Compares two commit trees for level completion validation:
- Checks commit structure, branch positions, and tags
- Allows flexible matching (order-independent, ref-name-independent)
- Provides detailed diff output for debugging
- Used by both the level system and test suite
Remote Repository Simulation
Remote operations (push/pull/fetch) are simulated by:- Creating a separate GitEngine instance for the “origin” repository
- Implementing network operation logic (transfer commits, update refs)
- Using the
o/prefix to denote remote tracking branches - Synchronizing state between local and remote engines
Project Structure
Key Technologies
Backbone.js
MVC framework for legacy UI components
React 17
Modern UI components and views
Flux
Unidirectional data flow architecture
Raphael.js
SVG graphics library for visualization
Q Promises
Promise library for animation chains
jQuery
DOM manipulation and UI widgets
Browserify
Module bundling and dependency management
Babel
JSX transformation and modern JavaScript
Testing Architecture
Location:__tests__/
The test suite uses Jasmine and several custom utilities:
- Headless GitEngine (
src/js/git/headless.js): Git engine without visualization for fast tests - Create helper (
__tests__/util/create.js): Set up test commit trees quickly - Mock utility (
__tests__/util/mock.js): Mock commands and responses - Test coverage: Git operations, remote operations, levels, tree comparison, command parsing
Internationalization
Location:src/js/intl/
Supports multiple languages:
- String translations in
strings.js(keyed by locale) - Dialog translations via
getDialog() - Level descriptions and hints localized
- Locale validation via
checkStrings.js - Use
intl.str()to access translated strings throughout the codebase
Build Process Flow
JavaScript Bundling
Browserify bundles all modules starting from entry points, handling
require() statements and dependenciesTemplate Processing
template.index.html is processed to inject bundled asset paths and generate index.htmlOptimization (Production)
- JavaScript minified with Terser
- CSS minified with clean-css
- Filenames hashed for cache busting
- HTML minified
Understanding the Codebase
When contributing, focus on these key areas:- GitEngine (
src/js/git/index.js) - Core git simulation logic - Commands (
src/js/commands/,src/js/git/commands.js) - Adding new commands - Visualization (
src/js/visuals/) - How commits are rendered - Levels (
src/levels/) - Creating educational content - Tests (
__tests__/) - Ensuring correctness