2 # stodo commits the changes made to a subversion backed todo db, and
3 # is released under the terms of the GPL version 2, or any later
4 # version, at your option. See the file README and COPYING for more
5 # information. Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
18 stodo - devtodo event script which attempts to commit the changes made to a .todo file after a change
25 --simulate, -s simulate: don't actually do anything
26 --quiet, -q don't output to STDOUT
27 --debug, -d debugging level (Default 0)
28 --help, -h display this help
29 --man, -m display manual
35 =item B<--simulate, -s>
37 Simulation mode, don't actually commit anything.
41 Don't output anything to STDOUT
45 Debug verbosity. (Default 0)
49 Display brief useage information.
57 =head1 ENVIRONMENTAL VARIABLES
63 Todo databse location, set by devtodo
65 =item B<STODO_NO_COMMIT>
67 If set, stodo assumes that there is no network, and doesn't commit
86 use Data::Diff qw(Diff);
87 use File::Basename qw(dirname basename);
91 # XXX parse config file
93 my %options = (quiet => 0,
100 GetOptions(\%options,'debug|d+','help|h|?','man|m','quiet|q+','simulate|s+');
102 pod2usage() if $options{help} or not exists $ENV{TODODB};
103 pod2usage({verbose=>2}) if $options{man};
105 $DEBUG = $options{debug};
107 # Figure out what we're doing
108 my $tododb = exists $ENV{TODODB} ? $ENV{TODODB} : '.todo';
109 if ($tododb !~ m#^/#) {
110 $tododb = cwd().'/'.$tododb;
112 if (not -r $tododb) {
113 "$tododb does not exist or is not readable";
115 $tododb = full_readlink($tododb);
116 my $base_dir = dirname($tododb);
117 my $tododb_name = basename($tododb);
119 if (not -e "$base_dir/.svn") {
120 print "$base_dir/.svn doesn't exist, not commiting\n" unless $options{quiet};
124 if (not -e "$base_dir/.svn/text-base/${tododb_name}.svn-base") {
125 print "$base_dir/.svn/text-base/${tododb_name}.svn-base doesn't exist, not committing\n" unless $options{quiet};
129 # Make sure that we can ping the subversion server
130 my $svn_entries_fh = new IO::File "$base_dir/.svn/entries",'r'
131 or die "unable to open $base_dir/.svn/entries: $!";
134 while (<$svn_entries_fh>) {
135 next unless m#^(?:\s+url=\")?(http|svn\+ssh|file):///?([^\/]+)#;
137 last if $url_type eq 'file';
141 if ($ENV{STODO_NO_COMMIT}) {
142 print "Exiting because of STODO_NO_COMMIT env variable\n" unless $options{quiet};
145 if (not defined $svn_host and (not defined $url_type or $url_type ne 'file')) {
146 die "Was unable to find which host the svn repository is located on";
148 if ($url_type ne 'file') {
149 qx(ping -q -c 3 $svn_host >/dev/null 2>&1);
151 print "Unable to ping $svn_host\n" unless $options{quiet};
152 exit 0 unless $options{simulate};
157 # See what has changed
158 #my $svn_fh = new IO::Handle;
159 #open($svn_fh,'-|','svn','diff',$tododb) or die "Unable to execute svn diff $tododb; $!";
161 # next unless /^[-+]/;
164 my $todo_db_old = fix_content(XMLin("$base_dir/.svn/text-base/${tododb_name}.svn-base",KeyAttr=>'time')->{note});
165 my $todo_db_new = fix_content(XMLin($tododb,KeyAttr=>'time')->{note});
169 my $diff = Diff($todo_db_new,$todo_db_old);
172 #print Dumper($diff);
174 if (exists $diff->{uniq_a}) {
175 foreach (values %{$diff->{uniq_a}}) {
176 my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$_}{qw(content priority)};
177 push @commit_message,qq( * Added todo: [$priority] $content);
181 if (exists $diff->{uniq_b}) {
182 foreach (values %{$diff->{uniq_b}}) {
183 my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$_}{qw(content priority)};
184 push @commit_message,qq( * Deleted todo: [$priority] $content);
188 if (exists $diff->{diff}) {
189 # This entry has either been edited, completed, or uncompleted
190 foreach my $entry_key (keys %{$diff->{diff}}) {
191 my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$todo_db_new->{$entry_key}}{qw(content priority)};
192 my $diff_entry = $diff->{diff}{$entry_key};
193 if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_a}{done}) {
194 push @commit_message,qq( * Finished todo: [$priority] $content);
196 if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_b}{done}) {
197 push @commit_message,qq( * Marked as undone: [$priority] $content);
199 if (exists $diff_entry->{diff}) {
201 if (exists $diff_entry->{diff}{content}) {
202 push @what_changed,'content';
204 if (exists $diff_entry->{diff}{priority}) {
205 push @what_changed,'priority';
207 if (not @what_changed) {
208 @what_changed = keys %{$diff_entry->{diff}};
210 push @commit_message,qq( * Todo changed ).join(' and ',@what_changed).qq( [$priority] $content);
215 if (exists $diff->{same}) {
216 foreach my $entry_key (keys %{$diff->{same}}) {
217 my ($content,$priority) = map {s/^\s*|\s*$//g; s/\n/ /g; $_;} @{$todo_db_new->{$entry_key}}{qw(content priority)};
218 my $diff_entry = $diff->{same}{$entry_key};
219 if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_a}{done}) {
220 push @commit_message,qq( * Finished todo: [$priority] $content);
222 if (exists $diff_entry->{uniq_a} and exists $diff_entry->{uniq_b}{done}) {
223 push @commit_message,qq( * Marked as undone: [$priority] $content);
225 if (exists $diff_entry->{diff}) {
227 if (exists $diff_entry->{diff}{content}) {
228 push @what_changed,'content';
230 if (exists $diff_entry->{diff}{priority}) {
231 push @what_changed,'priority';
233 if (not @what_changed) {
234 @what_changed = keys %{$diff_entry->{diff}};
236 push @commit_message,qq( * Todo changed ).join(' and ',@what_changed).qq( [$priority] $content);
242 if (not @commit_message) {
243 print "No commit message\n" if $DEBUG or $options{simulate};
247 my $commit_message = join('', map {qq($_\n)} @commit_message);
248 if ($options{simulate}) {
249 print "svn commit -F - $tododb <<EOF\n";
250 print $commit_message;
254 system('svn','commit','-m',$commit_message,$tododb);
258 # Commit the changes with an appropriate commit message
263 if (not ref(${[values %{$s}]}[0]) eq 'HASH') {
264 $s = {$s->{time} => $s};
266 foreach my $entry (values %{$s}) {
267 if (exists $entry->{content}) {
268 $entry->{content} =~ s/^\s*|\s*$//g;
270 if (exists $entry->{comment}) {
271 $entry->{comment} =~ s/^\s*|\s*$//g;
281 $file = readlink $file;