1. carbonica
  2. amp

Source

amp / lib / amp / repository / mercurial / repo_format / staging_area.rb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#######################################################################
#                  Licensing Information                              #
#                                                                     #
#  The following code is a derivative work of the code from the       #
#  Mercurial project, which is licensed GPLv2. This code therefore    #
#  is also licensed under the terms of the GNU Public License,        #
#  verison 2.                                                         #
#                                                                     #
#  For information on the license of this code when distributed       #
#  with and used in conjunction with the other modules in the         #
#  Amp project, please see the root-level LICENSE file.               #
#                                                                     #
#  © Michael J. Edgar and Ari Brown, 2009-2010                        #
#                                                                     #
#######################################################################

module Amp
  module Repositories
    module Mercurial
      class StagingArea < AbstractStagingArea
        
        attr_reader :dirstate
        attr_reader :repo
        alias_method :repository, :repo
        
        def initialize(repo)
          @ignore_all = nil
          @repo = repo
          @check_exec = false
        end
        
        ######### API Methods #################################################
        
        ##
        # All the files that are either added, modified, or merged.
        # 
        # @return [Array<String>] the files to be added in a commit
        def modified
          dirstate.files.select {|f| f.merged? || f.modified? || f.added? }
        end
        
        ##
        # All the files that are to be removed.
        # 
        # @return [Array<String>] the files to be removed in a commit
        def removed
          dirstate.files.select {|f| f.removed? }
        end
        
        ##
        # The directory used by the VCS to store magical information (.hg, .git, etc.).
        #
        # @api
        # @return [String] relative to root
        def vcs_dir
          '.hg'
        end
        
        ##
        # Adds a list of file paths to the repository for the next commit.
        # 
        # @api
        # @param [String, Array<String>] paths the paths of the files we need to
        #   add to the next commit
        # @return [Array<String>] which files WEREN'T added
        def add(*files)
          rejected = []
          files.flatten!
          
          repo.lock_working do
            files.each do |file|
              path = repo.working_join file
              stat = File.exist?(path) && File.lstat(path)
              
              if !stat
                UI.warn "#{file} does not exist!"
                rejected << file
              elsif File.ftype(path) != 'file' && File.ftype(path) != 'link'
                UI.warn "#{file} not added: only files and symlinks supported. Type is #{File.ftype path}"
                rejected << path
              else
                if stat.size > 10.mb
                  UI.warn "#{file}: files over 10MB may cause memory and performance problems\n" +
                              "(use 'amp revert #{file}' to unadd the file)\n"
                end
                dirstate.add file
              end
            end
          end
          rejected
        end
        
        ##
        # Removes the file (or files) from the repository. Marks them as removed
        # in the DirState, and if the :unlink option is provided, the files are
        # deleted from the filesystem.
        #
        # @api
        # @param list the list of files. Could also just be 1 file as a string.
        #   should be paths.
        # @param opts the options for this removal.
        # @option opts [Boolean] :unlink (false) whether or not to delete the
        #   files from the filesystem after marking them as removed from the
        #   DirState.
        # @return [Boolean] success?
        def remove(*args)
          list = args.last.is_a?(Hash) ? args[0..-2].flatten : args[0..-1].flatten
          opts = args.last.is_a?(Hash) ? args.last : {}
          # Should we delete the filez?
          if opts[:unlink]
              FileUtils.safe_unlink list.map {|f| repo.working_join(f)}
          end
          success = true
          repo.lock_working do
            # Save ourselves a dirstate write
            list.each do |f|
              if opts[:unlink] && File.exists?(repo.working_join(f))
                # Uh, why is the file still there? Don't remove it from the dirstate
                UI.warn("#{f} still exists!")
                success = false # no success
              else
                dirstate.remove f
              end
            end
          end
          
          success
        end
        
        ##
        # Set +file+ as normal and clean. Un-removes any files marked as removed, and
        # un-adds any files marked as added.
        # 
        # @param  [Array<String>] files the name of the files to mark as normal
        # @return [Boolean] success marker
        def normal(*files)
          files.each do |file|
            dirstate.normal(file)
          end
        end
        
        ##
        # Saves the staging area's state.  Any added files, removed files, "normalized" files
        # will have that status saved here.
        def save
          dirstate.write
        end
        
        ##
        # Copies a file from +source+ to +destination+, while being careful of the
        # specified options. This method will perform all necessary file manipulation
        # and dirstate changes and so forth. Just give 'er a source and a destination.
        #
        # @api
        # @param [String] source the path to the source file
        # @param [String] destination the path to the destination file
        # @param [Hash] opts the options for the copy
        # @option opts [Boolean] :after (false) should the file be deleted?
        # @return [Boolean] success?
        def copy(source, destination, opts)
          # Traverse repository subdirectories
          src    = repo.relative_join source
          target = repo.relative_join destination
          
          # Is there a tracked file at our destination? If so, get its state.
          state = dirstate[target].status
          # abstarget is the full path to the target. Needed for system calls
          # (just to be safe)
          abstarget = repo.working_join target
          
          # If true, we're copying into a directory, so be smart about it.
          if File.directory? abstarget
            abstarget = File.join abstarget, File.basename(src)
            target    = File.join target, File.basename(src)
          end
          abssrc = repo.working_join(src)
          
          
          exists = File.exist? abstarget
          # If the file's there, and we aren't forcing the copy, then we should let
          # the user know they might overwrite an existing file in the repo.
          if (!opts[:after] && exists || opts[:after] && [:merged, :normal].include?(state))
            unless opts[:force]
              Amp::UI.warn "#{target} not overwriting, file exists"
              return false
            end
          end
          
          return if opts[:after] && !exists
          unless opts[:"dry-run"]
            # Performs actual file copy from one location to another.
            # Overwrites file if it's there.
            begin
              File.safe_unlink(abstarget) if exists
              
              target_dir = File.dirname abstarget
              File.makedirs target_dir unless File.directory? target_dir
              File.copy(abssrc, abstarget)
            rescue Errno::ENOENT
              # This happens if the file has been deleted between the check up above
              # (exists = File.exist? abstarget) and the call to File.safe_unlink.
              Amp::UI.warn("#{target}: deleted in working copy in the last 2 microseconds")
            rescue StandardError => e
              Amp::UI.warn("#{target} - cannot copy: #{e}")
              return false
            end
          end
          
          # Be nice and give the user some output
          if opts[:verbose] || opts[:"dry-run"]
            action = opts[:rename] ? "moving" : "copying"
            Amp::UI.status("#{action} #{src} to #{target}")
          end
          return false if opts[:"dry-run"]
          
          # in case the source of the copy is marked as the destination of a 
          # different copy (that hasn't yet been committed either), we should
          # do some extra handling
          origsrc = dirstate.copy_map[src] || src
          if target == origsrc
            # We're copying back to our original location! D'oh.
            unless [:merged, :normal].include?(state)
              dirstate.maybe_dirty target
            end
          else
            if dirstate[origsrc].added? && origsrc == src
              # we copying an added (but uncommitted) file?
              UI.warn("#{origsrc} has not been committed yet, so no copy data " +
                      "will be stored for #{target}")
              if [:untracked, :removed].include?(dirstate[target].status)
                add target
              end
            else
              dirstate_copy src, target
            end
          end
          
          # Clean up if we're doing a move, and not a copy.
          remove(src, :unlink => !(opts[:after])) if opts[:rename]
        end
        
        ##
        # Copy a file from +source+ to +dest+. Really simple, peeps.
        # The reason this shit is even *slightly* complicated because
        # it deals with file types. Otherwise I could write this
        # in, what, 3 lines?
        # 
        # @param [String] source the from
        # @param [String] dest the to
        def dirstate_copy(source, dest)
          path = repo.working_join dest
          
          # wtf. if it's a link, it will trip the first if statement
          # and will say that it doesn't exist
          # and there's no way for a link to not get tripped on the first one.
          # so why are we even checking for a link in the second one? and
          # why do we say links are no good in the first and then say that
          # links are good in the second? i smell wtf.
          if !File.exist?(path) || File.ftype(path) == 'link'
            UI::warn "#{dest} doesn't exist!"
          elsif not (File.ftype(path) == 'file' || File.ftype(path) == 'link')
            UI::warn "copy failed: #{dest} is neither a file nor a symlink"
          else
            repo.lock_working do
              # HOME FREE!!!!!!! i love getting out of school before noon :-D
              # add it if it makes sense (like it was previously removed or untracked)
              # and then copy da hoe
              state  = dirstate[dest].status
              dirstate.add dest if [:untracked, :removed].include?(state)
              dirstate.copy source => dest
              
              #Amp::Logger.info("copy #{source} -> #{dest}")
            end
          end
        end
        
        ##
        # Marks a modified file to be included in the next commit.
        # If your VCS does this implicitly, this should be defined as a no-op.
        #
        # Mercurial: This is a no-op unless the specified files are not already
        # in the repository, so we should add them to the repo in that case.
        # 
        # @api
        # @param [[String]] filenames a list of files to include for committing
        # @return [Boolean] true for success, false for failure
        def include(*filenames)
          to_add = []
          
          filenames.each do |filename|
            unless dirstate[filename]
              to_add << filename
            end
          end
          
          add to_add if to_add.any?
        end
        
        ##
        # Returns a Symbol.
        # Possible results:
        # :added (subset of :included)
        # :removed
        # :untracked
        # :included
        # :normal
        #
        def file_status(filename)
          dirstate[filename].status
        end
        
        ##
        # Returns whether or not the repository is tracking the given file.
        #
        # @param [String] filename the file to look up
        # @return [Boolean] are we tracking the given file?
        def tracking?(filename)
          dirstate.tracking? filename
        end
        
        ##
        # Returns all files tracked by the repository *for the working directory* - not
        # to be confused with the most recent changeset.
        #
        # @return [Array<String>] all files tracked by the repository at this moment in
        #   time, including just-added files (for example) that haven't been committed yet.
        def all_files
          dirstate.all_files
        end
        
        ######### Optional API Methods ########################################
        
        ##
        # Returns whether the given directory is being ignored. Optional method - defaults to
        # +false+ at all times.
        #
        # @api-optional
        # @param [String] directory the directory to check against ignoring rules
        # @return [Boolean] are we ignoring this directory?
        def ignoring_directory?(directory)
          return true  if @ignore_all
          return false if @ignore_all == false
          dirstate.ignoring_directory? directory
        end

        ##
        # Returns whether the given file is being ignored. Optional method - defaults to
        # +false+ at all times.
        #
        # @api-optional
        # @param [String] file the file to check against ignoring rules
        # @return [Boolean] are we ignoring this file?
        def ignoring_file?(file)
          return true  if @ignore_all
          return false if @ignore_all == false
          dirstate.ignore file
        end
        
        ##
        # Retrives the dirstate from the staging_area. The staging area is reponsible
        # for properly maintaining the dirstate.
        #
        # @return [DirState]
        def dirstate
          return @dirstate if @dirstate ||= nil # the "||= nil" kills undefined ivar warning 
          
          opener = Amp::Opener.new repo.root
          opener.default = :open_hg
          
          @dirstate = DirState.new(repo.root, repo.config, opener)
          @dirstate.read!
        end
        
        ##
        # Calculates the difference (in bytes) between a file and its last tracked state.
        #
        # Supplements the built-in #status method so that its output will include deltas.
        #
        # @apioptional
        # @param [String] file the filename to look up
        # @param [File::Stats] st the current results of File.lstat(file)
        # @return [Fixnum] the number of bytes difference between the file and
        #  its last tracked state.
        def calculate_delta(file, st)
          state, mode, size, time = dirstate.files[file].to_a
          st && size >= 0 ? (size - st.size).abs : 0 # increase the delta, but don't forget to check that it's not nil
        end
        
        ##
        # Does a detailed look at a file, to see if it is clean, modified, or needs to have its
        # content checked precisely.
        #
        # Supplements the built-in #status method so that its output will be more
        # accurate.
        #
        # @param [String] file the filename to look up
        # @param [File::Stats] st the current results of File.lstat(file)
        # @return [Symbol] a symbol representing the current file's status
        def file_precise_status(file, st)
          state, mode, size, time = dirstate.files[file].to_a
          if (size >= 0 && (size != st.size || ((mode ^ st.mode) & 0100 and @check_exec))) || size == -2 || dirstate.copy_map[file]
            return :modified
          elsif time != st.mtime.to_i # DOH - we have to remember that times are stored as fixnums
            return :lookup
          else
            return :clean
          end
        end
        
      end
    end
  end
end