Class: MJ::VCS::Git::Repository

Inherits:
Object
  • Object
show all
Includes:
Tools::SubProcess
Defined in:
lib/mj/vcs/git.rb

Overview

A (local) git repository.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



105
106
107
108
# File 'lib/mj/vcs/git.rb', line 105

def initialize(path, noop=false)
    @path = path
    @noop = noop
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



29
30
31
# File 'lib/mj/vcs/git.rb', line 29

def path
  @path
end

Instance Method Details

#checkout(opts) ⇒ Object

Checkout a branch



40
41
42
43
44
45
46
# File 'lib/mj/vcs/git.rb', line 40

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, branch = 'master') ⇒ Object

Clone a repository



49
50
51
52
53
54
55
# File 'lib/mj/vcs/git.rb', line 49

def clone(url, branch = 'master' )
    # Initialize the repository
    init(url)
    remote_add(url, "origin")
    fetch("origin")
    checkout("-b master origin/#{branch}")
end

#exist?Boolean

Check if the repository exists

Returns:

  • (Boolean)

Raises:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mj/vcs/git.rb', line 58

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mj/vcs/git.rb', line 70

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.



84
85
86
87
88
89
90
# File 'lib/mj/vcs/git.rb', line 84

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/mj/vcs/git.rb', line 92

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



110
111
112
113
114
115
116
# File 'lib/mj/vcs/git.rb', line 110

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.



34
35
36
37
# File 'lib/mj/vcs/git.rb', line 34

def mkdir_p(dir)
    logger.debug "mkdir -p #{dir}"
    FileUtils.mkdir_p(dir) if !@noop
end

#rebase(branch, remote = "origin") ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mj/vcs/git.rb', line 118

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



133
134
135
136
137
138
139
# File 'lib/mj/vcs/git.rb', line 133

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