Class: RubyGit::Status::RenamedEntry

Inherits:
Entry
  • Object
show all
Defined in:
lib/ruby_git/status/renamed_entry.rb

Overview

Represents a renamed file in git status

Constant Summary

Constants inherited from Entry

Entry::RENAME_OPERATIONS, Entry::STATUS_CODES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entry

#fully_staged?, #ignored?, rename_operation_to_symbol, status_to_symbol, #unmerged?, #untracked?

Constructor Details

#initialize(index_status:, worktree_status:, submodule_status:, head_mode:, index_mode:, worktree_mode:, head_sha:, index_sha:, operation:, similarity_score:, path:, original_path:) ⇒ RenamedEntry

Initialize a new renamed entry

Examples:

RenamedEntry.new(
  index_status: :renamed,
  worktree_status: :modified,
  submodule_status: nil,
  head_mode: 0o100644,
  index_mode: 0o100644,
  worktree_mode: 0o100644,
  head_sha: 'd670460b4b4aece5915caf5c68d12f560a9fe3e4',
  index_sha: 'd670460b4b4aece5915caf5c68d12f560a9fe3e4',
  operation: :rename,
  similarity_score: 50,
  path: 'lib/new_name.rb',
  original_path: 'lib/old_name.rb'
)

Parameters:

  • index_status (Symbol)

    status in staging area

  • worktree_status (Symbol)

    status in worktree

  • submodule_status (SubmoduleStatus, nil)

    submodule status or nil

  • head_mode (Integer)

    mode of the file in HEAD

  • index_mode (Integer)

    mode of the file in the index

  • worktree_mode (Integer)

    mode of the file in the worktree

  • head_sha (String)

    SHA of this object in HEAD

  • index_sha (String)

    SHA of this object in the index

  • operation (Symbol)

    operation that was performed on the file

  • similarity_score (Integer)

    similarity index between the original and renamed file

  • path (String)

    new file path

  • original_path (String)

    original file path



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ruby_git/status/renamed_entry.rb', line 210

def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  index_status:, worktree_status:,
  submodule_status:,
  head_mode:, index_mode:, worktree_mode:,
  head_sha:, index_sha:,
  operation:, similarity_score:,
  path:, original_path:
)
  super(path)

  @index_status = index_status
  @worktree_status = worktree_status
  @submodule_status = submodule_status
  @head_mode = head_mode
  @index_mode = index_mode
  @worktree_mode = worktree_mode
  @head_sha = head_sha
  @index_sha = index_sha
  @operation = operation
  @similarity = similarity_score
  @original_path = original_path
end

Instance Attribute Details

#head_modeInteger (readonly)

The mode of the file in HEAD

Examples:

entry.head_mode #=> 0o100644

Returns:

  • (Integer)

    mode of the file in HEAD



45
46
47
# File 'lib/ruby_git/status/renamed_entry.rb', line 45

def head_mode
  @head_mode
end

#head_shaString (readonly)

The SHA of this object in HEAD

Examples:

entry.head_sha #=> 'd670460b4b4aece5915caf5c68d12f560a9fe3e4'

Returns:

  • (String)

    SHA of this object in HEAD



81
82
83
# File 'lib/ruby_git/status/renamed_entry.rb', line 81

def head_sha
  @head_sha
end

#index_modeInteger (readonly)

The mode of the file in the index

Examples:

entry.index_mode #=> 0o100644

Returns:

  • (Integer)

    mode of the file in the index



57
58
59
# File 'lib/ruby_git/status/renamed_entry.rb', line 57

def index_mode
  @index_mode
end

#index_shaString (readonly)

The SHA of this object in the index

Examples:

entry.index_sha #=> 'd670460b4b4aece5915caf5c68d12f560a9fe3e4'

Returns:

  • (String)

    SHA of this object in the index



93
94
95
# File 'lib/ruby_git/status/renamed_entry.rb', line 93

def index_sha
  @index_sha
end

#index_statusSymbol (readonly)

The status in the staging area

Examples:

entry.index_status #=> :renamed

Returns:

  • (Symbol)

    staging status



21
22
23
# File 'lib/ruby_git/status/renamed_entry.rb', line 21

def index_status
  @index_status
