update_gitignore

Function update_gitignore 

Source
pub fn update_gitignore(
    lockfile: &LockFile,
    project_dir: &Path,
    enabled: bool,
) -> Result<()>
Expand description

Update .gitignore with all installed resource file paths.

This function updates the project’s .gitignore file to include all resources that are installed by AGPM, preventing accidental commits of managed files. It preserves existing user entries while managing the AGPM section automatically.

§AGPM Section Management

The function maintains a dedicated section in .gitignore:

# AGPM managed entries - do not edit below this line
.claude/agents/example.md
.claude/snippets/shared.md
agpm.private.toml
agpm.private.lock
# End of AGPM managed entries

§Arguments

  • lockfile - The lockfile containing all installed resources and their paths
  • project_dir - The project root directory containing the .gitignore file
  • enabled - Whether gitignore management is enabled (can be disabled via config)

§Behavior

  • Creates new file: If no .gitignore exists, creates one with AGPM section
  • Updates existing file: Preserves user content, adds/replaces AGPM section
  • No-op when disabled: Returns early if gitignore management is disabled
  • Always included: Private config files (agpm.private.toml, agpm.private.lock)
  • Resource types: Includes agents, snippets, commands, and scripts
  • Excludes: Hooks and MCP servers (configuration only, not installed as files)

§Preservation of User Content

The function preserves all non-AGPM content:

  • Existing entries before AGPM section are kept unchanged
  • Existing entries after AGPM section are kept unchanged
  • Only the managed section between the markers is replaced

§Migration Support

Supports migration from legacy CCPM (Claude Code Package Manager):

  • Recognizes both # CCPM managed entries and # AGPM managed entries
  • Automatically converts to AGPM format on update

§Errors

Returns an error if:

  • File cannot be read due to permissions
  • File cannot be written due to permissions or disk space
  • Project directory doesn’t exist

§Examples

use agpm_cli::installer::update_gitignore;
use agpm_cli::lockfile::LockFile;
use std::path::Path;

let lockfile = LockFile::load(Path::new("agpm.lock"))?;
let project_dir = Path::new(".");

// Update .gitignore with all installed resources
update_gitignore(&lockfile, project_dir, true)?;

println!("Gitignore updated successfully");