Class: Match::GitHelper
- Inherits:
-
Object
- Object
- Match::GitHelper
- Defined in:
- match/lib/match/git_helper.rb
Constant Summary collapse
- MATCH_VERSION_FILE_NAME =
"match_version.txt"
Class Method Summary collapse
- .add_user_config(user_name, user_email) ⇒ Object
-
.branch_exists?(branch) ⇒ Boolean
Checks if a specific branch exists in the git repo.
-
.checkout_branch(branch) ⇒ Object
Create and checkout an specific branch in the git repo.
- .clear_changes ⇒ Object
- .clone(git_url, shallow_clone, manual_password: nil, skip_docs: false, branch: "master", git_full_name: nil, git_user_email: nil, clone_branch_directly: false) ⇒ Object
- .commit_changes(path, message, git_url, branch = "master", files_to_commmit = nil) ⇒ Object
- .generate_commit_message(params) ⇒ Object
- .match_version(workspace) ⇒ Object
Class Method Details
.add_user_config(user_name, user_email) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'match/lib/match/git_helper.rb', line 191 def self.add_user_config(user_name, user_email) # Add git config if needed commands = [] commands << "git config user.name \"#{user_name}\"" unless user_name.nil? commands << "git config user.email \"#{user_email}\"" unless user_email.nil? return if commands.empty? UI.("Add git user config to local git repo...") Dir.chdir(@dir) do commands.each do |command| FastlaneCore::CommandExecutor.execute(command: command, print_all: FastlaneCore::Globals.verbose?, print_command: FastlaneCore::Globals.verbose?) end end end |
.branch_exists?(branch) ⇒ Boolean
Checks if a specific branch exists in the git repo
180 181 182 183 184 185 186 187 188 189 |
# File 'match/lib/match/git_helper.rb', line 180 def self.branch_exists?(branch) return unless @dir result = Dir.chdir(@dir) do FastlaneCore::CommandExecutor.execute(command: "git --no-pager branch --list origin/#{branch.shellescape} --no-color -r", print_all: FastlaneCore::Globals.verbose?, print_command: FastlaneCore::Globals.verbose?) end return !result.empty? end |
.checkout_branch(branch) ⇒ Object
Create and checkout an specific branch in the git repo
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'match/lib/match/git_helper.rb', line 154 def self.checkout_branch(branch) return unless @dir commands = [] if branch_exists?(branch) # Checkout the branch if it already exists commands << "git checkout #{branch.shellescape}" else # If a new branch is being created, we create it as an 'orphan' to not inherit changes from the master branch. commands << "git checkout --orphan #{branch.shellescape}" # We also need to reset the working directory to not transfer any uncommitted changes to the new branch. commands << "git reset --hard" end UI.("Checking out branch #{branch}...") Dir.chdir(@dir) do commands.each do |command| FastlaneCore::CommandExecutor.execute(command: command, print_all: FastlaneCore::Globals.verbose?, print_command: FastlaneCore::Globals.verbose?) end end end |
.clear_changes ⇒ Object
146 147 148 149 150 151 |
# File 'match/lib/match/git_helper.rb', line 146 def self.clear_changes return unless @dir FileUtils.rm_rf(@dir) @dir = nil end |
.clone(git_url, shallow_clone, manual_password: nil, skip_docs: false, branch: "master", git_full_name: nil, git_user_email: nil, clone_branch_directly: false) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 74 75 |
# File 'match/lib/match/git_helper.rb', line 10 def self.clone(git_url, shallow_clone, manual_password: nil, skip_docs: false, branch: "master", git_full_name: nil, git_user_email: nil, clone_branch_directly: false) # Note: if you modify the parameters above, don't forget to also update the method call in # - runner.rb # - nuke.rb # - change_password.rb # - commands_generator.rb # return @dir if @dir @dir = Dir.mktmpdir command = "git clone '#{git_url}' '#{@dir}'" if shallow_clone command << " --depth 1 --no-single-branch" elsif clone_branch_directly command += " -b #{branch.shellescape} --single-branch" end UI.("Cloning remote git repo...") if branch && !clone_branch_directly UI.("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 branch && clone_branch_directly UI.error("You passed '#{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(git_full_name, git_user_email) UI.user_error!("Error cloning repo, make sure you have access to it '#{git_url}'") unless File.directory?(@dir) checkout_branch(branch) unless branch == "master" if !Helper.test? && GitHelper.match_version(@dir).nil? && manual_password.nil? && File.exist?(File.join(@dir, "README.md")) UI.important("Migrating to new match...") ChangePassword.update(params: { git_url: git_url, git_branch: branch, shallow_clone: shallow_clone }, from: "", to: Encrypt.new.password(git_url)) return self.clone(git_url, shallow_clone) end Encrypt.new.decrypt_repo(path: @dir, git_url: git_url, manual_password: manual_password) return @dir end |
.commit_changes(path, message, git_url, branch = "master", files_to_commmit = nil) ⇒ Object
95 96 97 98 99 100 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 |
# File 'match/lib/match/git_helper.rb', line 95 def self.commit_changes(path, , git_url, branch = "master", files_to_commmit = nil) files_to_commmit ||= [] Dir.chdir(path) do return if `git status`.include?("nothing to commit") Encrypt.new.encrypt_repo(path: path, git_url: git_url) commands = [] if files_to_commmit.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_commmit << 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 files_to_commmit << 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_commmit.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 commands << "git commit -m #{.shellescape}" commands << "GIT_TERMINAL_PROMPT=0 git push origin #{branch.shellescape}" UI.("Pushing changes to remote git repo...") commands.each do |command| FastlaneCore::CommandExecutor.execute(command: command, print_all: FastlaneCore::Globals.verbose?, print_command: FastlaneCore::Globals.verbose?) end end FileUtils.rm_rf(path) @dir = nil rescue => ex UI.error("Couldn't commit or push changes back to git...") UI.error(ex) end |
.generate_commit_message(params) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'match/lib/match/git_helper.rb', line 77 def self.(params) # 'Automatic commit via fastlane' [ "[fastlane]", "Updated", params[:type].to_s, "and platform", params[:platform] ].join(" ") end |
.match_version(workspace) ⇒ Object
88 89 90 91 92 93 |
# File 'match/lib/match/git_helper.rb', line 88 def self.match_version(workspace) path = File.join(workspace, MATCH_VERSION_FILE_NAME) if File.exist?(path) Gem::Version.new(File.read(path)) end end |