Class: Ballantine::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/ballantine/repository.rb

Constant Summary collapse

GITHUB_REGEXES =
[
  '^https?://(.+)/(.+)/(.+)\.git/?$', # protocol: https -> https://github.com/oohyun15/ballantine.git | https://github.com/oohyun15/ballantine.git/
  '^https?://(.+)/(.+)/(.+)/?$',      # protocol: https -> https://github.com/oohyun15/ballantine | https://github.com/oohyun15/ballantine/
  '^git@(.+):(.+)/(.+)\.git$',        # protocol: ssh   -> [email protected]:oohyun15/ballantine.git
  '^git@(.+):(.+)/(.+)/?$',           # protocol: ssh   -> [email protected]:oohyun15/ballantine | [email protected]:oohyun15/ballantine/
  '^git:(.+)/(.+)/(.+)\.git$',        # protocol: ssh   -> git:github.com/oohyun15/ballantine.git
  '^git:(.+)/(.+)/(.+)/?$',           # protocol: ssh   -> git:github.com/oohyun15/ballantine | git:github.com/oohyun15/ballantine/
  '^ssh://git@(.+)/(.+)/(.+)\.git$',  # protocol: ssh   -> ssh://[email protected]/oohyun15/ballantine.git
].freeze
FILE_GITMODULES =
".gitmodules"
PARSER_TOKEN =
"!#!#"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, remote_url:) ⇒ Repository

Returns a new instance of Repository.

Parameters:



46
47
48
49
50
51
52
53
54
# File 'lib/ballantine/repository.rb', line 46

def initialize(path:, remote_url:)
  @path = path
  @commits = []
  @sub_repos = retrieve_sub_repos
  @owner, @name = GITHUB_REGEXES.each do |regex|
    str = remote_url.match(regex)
    break [str[2], str[3]] if str
  end
end

Instance Attribute Details

#commitsObject (readonly)

associations



19
20
21
# File 'lib/ballantine/repository.rb', line 19

def commits
  @commits
end

#fromObject (readonly)

attributes



18
19
20
# File 'lib/ballantine/repository.rb', line 18

def from
  @from
end

#main_repoObject (readonly)

associations



19
20
21
# File 'lib/ballantine/repository.rb', line 19

def main_repo
  @main_repo
end

#nameObject (readonly)

attributes



18
19
20
# File 'lib/ballantine/repository.rb', line 18

def name
  @name
end

#ownerObject (readonly)

attributes



18
19
20
# File 'lib/ballantine/repository.rb', line 18

def owner
  @owner
end

#pathObject (readonly)

attributes



18
19
20
# File 'lib/ballantine/repository.rb', line 18

def path
  @path
end

#sub_reposObject (readonly)

associations



19
20
21
# File 'lib/ballantine/repository.rb', line 19

def sub_repos
  @sub_repos
end

#toObject (readonly)

attributes



18
19
20
# File 'lib/ballantine/repository.rb', line 18

def to
  @to
end

Class Method Details

.allArray<Repository>

Returns:



37
38
39
40
41
# File 'lib/ballantine/repository.rb', line 37

def all
  return [] unless defined?(@_collections)

  @_collections.values
end

.find(path:) ⇒ Repository, NilClass

Parameters:

Returns:



24
25
26
27
# File 'lib/ballantine/repository.rb', line 24

def find(path:)
  @_collections = {} unless defined?(@_collections)
  @_collections[path]
end

.find_or_create_by(path:, remote_url: nil) ⇒ Repository

Parameters:

Returns:



32
33
34
# File 'lib/ballantine/repository.rb', line 32

def find_or_create_by(path:, remote_url: nil)
  find(path:) || @_collections[path] = new(path:, remote_url:)
end

Instance Method Details

#check_commitsBoolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ballantine/repository.rb', line 102

def check_commits
  conf.print_log(binding) if conf.verbose

  authors = retrieve_authors
  authors.each do |author|
    commits = retrieve_commits(author)
    next if commits.empty?

    author.commits_hash[name] = commits
    # TODO: append `commits` to `repo.commits`
  end

  if sub_repos.any?
    sub_repos.each do |sub_repo|
      next if sub_repo.from.hash == sub_repo.to.hash

      Dir.chdir(sub_repo.path)
      sub_repo.check_commits
    end
    Dir.chdir(path)
  end

  true
end

#init_variables(target, source) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ballantine/repository.rb', line 61

def init_variables(target, source)
  conf.print_log(binding) if conf.verbose

  current_revision = %x(git rev-parse --abbrev-ref HEAD).chomp

  foo = lambda do |hash, context|
    hash = check_tag(hash)
    system("git checkout #{hash} -f &> /dev/null")
    system("git pull &> /dev/null")

    hash = %x(git --no-pager log -1 --format='%h').chomp
    commit = Commit.find_or_create_by(
      hash: hash,
      repo: self,
    )
    instance_variable_set("@#{context}", commit)

    if sub_repos.any?
      %x(git ls-tree HEAD #{sub_repos.map(&:path).join(" ")}).split("\n").map do |line|
        _, _, sub_hash, sub_path = line.split(" ")
        sub_repo = Repository.find(
          path: path + "/" + sub_path,
        )
        sub_commit = Commit.find_or_create_by(
          hash: sub_hash,
          repo: sub_repo,
        )
        sub_repo.instance_variable_set("@#{context}", sub_commit)
      end
    end
  end

  foo.call(target, "from")
  foo.call(source, "to")

  system("git checkout #{current_revision} -f &> /dev/null")

  true
end

#urlObject



56
# File 'lib/ballantine/repository.rb', line 56

def url; @url ||= "https://github.com/#{owner}/#{name}" end