Class: MJ::VCS::Git::Repository
- Inherits:
-
Object
- Object
- MJ::VCS::Git::Repository
- Includes:
- Tools::SubProcess
- Defined in:
- lib/mj/vcs/git.rb
Overview
A (local) git repository.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#checkout(opts) ⇒ Object
Checkout a branch.
-
#clone(url) ⇒ Object
Clone a repository.
-
#exist? ⇒ Boolean
Check if the repository exists.
- #fetch(remote = nil) ⇒ Object
-
#git(command, wd = path, &block) ⇒ Object
Execute
command
from working directorywd
. - #init(url) ⇒ Object
-
#initialize(path, noop = false) ⇒ Repository
constructor
If
noop
is true no methods invoked on this object actually do any changes. - #log(rev = "") ⇒ Object
-
#mkdir_p(dir) ⇒ Object
Create directory
dir
recursively if needed. - #rebase(branch, remote = "origin") ⇒ Object
-
#remote_add(url, name) ⇒ Object
Add a new remote.
Constructor Details
#initialize(path, noop = false) ⇒ Repository
If noop
is true no methods invoked on this object actually do any changes. The required actions will be logged but not executed.
93 94 95 96 |
# File 'lib/mj/vcs/git.rb', line 93 def initialize(path, noop=false) @path = path @noop = noop end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/mj/vcs/git.rb', line 17 def path @path end |
Instance Method Details
#checkout(opts) ⇒ Object
Checkout a branch
28 29 30 31 32 33 34 |
# File 'lib/mj/vcs/git.rb', line 28 def checkout(opts) if ! exist? and ! $noop raise GitError, "Repository #{path} does not exist!" end # Fetch only from remote if specified git("checkout " + opts) end |
#clone(url) ⇒ Object
Clone a repository
37 38 39 40 41 42 43 |
# File 'lib/mj/vcs/git.rb', line 37 def clone(url) # Initialize the repository init(url) remote_add(url, "origin") fetch("origin") checkout("-b master origin/master") end |
#exist? ⇒ Boolean
Check if the repository exists
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mj/vcs/git.rb', line 46 def exist? # If path does not exist the repo does not exist return false if !path.exist? # If the path is not a directory we have a problem raise GitError, "#{path.to_s} is not a directory!" if !path.directory? # If the path is a directory but no .git directory is found we have a problem too if !path.join( ".git" ).exist? raise GitError, "#{path.to_s} is not a git repo!" end return true end |
#fetch(remote = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mj/vcs/git.rb', line 58 def fetch(remote = nil) if ! exist? and ! $noop raise GitError, "Repository #{path} does not exist!" end # Fetch only from remote if specified cmd = "fetch" if remote cmd += " -q #{remote}" end git(cmd) end |
#git(command, wd = path, &block) ⇒ Object
Execute command
from working directory wd
. If specified call block
with every line of command output.
72 73 74 75 76 77 78 |
# File 'lib/mj/vcs/git.rb', line 72 def git(command, wd = path, &block) rc = self.class.execute "git #{command}", wd, &block if rc != 0 raise GitError, "git #{command || "" } failed with error code #{rc}"; end rc end |
#init(url) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mj/vcs/git.rb', line 80 def init(url) # If the repository already exists we cannot initialize if exist? raise GitError, "Repository #{path} already exists!" end # Create the repository parent directory if needed mkdir_p path # Initialize the repository git("init") end |
#log(rev = "") ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/mj/vcs/git.rb', line 98 def log(rev="") @lines = Array.new git("log #{rev}") do |line| @lines << line end @lines end |
#mkdir_p(dir) ⇒ Object
Create directory dir
recursively if needed.
22 23 24 25 |
# File 'lib/mj/vcs/git.rb', line 22 def mkdir_p(dir) logger.debug "mkdir -p #{dir}" FileUtils.mkdir_p(dir) if !@noop end |
#rebase(branch, remote = "origin") ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mj/vcs/git.rb', line 106 def rebase(branch, remote="origin") if ! exist? and ! $noop raise GitError, "Repository #{path} does not exist!" end # Fetch only from remote if specified cmd = "rebase " if remote cmd += "#{remote}/#{branch}" else cmd += "origin/#{branch}" end git(cmd) end |
#remote_add(url, name) ⇒ Object
Add a new remote
121 122 123 124 125 126 127 |
# File 'lib/mj/vcs/git.rb', line 121 def remote_add(url, name) if ! exist? and ! $noop raise GitError, "Repository #{path} does not exist!" end # Add the origin remote git("remote add #{name} #{url.to_s}") end |