diff app/models/changeset.rb @ 245:051f544170fe

* Update to SVN trunk revision 4993
author Chris Cannam
date Thu, 03 Mar 2011 11:42:28 +0000
parents 8661b858af72
children eeebe205a056 cbce1fd3b1b7
line wrap: on
line diff
--- a/app/models/changeset.rb	Thu Mar 03 11:40:10 2011 +0000
+++ b/app/models/changeset.rb	Thu Mar 03 11:42:28 2011 +0000
@@ -56,10 +56,6 @@
       revision.to_s
     end
   end
-  
-  def comments=(comment)
-    write_attribute(:comments, Changeset.normalize_comments(comment))
-  end
 
   def committed_on=(date)
     self.commit_date = date
@@ -75,10 +71,6 @@
     end
   end
   
-  def committer=(arg)
-    write_attribute(:committer, self.class.to_utf8(arg.to_s))
-  end
-
   def project
     repository.project
   end
@@ -88,20 +80,24 @@
   end
   
   def before_create
-    self.user = repository.find_committer_user(committer)
+    self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding)
+    self.comments  = self.class.normalize_comments(self.comments, repository.repo_log_encoding)
+    self.user = repository.find_committer_user(self.committer)
   end
-  
+
   def after_create
     scan_comment_for_issue_ids
   end
   
   TIMELOG_RE = /
     (
-    (\d+([.,]\d+)?)h?
+    ((\d+)(h|hours?))((\d+)(m|min)?)?
+    |
+    ((\d+)(h|hours?|m|min))
     |
     (\d+):(\d+)
     |
-    ((\d+)(h|hours?))?((\d+)(m|min)?)?
+    (\d+([\.,]\d+)?)h?
     )
     /x
   
@@ -161,11 +157,6 @@
     @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
   end
   
-  # Strips and reencodes a commit log before insertion into the database
-  def self.normalize_comments(str)
-    to_utf8(str.to_s.strip)
-  end
-
   # Creates a new Change from it's common parameters
   def create_change(change)
     Change.create(:changeset => self,
@@ -174,7 +165,7 @@
                   :from_path => change[:from_path],
                   :from_revision => change[:from_revision])
   end
-  
+
   private
 
   # Finds an issue that can be referenced by the commit message
@@ -244,15 +235,17 @@
     return @short_comments, @long_comments
   end
 
-  def self.to_utf8(str)
-    if str.respond_to?(:force_encoding)
-      str.force_encoding('UTF-8')
-      return str if str.valid_encoding?
-    else
-      return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
-    end
+  public
 
-    encoding = Setting.commit_logs_encoding.to_s.strip
+  # Strips and reencodes a commit log before insertion into the database
+  def self.normalize_comments(str, encoding)
+    Changeset.to_utf8(str.to_s.strip, encoding)
+  end
+
+  private
+
+  def self.to_utf8(str, encoding)
+    return str if str.blank?
     unless encoding.blank? || encoding == 'UTF-8'
       begin
         str = Iconv.conv('UTF-8', encoding, str)
@@ -260,12 +253,20 @@
         # do nothing here
       end
     end
-    # removes invalid UTF8 sequences
-    begin
-      Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + '  ')[0..-3]
-    rescue Iconv::InvalidEncoding
-      # "UTF-8//IGNORE" is not supported on some OS
-      str
+    if str.respond_to?(:force_encoding)
+      str.force_encoding('UTF-8')
+      if ! str.valid_encoding?
+        str = str.encode("US-ASCII", :invalid => :replace,
+              :undef => :replace, :replace => '?').encode("UTF-8")
+      end
+    else
+      # removes invalid UTF8 sequences
+      begin
+        str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + '  ')[0..-3]
+      rescue Iconv::InvalidEncoding
+        # "UTF-8//IGNORE" is not supported on some OS
+      end
     end
+    str
   end
 end