~ubuntu-branches/ubuntu/saucy/ruby-grit/saucy

« back to all changes in this revision

Viewing changes to lib/grit/git-ruby/internal/loose.rb

  • Committer: Package Import Robot
  • Author(s): Ondřej Surý
  • Date: 2013-04-15 09:24:29 UTC
  • Revision ID: package-import@ubuntu.com-20130415092429-e4w4shogdsmakpcp
Tags: 2.5.0-1
Initial release (Closes: #705448)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
require 'zlib'
13
13
require 'digest/sha1'
14
14
require 'grit/git-ruby/internal/raw_object'
 
15
require 'tempfile'
15
16
 
16
17
module Grit
17
18
  module GitRuby
60
61
          return RawObject.new(type, content)
61
62
        end
62
63
 
 
64
        # write an object to a temporary file, then atomically rename it
 
65
        # into place; this ensures readers never see a half-written file
 
66
        def safe_write(path, content)
 
67
          f =
 
68
            if RUBY_VERSION >= '1.9'
 
69
              Tempfile.open("tmp_obj_", File.dirname(path), :opt => "wb")
 
70
            else
 
71
              Tempfile.open("tmp_obj_", File.dirname(path))
 
72
            end
 
73
          begin
 
74
            f.write content
 
75
            f.fsync
 
76
            File.link(f.path, path)
 
77
          rescue Errno::EEXIST
 
78
            # The path already exists; we raced with another process,
 
79
            # but it's OK, because by definition the content is the
 
80
            # same. So we can just ignore the error.
 
81
          ensure
 
82
            f.unlink
 
83
            f.close
 
84
          end
 
85
        end
 
86
 
63
87
        # currently, I'm using the legacy format because it's easier to do
64
88
        # this function takes content and a type and writes out the loose object and returns a sha
65
89
        def put_raw_object(content, type)
76
100
            content = Zlib::Deflate.deflate(store)
77
101
 
78
102
            FileUtils.mkdir_p(@directory+'/'+sha1[0...2])
79
 
            File.open(path, 'wb') do |f|
80
 
              f.write content
81
 
            end
 
103
            safe_write(path, content)
82
104
          end
83
105
          return sha1
84
106
        end