Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 160 lines (129 sloc) 2.814 kb
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
1 #
b6eac62 @schacon renamed everything to Rugged from Ribbit
schacon authored
2 # Some example proposed usages of Rugged
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
3 # so we can make sure the API design is OK
4 #
5
6 #
7 # low level reading and writing
8 #
9
b6eac62 @schacon renamed everything to Rugged from Ribbit
schacon authored
10 repo = Rugged::Repository.new(path_to_repo)
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
11 if repo.exists(blob_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
12 # reading
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
13 blob_data, type = repo.read(blob_oid)
14 blob = repo.lookup(blob_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
15 puts blob.data
16 assert blob.data == blob_data && type == 'blob'
17
18 # writing
19 blob_data += "FU, SPIDERMAN 3"
20
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
21 new_oid = repo.write(blob_data, blob)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
22
23 blob.data(blob_data)
24 assert !blob.written?
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
25 new_oid2 = blob.write
26 assert new_oid == new_oid2
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
27 assert blob.written?
28 end
29
72fb9e3 @quirkey WS
quirkey authored
30 #
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
31 # getting the contents of a blob with commit and path
32 #
33
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
34 commit = repo.lookup(commit_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
35 commit.tree.each do |entry|
72fb9e3 @quirkey WS
quirkey authored
36 if (entry.name == path)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
37 && (entry.type == 'blob')
38 return entry.gobect.data
39 end
40 end
41
72fb9e3 @quirkey WS
quirkey authored
42 #
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
43 # getting commit data from oid
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
44 #
45
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
46 commit = repo.lookup(commit_oid, 'commit')
47 puts "oid: " + commit.oid
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
48 puts commit.author.name
49 puts commit.author.email
50 puts commit.author.date.strftime("%y-%m-%d")
51 puts
52 puts commit.message
53
54
72fb9e3 @quirkey WS
quirkey authored
55 #
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
56 # listing a single level tree
57 #
58
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
59 commit = repo.lookup(commit_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
60 commit.tree.each do |entry|
61 puts entry.name
62 end
63
64
72fb9e3 @quirkey WS
quirkey authored
65 #
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
66 # git ls-tree -rt
67 #
68
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
69 def ls_tree(tree_oid)
70 tree = repo.lookup(tree_oid, 'tree')
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
71 tree.each do |entry|
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
72 puts [entry.mode, entry.oid, entry.name].join(' ')
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
73 if entry.type == 'tree'
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
74 ls_tree(entry.oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
75 end
76 end
77 end
78
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
79 commit = repo.lookup(commit_oid)
80 ls_tree(tree_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
81
72fb9e3 @quirkey WS
quirkey authored
82 #
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
83 # updating a file and committing
84 #
85
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
86 commit = repo.lookup(head_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
87 tree = commit.tree
88 entry = tree.path("file.txt")
89 blob = entry.gobject
90 blob.data(blob.data + "EXTRA")
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
91 boid = blob.write
92 tree.add(entry.oid(boid))
93 toid = tree.write
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
94
b6eac62 @schacon renamed everything to Rugged from Ribbit
schacon authored
95 new_commit = Rugged::Commit.new
96 person = Rugged::Person.new("Scott", "scott@github.com", Time.now)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
97 commit.author(person)
98 commit.message("updated file.txt")
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
99 commit.parents([head_oid])
100 commit.tree(toid)
101 commit_oid = commit.write
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
102
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
103 repo.update_ref('refs/heads/master', commit_oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
104
105 #
106 # git log
107 #
108
109 walker = repo.walker
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
110 walker.push(head_oid)
111 while oid = walker.next
112 commit = repo.lookup(oid)
113 puts [commit.oid[0, 8], commit.short_message].join(' ')
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
114 end
115
116 #
117 # git log --since=1.week.ago A ^B
118 #
119
120 since_time = Time.parse("1 week ago")
121
122 walker = repo.walker
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
123 walker.push(a_oid)
124 walker.hide(b_oid)
125 while oid = walker.next
126 commit = repo.lookup(oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
127 if commit.author.date > since_time
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
128 walker.hide(oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
129 else
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
130 puts [commit.oid[0, 8], commit.short_message].join(' ')
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
131 end
132 end
133
134 #
135 # git log --all
136 #
137
138 walker = repo.walker
139 repo.branches(:heads).each do |branch|
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
140 walker.push(branch.oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
141 end
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
142 while oid = walker.next
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
143 ...
144 end
145
146
147 #
148 # finding the last commit that touched a file
149 #
150
151 walker = repo.walker
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
152 walker.push(head_oid)
153 while oid = walker.next
154 commit = repo.lookup(oid)
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
155 diff = commit.diff_tree(commit.parents[0])
156 if diff.include?(path)
9f0f2c7 @vmg Iterate and enumerate like it was meant to be
vmg authored
157 return commit.oid
7a866ea @schacon updated the RUBY_API a bit and added a USAGE file for examples
schacon authored
158 end
159 end
Something went wrong with that request. Please try again.