Skip to main content

Crate git_fast_import

Crate git_fast_import 

Source
Expand description

A library for generating git fast-import streams.

This crate provides a simple, type-safe API for creating git commits programmatically by generating a stream that can be piped to git fast-import.

§Example

Write to a buffer:

use git_fast_import::{GitFastImporter, GITHUB_BOT_AUTHOR};

let mut output = Vec::new();
let mut importer = GitFastImporter::new(
    &mut output,
    "main".to_string(),
    None,
    GITHUB_BOT_AUTHOR.to_string(),
);

let mut commit = importer.start_commit("Initial commit", chrono::Utc::now());
commit.add_file("hello.txt", b"Hello, World!").unwrap();
commit.finish().unwrap();
importer.finish().unwrap();

// `output` now contains a valid git fast-import stream

Pipe directly to git fast-import:

use git_fast_import::{GitFastImporter, GITHUB_BOT_AUTHOR};
use std::process::{Command, Stdio};

let mut child = Command::new("git")
    .args(["fast-import", "--quiet"])
    .stdin(Stdio::piped())
    .spawn()
    .expect("failed to spawn git fast-import");

let stdin = child.stdin.take().unwrap();
let mut importer = GitFastImporter::new(
    stdin,
    "main".to_string(),
    None,
    GITHUB_BOT_AUTHOR.to_string(),
);

let mut commit = importer.start_commit("Initial commit", chrono::Utc::now());
commit.add_file("src/lib.rs", b"// new file").unwrap();
commit.finish().unwrap();
importer.finish().unwrap();

let status = child.wait().expect("failed to wait on git fast-import");
assert!(status.success());

Structs§

CommitBuilder
Builder for constructing a single commit.
GitFastImporter
Generates a git fast-import stream.

Constants§

GITHUB_BOT_AUTHOR
A committer string for GitHub Actions bot.