ghq-cache 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bb9cbbbbff4e81ad27ebfe8843acde0445cb6cb
4
- data.tar.gz: eff270b98efb58c0734e4378cc0a011e4d8572e9
3
+ metadata.gz: 029edda1d5f7d745b5e4716a54cac2da429fb476
4
+ data.tar.gz: 2d5a4ba671db6667b1704c7b72a4ce1306b7f67c
5
5
  SHA512:
6
- metadata.gz: 83e3916598bc4235e6bfff056cb5d8a11d5179484cbc1cc9e7ebf8bde39d1124e349a1d99950f5ecac7a402798ea9532b639aed0f326e8b7208b95664be09db2
7
- data.tar.gz: 3ba1c5d1baee346614c3e3a3944437350b58bba04cbe0787c31fcde27d273b374c2e02e9c0b3f90046f19e458bb0f1085a788ef481196cd86cc4a87da8149519
6
+ metadata.gz: 1cb1143dabf1600831a6ca22f07575b88044e7cccd76074f09c9da9bec9ba37095350cf856ac77bd2f1f151202c77c08f43a4cf759206c26907980fbf99d0108
7
+ data.tar.gz: 756f5a3794784ce4c690b051214c70729b429d4f11b881e686a6fe4d47a1f58553ac5465ff887601d7acd4ea7516a864f22c30dbde6cf6353c2aee5b4d393248
data/README.md CHANGED
@@ -32,7 +32,7 @@ function ghq() {
32
32
  $GHQ $@
33
33
 
34
34
  # hook after ghq get
35
- ghq-cache refresh &
35
+ (ghq-cache refresh &)
36
36
  ;;
37
37
  list )
38
38
  if [ ! -e ~/.ghq-cache ]; then
@@ -54,7 +54,7 @@ function peco-src() {
54
54
  full_dir="${GOPATH}/src/${selected_dir}"
55
55
 
56
56
  # Log repository access to ghq-cache
57
- ghq-cache log $full_dir &
57
+ (ghq-cache log $full_dir &)
58
58
 
59
59
  BUFFER="cd ${full_dir}"
60
60
  zle accept-line
@@ -7,43 +7,46 @@ module Ghq
7
7
  class << self
8
8
  def build
9
9
  root = Logger.resume
10
+ obsolete_paths.each do |path|
11
+ root.remove_path(path)
12
+ end
10
13
  ghq_list.each do |path|
11
14
  root.register_path(File.join(root.name, path))
12
15
  end
13
16
 
14
- cache = build_paths(root).join("\n").concat("\n")
17
+ dirs = flatten(root).sort_by do |dir|
18
+ sort_key(dir)
19
+ end.reverse
20
+ cache = dirs.map(&:full_path).join("\n").concat("\n")
15
21
  File.write(CACHE_PATH, cache)
16
22
  end
17
23
 
18
24
  private
19
25
 
20
- def ghq_list
21
- `#{GHQ_PATH} list`.split("\n")
26
+ def flatten(*dirs)
27
+ dirs.map do |dir|
28
+ next dir if dir.children.empty?
29
+ flatten(*dir.children)
30
+ end.flatten
22
31
  end
23
32
 
24
- def build_paths(directory)
25
- paths = []
26
- sort_children(directory.children).each do |child|
27
- if child.children.empty?
28
- paths << File.join(directory.name, child.name)
29
- next
30
- end
33
+ def sort_key(dir)
34
+ return [dir.count] unless dir.parent
35
+ [dir.count, *sort_key(dir.parent)]
36
+ end
31
37
 
32
- build_paths(child).each do |path|
33
- if directory.root?
34
- paths << path
35
- next
36
- end
37
- paths << File.join(directory.name, path)
38
+ def obsolete_paths
39
+ cached_paths =
40
+ if File.exist?(CACHE_PATH)
41
+ File.read(CACHE_PATH).split("\n")
42
+ else
43
+ []
38
44
  end
39
- end
40
- paths
45
+ cached_paths - ghq_list
41
46
  end
42
47
 
43
- def sort_children(children)
44
- children.sort_by do |child|
45
- [-1 * child.count, child.name]
46
- end
48
+ def ghq_list
49
+ `#{GHQ_PATH} list`.split("\n")
47
50
  end
48
51
  end
49
52
  end
data/lib/ghq/cache/cli.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'thor'
2
+ require 'fileutils'
2
3
 
3
4
  module Ghq
4
5
  module Cache
@@ -13,6 +14,12 @@ module Ghq
13
14
  Logger.log(path)
14
15
  refresh
15
16
  end
17
+
18
+ desc 'purge', 'Delete ~/.ghq-cache and ~/.ghq-cache.log'
19
+ def purge
20
+ FileUtils.rm_f(Logger::LOG_PATH)
21
+ FileUtils.rm_f(Builder::CACHE_PATH)
22
+ end
16
23
  end
17
24
  end
18
25
  end
@@ -1,6 +1,6 @@
1
1
  module Ghq
2
2
  module Cache
3
- class Directory < Struct.new(:name, :count, :children)
3
+ class Directory < Struct.new(:name, :count, :children, :parent)
4
4
  def initialize(*)
5
5
  super
6
6
  self.count ||= 0
@@ -28,10 +28,23 @@ module Ghq
28
28
  child.register_path(relative_path)
29
29
  end
30
30
 
31
+ def remove_path(path)
32
+ relative_path = path.gsub(%r[^#{name}/?], '')
33
+ return if relative_path.empty?
34
+
35
+ child = create_or_find(relative_path.split('/').first)
36
+ children.delete(child)
37
+ end
38
+
31
39
  def root?
32
40
  self.name.include?('/')
33
41
  end
34
42
 
43
+ def full_path
44
+ return name unless parent
45
+ File.join(parent.full_path, name)
46
+ end
47
+
35
48
  private
36
49
 
37
50
  def create_or_find(child_name)
@@ -42,6 +55,7 @@ module Ghq
42
55
 
43
56
  child = Directory.new(child_name)
44
57
  self.children << child
58
+ child.parent = self
45
59
  child
46
60
  end
47
61
  end
@@ -1,5 +1,5 @@
1
1
  module Ghq
2
2
  module Cache
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghq-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-09 00:00:00.000000000 Z
11
+ date: 2015-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor