1 // Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
2 //               2009 Derek Scherger <derek@echologic.com>
3 //
4 // This program is made available under the GNU GPL version 2.0 or
5 // greater. See the accompanying file COPYING for details.
6 //
7 // This program is distributed WITHOUT ANY WARRANTY; without even the
8 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 // PURPOSE.
10 
11 #include "base.hh"
12 #include "cmd.hh"
13 #include "app_state.hh"
14 #include "database.hh"
15 #include "git_change.hh"
16 #include "git_export.hh"
17 #include "project.hh"
18 #include "rcs_import.hh"
19 #include "revision.hh"
20 #include "keys.hh"
21 #include "key_store.hh"
22 
23 using std::map;
24 using std::set;
25 using std::string;
26 using std::vector;
27 
28 CMD(rcs_import, "rcs_import", "", CMD_REF(debug), N_("RCSFILE..."),
29     N_("Parses versions in RCS files"),
30     N_("This command doesn't reconstruct or import revisions.  "
31        "You probably want to use cvs_import."),
32     options::opts::branch)
33 {
34   if (args.size() < 1)
35     throw usage(execid);
36 
37   for (args_vector::const_iterator i = args.begin();
38        i != args.end(); ++i)
39     test_parse_rcs_file(system_path((*i)(), origin::user));
40 }
41 
42 
43 CMD(cvs_import, "cvs_import", "", CMD_REF(vcs), N_("CVSROOT"),
44     N_("Imports all versions in a CVS repository"),
45     "",
46     options::opts::branch)
47 {
48   database db(app);
49   key_store keys(app);
50   project_t project(db);
51 
52   if (args.size() != 1)
53     throw usage(execid);
54 
55   E(!app.opts.branch().empty(), origin::user,
56     F("need base '--branch' argument for importing"));
57 
58   system_path cvsroot(idx(args, 0)(), origin::user);
59   require_path_is_directory(cvsroot,
60                             F("path '%s' does not exist") % cvsroot,
61                             F("'%s' is not a directory") % cvsroot);
62 
63   // make sure we can sign certs using the selected key; also requests
64   // the password (if necessary) up front rather than after some arbitrary
65   // amount of work
66   cache_user_key(app.opts, project, keys, app.lua);
67 
68   import_cvs_repo(project, keys, cvsroot, app.opts.branch);
69 }
70 
71 
72 CMD(git_export, "git_export", "", CMD_REF(vcs), (""),
73     N_("Produces a git fast-export data stream on stdout"),
74     (""),
75     options::opts::authors_file | options::opts::branches_file |
76     options::opts::log_revids | options::opts::log_certs |
77     options::opts::use_one_changelog |
78     options::opts::import_marks | options::opts::export_marks |
79     options::opts::refs)
80 {
81   database db(app);
82 
83   if (args.size() != 0)
84     throw usage(execid);
85 
86   map<string, string> author_map;
87   map<string, string> branch_map;
88 
89   if (!app.opts.authors_file.empty())
90     {
91       P(F("reading author mappings from '%s'") % app.opts.authors_file);
92       read_mappings(app.opts.authors_file, author_map);
93       validate_author_mappings(app.lua, author_map);
94     }
95 
96   if (!app.opts.branches_file.empty())
97     {
98       P(F("reading branch mappings from '%s'") % app.opts.branches_file);
99       read_mappings(app.opts.branches_file, branch_map);
100     }
101 
102   map<revision_id, size_t> marked_revs;
103 
104   if (!app.opts.import_marks.empty())
105     {
106       P(F("importing revision marks from '%s'") % app.opts.import_marks);
107       import_marks(app.opts.import_marks, marked_revs);
108     }
109 
110   set<revision_id> revision_set;
111   db.get_revision_ids(revision_set);
112 
113   // remove marked revs from the set to be exported
114   for (map<revision_id, size_t>::const_iterator
115          i = marked_revs.begin(); i != marked_revs.end(); ++i)
116     revision_set.erase(i->first);
117 
118   vector<revision_id> revisions;
119   toposort(db, revision_set, revisions);
120 
121   map<revision_id, git_change> change_map;
122 
123   load_changes(db, revisions, change_map);
124 
125   // needs author and branch maps
126   export_changes(db, app.lua,
127                  revisions, marked_revs,
128                  author_map, branch_map, change_map,
129                  app.opts.log_revids, app.opts.log_certs,
130                  app.opts.use_one_changelog);
131 
132   if (app.opts.refs.find("revs") != app.opts.refs.end())
133     export_rev_refs(revisions, marked_revs);
134 
135   if (app.opts.refs.find("roots") != app.opts.refs.end())
136     export_root_refs(db, marked_revs);
137 
138   if (app.opts.refs.find("leaves") != app.opts.refs.end())
139     export_leaf_refs(db, marked_revs);
140 
141   if (!app.opts.export_marks.empty())
142     {
143       P(F("exporting revision marks to '%s'") % app.opts.export_marks);
144       export_marks(app.opts.export_marks, marked_revs);
145     }
146 }
147 
148 // Local Variables:
149 // mode: C++
150 // fill-column: 76
151 // c-file-style: "gnu"
152 // indent-tabs-mode: nil
153 // End:
154 // vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
155