Fwd: Trouble adding and committing files to git

-------- Original Message --------
Subject: Trouble adding and committing files to git.
Date: Mon, 25 Jul 2011 11:55:53 +0100
From: Jen [email protected]
To: [email protected]

Hi,
I have some code that uploads a file, then does a git add and git commit
using the ‘Grit’ gem. Trouble is this code sometimes commits a file, and
sometimes doesn’t.

It usually works when a file has already been committed and is then
modified, but doesn’t add then commit files that have never been added
to the repo before.

Note files are always uploaded to the correct directory, even if not
tracked and committed in GIT.

Code from model:

#Using active model
class Upload
require ‘grit’
include Grit
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming

attr_accessor :message, :upload
#attr_accessible :message, :upload, :repo
#validates the presence of something in the message field
validates :message, :presence => true, :length => { :maximum =>50}
validates :upload, :presence => true
#Creates hash for storing the info about the uploaded file for
validation
def initialize(attributes = {})
attributes.each do |name, value|
send(“#{name}=”, value)
end
end
#takes the upload object and extracts the file.
def filesave
#Calls the .original_filename method on the upload object and stores in
the variable name
name = upload.original_filename
directory = “/home/resource_portal/website/public/data/upload”
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, “wb”) { |f| f.write(upload.read) }

commit the file to the git repo

committer = @user
repo =
Grit::Repo.new(“/home/resource_portal/website/public/data/upload”)
repo.add(upload)
repo.commit_all(message)
end
end