ghq-cache 0.1.1 → 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: 55bccb63fb14063d8f1a623c5a11d0eb4dcccb61
4
- data.tar.gz: 3f096369551f6fefd2b1fd667465ad7065d9c3fc
3
+ metadata.gz: 029edda1d5f7d745b5e4716a54cac2da429fb476
4
+ data.tar.gz: 2d5a4ba671db6667b1704c7b72a4ce1306b7f67c
5
5
  SHA512:
6
- metadata.gz: 1bd42136200a47e21a3cfa0e33bbfc1c0a27bba1684de63e05b149af01ff3381773bcad07eeb65d3a20b681f75babd472ab400b693ec328c4c0812a1228e3d0f
7
- data.tar.gz: a52b4ba9fd9703f12a5a632ce4241171ad654a9000f32a7db56a7f9617a5e8e2b10b68215ca9fb202146bb84e13eedb7e4cf0397499deb5873bf1ca90639bec4
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
@@ -1,4 +1,5 @@
1
1
  require 'thor'
2
+ require 'fileutils'
2
3
 
3
4
  module Ghq
4
5
  module Cache
@@ -11,6 +12,13 @@ module Ghq
11
12
  desc 'log PATH', 'Log your repository access'
12
13
  def log(path)
13
14
  Logger.log(path)
15
+ refresh
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)
14
22
  end
15
23
  end
16
24
  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.1.1"
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.1.1
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