end

#operationString (readonly)

The operation that was performed on the file: 'R' for or 'C' for copy)

Examples:

entry.operation #=> 'R'

Returns:

  • (String)

    operation



105
106
107
# File 'lib/ruby_git/status/renamed_entry.rb', line 105

def operation
  @operation
end

#original_pathString (readonly)

The original path before rename

Examples:

entry.original_path #=> 'lib/old_name.rb'

Returns:

  • (String)

    original file path



141
142
143
# File 'lib/ruby_git/status/renamed_entry.rb', line 141

def original_path
  @original_path
end

#pathString (readonly)

The path after the rename

Examples:

entry.path #=> 'lib/new_name.rb'

Returns:

  • (String)


129
130
131
# File 'lib/ruby_git/status/renamed_entry.rb', line 129

def path
  @path
end

#similarityInteger (readonly)

The similarity index between the original and renamed file

Examples:

entry.similarity #=> 95

Returns:

  • (Integer)

    similarity percentage



117
# File 'lib/ruby_git/status/renamed_entry.rb', line 117

attr_reader :similarity_score

#similarity_scoreInteger (readonly)

The similarity index between the original and renamed file

Examples:

entry.similarity #=> 95

Returns:

  • (Integer)

    similarity percentage



117
118
119
# File 'lib/ruby_git/status/renamed_entry.rb', line 117

def similarity_score
  @similarity_score
end

#worktree_modeInteger (readonly)

The mode of the file in the worktree

Examples:

entry.worktree_mode #=> 0o100644

Returns:

  • (Integer)

    mode of the file in the worktree



69
70
71
# File 'lib/ruby_git/status/renamed_entry.rb', line 69

def worktree_mode
  @worktree_mode
end

#worktree_statusSymbol (readonly)

The status in the worktree

Examples:

entry.worktree_status #=> :modified

Returns:

  • (Symbol)

    worktree status



33
34
35
# File 'lib/ruby_git/status/renamed_entry.rb', line 33

def worktree_status
  @worktree_status
end

Class Method Details

.parse(line) ⇒ RubyGit::Status::RenamedEntry

Parse a git status line to create a renamed entry

The line is expected to be in porcelain v2 format with NUL terminators.

The format is as follows: 2

Examples:

line = '2 RM N... 100644 100644 100644 d670460b4b4aece5915caf5c68d12f560a9fe3e4 ' \
  \d670460b4b4aece5915caf5c68d12f560a9fe3e4 50 lib/new_name.rb\0lib/old_name.rb'
RenamedEntry.parse(line) #=> #<RubyGit::Status::RenamedEntry:0x00000001046bd488 ...>

Parameters:

  • line (String)

    line from git status

Returns:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ruby_git/status/renamed_entry.rb', line 159

def self.parse(line) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  tokens = line.split(' ', 10)
  path, original_path = tokens[9].split("\0")

  new(
    index_status: Entry.status_to_symbol(tokens[1][0]),
    worktree_status: Entry.status_to_symbol(tokens[1][1]),
    submodule_status: SubmoduleStatus.parse(tokens[2]),
    head_mode: Integer(tokens[3], 8),
    index_mode: Integer(tokens[4], 8),
    worktree_mode: Integer(tokens[5], 8),
    head_sha: tokens[6],
    index_sha: tokens[7],
    operation: Entry.rename_operation_to_symbol(tokens[8][0]),
    similarity_score: tokens[8][1..].to_i,
    path: path,
    original_path: original_path
  )
end

Instance Method Details

#staged?Boolean

Does the entry have staged changes in the index?

  • An entry can have both staged and unstaged changes

Examples:

entry.ignored? #=> false

Returns:

  • (Boolean)


252
253
254
# File 'lib/ruby_git/status/renamed_entry.rb', line 252

def staged?
  index_status != :unmodified
end

#unstaged?Boolean

Does the entry have unstaged changes in the worktree?

  • An entry can have both staged and unstaged changes
  • All untracked entries are considered unstaged

Examples:

entry.ignored? #=> false

Returns:

  • (Boolean)


241
242
243
# File 'lib/ruby_git/status/renamed_entry.rb', line 241

def unstaged?
  worktree_status != :unmodified
end