Class: Match::Storage::GitStorage

Inherits:
Interface
  • Object
show all
Defined in:
match/lib/match/storage/git_storage.rb

Overview

Store the code signing identities in a git repo

Constant Summary collapse

MATCH_VERSION_FILE_NAME =
"match_version.txt"

Instance Attribute Summary collapse

Attributes inherited from Interface

#working_directory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Interface

#configure

Constructor Details

#initialize(type: nil, platform: nil, git_url: nil, shallow_clone: nil, skip_docs: false, branch: "master", git_full_name: nil, git_user_email: nil, clone_branch_directly: false) ⇒ GitStorage

Returns a new instance of GitStorage.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'match/lib/match/storage/git_storage.rb', line 37

def initialize(type: nil,
               platform: nil,
               git_url: nil,
               shallow_clone: nil,
               skip_docs: false,
               branch: "master",
               git_full_name: nil,
               git_user_email: nil,
               clone_branch_directly: false)
  self.git_url = git_url
  self.shallow_clone = shallow_clone
  self.skip_docs = skip_docs
  self.branch = branch
  self.git_full_name = git_full_name
  self.git_user_email = git_user_email
  self.clone_branch_directly = clone_branch_directly

  self.type = type if type
  self.platform = platform if platform
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



16
17
18
# File 'match/lib/match/storage/git_storage.rb', line 16

def branch
  @branch
end

#clone_branch_directlyObject

Returns the value of attribute clone_branch_directly.



19
20
21
# File 'match/lib/match/storage/git_storage.rb', line 19

def clone_branch_directly
  @clone_branch_directly
end

#git_full_nameObject

Returns the value of attribute git_full_name.



17
18
19
# File 'match/lib/match/storage/git_storage.rb', line 17

def git_full_name
  @git_full_name
end

#git_urlObject

User provided values



13
14
15
# File 'match/lib/match/storage/git_storage.rb', line 13

def git_url
  @git_url
end

#git_user_emailObject

Returns the value of attribute git_user_email.



18
19
20
# File 'match/lib/match/storage/git_storage.rb', line 18

def git_user_email
  @git_user_email
end

#platformObject

Returns the value of attribute platform.



21
22
23
# File 'match/lib/match/storage/git_storage.rb', line 21

def platform
  @platform
end

#shallow_cloneObject

Returns the value of attribute shallow_clone.



14
15
16
# File 'match/lib/match/storage/git_storage.rb', line 14

def shallow_clone
  @shallow_clone
end

#skip_docsObject

Returns the value of attribute skip_docs.



15
16
17
# File 'match/lib/match/storage/git_storage.rb', line 15

def skip_docs
  @skip_docs
end

#typeObject

Returns the value of attribute type.



20
21
22
# File 'match/lib/match/storage/git_storage.rb', line 20

def type
  @type
end

Class Method Details

