Class: Amp::Merges::MergeState

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/amp/merges/merge_state.rb

Overview

MergeState

MergeState handles the merge/ directory in the repository, in order to keep track of how well the current merge is progressing. There is a file called merge/state that lists all the files that need merging and a little info about whether it has beeen merged or not.

You can add a file to the mergestate, iterate over all of them, quickly look up to see if a file is still dirty, and so on.

Instance Method Summary collapse

Methods included from Enumerable

#inject, #select_map

Constructor Details

#initialize(repo) ⇒ MergeState

Initializes a new mergestate with the given repo, and reads in all the information from merge/state.

Parameters:

  • repo

    the repository being inspected



21
22
23
24
# File 'lib/amp/merges/merge_state.rb', line 21

def initialize(repo)
  @repo = repo
  read!
end

Instance Method Details

#[](dirty_file) ⇒ String

Accesses the the given file’s merge status - can be “u” for unmerged, or other stuff we haven’t figured out yet.

Parameters:

  • dirty_file (String)

    the path to the file for merging.

Returns:

  • (String)

    the status as a letter - so far “u” means unmerged or “r” for resolved.



52
53
54
# File 'lib/amp/merges/merge_state.rb', line 52

def [](dirty_file)
  @state[dirty_file] ? @state[dirty_file][0, 1] : ""
end

#add(fcl, fco, fca, fd, flags) ⇒ Object

Adds a file to the mergestate, which creates a separate file in the merge directory with all the information. I don’t know what these parameters are for yet.



60
61
62
63
64
65
66
67
68
# File 'lib/amp/merges/merge_state.rb', line 60

def add(fcl, fco, fca, fd, flags)
  hash = Digest::SHA1.new.update(fcl.path).hexdigest
  @repo.open("merge/#{hash}", "w") do |file|
    file.write fcl.data
  end
  @state[fd] = ["u", hash, fcl.path, fca.path, fca.file_node.hexlify,
                fco.path, flags]
  save
end

#each {|file, state| ... } ⇒ Object

Iterates over all the files that are involved in the current merging transaction.

Yields:

  • each file, sorted by filename, that needs merging.

Yield Parameters:

  • file

    the filename that needs (or has been) merged.

  • state

    all the information about the current merge with this file.



78
79
80
81
82
# File 'lib/amp/merges/merge_state.rb', line 78

def each
  @state.keys.sort.each do |key|
    yield(key, @state[key])
  end
end

#include?(dirty_file) ⇒ Boolean

Returns whether the file is part of a merge or not

Returns:

  • (Boolean)

    if the dirty file in our state and not nil?



41
42
43
# File 'lib/amp/merges/merge_state.rb', line 41

def include?(dirty_file)
  not @state[dirty_file].nil?
end

#mark(dirty_file, state) ⇒ Object

Marks the given file with a given state, which is 1 letter. “u” means unmerged, “r” means resolved.

Parameters:

  • dirty_file (String)

    the file path for marking

  • state (String)

    the state - “u” for unmerged, “r” for resolved.



90
91
92
93
# File 'lib/amp/merges/merge_state.rb', line 90

def mark(dirty_file, state)
  @state[dirty_file][0] = state
  save
end

#reset(node = nil) ⇒ Object Also known as: reset!

Resets the merge status, by clearing all merge information and files

Parameters:

  • node (defaults to: nil)

    the node we’re working with? seems kinda useless



30
31
32
33
34
# File 'lib/amp/merges/merge_state.rb', line 30

def reset(node = nil)
  @state = {}
  @local = node if node
  FileUtils.rm_rf @repo.join("merge")
end

#resolve(dirty_file, working_changeset, other_changeset) ⇒ Object

Resolves the given file for a merge between 2 changesets.

Parameters:

  • dirty_file

    the path to the file for merging

  • working_changeset

    the current changeset that is the destination of the merge

  • other_changeset

    the newer changeset, which we’re merging to



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/amp/merges/merge_state.rb', line 102

def resolve(dirty_file, working_changeset, other_changeset)
  return 0 if self[dirty_file] == "r"
  state, hash, lfile, afile, anode, ofile, flags = @state[dirty_file]
  r = true
  @repo.open("merge/#{hash}") do |file|
    @repo.working_write(dirty_file, file.read, flags)
    working_file  = working_changeset[dirty_file]
    other_file    = other_changeset[ofile]
    ancestor_file = @repo.versioned_file(afile, :file_id => anode)
    r = UI.file_merge(@repo, @local, lfile, working_file, other_file, ancestor_file)
  end
  
  mark(dirty_file, "r") if r.nil? || r == false
  return r
end

#saveObject Also known as: save!

Public access to writing the file.



120
121
122
# File 'lib/amp/merges/merge_state.rb', line 120

def save
  write!
end