Class: Git

Inherits:
SCM
  • Object
show all
Defined in:
lib/version50/git.rb

Instance Method Summary collapse

Methods inherited from SCM

#initialize, #recover, #save

Constructor Details

This class inherits a constructor from SCM

Instance Method Details

#branch(b) ⇒ Object

create a branch



5
6
7
# File 'lib/version50/git.rb', line 5

def branch b
    `git branch #{b}`
end

#checkout(b) ⇒ Object

checkout a branch



10
11
12
13
# File 'lib/version50/git.rb', line 10

def checkout b
    super
    `git checkout #{b}`
end

#commit(options = {}) ⇒ Object

commit a new version without pushing



21
22
23
24
25
26
27
28
# File 'lib/version50/git.rb', line 21

def commit(options = {})
    # prompt for commit message
    message = super

    # add all files and commit
    `git add --all`
    `git commit -m "#{message}"`
end

#config(info) ⇒ Object

configure the repo with user’s info



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/version50/git.rb', line 31

def config info
    # configure git user
    `git config user.name "#{info['name']}"`
    `git config user.email "#{info['email']}"`

    # configure remote if not already
    origin = `git remote`
    if origin == '' && info['remote'] != ''
        `git remote add origin #{info['remote']}`
    end
end

#download(url, path = '') ⇒ Object

clone a repository



16
17
18
# File 'lib/version50/git.rb', line 16

def download(url, path = '')
    `git clone #{url} #{path} > /dev/null 2> /dev/null`
end

#initObject

create a new repo



44
45
46
47
# File 'lib/version50/git.rb', line 44

def init
    `git init`
    `echo ".version50\n.version50-warps" > .gitignore`
end

#logObject

view the project history



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/version50/git.rb', line 50

def log
    # great idea or greatest idea?
    delimiter = '!@#%^&*'
    history = `git log --graph --pretty=format:'#{delimiter} %h #{delimiter} %s #{delimiter} %cr #{delimiter} %an' --abbrev-commit`

    # iterate over history lines
    commits = []
    lines = history.split "\n"
    lines.each_with_index do |line, i|
        # get information from individual commits
        commit = line.split(delimiter).map { |s| s.strip }
        commits.push({
            :id => commit[1],
            :message => commit[2],
            :timestamp => commit[3],
            :author => commit[4]
        })
    end

    return commits
end

#presentObject

return to the present



73
74
75
76
# File 'lib/version50/git.rb', line 73

def present
    `git reset --hard`
    `git checkout master > /dev/null 2> /dev/null`
end

#pullObject



78
79
80
81
82
# File 'lib/version50/git.rb', line 78

def pull
    # save before doing anything
    self.save({ :quiet => true })
    `git pull --rebase`
end

#pushObject

push existing commits



85
86
87
# File 'lib/version50/git.rb', line 85

def push
    `git push -u origin master > /dev/null 2>&1`
end

#resetObject

reset all changes since last commit



90
91
92
# File 'lib/version50/git.rb', line 90

def reset
    `git reset --hard`
end

#statusObject

view changed files



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
145
# File 'lib/version50/git.rb', line 95

def status
    # get status from SCM
    status = `git status`

    # iterate over each line in status
    tracked = 0
    added, modified, deleted = [], [], []
    status.split("\n").each do |line|
        # ignore git system lines
        if tracked > 0 && line && line !=~ /\(use "git add <file>\.\.\." to include in what will be committed\)/ &&
                line !=~ /\(use "git add <file>\.\.\." to update what will be committed\)/ &&
                line !=~ /\(use "git checkout -- <file>\.\.\." to discard changes in working directory\)/

            # untracked files, so mark as added
            if tracked == 1
                # determine filename
                line =~ /^#\s*([\w\/\.\-]+)/
                if $1
                    added.push $1
                end

            # currently-tracked files
            elsif tracked == 2
                # determine filename and modified status
                line =~ /^#\s*([\w]+):\s*([\w\/\.\-]+)/

                # tracked and modified
                if $1 == 'modified'
                    modified.push $2

                # tracked and deleted
                elsif $1 == 'deleted'
                    deleted.push $2
                end
            end
        end

        # make sure untracked files are marked as added
        if line =~ /Untracked files:/
            tracked = 1
        elsif line =~ /Changes not staged for commit:/ || line =~ /Changes to be committed:/
            tracked = 2
        end
    end

    return {
        :added => added,
        :deleted => deleted,
        :modified => modified
    }
end

#warp(r = nil) ⇒ Object

warp to a specific version



148
149
150
151
152
153
154
# File 'lib/version50/git.rb', line 148

def warp r = nil
    # save current state and determine revision to warp to
    revision = super r
    if revision
        `git checkout #{revision[:id]} -f > /dev/null 2> /dev/null`
    end
end