or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

git-repo-info

git-repo-info is a lightweight Node.js library that retrieves Git repository information without requiring the git command to be available on the system. It reads Git metadata directly from the filesystem to extract information such as current branch name, commit SHA, tags, committer details, and repository paths.

Package Information

  • Package Name: git-repo-info
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install git-repo-info

Core Imports

import gitRepoInfo from "git-repo-info";

For CommonJS:

const gitRepoInfo = require("git-repo-info");

Basic Usage

const gitRepoInfo = require("git-repo-info");

// Get info for current directory (searches upward for .git)
const info = gitRepoInfo();

console.log(info.branch);              // 'main'
console.log(info.sha);                 // '5359aabd3872d9ffd160712e9615c5592dfe6745'
console.log(info.abbreviatedSha);      // '5359aabd38'
console.log(info.tag);                 // 'v1.0.0' or null
console.log(info.lastTag);             // 'v0.9.0' or null
console.log(info.commitsSinceLastTag); // 5

// Get info for specific directory
const specificInfo = gitRepoInfo('/path/to/repo');

Capabilities

Repository Information Retrieval

Retrieves comprehensive Git repository information from the filesystem without requiring the git command.

/**
 * Retrieves Git repository information for the specified path or current directory
 * @param gitPath - Optional path to the repository's .git folder. If not provided, searches upward from current directory
 * @returns GitRepoInfo object containing repository metadata
 */
function gitRepoInfo(gitPath?: string): GitRepoInfo;

interface GitRepoInfo {
  /** The current branch name (null if in detached HEAD state or repo not found) */
  branch: string | null;
  /** SHA hash of the current commit (null if repo not found) */
  sha: string | null;
  /** First 10 characters of the current SHA (null if repo not found) */
  abbreviatedSha: string | null;
  /** Tag for the current SHA (null if no tag exists or repo not found) */
  tag: string | null;
  /** Tag for the closest tagged ancestor (null if no ancestor is tagged or repo not found) */
  lastTag: string | null;
  /** Number of commits since the closest tagged ancestor (0 if current commit is tagged, Infinity if no ancestor is tagged) */
  commitsSinceLastTag: number;
  /** Committer name and email for the current SHA (null if commit data unavailable or repo not found) */
  committer: string | null;
  /** Commit date for the current SHA in ISO format (null if commit data unavailable or repo not found) */
  committerDate: string | null;
  /** Author name and email for the current SHA (null if commit data unavailable or repo not found) */
  author: string | null;
  /** Authored date for the current SHA in ISO format (null if commit data unavailable or repo not found) */
  authorDate: string | null;
  /** Commit message for the current SHA (null if commit data unavailable or repo not found) */
  commitMessage: string | null;
  /** Root directory for the Git repo or submodule (null if repo not found) */
  root: string | null;
  /** Directory containing Git metadata for this repo or submodule (null if repo not found) */
  commonGitDir: string | null;
  /** Directory containing Git metadata specific to worktree (same as commonGitDir if not in worktree, null if repo not found) */
  worktreeGitDir: string | null;
  /** Array of parent commit SHA hashes (available when commit data is accessible) */
  parents?: string[];
}

Usage Examples:

const gitRepoInfo = require("git-repo-info");

// Basic usage - get info for current directory
const info = gitRepoInfo();
if (info.sha) {
  console.log(`Current commit: ${info.sha} on branch ${info.branch}`);
  if (info.tag) {
    console.log(`Tagged as: ${info.tag}`);
  } else if (info.lastTag) {
    console.log(`${info.commitsSinceLastTag} commits since ${info.lastTag}`);
  }
}

// Get info for specific path
const projectInfo = gitRepoInfo('/path/to/project');

// Handle repositories that might not exist
const safeInfo = gitRepoInfo('./maybe-not-a-repo');
if (!safeInfo.sha) {
  console.log('Not a git repository');
}

// Access detailed commit information (when available)
if (info.committer && info.commitMessage) {
  console.log(`Last commit by ${info.committer}: "${info.commitMessage}"`);
  console.log(`Committed on: ${info.committerDate}`);
}

// Working with worktrees and submodules
console.log(`Repository root: ${info.root}`);
console.log(`Git directory: ${info.commonGitDir}`);
if (info.worktreeGitDir !== info.commonGitDir) {
  console.log(`Worktree git directory: ${info.worktreeGitDir}`);
}

Error Handling Configuration

Controls error handling behavior of the library.

/** Controls whether errors are suppressed (default: true) */
gitRepoInfo._suppressErrors: boolean;

When _suppressErrors is true (default), the function returns an object with null values instead of throwing errors. When false, errors are thrown for debugging purposes.

Internal Testing Utilities

Utility functions exposed for testing purposes.

/**
 * Internal function to find repository location (exposed for testing)
 * @param startingPath - Starting directory for repository search
 * @returns Repository path information or null
 */
gitRepoInfo._findRepo(startingPath?: string): PathInfo | null;

/**
 * Internal function to change the Git directory name (exposed for testing)
 * @param newDirName - New directory name to use instead of '.git'
 */
gitRepoInfo._changeGitDir(newDirName: string): void;

interface PathInfo {
  /** Directory containing worktree-specific Git metadata */
  worktreeGitDir: string;
  /** Directory containing common Git metadata */
  commonGitDir: string;
  /** Root directory of the repository */
  root: string;
}

Supported Repository Types

The library supports various Git repository configurations:

  • Standard repositories: Regular .git directories
  • Worktrees: Git worktrees created with git worktree add
  • Submodules: Git submodules with separate metadata directories
  • Packed references: Repositories with packed-refs for performance
  • Bare repositories: Repositories without working directories

Error Handling

By default, the library suppresses errors and returns an object with null values when:

  • No Git repository is found
  • Git metadata is corrupted or inaccessible
  • Required zlib functionality is unavailable (Node.js compatibility)
// Default behavior - returns object with null values on error
const info = gitRepoInfo('/not/a/repo');
console.log(info.sha); // null

// Enable error throwing for debugging
gitRepoInfo._suppressErrors = false;
try {
  const info = gitRepoInfo('/not/a/repo');
} catch (error) {
  console.error('Repository error:', error.message);
}

Environment Compatibility

  • Node.js: Minimum version 4.0+
  • Dependencies: Uses only Node.js built-in modules (fs, path, zlib)
  • zlib Support: Gracefully handles environments without zlib.inflateSync (Node.js v0.10)
  • Cross-platform: Works on Windows, macOS, and Linux filesystems