git_wrapper 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/README.md +74 -74
  2. data/git_wrapper.gemspec +21 -21
  3. data/lib/git_wrapper/commands/add.rb +20 -20
  4. data/lib/git_wrapper/commands/branch.rb +72 -72
  5. data/lib/git_wrapper/commands/checkout.rb +20 -20
  6. data/lib/git_wrapper/commands/commit.rb +20 -20
  7. data/lib/git_wrapper/commands/config.rb +25 -25
  8. data/lib/git_wrapper/commands/diff.rb +27 -27
  9. data/lib/git_wrapper/commands/fetch.rb +20 -20
  10. data/lib/git_wrapper/commands/git.rb +43 -43
  11. data/lib/git_wrapper/commands/init.rb +15 -15
  12. data/lib/git_wrapper/commands/log.rb +81 -81
  13. data/lib/git_wrapper/commands/merge.rb +15 -15
  14. data/lib/git_wrapper/commands/pull.rb +20 -20
  15. data/lib/git_wrapper/commands/push.rb +26 -26
  16. data/lib/git_wrapper/commands/remote.rb +48 -48
  17. data/lib/git_wrapper/commands/remove.rb +15 -15
  18. data/lib/git_wrapper/commands/reset.rb +35 -35
  19. data/lib/git_wrapper/commands/revert.rb +22 -22
  20. data/lib/git_wrapper/commands/shell.rb +66 -66
  21. data/lib/git_wrapper/commands/show.rb +39 -39
  22. data/lib/git_wrapper/commands/status.rb +16 -16
  23. data/lib/git_wrapper/commands/tag.rb +53 -53
  24. data/lib/git_wrapper/repository.rb +208 -208
  25. data/lib/git_wrapper/results/diff_name_status.rb +27 -27
  26. data/lib/git_wrapper/results/file_status.rb +21 -21
  27. data/lib/git_wrapper/results/log_info.rb +22 -22
  28. data/lib/git_wrapper/results/status_porcelain.rb +39 -39
  29. data/lib/git_wrapper/version.rb +3 -3
  30. data/lib/git_wrapper.rb +46 -44
  31. data/spec/repository_spec.rb +888 -888
  32. data/spec/spec_helper.rb +9 -9
  33. data/spec/status_porcelain_parser_spec.rb +86 -86
  34. data/spec/support/helpers/file_helper.rb +40 -36
  35. data/spec/support/matchers/git_status_matchers.rb +39 -39
  36. metadata +17 -7
