summaryrefslogtreecommitdiffstats
path: root/lib/git/branch.rb
blob: f9106abf670d3abd9e95a3eb7cfd897dcd8b42f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module Git
  class Branch < Path
    
    attr_accessor :full, :remote, :name
    
    @base = nil
    @gcommit = nil
    
    def initialize(base, name)
      @remote = nil
      @full = name
      @base = base
      
      parts = name.split('/')
      if parts[1]
        @remote = Git::Remote.new(@base, parts[0])
        @name = parts[1]
      else
        @name = parts[0]
      end
    end
    
    def gcommit
      @gcommit = @base.object(@full) if !@gcommit
      @gcommit
    end
    
    def checkout
      check_if_create
      @base.checkout(@full)
    end
    
    def archive(file, opts = {})
      @base.lib.archive(@full, file, opts)
    end
    
    # g.branch('new_branch').in_branch do
    #   # create new file
    #   # do other stuff
    #   return true # auto commits and switches back
    # end
    def in_branch (message = 'in branch work')
      old_current = @base.lib.branch_current
      checkout
      if yield
        @base.commit_all(message)
      else
        @base.reset_hard
      end
      @base.checkout(old_current)
    end
    
    def create
      check_if_create
    end
    
    def delete
      @base.lib.branch_delete(@name)
    end
    
    def current
      determine_current
    end
    
    def merge(branch = nil, message = nil)
      if branch
        in_branch do 
          @base.merge(branch, message)
          false
        end
        # merge a branch into this one
      else
        # merge this branch into the current one
        @base.merge(@name)
      end
    end
    
    def to_a
      [@full]
    end
    
    def to_s
      @full
    end
    
    private 

      def check_if_create
        @base.lib.branch_new(@name) rescue nil
      end
      
      def determine_current
        @base.lib.branch_current == @name
      end
    
  end
end