Module: Prick::Git
- Defined in:
- lib/prick/git.rb
Overview
Tags have a ‘v’ prefixed the version in the git repository but this is made transparent to the application
Class Method Summary collapse
-
.add(*files) ⇒ Object
Add a file to the index of the current branch.
-
.branch?(name) ⇒ Boolean
Check if branch exist.
-
.checkout_branch(name, create: false) ⇒ Object
Check out branch.
-
.checkout_tag(version) ⇒ Object
Checkout a version tag as a detached head.
-
.clean? ⇒ Boolean
Returns true if the repository has no modified files.
-
.commit(msg) ⇒ Object
Commit changes on the current branch“.
-
.create_branch(name) ⇒ Object
Create a branch.
-
.create_tag(version) ⇒ Object
List tag versions Create version tag.
-
.current_branch ⇒ Object
Name of the current branch.
-
.current_tag ⇒ Object
The current tag.
-
.delete_branch(name) ⇒ Object
Destroy branch.
- .delete_tag(version, remote: false) ⇒ Object
-
.detached? ⇒ Boolean
Returns true if the repository is on a detached branch.
- .init ⇒ Object
-
.list_branches ⇒ Object
List branches.
- .list_tags ⇒ Object
-
.merge_branch(name, exclude_files: [], fail: false) ⇒ Object
Merge a branch.
-
.read(file, tag: nil, branch: nil) ⇒ Object
Return content of file.
-
.readlines(file, tag: nil, branch: nil) ⇒ Object
Return content of file in the given tag or branch.
- .rm(file) ⇒ Object
- .rm_rf(file) ⇒ Object
-
.tag?(version) ⇒ Boolean
Return true if ‘version` has an associated tag.
Class Method Details
.add(*files) ⇒ Object
Add a file to the index of the current branch
111 112 113 114 115 116 117 |
# File 'lib/prick/git.rb', line 111 def self.add(*files) Array(files).each { |file| Dir.chdir(File.dirname(file)) { Command.command "git add '#{File.basename(file)}'" } } end |
.branch?(name) ⇒ Boolean
Check if branch exist
62 63 64 65 |
# File 'lib/prick/git.rb', line 62 def self.branch?(name) Command.command("git show-ref --verify --quiet 'refs/heads/#{name}'", fail: false) Command.status == 0 end |
.checkout_branch(name, create: false) ⇒ Object
Check out branch
78 79 80 81 82 83 84 |
# File 'lib/prick/git.rb', line 78 def self.checkout_branch(name, create: false) if create Command.command "git checkout -b #{name}" else Command.command "git checkout #{name}" end end |
.checkout_tag(version) ⇒ Object
Checkout a version tag as a detached head
48 49 50 |
# File 'lib/prick/git.rb', line 48 def self.checkout_tag(version) Command.command "git checkout 'v#{version}'" end |
.clean? ⇒ Boolean
Returns true if the repository has no modified files. Requires the repository to have at least one commit. Creating the repository using Project::initialize_directory guarantees that
15 16 17 18 19 |
# File 'lib/prick/git.rb', line 15 def self.clean?() Command.command("git status").find { |l| l =~ /Changes to be committed:/ || l =~ /^\s*modified:/ }.nil? end |
.commit(msg) ⇒ Object
Commit changes on the current branch“
154 155 156 |
# File 'lib/prick/git.rb', line 154 def self.commit(msg) Command.command "git commit -m '#{msg}'" end |
.create_branch(name) ⇒ Object
Create a branch
68 69 70 |
# File 'lib/prick/git.rb', line 68 def self.create_branch(name) Command.command "git branch #{name}" end |
.create_tag(version) ⇒ Object
List tag versions Create version tag
38 39 40 |
# File 'lib/prick/git.rb', line 38 def self.create_tag(version) Command.command "git tag -a 'v#{version}' -m 'Release #{version}'" end |
.current_branch ⇒ Object
Name of the current branch
57 58 59 |
# File 'lib/prick/git.rb', line 57 def self.current_branch() Command.command("git rev-parse --abbrev-ref HEAD").first end |
.current_tag ⇒ Object
The current tag. This is only defined if the repository is in “detached head” mode
29 |
# File 'lib/prick/git.rb', line 29 def self.current_tag() raise "Not yet implemented" end |
.delete_branch(name) ⇒ Object
Destroy branch
73 74 75 |
# File 'lib/prick/git.rb', line 73 def self.delete_branch(name) Command.command "git branch -D #{name}", fail: false end |
.delete_tag(version, remote: false) ⇒ Object
42 43 44 45 |
# File 'lib/prick/git.rb', line 42 def self.delete_tag(version, remote: false) Command.command "git tag -d 'v#{version}'", fail: false Command.command("git push --delete origin 'v#{version}'", fail: false) if remote end |
.detached? ⇒ Boolean
Returns true if the repository is on a detached branch
22 23 24 25 |
# File 'lib/prick/git.rb', line 22 def self.detached? line = File.readlines(".git/HEAD").first.chomp line =~ /^ref:/ ? false : true end |
.init ⇒ Object
8 9 10 |
# File 'lib/prick/git.rb', line 8 def self.init Command.command "git init" end |
.list_branches ⇒ Object
List branches
106 107 108 |
# File 'lib/prick/git.rb', line 106 def self.list_branches Command.command "git branch --format='%(refname:short)'" end |
.list_tags ⇒ Object
52 53 54 |
# File 'lib/prick/git.rb', line 52 def self. Command.command("git tag").map { |tag| tag.sub(/^v(#{VERSION_SUB_RE})/, '\1') } end |
.merge_branch(name, exclude_files: [], fail: false) ⇒ Object
Merge a branch
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/prick/git.rb', line 87 def self.merge_branch(name, exclude_files: [], fail: false) # Save content of excluded files files = {} exclude_files.each { |file| next if !File.exist?(file) files[file] = File.readlines(file) } Command.command "git merge --no-commit #{name}", fail: false # Reinstate included files files.each { |path, content| File.open(path, "w") { |file| file.puts(content) } # Resolve git unmerged status Git.add(path) } end |
.read(file, tag: nil, branch: nil) ⇒ Object
Return content of file
131 132 133 134 135 136 137 138 139 |
# File 'lib/prick/git.rb', line 131 def self.read(file, tag: nil, branch: nil) !(tag && branch) or raise Internal, "Can't use both tag: and branch: options" if tag Command.command "git show v#{tag}:#{file}" else branch ||= "HEAD" Command.command "git show #{branch}:#{file}" end.join("\n") end |
.readlines(file, tag: nil, branch: nil) ⇒ Object
Return content of file in the given tag or branch. Defaults to HEAD
120 121 122 123 124 125 126 127 128 |
# File 'lib/prick/git.rb', line 120 def self.readlines(file, tag: nil, branch: nil) !(tag && branch) or raise Internal, "Can't use both tag: and branch: options" if tag Command.command "git show v#{tag}:#{file}" else branch ||= "HEAD" Command.command "git show #{branch}:#{file}" end.map { |l| "#{l}\n" } end |
.rm(file) ⇒ Object
141 142 143 144 145 |
# File 'lib/prick/git.rb', line 141 def self.rm(file) Dir.chdir(File.dirname(file)) { Command.command "git rm -f '#{File.basename(file)}'", fail: false } end |
.rm_rf(file) ⇒ Object
147 148 149 150 151 |
# File 'lib/prick/git.rb', line 147 def self.rm_rf(file) Dir.chdir(File.dirname(file)) { Command.command "git rm -rf '#{File.basename(file)}'", fail: false } end |
.tag?(version) ⇒ Boolean
Return true if ‘version` has an associated tag
32 33 34 |
# File 'lib/prick/git.rb', line 32 def self.tag?(version) !.grep(version).empty? end |