]> pd.if.org Git - scripts/commitdiff
rewrite git-slurp in perl master
authorNathan Wagner <nw@hydaspes.if.org>
Sat, 29 Sep 2018 18:59:11 +0000 (18:59 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 29 Sep 2018 18:59:11 +0000 (18:59 +0000)
git-slurp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index a21539a..6022b65
--- a/git-slurp
+++ b/git-slurp
@@ -1,28 +1,44 @@
-#!/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");
+}