#!/usr/bin/perl -w
#
# Add new file to a GIT repository.
# Copyright (c) Petr Baudis, 2005
# Copyright (c) David Greaves, 2005
#

# FIXME: Those files are omitted from show-diff output!

use IO::File;   # leads to less perlish syntax and is standard in perl dists
use Pod::Usage;
use Cogito;

my $processed_stdin;
my $queue;
my $count = 0;
my $recurse;

sub add
# takes a list of filenames, - is opened and treated as a standard list
  {
    help(-verbose=>0, -exitval=>1) unless $_[0]; # exit if no args
    help(-verbose=>1, -exitval=>0) if $_[0] eq "--help";
    help(-verbose=>2, -exitval=>0) if $_[0] eq "--man";

    if ($_[0] eq '-r') { # Recurse?
      $recurse = 1;
      shift;
    }

    $queue = IO::File->new(">> $repo/add-queue") || die "Couldn't open add-queue $!";
    _add_files(@_);
    $queue->close;
    error("Added $count files for commital") if $verbose;
  }

sub _add_files
  {
    foreach $filename (@_) {
      if ($filename eq "-" && !$processed_stdin) {
	$processed_stdin=1; # avoid trying to process it twice if someone does "git add foo - bar -"
	while (<STDIN>) {
	  chomp;
	  _add_files($_);
	}
      } elsif (ignore($filename)) {
	next;
      } elsif (-d $filename && $recurse) {
	# FIX recurse into directory...
	# _add_files(glob "$filename/*"); # git ignores .files FIX: Check this?
      } else {
	error("adding non-existent file $filename") unless -f $filename;
	$queue->print($filename);
	$count++;
      }
    }
  }

1;
__END__

=head1 git add

add - Add new file or files to a GIT repository.

=head1 SYNOPSIS

add - Add new file or files to a GIT repository.

Usage: git add [-r] (-|FILE)...";

=head1 OPTIONS

	[-r]  recurse on any directories listed in FILE...
              not implemented yet but I want to see the help :)

=head1 DESCRIPTION

Takes a list of file names at the command line, and schedules them for
addition to the GIT repository at the next commit.


=cut
