Class: Git::FileRef

Inherits:
Data
  • Object
show all
Defined in:
lib/git/file_ref.rb

Overview

Immutable value object representing a reference to a file at a specific point in time

FileRef encapsulates the mode, SHA, and path of a file as it exists on one side of a diff. This is used to represent either the source (before) or destination (after) state of a file in a diff operation.

When a file doesn't exist on a side of the diff (e.g., src for new files, dst for deleted files), the entire FileRef should be nil rather than having a FileRef with nil attributes.

Examples:

A modified file's source reference

src = Git::FileRef.new(mode: '100644', sha: 'abc1234', path: 'lib/foo.rb')

A new file (src would be nil, not a FileRef)

# src = nil
dst = Git::FileRef.new(mode: '100644', sha: 'def5678', path: 'lib/new_file.rb')

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode

Returns:

  • (Object)

    the current value of mode



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
# File 'lib/git/file_ref.rb', line 33

FileRef = Data.define(:mode, :sha, :path) do
  # Check if this is a regular file (not executable, symlink, etc.)
  #
  # @return [Boolean] true if mode is 100644
  #
  def regular_file?
    mode == '100644'
  end

  # Check if this is an executable file
  #
  # @return [Boolean] true if mode is 100755
  #
  def executable?
    mode == '100755'
  end

  # Check if this is a symbolic link
  #
  # @return [Boolean] true if mode is 120000
  #
  def symlink?
    mode == '120000'
  end

  # Return the mode as an integer (parsed as octal)
  #
  # Useful for bit operations on file permissions.
  #
  # @return [Integer] the mode as an integer
  #
  # @example Check file permissions
  #   ref.mode_bits & 0o777  # => 0o644 (420 decimal)
  #
  # @example Check if group writable
  #   (ref.mode_bits & 0o020) != 0
  #
  def mode_bits
    mode.to_i(8)
  end
end

#pathObject (readonly)

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



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
# File 'lib/git/file_ref.rb', line 33

FileRef = Data.define(:mode, :sha, :path) do
  # Check if this is a regular file (not executable, symlink, etc.)
  #
  # @return [Boolean] true if mode is 100644
  #
  def regular_file?
    mode == '100644'
  end

  # Check if this is an executable file
  #
  # @return [Boolean] true if mode is 100755
  #
  def executable?
    mode == '100755'
  end

  # Check if this is a symbolic link
  #
  # @return [Boolean] true if mode is 120000
  #
  def symlink?
    mode == '120000'
  end

  # Return the mode as an integer (parsed as octal)
  #
  # Useful for bit operations on file permissions.
  #
  # @return [Integer] the mode as an integer
  #
  # @example Check file permissions
  #   ref.mode_bits & 0o777  # => 0o644 (420 decimal)
  #
  # @example Check if group writable
  #   (ref.mode_bits & 0o020) != 0
  #
  def mode_bits
    mode.to_i(8)
  end
end

#shaObject (readonly)

Returns the value of attribute sha

Returns:

  • (Object)

    the current value of sha



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
# File 'lib/git/file_ref.rb', line 33

FileRef = Data.define(:mode, :sha, :path) do
  # Check if this is a regular file (not executable, symlink, etc.)
  #
  # @return [Boolean] true if mode is 100644
  #
  def regular_file?
    mode == '100644'
  end

  # Check if this is an executable file
  #
  # @return [Boolean] true if mode is 100755
  #
  def executable?
    mode == '100755'
  end

  # Check if this is a symbolic link
  #
  # @return [Boolean] true if mode is 120000
  #
  def symlink?
    mode == '120000'
  end

  # Return the mode as an integer (parsed as octal)
  #
  # Useful for bit operations on file permissions.
  #
  # @return [Integer] the mode as an integer
  #
  # @example Check file permissions
  #   ref.mode_bits & 0o777  # => 0o644 (420 decimal)
  #
  # @example Check if group writable
  #   (ref.mode_bits & 0o020) != 0
  #
  def mode_bits
    mode.to_i(8)
  end
end

Instance Method Details

#executable?Boolean

Check if this is an executable file

Returns:

  • (Boolean)

    true if mode is 100755



46
47
48
# File 'lib/git/file_ref.rb', line 46

def executable?
  mode == '100755'
end

#mode_bitsInteger

Return the mode as an integer (parsed as octal)

Useful for bit operations on file permissions.

Examples:

Check file permissions

ref.mode_bits & 0o777  # => 0o644 (420 decimal)

Check if group writable

(ref.mode_bits & 0o020) != 0

Returns:

  • (Integer)

    the mode as an integer



70
71
72
# File 'lib/git/file_ref.rb', line 70

def mode_bits
  mode.to_i(8)
end

#regular_file?Boolean

Check if this is a regular file (not executable, symlink, etc.)

Returns:

  • (Boolean)

    true if mode is 100644



38
39
40
# File 'lib/git/file_ref.rb', line 38

def regular_file?
  mode == '100644'
end

#symlink?Boolean

Check if this is a symbolic link

Returns:

  • (Boolean)

    true if mode is 120000



54
55
56
# File 'lib/git/file_ref.rb', line 54

def symlink?
  mode == '120000'
end