Class: Fastlane::Cryptex::GitHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/cryptex/git_helper.rb

Class Method Summary collapse

Class Method Details

.branch_exists?(branch) ⇒ Boolean

Checks if a specific branch exists in the git repo

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 111

def self.branch_exists?(branch)
  return unless @dir

  result = Dir.chdir(@dir) do
    FastlaneCore::CommandExecutor.execute(command: "git branch --list origin/#{branch.shellescape} --no-color -r",
                                          print_all: $verbose,
                                          print_command: $verbose)
  end
  return !result.empty?
end

.checkout_branch(branch) ⇒ Object

Create and checkout an specific branch in the git repo



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 85

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.message "Checking out branch #{branch}..."

  Dir.chdir(@dir) do
    commands.each do |command|
      FastlaneCore::CommandExecutor.execute(command: command,
                                            print_all: $verbose,
                                            print_command: $verbose)
    end
  end
end

.clear_changesObject



76
77
78
79
80
81
82
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 76

def self.clear_changes
  return unless @dir

  FileUtils.rm_rf(@dir)
  UI.success "🔒  Successfully encrypted certificates repo" # so the user is happy
  @dir = nil
end

.clone(git_url, shallow_clone, manual_password: nil, skip_docs: false, branch: "master", digest: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 4

def self.clone(git_url, shallow_clone, manual_password: nil, skip_docs: false, branch: "master", digest: nil)
  return @dir if @dir

  @dir = Dir.mktmpdir

  command = "git clone '#{git_url}' '#{@dir}'"
  command << " --depth 1" if shallow_clone

  UI.message "Cloning remote git repo..."
  begin
    FastlaneCore::CommandExecutor.execute(command: "GIT_TERMINAL_PROMPT=0 #{command}",
                                        print_all: $verbose,
                                    print_command: $verbose)
  rescue StandardError
    UI.error("Error cloning Repo")
    UI.error("Run the following command manually to make sure you're properly authenticated:")
    UI.command(command)
    UI.user_error!("Error cloning cryptex git repo, please make sure you have access to the repository - see instructions above")
  end

  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"

  copy_readme(@dir) unless skip_docs
  Cryptex::Encrypt.new.decrypt_repo(path: @dir, git_url: git_url, manual_password: manual_password, digest: digest)

  return @dir
end

.commit_changes(path, message, git_url, branch = "master", digest: nil) ⇒ Object



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
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 49

def self.commit_changes(path, message, git_url, branch = "master", digest: nil)
  Dir.chdir(path) do
    return if `git status`.include?("nothing to commit")

    Cryptex::Encrypt.new.encrypt_repo(path: path, git_url: git_url, digest: digest)
    File.write("cryptex_version.txt", Cryptex::VERSION) # unencrypted

    commands = []
    commands << "git add -A"
    commands << "git commit -m #{message.shellescape}"
    commands << "GIT_TERMINAL_PROMPT=0 git push origin #{branch.shellescape}"

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

    commands.each do |command|
      FastlaneCore::CommandExecutor.execute(command: command,
                                          print_all: $verbose,
                                      print_command: $verbose)
    end
  end
  FileUtils.rm_rf(path)
  @dir = nil
rescue StandardError => ex
  UI.error("Couldn't commit or push changes back to git...")
  UI.error(ex)
end

.copy_readme(directory) ⇒ Object

Copies the README.md into the git repo



123
124
125
126
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 123

def self.copy_readme(directory)
  template = File.read("#{Cryptex::ROOT}/assets/READMETemplate.md")
  File.write(File.join(directory, "README.md"), template)
end

.cryptex_version(workspace) ⇒ Object



42
43
44
45
46
47
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 42

def self.cryptex_version(workspace)
  path = File.join(workspace, "cryptex_version.txt")
  if File.exist?(path)
    Gem::Version.new(File.read(path))
  end
end

.generate_commit_message(params) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/fastlane/plugin/cryptex/git_helper.rb', line 34

def self.generate_commit_message(params)
  # 'Automatic commit via fastlane'
  [
    "[fastlane]",
    "Updated"
  ].join(" ")
end