Class: Init

Inherits:
Object
  • Object
show all
Includes:
GitHelpersMixin
Defined in:
lib/git_bpf/commands/init.rb

Overview

init:

Instance Method Summary collapse

Methods included from GitHelpersMixin

#branchExists?, #context, #promptYN, #refExists?, #terminate

Instance Method Details

#execute(opts, argv) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/git_bpf/commands/init.rb', line 32

def execute(opts, argv)
  if argv.length > 1
    run 'init', '--help'
    terminate
  end

  # TODO: There's likely a better way to do this.
  source_path = File.join File.dirname(__FILE__), '..'
  target = Repository.new(argv.length == 1 ? argv.pop : Dir.getwd)


  #
  # 1. Link source scripts directory.
  #
  ohai "1. Linking scripts directory to '#{source_path}'."

  scripts = File.join(target.path, '.git', opts.script_dir_name)

  if not File.exists? scripts
    File.symlink source_path, scripts
  elsif File.symlink? scripts
    opoo "Symbolic link already exists."
  else
    terminate "Cannot create symbolic link (#{scripts})."
  end


  #
  # 2. Create aliases for commands.
  #
  commands = [
    'recreate-branch',
    'share-rerere',
  ]

  ohai "2. Creating aliases for commands:", commands.shell_list

  commands.each do |name|
    command = "!_git-bpf #{name}"
    target.cmd("config", "--local", "alias.#{name}", command)
  end


  #
  # 3. Set up rerere sharing.
  #
  ohai "3. Setting up rerere sharing."

  target.config(true, "rerere.enabled", "true")
  target.config(true, "rerere.autoupdate", "true")

  rerere_path = File.join(target.git_dir, 'rr-cache')
  target_remote_url = target.remoteUrl(opts.remote_name)

  if not File.directory? rerere_path
    rerere = Repository::clone target_remote_url, rerere_path
  elsif not File.directory? File.join(rerere_path, '.git')
    opoo "Rerere cache directory already exists; Initializing repository in existing rr-cache directory."
    rerere = Repository.init rerere_path
    rerere.cmd("remote", "add", opts.remote_name, target_remote_url)
  else
    opoo "Rerere cache directory already exists and is a repository."
    rerere = Repository.new rerere_path
  end

  rerere.fetch opts.remote_name

  if rerere.branch?('rr-cache', opts.remote_name)
    # Remote has branch 'rr-cache', make sure we are currently on it.
    if not rerere.head.include? "rr-cache"
      rerere.cmd("checkout", "rr-cache")
    end
  else
    # Create orphan branch 'rr-cache' and push to remote.
    rerere.cmd("checkout", "--orphan", "rr-cache")
    rerere.cmd("rm", "-rf", "--ignore-unmatch", "#{rerere_path}/")
    rerere.cmd("commit", "-a", "--allow-empty", "-m", "Automatically creating branch to track conflict resolutions.")
    rerere.cmd("push", opts.remote_name, "rr-cache")
  end


  #
  # 4. Symlink git-hooks.
  #
  hooks_dir = File.join(target.git_dir, "hooks")
  hooks = [
    'post-merge',
    'post-checkout'
  ]

  ohai "4. Creating symbolic links to git-hooks:", hooks.shell_list

  hooks.each do |name|
    target_hook_path = File.join(hooks_dir, name)
    source_hook_path = File.join(scripts, "hooks", "#{name}.rb")
    files = Dir.glob("#{target_hook_path}*")
    write = files.empty?

    if not write and promptYN "Existing hook '#{name}' detected, overwrite?"
      write = File.delete(files.shell_s) > 0
    end

    if write
      File.symlink source_hook_path, target_hook_path
    else
      opoo "Couldn't link '#{name}' hook as it already exists."
    end
  end

  #
  # Success!
  #
  ohai "Success!"
end

#options(opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/git_bpf/commands/init.rb', line 14

def options(opts)
  opts.script_dir_name = 'git-bpf'
  opts.remote_name = 'origin'
  opts.rerere_branch = 'rr-cache'

  [
    ['-d', '--directory-name NAME',
      "",
      lambda { |n| opts.script_dir_name = n }],
    ['-r', '--remote-name NAME',
      "",
      lambda { |n| opts.remote_name = n }],
    ['-b', '--rerere-branch NAME',
      "",
      lambda { |n| opts.rerere_branch = n }],
  ]
end