Class: Mbeditor::GitDiffService

Inherits:
Object
  • Object
show all
Includes:
GitService
Defined in:
app/services/mbeditor/git_diff_service.rb

Overview

Produces original/modified content pairs suitable for Monaco’s diff editor.

Modes


  1. Working tree vs HEAD (default when no sha given) original = HEAD version of the file modified = current on-disk content

  2. Specific commit vs its parent original = file at <base_sha> modified = file at <head_sha>

Constant Summary

Constants included from GitService

Mbeditor::GitService::SAFE_GIT_REF

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GitService

ahead_behind, current_branch, parse_git_log, parse_git_log_with_parents, resolve_path, run_git, upstream_branch

Constructor Details

#initialize(repo_path:, file_path:, base_sha: nil, head_sha: nil) ⇒ GitDiffService

Returns a new instance of GitDiffService.



20
21
22
23
24
25
# File 'app/services/mbeditor/git_diff_service.rb', line 20

def initialize(repo_path:, file_path:, base_sha: nil, head_sha: nil)
  @repo_path = repo_path.to_s
  @file_path = file_path.to_s
  @base_sha  = base_sha.presence
  @head_sha  = head_sha.presence
end

Instance Attribute Details

#base_shaObject (readonly)

Returns the value of attribute base_sha.



18
19
20
# File 'app/services/mbeditor/git_diff_service.rb', line 18

def base_sha
  @base_sha
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



18
19
20
# File 'app/services/mbeditor/git_diff_service.rb', line 18

def file_path
  @file_path
end

#head_shaObject (readonly)

Returns the value of attribute head_sha.



18
19
20
# File 'app/services/mbeditor/git_diff_service.rb', line 18

def head_sha
  @head_sha
end

#repo_pathObject (readonly)

Returns the value of attribute repo_path.



18
19
20
# File 'app/services/mbeditor/git_diff_service.rb', line 18

def repo_path
  @repo_path
end

Instance Method Details

#callObject

Returns { original: String, modified: String } or raises RuntimeError.



28
29
30
31
32
33
34
35
36
37
# File 'app/services/mbeditor/git_diff_service.rb', line 28

def call
  if base_sha && head_sha
    diff_between_commits
  elsif base_sha
    # base_sha provided but head_sha is nil: diff that ref vs the working tree
    { "original" => file_at_ref(base_sha, file_path), "modified" => on_disk_content }
  else
    diff_working_tree_vs_head
  end
end