@@ -1,889 +1,889 @@
1
- require 'spec_helper'
2
-
3
- describe GitWrapper, '-> Repository' do
4
-
5
- before(:each) do
6
- @file_helper = FileHelper.new
7
- end
8
-
9
- after(:each) do
10
- @file_helper.remove_temp_folders
11
- end
12
-
13
- it 'Init repo' do
14
- repo = Repository.new(@file_helper.create_temp_folder)
15
- repo.init.should be_true
16
- repo.log_output.last.should eq("Initialized empty Git repository in #{repo.location}/.git/\n")
17
- end
18
-
19
- it 'Init bare repo' do
20
- repo = Repository.new(@file_helper.create_temp_folder)
21
- repo.init_bare.should be_true
22
- repo.log_output.last.should eq("Initialized empty Git repository in #{repo.location}/\n")
23
- end
24
-
25
- it 'Init repo in new folder' do
26
- folder_name = @file_helper.create_temp_folder
27
- repo = Repository.new(folder_name)
28
- repo.init.should be_true
29
- repo.location.should eq(folder_name)
30
- Dir.exists?(repo.location).should be_true
31
- FileUtils.rm_rf repo.location
32
- end
33
-
34
- it 'Is initialized repo' do
35
- repo = Repository.new(@file_helper.create_temp_folder)
36
- repo.initialized?.should be_false
37
- repo.init
38
- repo.initialized?.should be_true
39
- repo.bare?.should be_false
40
- end
41
-
42
- it 'Is initialized bare repo' do
43
- repo = Repository.new(@file_helper.create_temp_folder)
44
- repo.initialized?.should be_false
45
- repo.init_bare
46
- repo.initialized?.should be_true
47
- repo.bare?.should be_true
48
- end
49
-
50
- it 'Status into new repo' do
51
- repo = Repository.new(@file_helper.create_temp_folder)
52
- repo.init
53
- repo.status.should be_empty
54
- end
55
-
56
- it 'Add one file into repo' do
57
- repo = Repository.new(@file_helper.create_temp_folder)
58
- repo.init
59
-
60
- file1 = @file_helper.create_temp_file(repo.location, 'test')
61
- file2 = @file_helper.create_temp_file(repo.location, 'test')
62
-
63
- initial_status = repo.status
64
- initial_status.should have(2).items
65
- initial_status[0].should be_git_untracked(File.basename(file1))
66
- initial_status[1].should be_git_untracked(File.basename(file2))
67
-
68
- repo.add(file1).should be_true
69
- repo.add('0123456.789').should be_false
70
-
71
- final_status = repo.status
72
- final_status.should have(2).items
73
- final_status[0].should be_git_new_file(File.basename(file1))
74
- final_status[1].should be_git_untracked(File.basename(file2))
75
- end
76
-
77
- it 'Add all files into repo' do
78
- repo = Repository.new(@file_helper.create_temp_folder)
79
- repo.init
80
-
81
- file1 = @file_helper.create_temp_file(repo.location, 'test')
82
- file2 = @file_helper.create_temp_file(repo.location, 'test')
83
-
84
- initial_status = repo.status
85
- initial_status.should have(2).items
86
- initial_status[0].should be_git_untracked(File.basename(file1))
87
- initial_status[1].should be_git_untracked(File.basename(file2))
88
-
89
- repo.add_all.should be_true
90
-
91
- final_status = repo.status
92
- final_status.should have(2).items
93
- final_status[0].should be_git_new_file(File.basename(file1))
94
- final_status[1].should be_git_new_file(File.basename(file2))
95
- end
96
-
97
- it 'Commit repo' do
98
- repo = Repository.new(@file_helper.create_temp_folder)
99
- repo.init
100
-
101
- file = @file_helper.create_temp_file(repo.location, 'test')
102
-
103
- repo.status.first.should be_git_untracked(File.basename(file))
104
- repo.add_all
105
- repo.status.first.should be_git_new_file(File.basename(file))
106
-
107
- repo.commit('comment').should be_true
108
- repo.commit('comment').should be_false
109
-
110
- repo.status.should be_empty
111
- end
112
-
113
- it 'Commit clean directory' do
114
- repo = Repository.new(@file_helper.create_temp_folder)
115
- repo.init
116
-
117
- repo.commit('comment').should be_false
118
- end
119
-
120
- it 'Commt in name of another user' do
121
- repo = Repository.new(@file_helper.create_temp_folder)
122
- repo.init
123
- @file_helper.create_temp_file(repo.location, 'test')
124
- repo.add_all
125
- repo.commit('first_commit', :author_name => 'another_author', :author_email => 'another_author@mail.com').should be_true
126
-
127
- log = repo.log.first
128
- log.author_name.should eq 'another_author'
129
- end
130
-
131
- it 'Delete file' do
132
- repo = Repository.new(@file_helper.create_temp_folder)
133
- repo.init
134
-
135
- file = @file_helper.create_temp_file(repo.location, 'test')
136
-
137
- repo.add_all
138
- repo.commit('comments')
139
- repo.status.should be_empty
140
-
141
- repo.remove(File.basename(file)).should be_true
142
- repo.status.first.should be_git_deleted(File.basename(file))
143
-
144
- repo.remove('0123456.789').should be_false
145
- end
146
-
147
- it 'Add remote to repo' do
148
- repo = Repository.new(@file_helper.create_temp_folder)
149
- repo.init
150
-
151
- repo.remotes.should be_empty
152
-
153
- repo.add_remote('origin', @file_helper.create_temp_folder).should be_true
154
- repo.add_remote('origin', '0123456789').should be_false
155
-
156
- repo.remotes.should have(1).items
157
- repo.remotes.first.should eq('origin')
158
- end
159
-
160
- it 'Remove remote from repo' do
161
- repo = Repository.new(@file_helper.create_temp_folder)
162
- repo.init
163
-
164
- repo.remotes.should be_empty
165
-
166
- repo.add_remote('origin', @file_helper.create_temp_folder)
167
-
168
- repo.remotes.first.should eq('origin')
169
-
170
- repo.remove_remote('origin').should be_true
171
- repo.remove_remote('origin').should be_false
172
-
173
- repo.remotes.should be_empty
174
- end
175
-
176
- it 'Pull from another repo' do
177
- repo1 = Repository.new(@file_helper.create_temp_folder)
178
- file_name1 = @file_helper.create_temp_file(repo1.location, 'test')
179
- repo1.init
180
- repo1.add_all
181
- repo1.commit('...')
182
-
183
- repo2 = Repository.new(@file_helper.create_temp_folder)
184
- repo2.init
185
- repo2.add_remote('origin', repo1.location)
186
-
187
- file_name2 = "#{repo2.location}/#{File.basename(file_name1)}"
188
-
189
- File.exist?(file_name2).should be_false
190
-
191
- repo2.pull('origin').should be_true
192
- repo2.pull('origin_2').should be_false
193
-
194
- File.exist?(file_name2).should be_true
195
- end
196
-
197
- it 'Push into bare repo' do
198
- repo1 = Repository.new(@file_helper.create_temp_folder)
199
- repo1.init_bare
200
-
201
- repo2 = Repository.new(@file_helper.create_temp_folder)
202
- repo2.init
203
- file_name1 = @file_helper.create_temp_file(repo2.location, 'test')
204
-
205
- repo2.add_remote('origin', repo1.location)
206
- repo2.add_all
207
- repo2.commit('...')
208
- repo2.push('origin').should be_true
209
- repo2.push('origin_2').should be_false
210
-
211
- repo3 = Repository.new(@file_helper.create_temp_folder)
212
- repo3.init
213
- repo3.add_remote('origin', repo1.location)
214
-
215
- file_name2 = "#{repo3.location}/#{File.basename(file_name1)}"
216
-
217
- File.exist?(file_name2).should be_false
218
-
219
- repo3.pull('origin')
220
-
221
- File.exist?(file_name2).should be_true
222
- end
223
-
224
- it 'Show base, mine and theirs version of a file' do
225
- master_repo = Repository.new(@file_helper.create_temp_folder)
226
- master_repo.init_bare
227
-
228
- my_repo = Repository.new(@file_helper.create_temp_folder)
229
- my_repo.init
230
- my_repo.add_remote('origin', master_repo.location)
231
- my_file = @file_helper.create_temp_file(my_repo.location, 'version base')
232
- my_repo.add_all
233
- my_repo.commit('base commit')
234
- my_repo.push
235
-
236
- their_repo = Repository.new(@file_helper.create_temp_folder)
237
- their_repo.init
238
- their_repo.add_remote('origin', master_repo.location)
239
- their_repo.pull
240
- their_file = "#{their_repo.location}/#{File.basename(my_file)}"
241
- File.open(their_file, 'w') { |f| f.puts 'version theirs' }
242
- their_repo.add_all
243
- their_repo.commit('theris commit')
244
- their_repo.push
245
-
246
- File.open(my_file, 'w') { |f| f.puts 'version mine' }
247
- my_repo.add_all
248
- my_repo.commit('mine commit')
249
-
250
- my_repo.pull.should be_false
251
- my_repo.status.first.should be_git_merge_conflict(File.basename(my_file))
252
-
253
- my_repo.show_base(File.basename(my_file)).should eq("version base\n")
254
- my_repo.show_mine(File.basename(my_file)).should eq("version mine\n")
255
- my_repo.show_theirs(File.basename(my_file)).should eq("version theirs\n")
256
- end
257
-
258
- it 'Show file from differente versions' do
259
- repo = Repository.new(@file_helper.create_temp_folder)
260
- repo.init
261
-
262
- file_name = @file_helper.create_temp_file(repo.location, 'version 1')
263
- repo.add_all
264
- repo.commit 'first commit'
265
-
266
- repo.checkout 'master', 'test'
267
-
268
- File.open(file_name, 'w') { |f| f.puts 'version 2' }
269
- repo.add_all
270
- repo.commit 'seccond_commit'
271
-
272
- repo.checkout 'master'
273
-
274
- repo.show(File.basename(file_name)).should eq "version 1\n"
275
- repo.show(File.basename(file_name), 'test').should eq "version 2\n"
276
- end
277
-
278
- it 'Show commit logs' do
279
- repo = Repository.new(@file_helper.create_temp_folder)
280
- repo.init
281
-
282
- file_name1 = @file_helper.create_temp_file(repo.location, 'test')
283
- file_name2 = @file_helper.create_temp_file(repo.location, 'test')
284
-
285
- repo.add_all
286
-
287
- repo.commit('first commit')
288
-
289
- log = repo.log
290
-
291
- log.should have(1).items
292
- log.first.subject.should eq('first commit')
293
- log.first.parents.should be_empty
294
- log.first.merge?.should be_false
295
-
296
- File.open(file_name1, 'w') { |f| f.puts 'test 2' }
297
-
298
- repo.add_all
299
- repo.commit('second commit')
300
-
301
- repo.log(:file_name => File.basename(file_name1)).should have(2).items
302
- repo.log(:file_name => File.basename(file_name2)).should have(1).items
303
- end
304
-
305
- it 'Show merge logs' do
306
- repo = Repository.new(@file_helper.create_temp_folder)
307
- repo.init
308
-
309
- @file_helper.create_temp_file(repo.location, 'file1')
310
- repo.add_all
311
- repo.commit 'first commit'
312
-
313
- repo.checkout 'master', 'test'
314
-
315
- @file_helper.create_temp_file(repo.location, 'file2')
316
- repo.add_all
317
- repo.commit 'second commit'
318
- second_commit = repo.log.first
319
-
320
- repo.checkout 'master'
321
-
322
- @file_helper.create_temp_file(repo.location, 'file2')
323
- repo.add_all
324
- repo.commit 'third commit'
325
- third_commit = repo.log.first
326
-
327
- repo.merge 'test'
328
-
329
- log = repo.log.first
330
- log.subject.should eq "Merge branch 'test'"
331
- log.merge?.should be_true
332
- log.parents.first.should eq third_commit.commit_hash
333
- log.parents.last.should eq second_commit.commit_hash
334
- end
335
-
336
- it 'Show existent branches' do
337
- origin = Repository.new(@file_helper.create_temp_folder)
338
- origin.init_bare
339
-
340
- repo = Repository.new(@file_helper.create_temp_folder)
341
- repo.init
342
- repo.add_remote 'origin', origin.location
343
- @file_helper.create_temp_file(repo.location, 'test')
344
-
345
- repo.branches.should be_empty
346
-
347
- repo.add_all
348
- repo.commit('first commit')
349
- repo.push
350
-
351
- branches = repo.branches
352
- branches.should have(2).items
353
- branches.should include('master')
354
- branches.should include('remotes/origin/master')
355
- end
356
-
357
- it 'Create a new branch' do
358
- repo = Repository.new(@file_helper.create_temp_folder)
359
- repo.init
360
- @file_helper.create_temp_file(repo.location, 'test')
361
- repo.add_all
362
- repo.commit('first commit')
363
-
364
- repo.branch("branch1").should be_true
365
-
366
- repo.branches.should include('branch1')
367
- end
368
-
369
- it 'Remove an existing branch' do
370
- repo = Repository.new(@file_helper.create_temp_folder)
371
- repo.init
372
- @file_helper.create_temp_file(repo.location, 'test')
373
- repo.add_all
374
- repo.commit('first commit')
375
-
376
- repo.branch("branch1")
377
-
378
- repo.branches.should include('branch1')
379
-
380
- repo.remove_branch('branch1').should be_true
381
- repo.remove_branch('branch1').should be_false
382
-
383
- repo.branches.should_not include('branch1')
384
- end
385
-
386
- it 'Get a current branch' do
387
- repo = Repository.new(@file_helper.create_temp_folder)
388
- repo.init
389
-
390
- repo.current_branch.should eq('master')
391
-
392
- @file_helper.create_temp_file(repo.location, 'test')
393
- repo.add_all
394
- repo.commit('first commit')
395
-
396
- repo.current_branch.should eq('master')
397
-
398
- repo.branch("branch1")
399
-
400
- repo.current_branch.should eq('master')
401
-
402
- repo.checkout('branch1')
403
-
404
- repo.current_branch.should eq('branch1')
405
- end
406
-
407
- it 'Branch an existing commit' do
408
- repo = Repository.new(@file_helper.create_temp_folder)
409
- repo.init
410
- file_name = @file_helper.create_temp_file(repo.location, 'version master')
411
- repo.add_all
412
- repo.commit('first commit')
413
-
414
- repo.checkout("master", "branch1");
415
-
416
- File.open(file_name, 'w') { |f| f.puts 'version branch1' }
417
-
418
- repo.add_all
419
- repo.commit('version branch1')
420
-
421
- repo.checkout('master')
422
-
423
- File.open(file_name, 'r') do |f|
424
- f.gets.should eq("version master\n")
425
- end
426
-
427
- repo.branch('branch2', 'branch1')
428
- repo.checkout('branch2')
429
-
430
- File.open(file_name, 'r') do |f|
431
- f.gets.should eq("version branch1\n")
432
- end
433
- end
434
-
435
- it 'Checkout an existing branch' do
436
- repo = Repository.new(@file_helper.create_temp_folder)
437
- repo.init
438
- file_name = @file_helper.create_temp_file(repo.location, 'version master')
439
- repo.add_all
440
- repo.commit('commit master')
441
-
442
- repo.current_branch.should eq('master')
443
-
444
- repo.branch('branch1')
445
- repo.checkout('branch1').should be_true
446
- repo.checkout('0123456789').should be_false
447
-
448
- repo.current_branch.should eq('branch1')
449
- File.open(file_name, 'r') do |f|
450
- f.gets.should eq("version master\n")
451
- end
452
-
453
- File.open(file_name, 'w') { |f| f.puts 'version branch1' }
454
- repo.add_all
455
- repo.commit('commit branch1')
456
-
457
- repo.checkout('master')
458
- repo.current_branch.should eq('master')
459
- File.open(file_name, 'r') do |f|
460
- f.gets.should eq("version master\n")
461
- end
462
-
463
- repo.checkout('branch1')
464
- repo.current_branch.should eq('branch1')
465
- File.open(file_name, 'r') do |f|
466
- f.gets.should eq("version branch1\n")
467
- end
468
- end
469
-
470
- it 'Checkout into a new branch' do
471
- repo = Repository.new(@file_helper.create_temp_folder)
472
- repo.init
473
- @file_helper.create_temp_file(repo.location, 'version master')
474
- repo.add_all
475
- repo.commit('commit master')
476
-
477
- repo.checkout('master', 'branch1').should be_true
478
-
479
- repo.current_branch.should eq('branch1')
480
- end
481
-
482
- it 'Create a new tag' do
483
- repo = Repository.new(@file_helper.create_temp_folder)
484
- repo.init
485
- @file_helper.create_temp_file(repo.location, 'test')
486
- repo.add_all
487
- repo.commit('first commit')
488
-
489
- repo.tag('tag1').should be_true
490
-
491
- repo.tags.should include('tag1')
492
- end
493
-
494
- it 'Remove an existing tag' do
495
- repo = Repository.new(@file_helper.create_temp_folder)
496
- repo.init
497
- @file_helper.create_temp_file(repo.location, 'test')
498
- repo.add_all
499
- repo.commit('first commit')
500
-
501
- repo.tag('tag1')
502
-
503
- repo.tags.should include('tag1')
504
-
505
- repo.remove_tag('tag1').should be_true
506
- repo.remove_tag('tag1').should be_false
507
-
508
- repo.tags.should_not include('tag1')
509
- end
510
-
511
- it 'Create a new tag from existing commit' do
512
- repo = Repository.new(@file_helper.create_temp_folder)
513
- repo.init
514
- file_name = @file_helper.create_temp_file(repo.location, 'version master')
515
- repo.add_all
516
- repo.commit('commit master')
517
-
518
- repo.checkout('master', 'branch1')
519
-
520
- File.open(file_name, 'w') { |f| f.puts 'version branch1' }
521
-
522
- repo.add_all
523
- repo.commit('version branch1')
524
-
525
- repo.tag('tag1', 'master').should be_true
526
-
527
- repo.checkout('tag1', 'branch2')
528
-
529
- File.open(file_name, 'r') do |f|
530
- f.gets.should eq("version master\n")
531
- end
532
-
533
- repo.checkout('branch1')
534
-
535
- File.open(file_name, 'r') do |f|
536
- f.gets.should eq("version branch1\n")
537
- end
538
- end
539
-
540
- it 'Merge two branches' do
541
- repo = Repository.new(@file_helper.create_temp_folder)
542
- repo.init
543
- file_name1 = @file_helper.create_temp_file(repo.location, 'version master')
544
- repo.add_all
545
- repo.commit('commit master')
546
-
547
- repo.checkout('master', 'branch1')
548
-
549
- file_name2 = @file_helper.create_temp_file(repo.location, 'version branch1')
550
-
551
- repo.add_all
552
- repo.commit("commit branch 1")
553
-
554
- repo.checkout("master")
555
-
556
- File.exists?(file_name1).should be_true
557
- File.exists?(file_name2).should be_false
558
-
559
- repo.merge('branch1').should be_true
560
-
561
- File.exists?(file_name1).should be_true
562
- File.exists?(file_name2).should be_true
563
- end
564
-
565
- it 'Merge with conflicts' do
566
- master_repo = Repository.new(@file_helper.create_temp_folder)
567
- master_repo.init_bare
568
-
569
- my_repo = Repository.new(@file_helper.create_temp_folder)
570
- my_repo.init
571
- my_repo.add_remote('origin', master_repo.location)
572
- my_file = @file_helper.create_temp_file(my_repo.location, 'version base')
573
- my_repo.add_all
574
- my_repo.commit('base commit')
575
- my_repo.push
576
-
577
- their_repo = Repository.new(@file_helper.create_temp_folder)
578
- their_repo.init
579
- their_repo.add_remote('origin', master_repo.location)
580
- their_repo.pull
581
- their_file = "#{their_repo.location}/#{File.basename(my_file)}"
582
- File.open(their_file, 'w') { |f| f.puts 'version theirs' }
583
- their_repo.add_all
584
- their_repo.commit('theris commit')
585
- their_repo.push
586
-
587
- File.open(my_file, 'w') { |f| f.puts 'version mine' }
588
- my_repo.add_all
589
- my_repo.commit('mine commit')
590
-
591
- my_repo.fetch
592
- my_repo.merge('origin/master').should be_false
593
- my_repo.status.first.should be_git_merge_conflict(File.basename(my_file))
594
-
595
- my_repo.show_base(File.basename(my_file)).should eq("version base\n")
596
- my_repo.show_mine(File.basename(my_file)).should eq("version mine\n")
597
- my_repo.show_theirs(File.basename(my_file)).should eq("version theirs\n")
598
- end
599
-
600
- it 'Fetch from remote' do
601
- remote_repo = Repository.new(@file_helper.create_temp_folder)
602
- remote_repo.init_bare
603
-
604
- repo1 = Repository.new(@file_helper.create_temp_folder)
605
- repo1.init
606
- repo1.add_remote('origin', remote_repo.location)
607
- @file_helper.create_temp_file(repo1.location, 'file 1')
608
- repo1.add_all
609
- repo1.commit('first commit')
610
- repo1.push
611
-
612
- repo2 = Repository.new(@file_helper.create_temp_folder)
613
- repo2.init
614
- repo2.add_remote('origin', remote_repo.location)
615
- repo2.branches.should be_empty
616
-
617
- repo2.fetch.should be_true
618
-
619
- repo2.branches.should have(1).items
620
- repo2.branches.should include('remotes/origin/master')
621
- end
622
-
623
- it 'Show diff file status between working tree and remote branch' do
624
- remote_repo = Repository.new(@file_helper.create_temp_folder)
625
- remote_repo.init_bare
626
-
627
- repo1 = Repository.new(@file_helper.create_temp_folder)
628
- repo1.init
629
- repo1.add_remote('origin', remote_repo.location)
630
- file1 = @file_helper.create_temp_file(repo1.location, 'file 1')
631
- file2 = @file_helper.create_temp_file(repo1.location, 'file 2')
632
- file3 = @file_helper.create_temp_file(repo1.location, 'file 3')
633
- repo1.add_all
634
- repo1.commit('first commit')
635
- repo1.push
636
-
637
- repo2 = Repository.new(@file_helper.create_temp_folder)
638
- repo2.init
639
- repo2.add_remote('origin', remote_repo.location)
640
- repo2.pull
641
-
642
- repo1.remove file1
643
- File.open(file2, 'w') { |f| f.puts 'file 2 v.2' }
644
- file4 = @file_helper.create_temp_file(repo1.location, 'file 3')
645
- repo1.add_all
646
- repo1.commit('second commit')
647
- repo1.push
648
-
649
- repo2.fetch.should be_true
650
-
651
- diff = repo2.diff('origin/master')
652
- diff.should have(3).items
653
- diff.select { |d| d.file_name == File.basename(file1) }.first.status.should be(:new_file)
654
- diff.select { |d| d.file_name == File.basename(file2) }.first.status.should be(:modified)
655
- diff.select { |d| d.file_name == File.basename(file3) }.should be_empty
656
- diff.select { |d| d.file_name == File.basename(file4) }.first.status.should be(:deleted)
657
-
658
- diff_reverse = repo2.diff_reverse('origin/master')
659
- diff_reverse.should have(3).items
660
- diff_reverse.select { |d| d.file_name == File.basename(file1) }.first.status.should be(:deleted)
661
- diff_reverse.select { |d| d.file_name == File.basename(file2) }.first.status.should be(:modified)
662
- diff_reverse.select { |d| d.file_name == File.basename(file3) }.should be_empty
663
- diff_reverse.select { |d| d.file_name == File.basename(file4) }.first.status.should be(:new_file)
664
- end
665
-
666
- it 'Revert a specific commit' do
667
- repo = Repository.new(@file_helper.create_temp_folder)
668
- repo.init
669
-
670
- file1 = @file_helper.create_temp_file(repo.location, 'file_1')
671
- file2 = @file_helper.create_temp_file(repo.location, 'file_2')
672
-
673
- repo.add_all
674
- repo.commit 'first commit'
675
-
676
- file3 = @file_helper.create_temp_file(repo.location, 'file_3')
677
- repo.remove file2
678
-
679
- repo.add_all
680
- repo.commit 'second commit'
681
-
682
- File.exist?(file1).should be_true
683
- File.exist?(file2).should be_false
684
- File.exist?(file3).should be_true
685
-
686
- last_log = repo.log.first
687
- last_log.subject.should eq 'second commit'
688
-
689
- repo.revert(last_log.commit_hash).should be_true
690
-
691
- File.exist?(file1).should be_true
692
- File.exist?(file2).should be_true
693
- File.exist?(file3).should be_false
694
- end
695
-
696
- it 'Revert a specific merge' do
697
- repo = Repository.new(@file_helper.create_temp_folder)
698
- repo.init
699
-
700
- file1 = @file_helper.create_temp_file(repo.location, 'file_1')
701
- repo.add_all
702
- repo.commit 'first commit'
703
-
704
- repo.branch 'test'
705
-
706
- file2 = @file_helper.create_temp_file(repo.location, 'file_2')
707
- repo.add_all
708
- repo.commit 'second commit'
709
-
710
- repo.checkout 'test'
711
-
712
- file3 = @file_helper.create_temp_file(repo.location, 'file_3')
713
- repo.add_all
714
- repo.commit 'third commit'
715
-
716
- File.exist?(file1).should be_true
717
- File.exist?(file2).should be_false
718
- File.exist?(file3).should be_true
719
-
720
- repo.checkout 'master'
721
-
722
- File.exist?(file1).should be_true
723
- File.exist?(file2).should be_true
724
- File.exist?(file3).should be_false
725
-
726
- repo.merge 'test'
727
-
728
- File.exist?(file1).should be_true
729
- File.exist?(file2).should be_true
730
- File.exist?(file3).should be_true
731
-
732
- last_log = repo.log.first
733
- last_log.subject.should eq "Merge branch 'test'"
734
-
735
- repo.revert(last_log.commit_hash).should be_true
736
-
737
- File.exist?(file1).should be_true
738
- File.exist?(file2).should be_true
739
- File.exist?(file3).should be_false
740
- end
741
-
742
- it 'Reset index only' do
743
- repo = Repository.new(@file_helper.create_temp_folder)
744
- repo.init
745
-
746
- file1 = @file_helper.create_temp_file(repo.location, 'file_1')
747
- repo.add_all
748
- repo.commit 'first commit'
749
-
750
- file2 = @file_helper.create_temp_file(repo.location, 'file_2')
751
- repo.add_all
752
-
753
- File.exist?(file1).should be_true
754
- File.exist?(file2).should be_true
755
- repo.status.should have(1).items
756
- repo.status.first.should be_git_new_file(File.basename(file2))
757
-
758
- repo.reset.should be_true
759
-
760
- File.exist?(file1).should be_true
761
- File.exist?(file2).should be_true
762
- repo.status.should have(1).items
763
- repo.status.first.should be_git_untracked(File.basename(file2))
764
- end
765
-
766
- it 'Reset index and working tree (hard)' do
767
- repo = Repository.new(@file_helper.create_temp_folder)
768
- repo.init
769
-
770
- file1 = @file_helper.create_temp_file(repo.location, 'file_1')
771
- repo.add_all
772
- repo.commit 'first commit'
773
-
774
- file2 = @file_helper.create_temp_file(repo.location, 'file_2')
775
- repo.add_all
776
-
777
- File.exist?(file1).should be_true
778
- File.exist?(file2).should be_true
779
- repo.status.should have(1).items
780
- repo.status.first.should be_git_new_file(File.basename(file2))
781
-
782
- repo.reset(:mode => :hard).should be_true
783
-
784
- File.exist?(file1).should be_true
785
- File.exist?(file2).should be_false
786
- repo.status.should be_empty
787
- end
788
-
789
- it 'Reset index only to specific commit' do
790
- repo = Repository.new(@file_helper.create_temp_folder)
791
- repo.init
792
-
793
- file1 = @file_helper.create_temp_file(repo.location, 'file_1')
794
- repo.add_all
795
- repo.commit 'first commit'
796
-
797
- file2 = @file_helper.create_temp_file(repo.location, 'file_2')
798
- repo.add_all
799
- repo.commit 'second commit'
800
-
801
- file3 = @file_helper.create_temp_file(repo.location, 'file_2')
802
- repo.add_all
803
-
804
- File.exist?(file1).should be_true
805
- File.exist?(file2).should be_true
806
- File.exist?(file3).should be_true
807
-
808
- repo.status.should have(1).items
809
- repo.status.first.should be_git_new_file(File.basename(file3))
810
-
811
- repo.log.should have(2).items
812
- repo.log.first.subject.should eq 'second commit'
813
-
814
- repo.reset(:commit => repo.log.last.commit_hash).should be_true
815
-
816
- File.exist?(file1).should be_true
817
- File.exist?(file2).should be_true
818
- File.exist?(file3).should be_true
819
-
820
- repo.status.should have(2).items
821
- repo.status.first.should be_git_untracked(File.basename(file2))
822
- repo.status.last.should be_git_untracked(File.basename(file3))
823
-
824
- repo.log.should have(1).items
825
- repo.log.first.subject.should eq 'first commit'
826
- end
827
-
828
- it 'Reset index and working tree (hard) to specific commit' do
829
- repo = Repository.new(@file_helper.create_temp_folder)
830
- repo.init
831
-
832
- file1 = @file_helper.create_temp_file(repo.location, 'file_1')
833
- repo.add_all
834
- repo.commit 'first commit'
835
-
836
- file2 = @file_helper.create_temp_file(repo.location, 'file_2')
837
- repo.add_all
838
- repo.commit 'second commit'
839
-
840
- file3 = @file_helper.create_temp_file(repo.location, 'file_2')
841
- repo.add_all
842
-
843
- File.exist?(file1).should be_true
844
- File.exist?(file2).should be_true
845
- File.exist?(file3).should be_true
846
-
847
- repo.status.should have(1).items
848
- repo.status.first.should be_git_new_file(File.basename(file3))
849
-
850
- repo.log.should have(2).items
851
- repo.log.first.subject.should eq 'second commit'
852
-
853
- repo.reset(:mode => :hard, :commit => repo.log.last.commit_hash).should be_true
854
-
855
- File.exist?(file1).should be_true
856
- File.exist?(file2).should be_false
857
- File.exist?(file3).should be_false
858
-
859
- repo.status.should be_empty
860
-
861
- repo.log.should have(1).items
862
- repo.log.first.subject.should eq 'first commit'
863
- end
864
-
865
- it 'Config user and email' do
866
- repo = Repository.new(@file_helper.create_temp_folder)
867
- repo.init
868
-
869
- repo.config('user.name', 'user_test').should be_true
870
- repo.config('user.email', 'user_test@mail.com').should be_true
871
-
872
- repo.config('user.name').should eq 'user_test'
873
- repo.config('user.email').should eq 'user_test@mail.com'
874
- end
875
-
876
- it 'Log specific user on commit' do
877
- repo = Repository.new(@file_helper.create_temp_folder)
878
- repo.init
879
-
880
- repo.config('user.name', 'user_test').should be_true
881
-
882
- @file_helper.create_temp_file(repo.location, 'file')
883
- repo.add_all
884
- repo.commit 'test'
885
-
886
- repo.log.first.commiter_name.should eq 'user_test'
887
- end
888
-
1
+ require 'spec_helper'
2
+
3
+ describe GitWrapper, '-> Repository' do
4
+
5
+ before(:each) do
6
+ @file_helper = FileHelper.new
7
+ end
8
+
9
+ after(:each) do
10
+ @file_helper.remove_temp_folders
11
+ end
12
+
13
+ it 'Init repo' do
14
+ repo = Repository.new(@file_helper.create_temp_folder)
15
+ repo.init.should be_true
16
+ repo.log_output.last.should eq("Initialized empty Git repository in #{repo.location}/.git/\n")
17
+ end
18
+
19
+ it 'Init bare repo' do
20
+ repo = Repository.new(@file_helper.create_temp_folder)
21
+ repo.init_bare.should be_true
22
+ repo.log_output.last.should eq("Initialized empty Git repository in #{repo.location}/\n")
23
+ end
24
+
25
+ it 'Init repo in new folder' do
26
+ folder_name = @file_helper.create_temp_folder
27
+ repo = Repository.new(folder_name)
28
+ repo.init.should be_true
29
+ repo.location.should eq(folder_name)
30
+ Dir.exists?(repo.location).should be_true
31
+ FileUtils.rm_rf repo.location
32
+ end
33
+
34
+ it 'Is initialized repo' do
35
+ repo = Repository.new(@file_helper.create_temp_folder)
36
+ repo.initialized?.should be_false
37
+ repo.init
38
+ repo.initialized?.should be_true
39
+ repo.bare?.should be_false
40
+ end
41
+
42
+ it 'Is initialized bare repo' do
43
+ repo = Repository.new(@file_helper.create_temp_folder)
44
+ repo.initialized?.should be_false
45
+ repo.init_bare
46
+ repo.initialized?.should be_true
47
+ repo.bare?.should be_true
48
+ end
49
+
50
+ it 'Status into new repo' do
51
+ repo = Repository.new(@file_helper.create_temp_folder)
52
+ repo.init
53
+ repo.status.should be_empty
54
+ end
55
+
56
+ it 'Add one file into repo' do
57
+ repo = Repository.new(@file_helper.create_temp_folder)
58
+ repo.init
59
+
60
+ file1 = @file_helper.create_temp_file(repo.location, 'test')
61
+ file2 = @file_helper.create_temp_file(repo.location, 'test')
62
+
63
+ initial_status = repo.status
64
+ initial_status.should have(2).items
65
+ initial_status[0].should be_git_untracked(File.basename(file1))
66
+ initial_status[1].should be_git_untracked(File.basename(file2))
67
+
68
+ repo.add(file1).should be_true
69
+ repo.add('0123456.789').should be_false
70
+
71
+ final_status = repo.status
72
+ final_status.should have(2).items
73
+ final_status[0].should be_git_new_file(File.basename(file1))
74
+ final_status[1].should be_git_untracked(File.basename(file2))
75
+ end
76
+
77
+ it 'Add all files into repo' do
78
+ repo = Repository.new(@file_helper.create_temp_folder)
79
+ repo.init
80
+
81
+ file1 = @file_helper.create_temp_file(repo.location, 'test')
82
+ file2 = @file_helper.create_temp_file(repo.location, 'test')
83
+
84
+ initial_status = repo.status
85
+ initial_status.should have(2).items
86
+ initial_status[0].should be_git_untracked(File.basename(file1))
87
+ initial_status[1].should be_git_untracked(File.basename(file2))
88
+
89
+ repo.add_all.should be_true
90
+
91
+ final_status = repo.status
92
+ final_status.should have(2).items
93
+ final_status[0].should be_git_new_file(File.basename(file1))
94
+ final_status[1].should be_git_new_file(File.basename(file2))
95
+ end
96
+
97
+ it 'Commit repo' do
98
+ repo = Repository.new(@file_helper.create_temp_folder)
99
+ repo.init
100
+
101
+ file = @file_helper.create_temp_file(repo.location, 'test')
102
+
103
+ repo.status.first.should be_git_untracked(File.basename(file))
104
+ repo.add_all
105
+ repo.status.first.should be_git_new_file(File.basename(file))
106
+
107
+ repo.commit('comment').should be_true
108
+ repo.commit('comment').should be_false
109
+
110
+ repo.status.should be_empty
111
+ end
112
+
113
+ it 'Commit clean directory' do
114
+ repo = Repository.new(@file_helper.create_temp_folder)
115
+ repo.init
116
+
117
+ repo.commit('comment').should be_false
118
+ end
119
+
120
+ it 'Commt in name of another user' do
121
+ repo = Repository.new(@file_helper.create_temp_folder)
122
+ repo.init
123
+ @file_helper.create_temp_file(repo.location, 'test')
124
+ repo.add_all
125
+ repo.commit('first_commit', :author_name => 'another_author', :author_email => 'another_author@mail.com').should be_true
126
+
127
+ log = repo.log.first
128
+ log.author_name.should eq 'another_author'
129
+ end
130
+
131
+ it 'Delete file' do
132
+ repo = Repository.new(@file_helper.create_temp_folder)
133
+ repo.init
134
+
135
+ file = @file_helper.create_temp_file(repo.location, 'test')
136
+
137
+ repo.add_all
138
+ repo.commit('comments')
139
+ repo.status.should be_empty
140
+
141
+ repo.remove(File.basename(file)).should be_true
142
+ repo.status.first.should be_git_deleted(File.basename(file))
143
+
144
+ repo.remove('0123456.789').should be_false
145
+ end
146
+
147
+ it 'Add remote to repo' do
148
+ repo = Repository.new(@file_helper.create_temp_folder)
149
+ repo.init
150
+
151
+ repo.remotes.should be_empty
152
+
153
+ repo.add_remote('origin', @file_helper.create_temp_folder).should be_true
154
+ repo.add_remote('origin', '0123456789').should be_false
155
+
156
+ repo.remotes.should have(1).items
157
+ repo.remotes.first.should eq('origin')
158
+ end
159
+
160
+ it 'Remove remote from repo' do
161
+ repo = Repository.new(@file_helper.create_temp_folder)
162
+ repo.init
163
+
164
+ repo.remotes.should be_empty
165
+
166
+ repo.add_remote('origin', @file_helper.create_temp_folder)
167
+
168
+ repo.remotes.first.should eq('origin')
169
+
170
+ repo.remove_remote('origin').should be_true
171
+ repo.remove_remote('origin').should be_false
172
+
173
+ repo.remotes.should be_empty
174
+ end
175
+
176
+ it 'Pull from another repo' do
177
+ repo1 = Repository.new(@file_helper.create_temp_folder)
178
+ file_name1 = @file_helper.create_temp_file(repo1.location, 'test')
179
+ repo1.init
180
+ repo1.add_all
181
+ repo1.commit('...')
182
+
183
+ repo2 = Repository.new(@file_helper.create_temp_folder)
184
+ repo2.init
185
+ repo2.add_remote('origin', repo1.location)
186
+
187
+ file_name2 = "#{repo2.location}/#{File.basename(file_name1)}"
188
+
189
+ File.exist?(file_name2).should be_false
190
+
191
+ repo2.pull('origin').should be_true
192
+ repo2.pull('origin_2').should be_false
193
+
194
+ File.exist?(file_name2).should be_true
195
+ end
196
+
197
+ it 'Push into bare repo' do
198
+ repo1 = Repository.new(@file_helper.create_temp_folder)
199
+ repo1.init_bare
200
+
201
+ repo2 = Repository.new(@file_helper.create_temp_folder)
202
+ repo2.init
203
+ file_name1 = @file_helper.create_temp_file(repo2.location, 'test')
204
+
205
+ repo2.add_remote('origin', repo1.location)
206
+ repo2.add_all
207
+ repo2.commit('...')
208
+ repo2.push('origin').should be_true
209
+ repo2.push('origin_2').should be_false
210
+
211
+ repo3 = Repository.new(@file_helper.create_temp_folder)
212
+ repo3.init
213
+ repo3.add_remote('origin', repo1.location)
214
+
215
+ file_name2 = "#{repo3.location}/#{File.basename(file_name1)}"
216
+
217
+ File.exist?(file_name2).should be_false
218
+
219
+ repo3.pull('origin')
220
+
221
+ File.exist?(file_name2).should be_true
222
+ end
223
+
224
+ it 'Show base, mine and theirs version of a file' do
225
+ master_repo = Repository.new(@file_helper.create_temp_folder)
226
+ master_repo.init_bare
227
+
228
+ my_repo = Repository.new(@file_helper.create_temp_folder)
229
+ my_repo.init
230
+ my_repo.add_remote('origin', master_repo.location)
231
+ my_file = @file_helper.create_temp_file(my_repo.location, 'version base')
232
+ my_repo.add_all
233
+ my_repo.commit('base commit')
234
+ my_repo.push
235
+
236
+ their_repo = Repository.new(@file_helper.create_temp_folder)
237
+ their_repo.init
238
+ their_repo.add_remote('origin', master_repo.location)
239
+ their_repo.pull
240
+ their_file = "#{their_repo.location}/#{File.basename(my_file)}"
241
+ File.open(their_file, 'w') { |f| f.puts 'version theirs' }
242
+ their_repo.add_all
243
+ their_repo.commit('theris commit')
244
+ their_repo.push
245
+
246
+ File.open(my_file, 'w') { |f| f.puts 'version mine' }
247
+ my_repo.add_all
248
+ my_repo.commit('mine commit')
249
+
250
+ my_repo.pull.should be_false
251
+ my_repo.status.first.should be_git_merge_conflict(File.basename(my_file))
252
+
253
+ my_repo.show_base(File.basename(my_file)).should eq("version base\n")
254
+ my_repo.show_mine(File.basename(my_file)).should eq("version mine\n")
255
+ my_repo.show_theirs(File.basename(my_file)).should eq("version theirs\n")
256
+ end
257
+
258
+ it 'Show file from differente versions' do
259
+ repo = Repository.new(@file_helper.create_temp_folder)
260
+ repo.init
261
+
262
+ file_name = @file_helper.create_temp_file(repo.location, 'version 1')
263
+ repo.add_all
264
+ repo.commit 'first commit'
265
+
266
+ repo.checkout 'master', 'test'
267
+
268
+ File.open(file_name, 'w') { |f| f.puts 'version 2' }
269
+ repo.add_all
270
+ repo.commit 'seccond_commit'
271
+
272
+ repo.checkout 'master'
273
+
274
+ repo.show(File.basename(file_name)).should eq "version 1\n"
275
+ repo.show(File.basename(file_name), 'test').should eq "version 2\n"
276
+ end
277
+
278
+ it 'Show commit logs' do
279
+ repo = Repository.new(@file_helper.create_temp_folder)
280
+ repo.init
281
+
282
+ file_name1 = @file_helper.create_temp_file(repo.location, 'test')
283
+ file_name2 = @file_helper.create_temp_file(repo.location, 'test')
284
+
285
+ repo.add_all
286
+
287
+ repo.commit('first commit')
288
+
289
+ log = repo.log
290
+
291
+ log.should have(1).items
292
+ log.first.subject.should eq('first commit')
293
+ log.first.parents.should be_empty
294
+ log.first.merge?.should be_false
295
+
296
+ File.open(file_name1, 'w') { |f| f.puts 'test 2' }
297
+
298
+ repo.add_all
299
+ repo.commit('second commit')
300
+
301
+ repo.log(:file_name => File.basename(file_name1)).should have(2).items
302
+ repo.log(:file_name => File.basename(file_name2)).should have(1).items
303
+ end
304
+
305
+ it 'Show merge logs' do
306
+ repo = Repository.new(@file_helper.create_temp_folder)
307
+ repo.init
308
+
309
+ @file_helper.create_temp_file(repo.location, 'file1')
310
+ repo.add_all
311
+ repo.commit 'first commit'
312
+
313
+ repo.checkout 'master', 'test'
314
+
315
+ @file_helper.create_temp_file(repo.location, 'file2')
316
+ repo.add_all
317
+ repo.commit 'second commit'
318
+ second_commit = repo.log.first
319
+
320
+ repo.checkout 'master'
321
+
322
+ @file_helper.create_temp_file(repo.location, 'file2')
323
+ repo.add_all
324
+ repo.commit 'third commit'
325
+ third_commit = repo.log.first
326
+
327
+ repo.merge 'test'
328
+
329
+ log = repo.log.first
330
+ log.subject.should eq "Merge branch 'test'"
331
+ log.merge?.should be_true
332
+ log.parents.first.should eq third_commit.commit_hash
333
+ log.parents.last.should eq second_commit.commit_hash
334
+ end
335
+
336
+ it 'Show existent branches' do
337
+ origin = Repository.new(@file_helper.create_temp_folder)
338
+ origin.init_bare
339
+
340
+ repo = Repository.new(@file_helper.create_temp_folder)
341
+ repo.init
342
+ repo.add_remote 'origin', origin.location
343
+ @file_helper.create_temp_file(repo.location, 'test')
344
+
345
+ repo.branches.should be_empty
346
+
347
+ repo.add_all
348
+ repo.commit('first commit')
349
+ repo.push
350
+
351
+ branches = repo.branches
352
+ branches.should have(2).items
353
+ branches.should include('master')
354
+ branches.should include('remotes/origin/master')
355
+ end
356
+
357
+ it 'Create a new branch' do
358
+ repo = Repository.new(@file_helper.create_temp_folder)
359
+ repo.init
360
+ @file_helper.create_temp_file(repo.location, 'test')
361
+ repo.add_all
362
+ repo.commit('first commit')
363
+
364
+ repo.branch("branch1").should be_true
365
+
366
+ repo.branches.should include('branch1')
367
+ end
368
+
369
+ it 'Remove an existing branch' do
370
+ repo = Repository.new(@file_helper.create_temp_folder)
371
+ repo.init
372
+ @file_helper.create_temp_file(repo.location, 'test')
373
+ repo.add_all
374
+ repo.commit('first commit')
375
+
376
+ repo.branch("branch1")
377
+
378
+ repo.branches.should include('branch1')
379
+
380
+ repo.remove_branch('branch1').should be_true
381
+ repo.remove_branch('branch1').should be_false
382
+
383
+ repo.branches.should_not include('branch1')
384
+ end
385
+
386
+ it 'Get a current branch' do
387
+ repo = Repository.new(@file_helper.create_temp_folder)
388
+ repo.init
389
+
390
+ repo.current_branch.should eq('master')
391
+
392
+ @file_helper.create_temp_file(repo.location, 'test')
393
+ repo.add_all
394
+ repo.commit('first commit')
395
+
396
+ repo.current_branch.should eq('master')
397
+
398
+ repo.branch("branch1")
399
+
400
+ repo.current_branch.should eq('master')
401
+
402
+ repo.checkout('branch1')
403
+
404
+ repo.current_branch.should eq('branch1')
405
+ end
406
+
407
+ it 'Branch an existing commit' do
408
+ repo = Repository.new(@file_helper.create_temp_folder)
409
+ repo.init
410
+ file_name = @file_helper.create_temp_file(repo.location, 'version master')
411
+ repo.add_all
412
+ repo.commit('first commit')
413
+
414
+ repo.checkout("master", "branch1");
415
+
416
+ File.open(file_name, 'w') { |f| f.puts 'version branch1' }
417
+
418
+ repo.add_all
419
+ repo.commit('version branch1')
420
+
421
+ repo.checkout('master')
422
+
423
+ File.open(file_name, 'r') do |f|
424
+ f.gets.should eq("version master\n")
425
+ end
426
+
427
+ repo.branch('branch2', 'branch1')
428
+ repo.checkout('branch2')
429
+
430
+ File.open(file_name, 'r') do |f|
431
+ f.gets.should eq("version branch1\n")
432
+ end
433
+ end
434
+
435
+ it 'Checkout an existing branch' do
436
+ repo = Repository.new(@file_helper.create_temp_folder)
437
+ repo.init
438
+ file_name = @file_helper.create_temp_file(repo.location, 'version master')
439
+ repo.add_all
440
+ repo.commit('commit master')
441
+
442
+ repo.current_branch.should eq('master')
443
+
444
+ repo.branch('branch1')
445
+ repo.checkout('branch1').should be_true
446
+ repo.checkout('0123456789').should be_false
447
+
448
+ repo.current_branch.should eq('branch1')
449
+ File.open(file_name, 'r') do |f|
450
+ f.gets.should eq("version master\n")
451
+ end
452
+
453
+ File.open(file_name, 'w') { |f| f.puts 'version branch1' }
454
+ repo.add_all
455
+ repo.commit('commit branch1')
456
+
457
+ repo.checkout('master')
458
+ repo.current_branch.should eq('master')
459
+ File.open(file_name, 'r') do |f|
460
+ f.gets.should eq("version master\n")
461
+ end
462
+
463
+ repo.checkout('branch1')
464
+ repo.current_branch.should eq('branch1')
465
+ File.open(file_name, 'r') do |f|
466
+ f.gets.should eq("version branch1\n")
467
+ end
468
+ end
469
+
470
+ it 'Checkout into a new branch' do
471
+ repo = Repository.new(@file_helper.create_temp_folder)
472
+ repo.init
473
+ @file_helper.create_temp_file(repo.location, 'version master')
474
+ repo.add_all
475
+ repo.commit('commit master')
476
+
477
+ repo.checkout('master', 'branch1').should be_true
478
+
479
+ repo.current_branch.should eq('branch1')
480
+ end
481
+
482
+ it 'Create a new tag' do
483
+ repo = Repository.new(@file_helper.create_temp_folder)
484
+ repo.init
485
+ @file_helper.create_temp_file(repo.location, 'test')
486
+ repo.add_all
487
+ repo.commit('first commit')
488
+
489
+ repo.tag('tag1').should be_true
490
+
491
+ repo.tags.should include('tag1')
492
+ end
493
+
494
+ it 'Remove an existing tag' do
495
+ repo = Repository.new(@file_helper.create_temp_folder)
496
+ repo.init
497
+ @file_helper.create_temp_file(repo.location, 'test')
498
+ repo.add_all
499
+ repo.commit('first commit')
500
+
501
+ repo.tag('tag1')
502
+
503
+ repo.tags.should include('tag1')
504
+
505
+ repo.remove_tag('tag1').should be_true
506
+ repo.remove_tag('tag1').should be_false
507
+
508
+ repo.tags.should_not include('tag1')
509
+ end
510
+
511
+ it 'Create a new tag from existing commit' do
512
+ repo = Repository.new(@file_helper.create_temp_folder)
513
+ repo.init
514
+ file_name = @file_helper.create_temp_file(repo.location, 'version master')
515
+ repo.add_all
516
+ repo.commit('commit master')
517
+
518
+ repo.checkout('master', 'branch1')
519
+
520
+ File.open(file_name, 'w') { |f| f.puts 'version branch1' }
521
+
522
+ repo.add_all
523
+ repo.commit('version branch1')
524
+
525
+ repo.tag('tag1', 'master').should be_true
526
+
527
+ repo.checkout('tag1', 'branch2')
528
+
529
+ File.open(file_name, 'r') do |f|
530
+ f.gets.should eq("version master\n")
531
+ end
532
+
533
+ repo.checkout('branch1')
534
+
535
+ File.open(file_name, 'r') do |f|
536
+ f.gets.should eq("version branch1\n")
537
+ end
538
+ end
539
+
540
+ it 'Merge two branches' do
541
+ repo = Repository.new(@file_helper.create_temp_folder)
542
+ repo.init
543
+ file_name1 = @file_helper.create_temp_file(repo.location, 'version master')
544
+ repo.add_all
545
+ repo.commit('commit master')
546
+
547
+ repo.checkout('master', 'branch1')
548
+
549
+ file_name2 = @file_helper.create_temp_file(repo.location, 'version branch1')
550
+
551
+ repo.add_all
552
+ repo.commit("commit branch 1")
553
+
554
+ repo.checkout("master")
555
+
556
+ File.exists?(file_name1).should be_true
557
+ File.exists?(file_name2).should be_false
558
+
559
+ repo.merge('branch1').should be_true
560
+
561
+ File.exists?(file_name1).should be_true
562
+ File.exists?(file_name2).should be_true
563
+ end
564
+
565
+ it 'Merge with conflicts' do
566
+ master_repo = Repository.new(@file_helper.create_temp_folder)
567
+ master_repo.init_bare
568
+
569
+ my_repo = Repository.new(@file_helper.create_temp_folder)
570
+ my_repo.init
571
+ my_repo.add_remote('origin', master_repo.location)
572
+ my_file = @file_helper.create_temp_file(my_repo.location, 'version base')
573
+ my_repo.add_all
574
+ my_repo.commit('base commit')
575
+ my_repo.push
576
+
577
+ their_repo = Repository.new(@file_helper.create_temp_folder)
578
+ their_repo.init
579
+ their_repo.add_remote('origin', master_repo.location)
580
+ their_repo.pull
581
+ their_file = "#{their_repo.location}/#{File.basename(my_file)}"
582
+ File.open(their_file, 'w') { |f| f.puts 'version theirs' }
583
+ their_repo.add_all
584
+ their_repo.commit('theris commit')
585
+ their_repo.push
586
+
587
+ File.open(my_file, 'w') { |f| f.puts 'version mine' }
588
+ my_repo.add_all
589
+ my_repo.commit('mine commit')
590
+
591
+ my_repo.fetch
592
+ my_repo.merge('origin/master').should be_false
593
+ my_repo.status.first.should be_git_merge_conflict(File.basename(my_file))
594
+
595
+ my_repo.show_base(File.basename(my_file)).should eq("version base\n")
596
+ my_repo.show_mine(File.basename(my_file)).should eq("version mine\n")
597
+ my_repo.show_theirs(File.basename(my_file)).should eq("version theirs\n")
598
+ end
599
+
600
+ it 'Fetch from remote' do
601
+ remote_repo = Repository.new(@file_helper.create_temp_folder)
602
+ remote_repo.init_bare
603
+
604
+ repo1 = Repository.new(@file_helper.create_temp_folder)
605
+ repo1.init
606
+ repo1.add_remote('origin', remote_repo.location)
607
+ @file_helper.create_temp_file(repo1.location, 'file 1')
608
+ repo1.add_all
609
+ repo1.commit('first commit')
610
+ repo1.push
611
+
612
+ repo2 = Repository.new(@file_helper.create_temp_folder)
613
+ repo2.init
614
+ repo2.add_remote('origin', remote_repo.location)
615
+ repo2.branches.should be_empty
616
+
617
+ repo2.fetch.should be_true
618
+
619
+ repo2.branches.should have(1).items
620
+ repo2.branches.should include('remotes/origin/master')
621
+ end
622
+
623
+ it 'Show diff file status between working tree and remote branch' do
624
+ remote_repo = Repository.new(@file_helper.create_temp_folder)
625
+ remote_repo.init_bare
626
+
627
+ repo1 = Repository.new(@file_helper.create_temp_folder)
628
+ repo1.init
629
+ repo1.add_remote('origin', remote_repo.location)
630
+ file1 = @file_helper.create_temp_file(repo1.location, 'file 1')
631
+ file2 = @file_helper.create_temp_file(repo1.location, 'file 2')
632
+ file3 = @file_helper.create_temp_file(repo1.location, 'file 3')
633
+ repo1.add_all
634
+ repo1.commit('first commit')
635
+ repo1.push
636
+
637
+ repo2 = Repository.new(@file_helper.create_temp_folder)
638
+ repo2.init
639
+ repo2.add_remote('origin', remote_repo.location)
640
+ repo2.pull
641
+
642
+ repo1.remove file1
643
+ File.open(file2, 'w') { |f| f.puts 'file 2 v.2' }
644
+ file4 = @file_helper.create_temp_file(repo1.location, 'file 3')
645
+ repo1.add_all
646
+ repo1.commit('second commit')
647
+ repo1.push
648
+
649
+ repo2.fetch.should be_true
650
+
651
+ diff = repo2.diff('origin/master')
652
+ diff.should have(3).items
653
+ diff.select { |d| d.file_name == File.basename(file1) }.first.status.should be(:new_file)
654
+ diff.select { |d| d.file_name == File.basename(file2) }.first.status.should be(:modified)
655
+ diff.select { |d| d.file_name == File.basename(file3) }.should be_empty
656
+ diff.select { |d| d.file_name == File.basename(file4) }.first.status.should be(:deleted)
657
+
658
+ diff_reverse = repo2.diff_reverse('origin/master')
659
+ diff_reverse.should have(3).items
660
+ diff_reverse.select { |d| d.file_name == File.basename(file1) }.first.status.should be(:deleted)
661
+ diff_reverse.select { |d| d.file_name == File.basename(file2) }.first.status.should be(:modified)
662
+ diff_reverse.select { |d| d.file_name == File.basename(file3) }.should be_empty
663
+ diff_reverse.select { |d| d.file_name == File.basename(file4) }.first.status.should be(:new_file)
664
+ end
665
+
666
+ it 'Revert a specific commit' do
667
+ repo = Repository.new(@file_helper.create_temp_folder)
668
+ repo.init
669
+
670
+ file1 = @file_helper.create_temp_file(repo.location, 'file_1')
671
+ file2 = @file_helper.create_temp_file(repo.location, 'file_2')
672
+
673
+ repo.add_all
674
+ repo.commit 'first commit'
675
+
676
+ file3 = @file_helper.create_temp_file(repo.location, 'file_3')
677
+ repo.remove file2
678
+
679
+ repo.add_all
680
+ repo.commit 'second commit'
681
+
682
+ File.exist?(file1).should be_true
683
+ File.exist?(file2).should be_false
684
+ File.exist?(file3).should be_true
685
+
686
+ last_log = repo.log.first
687
+ last_log.subject.should eq 'second commit'
688
+
689
+ repo.revert(last_log.commit_hash).should be_true
690
+
691
+ File.exist?(file1).should be_true
692
+ File.exist?(file2).should be_true
693
+ File.exist?(file3).should be_false
694
+ end
695
+
696
+ it 'Revert a specific merge' do
697
+ repo = Repository.new(@file_helper.create_temp_folder)
698
+ repo.init
699
+
700
+ file1 = @file_helper.create_temp_file(repo.location, 'file_1')
701
+ repo.add_all
702
+ repo.commit 'first commit'
703
+
704
+ repo.branch 'test'
705
+
706
+ file2 = @file_helper.create_temp_file(repo.location, 'file_2')
707
+ repo.add_all
708
+ repo.commit 'second commit'
709
+
710
+ repo.checkout 'test'
711
+
712
+ file3 = @file_helper.create_temp_file(repo.location, 'file_3')
713
+ repo.add_all
714
+ repo.commit 'third commit'
715
+
716
+ File.exist?(file1).should be_true
717
+ File.exist?(file2).should be_false
718
+ File.exist?(file3).should be_true
719
+
720
+ repo.checkout 'master'
721
+
722
+ File.exist?(file1).should be_true
723
+ File.exist?(file2).should be_true
724
+ File.exist?(file3).should be_false
725
+
726
+ repo.merge 'test'
727
+
728
+ File.exist?(file1).should be_true
729
+ File.exist?(file2).should be_true
730
+ File.exist?(file3).should be_true
731
+
732
+ last_log = repo.log.first
733
+ last_log.subject.should eq "Merge branch 'test'"
734
+
735
+ repo.revert(last_log.commit_hash).should be_true
736
+
737
+ File.exist?(file1).should be_true
738
+ File.exist?(file2).should be_true
739
+ File.exist?(file3).should be_false
740
+ end
741
+
742
+ it 'Reset index only' do
743
+ repo = Repository.new(@file_helper.create_temp_folder)
744
+ repo.init
745
+
746
+ file1 = @file_helper.create_temp_file(repo.location, 'file_1')
747
+ repo.add_all
748
+ repo.commit 'first commit'
749
+
750
+ file2 = @file_helper.create_temp_file(repo.location, 'file_2')
751
+ repo.add_all
752
+
753
+ File.exist?(file1).should be_true
754
+ File.exist?(file2).should be_true
755
+ repo.status.should have(1).items
756
+ repo.status.first.should be_git_new_file(File.basename(file2))
757
+
758
+ repo.reset.should be_true
759
+
760
+ File.exist?(file1).should be_true
761
+ File.exist?(file2).should be_true
762
+ repo.status.should have(1).items
763
+ repo.status.first.should be_git_untracked(File.basename(file2))
764
+ end
765
+
766
+ it 'Reset index and working tree (hard)' do
767
+ repo = Repository.new(@file_helper.create_temp_folder)
768
+ repo.init
769
+
770
+ file1 = @file_helper.create_temp_file(repo.location, 'file_1')
771
+ repo.add_all
772
+ repo.commit 'first commit'
773
+
774
+ file2 = @file_helper.create_temp_file(repo.location, 'file_2')
775
+ repo.add_all
776
+
777
+ File.exist?(file1).should be_true
778
+ File.exist?(file2).should be_true
779
+ repo.status.should have(1).items
780
+ repo.status.first.should be_git_new_file(File.basename(file2))
781
+
782
+ repo.reset(:mode => :hard).should be_true
783
+
784
+ File.exist?(file1).should be_true
785
+ File.exist?(file2).should be_false
786
+ repo.status.should be_empty
787
+ end
788
+
789
+ it 'Reset index only to specific commit' do
790
+ repo = Repository.new(@file_helper.create_temp_folder)
791
+ repo.init
792
+
793
+ file1 = @file_helper.create_temp_file(repo.location, 'file_1')
794
+ repo.add_all
795
+ repo.commit 'first commit'
796
+
797
+ file2 = @file_helper.create_temp_file(repo.location, 'file_2')
798
+ repo.add_all
799
+ repo.commit 'second commit'
800
+
801
+ file3 = @file_helper.create_temp_file(repo.location, 'file_2')
802
+ repo.add_all
803
+
804
+ File.exist?(file1).should be_true
805
+ File.exist?(file2).should be_true
806
+ File.exist?(file3).should be_true
807
+
808
+ repo.status.should have(1).items
809
+ repo.status.first.should be_git_new_file(File.basename(file3))
810
+
811
+ repo.log.should have(2).items
812
+ repo.log.first.subject.should eq 'second commit'
813
+
814
+ repo.reset(:commit => repo.log.last.commit_hash).should be_true
815
+
816
+ File.exist?(file1).should be_true
817
+ File.exist?(file2).should be_true
818
+ File.exist?(file3).should be_true
819
+
820
+ repo.status.should have(2).items
821
+ repo.status.first.should be_git_untracked(File.basename(file2))
822
+ repo.status.last.should be_git_untracked(File.basename(file3))
823
+
824
+ repo.log.should have(1).items
825
+ repo.log.first.subject.should eq 'first commit'
826
+ end
827
+
828
+ it 'Reset index and working tree (hard) to specific commit' do
829
+ repo = Repository.new(@file_helper.create_temp_folder)
830
+ repo.init
831
+
832
+ file1 = @file_helper.create_temp_file(repo.location, 'file_1')
833
+ repo.add_all
834
+ repo.commit 'first commit'
835
+
836
+ file2 = @file_helper.create_temp_file(repo.location, 'file_2')
837
+ repo.add_all
838
+ repo.commit 'second commit'
839
+
840
+ file3 = @file_helper.create_temp_file(repo.location, 'file_2')
841
+ repo.add_all
842
+
843
+ File.exist?(file1).should be_true
844
+ File.exist?(file2).should be_true
845
+ File.exist?(file3).should be_true
846
+
847
+ repo.status.should have(1).items
848
+ repo.status.first.should be_git_new_file(File.basename(file3))
849
+
850
+ repo.log.should have(2).items
851
+ repo.log.first.subject.should eq 'second commit'
852
+
853
+ repo.reset(:mode => :hard, :commit => repo.log.last.commit_hash).should be_true
854
+
855
+ File.exist?(file1).should be_true
856
+ File.exist?(file2).should be_false
857
+ File.exist?(file3).should be_false
858
+
859
+ repo.status.should be_empty
860
+
861
+ repo.log.should have(1).items
862
+ repo.log.first.subject.should eq 'first commit'
863
+ end
864
+
865
+ it 'Config user and email' do
866
+ repo = Repository.new(@file_helper.create_temp_folder)
867
+ repo.init
868
+
869
+ repo.config('user.name', 'user_test').should be_true
870
+ repo.config('user.email', 'user_test@mail.com').should be_true
871
+
872
+ repo.config('user.name').should eq 'user_test'
873
+ repo.config('user.email').should eq 'user_test@mail.com'
874
+ end
875
+
876
+ it 'Log specific user on commit' do
877
+ repo = Repository.new(@file_helper.create_temp_folder)
878
+ repo.init
879
+
880
+ repo.config('user.name', 'user_test').should be_true
881
+
882
+ @file_helper.create_temp_file(repo.location, 'file')
883
+ repo.add_all
884
+ repo.commit 'test'
885
+
886
+ repo.log.first.commiter_name.should eq 'user_test'
887
+ end
888
+
889
889
  end