Class: GitClient

Inherits:
Object
  • Object
show all
Defined in:
lib/integrations/git_client.rb

Constant Summary collapse

COMMIT_REGEX =
/^[0-9a-f]{40}\|\|\|/
TREE_PERMISSION =
"040000"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GitClient

Returns a new instance of GitClient.



16
17
18
19
20
# File 'lib/integrations/git_client.rb', line 16

def initialize(config)
  @git_dir = config["git_dir"]
  @default_branch = config["default_branch"]
  @notification_url = config["notification_url"]
end

Instance Attribute Details

#notification_urlObject

Returns the value of attribute notification_url.



14
15
16
# File 'lib/integrations/git_client.rb', line 14

def notification_url
  @notification_url
end

Instance Method Details

#commits(branch) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/integrations/git_client.rb', line 22

def commits(branch)
  commit_ids = short_log(branch)
  cache, index = commits_to_index(branch, commit_ids)
  return [] if index == 0

  commits = full_log(branch, index)
    .reduce([], &fold_reducer(COMMIT_REGEX))
    .map { |commit_lines| Commit.from_git(commit_lines, self) }

  commits = SourceTree
    .new()
    .merge_collapse(commits)

  commits.last.set_cached(cache)
  cache_commits(commits)

  commits.reverse()
end

#diff(commit_id_a, commit_id_b) ⇒ Object



85
86
87
# File 'lib/integrations/git_client.rb', line 85

def diff(commit_id_a, commit_id_b)
  exec("diff --full-index -l 10000 -U0 #{commit_id_a} #{commit_id_b}")
end

#diff_tree(commit_id) ⇒ Object



41
42
43
# File 'lib/integrations/git_client.rb', line 41

def diff_tree(commit_id)
  exec("diff-tree -t -r #{commit_id}")
end

#file_tree(commit_id) ⇒ Object



66
67
68
# File 'lib/integrations/git_client.rb', line 66

def file_tree(commit_id)
  exec("ls-tree -r #{commit_id}")
end

#ls_tree(commit_id) ⇒ Object



62
63
64
# File 'lib/integrations/git_client.rb', line 62

def ls_tree(commit_id)
  exec("ls-tree -r -t #{commit_id}")
end

#parse_diff_tree(tree) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/integrations/git_client.rb', line 45

def parse_diff_tree(tree)
  tree
    .reduce([]) do |acc, object|
      a_perm, b_perm, a_id, b_id, operation, path = object[1..-1].split(" ")
      if a_perm == TREE_PERMISSION || b_perm == TREE_PERMISSION
        acc << {
          a_tree_id: a_id,
          b_tree_id: b_id,
          path: path,
          operation: operation == "D" ? :delete : :change
        }
      end
      acc
    end
    .sort_by { |tree| tree[:path].split(File::SEPARATOR).length }
end

#parse_ls_tree(tree) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/integrations/git_client.rb', line 70

def parse_ls_tree(tree)
  tree.reduce([]) do |acc, object|
    perm, type, tree_id, path = object.split(" ")
    if type == "tree"
      acc << {
        a_tree_id: "0"*40,
        b_tree_id: tree_id,
        path: path,
        operation: :change
      }
    end
    acc
  end
end

#remote_branchesObject



89
90
91
92
93
94
# File 'lib/integrations/git_client.rb', line 89

def remote_branches()
  exec("branch -r")
    .map { |branch| branch.split(" -> ").last.strip() }
    .select { |branch| branch.index("origin/") == 0 }
    .map { |branch| "remotes/#{branch}" }
end