webmat / git_remote_branch

A tool to simplify working with remote branches

This URL has Read+Write access

git_remote_branch / lib / param_reader.rb
100644 64 lines (50 sloc) 1.357 kb
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
module GitRemoteBranch
  
  private
  HELP_PARAMS = {:action => :help}
 
  public
  def read_params(argv)
    #TODO Some validation on the params
    
    p={}
    p[:silent] = silent!(argv)
    p[:explain] = explain_mode!(argv)
    
    p[:action] = get_action(argv[0]) or return HELP_PARAMS
 
    return HELP_PARAMS if p[:action] == :help
 
    p[:branch] = get_branch(argv[1])
    p[:origin] = get_origin(argv[2])
    
    # If in explain mode, the user doesn't have to specify a branch or be on in
    # actual repo to get the explanation.
    # Of course if he is, the explanation will be made better by using contextual info.
    if p[:explain]
      p[:branch] ||= "branch_to_#{p[:action]}"
      p[:current_branch] = begin
        get_current_branch
      rescue NotOnGitRepositoryError, InvalidBranchError
        'current_branch'
      end
 
    else
      return HELP_PARAMS unless p[:branch]
      p[:current_branch] = get_current_branch
    end
    return p
  end
 
  def explain_mode!(argv)
    if argv[0].to_s.downcase == 'explain'
      argv.shift
      true
    else
      false
    end
  end
  
  def silent!(argv)
    !!argv.delete('--silent')
  end
 
  def get_action(action)
    ALIAS_REVERSE_MAP[action.to_s.downcase]
  end
 
  def get_branch(branch)
    branch
  end
  
  def get_origin(origin)
    return origin || 'origin'
  end
end