.configure(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'match/lib/match/storage/git_storage.rb', line 23

def self.configure(params)
  return self.new(
    type: params[:type].to_s,
    platform: params[:platform].to_s,
    git_url: params[:git_url],
    shallow_clone: params[:shallow_clone],
    skip_docs: params[:skip_docs],
    branch: params[:git_branch],
    git_full_name: params[:git_full_name],
    git_user_email: params[:git_user_email],
    clone_branch_directly: params[:clone_branch_directly]
  )
end

Instance Method Details

#clear_changesObject



151
152
153
154
155
156
# File 'match/lib/match/storage/git_storage.rb', line 151

def clear_changes
  return unless @working_directory

  FileUtils.rm_rf(self.working_directory)
  self.working_directory = nil
end

#downloadObject



58
59
60
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 'match/lib/match/storage/git_storage.rb', line 58

def download
  # Check if we already have a functional working_directory
  return self.working_directory if @working_directory

  # No existing working directory, creating a new one now
  self.working_directory = Dir.mktmpdir

  command = "git clone '#{self.git_url}' '#{self.working_directory}'"
  if self.shallow_clone
    command << " --depth 1 --no-single-branch"
  elsif self.clone_branch_directly
    command += " -b #{self.branch.shellescape} --single-branch"
  end

  UI.message("Cloning remote git repo...")
  if self.branch && !self.clone_branch_directly
    UI.message("If cloning the repo takes too long, you can use the `clone_branch_directly` option in match.")
  end

  begin
    # GIT_TERMINAL_PROMPT will fail the `git clone` command if user credentials are missing
    FastlaneCore::CommandExecutor.execute(command: "GIT_TERMINAL_PROMPT=0 #{command}",
                                        print_all: FastlaneCore::Globals.verbose?,
                                    print_command: FastlaneCore::Globals.verbose?)
  rescue
    UI.error("Error cloning certificates repo, please make sure you have read access to the repository you want to use")
    if self.branch && self.clone_branch_directly
      UI.error("You passed '#{self.branch}' as branch in combination with the `clone_branch_directly` flag. Please remove `clone_branch_directly` flag on the first run for _match_ to create the branch.")
    end
    UI.error("Run the following command manually to make sure you're properly authenticated:")
    UI.command(command)
    UI.user_error!("Error cloning certificates git repo, please make sure you have access to the repository - see instructions above")
  end

  add_user_config(self.git_full_name, self.git_user_email)

  unless File.directory?(self.working_directory)
    UI.user_error!("Error cloning repo, make sure you have access to it '#{self.git_url}'")
  end

  checkout_branch unless self.branch == "master"
end

#generate_commit_messageObject

Generate the commit message based on the user’s parameters



159
160
161
162
163
164
165
166
167
# File 'match/lib/match/storage/git_storage.rb', line 159

def generate_commit_message
  [
    "[fastlane]",
    "Updated",
    self.type,
    "and platform",
    self.platform
  ].join(" ")
end

#save_changes!(files_to_commit: [], custom_message: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'match/lib/match/storage/git_storage.rb', line 101

def save_changes!(files_to_commit: [], custom_message: nil)
  Dir.chdir(File.expand_path(self.working_directory)) do
    commands = []

    if files_to_commit.count > 0 # e.g. for nuke this is treated differently
      if !File.exist?(MATCH_VERSION_FILE_NAME) || File.read(MATCH_VERSION_FILE_NAME) != Fastlane::VERSION.to_s
        files_to_commit << MATCH_VERSION_FILE_NAME
        File.write(MATCH_VERSION_FILE_NAME, Fastlane::VERSION) # stored unencrypted
      end

      template = File.read("#{Match::ROOT}/lib/assets/READMETemplate.md")
      readme_path = "README.md"
      if (!File.exist?(readme_path) || File.read(readme_path) != template) && !self.skip_docs
        files_to_commit << readme_path
        File.write(readme_path, template)
      end

      # `git add` each file we want to commit
      #   - Fixes https://github.com/fastlane/fastlane/issues/8917
      #   - Fixes https://github.com/fastlane/fastlane/issues/8793
      #   - Replaces, closes and fixes https://github.com/fastlane/fastlane/pull/8919
      commands += files_to_commit.map do |current_file|
        "git add #{current_file.shellescape}"
      end
    else
      # No specific list given, e.g. this happens on `fastlane match nuke`
      # We just want to run `git add -A` to commit everything
      commands << "git add -A"
    end
    commit_message = custom_message || generate_commit_message
    commands << "git commit -m #{commit_message.shellescape}"
    commands << "GIT_TERMINAL_PROMPT=0 git push origin #{self.branch.shellescape}"

    UI.message("Pushing changes to remote git repo...")

    begin
      commands.each do |command|
        FastlaneCore::CommandExecutor.execute(command: command,
                                            print_all: FastlaneCore::Globals.verbose?,
                                        print_command: FastlaneCore::Globals.verbose?)
      end

      self.clear_changes
    rescue => ex
      UI.error("Couldn't commit or push changes back to git...")
      UI.error(ex)
    end
  end
end