-#!/bin/sh
+#!/usr/bin/perl
-# suck in a pile of files, commit based on timestamp,
-# set author
+use File::Find;
+use File::Find::Closures qw(find_regular_files);
+use Git::Repository;
+use Time::Stamp qw(gmstamp);
-: ${AUTHOR:="Unknown <>"}
+my ($want, $files) = find_regular_files();
-base="$1"
+my $author = shift;
+$author = 'Unknown' unless defined $author;
+my $email = shift;
+$email = '<>' unless defined $email;
-test -d "$base/.git" || git init "$base"
+my $dir = shift;
+$dir = '.' unless $dir;
-cd $base
+File::Find::find($want, $dir);
-set -x
-prevts=
-find . -name .git -prune -o -type f -printf '%T@ %P\n' | sort -n | while read ts fn; do
- if [ -n "$prevts" ] && [ "$prevts" != "$ts" ]; then
- git commit --date="$prevts" --author="$AUTHOR" -m 'auto commit for import'
- printf '\n' "$ts"
- fi
- git add "$fn"
- printf '%s\n' "$fn"
- prevts="$ts"
-done
+my %ts;
-find . -name .git -prune -o -type f -printf '%T@ %P\n' | sort -n | tail -1 | while read ts fn; do
- git commit --date="$ts" --author="$AUTHOR" -m 'auto commit for import'
-done
+foreach ($files->()) {
+ next if m|^\.git/|;
+ next if m|^git-slurp$|;
+ my $ts = gmstamp((stat $_)[9]);
+ push @{$ts{$ts}}, $_;
+}
+
+my $git = Git::Repository->new({
+ env => {
+ GIT_AUTHOR_EMAIL => $email,
+ GIT_AUTHOR_NAME => $author,
+ },
+ }
+);
+
+print $git->work_tree, "\n";
+foreach my $ts (sort { $a cmp $b } keys %ts) {
+ print "$ts @{$ts{$ts}}\n";
+ foreach my $file (@{$ts{$ts}}) {
+ $git->run('add', $file);
+ }
+ $git->run('commit', '--date', $ts, '-m', "add files for $ts");
+}