Class: History

Inherits:
Object
  • Object
show all
Defined in:
lib/abelard/history.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(archive, dir) ⇒ History

archive is a Directory, dir is a path to store in git



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
# File 'lib/abelard/history.rb', line 8

def initialize(archive, dir)
  @archive = archive
  if File.directory? dir
    begin
      repo = Rugged::Repository.discover(dir)

      repo_base = Pathname.new(repo.workdir).realpath.to_s
      real_dir = Pathname.new(dir).realpath.to_s
      raise "confused! #{repo_base} #{real_dir}" unless real_dir.start_with?(repo_base)
      @relative_root = real_dir[repo_base.length+1..-1] || ""
      $stderr.puts "#{real_dir} in #{repo_base} : #{@relative_root}"

      check_repo_clean(repo, @relative_root)
    rescue Rugged::RepositoryError
      repo = Rugged::Repository.init_at(dir)
      @relative_root = ""
    end
  elsif File.exist? dir
    fail "#{dir} exists as file"
  else
    Dir.mkdir(dir)
    repo = Rugged::Repository.init_at(dir)
    @relative_root = ""
  end
  @repo = repo
  @dir_path = dir
end

Instance Method Details

#check_repo_clean(repo, sub) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/abelard/history.rb', line 59

def check_repo_clean(repo, sub)
  $stderr.puts "check_repo_clean(#{repo},#{sub})"
  clean = true
  repo.status do |file, data|
    change = classify_file(sub, file)
    clean = false if change == :real
  end
  clean
end

#classify_file(subdir, file) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/abelard/history.rb', line 129

def classify_file(subdir, file)
  # normally 1 archive = 1 repo, but if you have a repo of several
  # archives, ignore file changes outside
  return :outside unless file.start_with?(subdir)

  filename = Pathname.new(file).basename.to_s

  return :real if filename.start_with?("post-") or filename.start_with?("comment-")
  return :top if filename.start_with?("feed") or filename.start_with?("channel")

  return :unknown
end

#commit_postsObject



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
# File 'lib/abelard/history.rb', line 69

def commit_posts
  repo = @repo
  sub = @relative_root

  commits = 0
  todo = { real: [] }
  @repo.status do |file, data|
    change = classify_file(sub, file)
    todo[change] ||= []
    todo[change] << file
  end
  if todo[:top]
    todo[:top].each do |file|
      repo.index.add file
      repo_entry = entry(file)
      item = Item.new(LibXML::XML::Parser.file(repo_entry.path).parse, repo_entry.path)

      author = {:email => "#{item.author}@example.org",
                :time => item.timestamp,
                :name => item.author}
      parents = []
      parents << repo.head.target unless repo.head_unborn?
      commit = Rugged::Commit.create(repo,
                                     :author => author,
                                     :message => "feed info",
                                     :committer => author,
                                     :parents => parents,
                                     :tree => repo.index.write_tree(repo),
                                     :update_ref => "HEAD")
      commits = commits+1
    end
  end

  to_commit = @archive.sort_entries(todo[:real].map { |f| entry(f) })
  
  to_commit.each do |entry|
    file = entry.git_fn
    repo.index.add file
    repo_entry = entry(file)
    item = Item.new(LibXML::XML::Parser.file(repo_entry.path).parse, repo_entry.path)

    author = {:email => "#{item.author}@example.org",
              :time => item.timestamp,
              :name => item.author}

    $stderr.puts "Adding #{file}"

    commit = Rugged::Commit.create(repo,
                                   :author => author,
                                   :message => "post",
                                   :committer => author,
                                   :parents => [repo.head.target],
                                   :tree => repo.index.write_tree(repo),
                                   :update_ref => "HEAD")
    commits = commits+1
  end

  repo.index.write if commits > 0
end

#entry(from_git) ⇒ Object



55
56
57
# File 'lib/abelard/history.rb', line 55

def entry(from_git)
  Entry.new(from_git, @relative_root, @repo)
end