git-ce 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ module Git
2
+
3
+ # object that holds all the available branches
4
+ class Branches
5
+
6
+ include Enumerable
7
+
8
+ def initialize(base)
9
+ @branches = {}
10
+
11
+ @base = base
12
+
13
+ @base.lib.branches_all.each do |b|
14
+ @branches[b[0]] = Git::Branch.new(@base, b[0])
15
+ end
16
+ end
17
+
18
+ def local
19
+ self.select { |b| !b.remote }
20
+ end
21
+
22
+ def remote
23
+ self.select { |b| b.remote }
24
+ end
25
+
26
+ # array like methods
27
+
28
+ def size
29
+ @branches.size
30
+ end
31
+
32
+ def each(&block)
33
+ @branches.values.each(&block)
34
+ end
35
+
36
+ # Returns the target branch
37
+ #
38
+ # Example:
39
+ # Given (git branch -a):
40
+ # master
41
+ # remotes/working/master
42
+ #
43
+ # g.branches['master'].full #=> 'master'
44
+ # g.branches['working/master'].full => 'remotes/working/master'
45
+ # g.branches['remotes/working/master'].full => 'remotes/working/master'
46
+ #
47
+ # @param [#to_s] branch_name the target branch name.
48
+ # @return [Git::Branch] the target branch.
49
+ def [](branch_name)
50
+ @branches.values.inject(@branches) do |branches, branch|
51
+ branches[branch.full] ||= branch
52
+
53
+ # This is how Git (version 1.7.9.5) works.
54
+ # Lets you ignore the 'remotes' if its at the beginning of the branch full name (even if is not a real remote branch).
55
+ branches[branch.full.sub('remotes/', '')] ||= branch if branch.full =~ /^remotes\/.+/
56
+
57
+ branches
58
+ end[branch_name.to_s]
59
+ end
60
+
61
+ def to_s
62
+ out = ''
63
+ @branches.each do |k, b|
64
+ out << (b.current ? '* ' : ' ') << b.to_s << "\n"
65
+ end
66
+ out
67
+ end
68
+
69
+ end
70
+
71
+ end
data/lib/git/config.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Git
2
+
3
+ class Config
4
+
5
+ attr_writer :binary_path, :git_ssh
6
+
7
+ def initialize
8
+ @binary_path = nil
9
+ @git_ssh = nil
10
+ end
11
+
12
+ def binary_path
13
+ @binary_path || 'git'
14
+ end
15
+
16
+ def git_ssh
17
+ @git_ssh || ENV['GIT_SSH']
18
+ end
19
+
20
+ end
21
+
22
+ end
data/lib/git/diff.rb ADDED
@@ -0,0 +1,159 @@
1
+ module Git
2
+
3
+ # object that holds the last X commits on given branch
4
+ class Diff
5
+ include Enumerable
6
+
7
+ def initialize(base, from = nil, to = nil)
8
+ @base = base
9
+ @from = from && from.to_s
10
+ @to = to && to.to_s
11
+
12
+ @path = nil
13
+ @full_diff = nil
14
+ @full_diff_files = nil
15
+ @stats = nil
16
+ end
17
+ attr_reader :from, :to
18
+
19
+ def name_status
20
+ cache_name_status
21
+ end
22
+
23
+ def path(path)
24
+ @path = path
25
+ return self
26
+ end
27
+
28
+ def size
29
+ cache_stats
30
+ @stats[:total][:files]
31
+ end
32
+
33
+ def lines
34
+ cache_stats
35
+ @stats[:total][:lines]
36
+ end
37
+
38
+ def deletions
39
+ cache_stats
40
+ @stats[:total][:deletions]
41
+ end
42
+
43
+ def insertions
44
+ cache_stats
45
+ @stats[:total][:insertions]
46
+ end
47
+
48
+ def stats
49
+ cache_stats
50
+ @stats
51
+ end
52
+
53
+ # if file is provided and is writable, it will write the patch into the file
54
+ def patch(file = nil)
55
+ cache_full
56
+ @full_diff
57
+ end
58
+ alias_method :to_s, :patch
59
+
60
+ # enumerable methods
61
+
62
+ def [](key)
63
+ process_full
64
+ @full_diff_files.assoc(key)[1]
65
+ end
66
+
67
+ def each(&block) # :yields: each Git::DiffFile in turn
68
+ process_full
69
+ @full_diff_files.map { |file| file[1] }.each(&block)
70
+ end
71
+
72
+ class DiffFile
73
+ attr_accessor :patch, :path, :mode, :src, :dst, :type
74
+ @base = nil
75
+
76
+ def initialize(base, hash)
77
+ @base = base
78
+ @patch = hash[:patch]
79
+ @path = hash[:path]
80
+ @mode = hash[:mode]
81
+ @src = hash[:src]
82
+ @dst = hash[:dst]
83
+ @type = hash[:type]
84
+ @binary = hash[:binary]
85
+ end
86
+
87
+ def binary?
88
+ !!@binary
89
+ end
90
+
91
+ def blob(type = :dst)
92
+ if type == :src
93
+ @base.object(@src) if @src != '0000000'
94
+ else
95
+ @base.object(@dst) if @dst != '0000000'
96
+ end
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def cache_full
103
+ @full_diff ||= @base.lib.diff_full(@from, @to, {:path_limiter => @path})
104
+ end
105
+
106
+ def process_full
107
+ return if @full_diff_files
108
+ cache_full
109
+ @full_diff_files = process_full_diff
110
+ end
111
+
112
+ def cache_stats
113
+ @stats ||= @base.lib.diff_stats(@from, @to, {:path_limiter => @path})
114
+ end
115
+
116
+ def cache_name_status
117
+ @name_status ||= @base.lib.diff_name_status(@from, @to, {:path => @path})
118
+ end
119
+
120
+ # break up @diff_full
121
+ def process_full_diff
122
+ defaults = {
123
+ :mode => '',
124
+ :src => '',
125
+ :dst => '',
126
+ :type => 'modified'
127
+ }
128
+ final = {}
129
+ current_file = nil
130
+ if @full_diff.encoding.name != "UTF-8"
131
+ full_diff_utf8_encoded = @full_diff.encode("UTF-8", "binary", { :invalid => :replace, :undef => :replace })
132
+ else
133
+ full_diff_utf8_encoded = @full_diff
134
+ end
135
+ full_diff_utf8_encoded.split("\n").each do |line|
136
+ if m = /^diff --git a\/(.*?) b\/(.*?)/.match(line)
137
+ current_file = m[1]
138
+ final[current_file] = defaults.merge({:patch => line, :path => current_file})
139
+ else
140
+ if m = /^index (.......)\.\.(.......)( ......)*/.match(line)
141
+ final[current_file][:src] = m[1]
142
+ final[current_file][:dst] = m[2]
143
+ final[current_file][:mode] = m[3].strip if m[3]
144
+ end
145
+ if m = /^([[:alpha:]]*?) file mode (......)/.match(line)
146
+ final[current_file][:type] = m[1]
147
+ final[current_file][:mode] = m[2]
148
+ end
149
+ if m = /^Binary files /.match(line)
150
+ final[current_file][:binary] = true
151
+ end
152
+ final[current_file][:patch] << "\n" + line
153
+ end
154
+ end
155
+ final.map { |e| [e[0], DiffFile.new(@base, e[1])] }
156
+ end
157
+
158
+ end
159
+ end
data/lib/git/index.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Git
2
+ class Index < Git::Path
3
+
4
+ end
5
+ end