@thinkeloquent/git-file-id
v0.1.0
Published
Generate GitHub URLs from Git file paths with commit hash - WASM module
Maintainers
Readme
git-file-id
A Rust/WASM library to generate GitHub URLs from Git file paths with commit hashes, and access Git objects (blobs, trees, commits, tags).
Features
- Generate GitHub URLs for files with specific commit hashes
- Search upward for
.gitdirectory from any file path - Access Git objects: blobs, trees, commits, and tags
- Support for both SSH and HTTPS GitHub remotes
- Fast WASM implementation
Installation
npm install git-file-idUsage
Basic Usage - Generate GitHub URL
const { GitFileId, generate_github_url_direct } = require('git-file-id');
// Quick one-liner
const url = await generate_github_url_direct('/path/to/your/file.js');
console.log(url);
// Output: https://github.com/owner/repo/blob/abc123.../path/to/file.js
// Using the class for multiple operations
const git = new GitFileId();
await git.find_repository('/path/to/your/file.js');
const url = await git.generate_github_url('/path/to/your/file.js');
console.log(url);Access Git Objects
const git = new GitFileId();
await git.find_repository('/path/to/repo');
// Get commit information
const commit = await git.get_commit('abc123...');
console.log(commit.message);
console.log(commit.author_name);
// Get blob (file) information
const blob = await git.get_blob('def456...');
console.log(blob.size);
console.log(blob.is_binary);
// Get tree (directory) information
const tree = await git.get_tree('789abc...');
console.log(tree.len); // Number of entries
// Get tag information
const tag = await git.get_tag('tag123...');
console.log(tag.name);
console.log(tag.message);Other Operations
const git = new GitFileId();
await git.find_repository('/path/to/repo');
// Get file hash for current branch
const hash = await git.get_file_hash('src/main.js');
// Get file status
const status = await git.get_file_status('src/main.js');
// Returns: 'clean', 'modified', 'untracked', etc.
// Get HEAD reference
const head = await git.get_head_ref();
// Returns: branch name or commit hash if detached
// List all references
const refs = await git.list_refs();
// Returns: array of reference names (branches, tags, etc.)API Reference
GitFileId Class
Constructor
new GitFileId()- Create a new instance
Methods
find_repository(file_path: string)- Find the Git repository rootgenerate_github_url(file_path: string)- Generate GitHub URL for a fileget_file_hash(file_path: string)- Get the commit hash for a fileget_blob(hash: string)- Get blob object by hashget_tree(hash: string)- Get tree object by hashget_commit(hash: string)- Get commit object by hashget_tag(hash: string)- Get tag object by hashlist_refs()- List all Git referencesget_file_status(file_path: string)- Get file statusget_head_ref()- Get current HEAD reference
Standalone Functions
generate_github_url_direct(file_path: string)- Generate GitHub URL directly without creating an instance
Git Object Types
GitBlob
id: String - Object hashsize: Number - Blob size in bytesis_binary: Boolean - Whether the blob is binary
GitTree
id: String - Object hashlen: Number - Number of entries in the tree
GitCommit
id: String - Commit hashmessage: String - Commit messageauthor_name: String - Author nameauthor_email: String - Author emailtime: Number - Commit timestamp (seconds since epoch)tree_id: String - Tree object hash
GitTag
id: String - Tag hashname: String - Tag namemessage: String - Tag messagetarget_id: String - Target object hash
Building from Source
# Clone the repository
git clone https://github.com/yourusername/git-file-id.git
cd git-file-id
# Build WASM module
wasm-pack build --target nodejs
# Run tests
cargo testLicense
MIT
