Changeset 1239 for trunk/lib/File.cc


Ignore:
Timestamp:
Oct 24, 2010, 1:57:56 AM (12 years ago)
Author:
Peter Johansson
Message:

move 'update copyright' code from File class to CopyrightVisitor? class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/File.cc

    r1234 r1239  
    6565  {
    6666    return "blame_output/" + local_path() + ".html";
    67   }
    68 
    69 
    70   std::map<int, std::set<Alias> >
    71   File::copyright_map(std::map<std::string, Alias>& alias,
    72                       const std::map<int, svn_revnum_t>& year2rev) const
    73   {
    74     using namespace std;
    75     map<int, set<Alias> > year_authors;
    76     const Stats& stats = stats_["add"];
    77 
    78     // loop over all years
    79     for (std::map<int, svn_revnum_t>::const_iterator rev_iter=year2rev.begin();
    80          rev_iter!=year2rev.end(); ++rev_iter) {
    81 
    82       svn_revnum_t last_rev_this_year = rev_iter->second;
    83       svn_revnum_t last_rev_last_year = 0;
    84       if (rev_iter != year2rev.begin()) {
    85         last_rev_last_year = (--rev_iter)->second;
    86         ++rev_iter;
    87       }
    88       // do not go beyond BASE rev of file
    89       last_rev_this_year = std::min(last_rev_this_year, last_changed_rev());
    90       last_rev_last_year = std::min(last_rev_last_year, last_changed_rev());
    91       // loop over authors
    92       for (std::set<std::string>::const_iterator a_iter=stats.authors().begin();
    93            a_iter!=stats.authors().end(); ++a_iter) {
    94 
    95         // check if anything has been added since last year
    96         if ( (stats(LineTypeParser::code, *a_iter, last_rev_this_year) >
    97               stats(LineTypeParser::code, *a_iter, last_rev_last_year)) ||
    98              (stats(LineTypeParser::comment, *a_iter, last_rev_this_year) >
    99               stats(LineTypeParser::comment, *a_iter, last_rev_last_year)) ) {
    100        
    101        
    102           // find username in map of aliases
    103           std::map<string,Alias>::iterator name(alias.lower_bound(*a_iter));
    104          
    105           // if alias exist insert alias
    106           if (name != alias.end() && name->first==*a_iter)
    107             year_authors[rev_iter->first].insert(name->second);
    108           else {
    109             // else insert user name
    110             Alias a(*a_iter,alias.size());
    111             year_authors[rev_iter->first].insert(a);
    112             std::cerr << "svncopyright: warning: no copyright alias found for `"
    113                       << *a_iter << "'\n";
    114             // insert alias to avoid multiple warnings.
    115             alias.insert(name, std::make_pair(*a_iter, a));
    116           }
    117         }
    118       }
    119     }
    120     return year_authors;
    121   }
    122 
    123 
    124   std::string
    125   File::copyright_block(const std::map<int, std::set<Alias> >& year_authors,
    126                         const std::string& prefix) const
    127   {
    128     using namespace std;
    129     stringstream ss;
    130     for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
    131          i!=year_authors.end();) {
    132       ss << prefix << Configuration::instance().copyright_string() << " "
    133          << 1900+i->first;
    134       map<int, set<Alias> >::const_iterator j = i;
    135       assert(i!=year_authors.end());
    136       while (++j!=year_authors.end() &&
    137              i->second == j->second){
    138         ss << ", " << 1900+(j->first);
    139       }
    140       // printing authors
    141       std::vector<Alias> vec_alias;
    142       back_insert_iterator<std::vector<Alias> > ii(vec_alias);
    143       std::copy(i->second.begin(), i->second.end(), ii);
    144       // sort with respect to id
    145       std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
    146       for (std::vector<Alias>::iterator a=vec_alias.begin();
    147            a!=vec_alias.end(); ++a){
    148         if (a!=vec_alias.begin())
    149           ss << ",";
    150         ss << " " << a->name();
    151       }
    152       ss << "\n";
    153       i = j;
    154     }
    155     return ss.str();
    156   }
    157 
    158   bool File::detect_copyright(std::string& block, size_t& start_at_line,
    159                               size_t& end_at_line, std::string& prefix) const
    160   {
    161     using namespace std;
    162     LineTypeParser parser(path());
    163     std::ifstream is(path().c_str());
    164     std::string line;
    165     while (std::getline(is, line))
    166       parser.parse(line);
    167     if (!parser.copyright_found())
    168       return false;
    169     block = parser.block();
    170     start_at_line = parser.start_line();
    171     end_at_line = parser.end_line();
    172     prefix = parser.prefix();
    173     return true;
    17467  }
    17568
     
    324217
    325218
    326   void File::print_copyright(std::map<std::string, Alias>& alias,
    327                              bool verbose,
    328                              const std::map<int,svn_revnum_t>& y2rev) const
    329   {
    330     if (ignore() || svncopyright_ignore())
    331       return;
    332 
    333     std::string old_block;
    334     size_t start_line=0;
    335     size_t end_line=0;
    336     std::string prefix;
    337     if (!detect_copyright(old_block, start_line, end_line, prefix)){
    338       if (Configuration::instance().missing_copyright_warning())
    339         std::cerr << "svncopyright: warning: no copyright statement found in `"
    340                   << path_ << "'\n";
    341       return;
    342     }
    343     std::map<int, std::set<Alias> > map=copyright_map(alias, y2rev);
    344     std::string new_block = copyright_block(map, prefix);
    345     if (old_block==new_block)
    346       return;
    347     if (verbose)
    348       std::cout << "Updating copyright in '" << path_ << "'" << std::endl;
    349     update_copyright(new_block, start_line, end_line);
    350   }
    351 
    352 
    353219  void File::print_core(const bool verbose) const
    354220  {
     
    411277  }
    412278
    413 
    414   void File::update_copyright(const std::string& new_block,
    415                               size_t start_at_line, size_t end_at_line) const
    416   {
    417     // embrace filename with brackets #filename#
    418     std::string tmpname = concatenate_path(directory_name(path()),
    419                                            "#" + file_name(path()) + "#");
    420     std::ofstream tmp(tmpname.c_str());
    421     assert(tmp);
    422     using namespace std;
    423     ifstream is(path().c_str());
    424     assert(is.good());
    425     string line;
    426     // Copy lines before block
    427     for (size_t i=1; i<start_at_line; ++i){
    428       assert(is.good());
    429       getline(is, line);
    430       tmp << line << "\n";
    431     }
    432     // Printing copyright statement
    433     tmp << new_block;
    434     // Ignore old block lines
    435     for (size_t i=start_at_line; i<end_at_line; ++i){
    436       assert(is.good());
    437       getline(is, line);
    438     }
    439     // Copy lines after block
    440     while(is.good()) {
    441       char ch=is.get();
    442       if (is.good())
    443         tmp.put(ch);
    444     }
    445 
    446     is.close();
    447     tmp.close();
    448    
    449     // finally rename file
    450     rename(tmpname, path());
    451   }
    452 
    453 
    454279}} // end of namespace svndigest and namespace theplu
Note: See TracChangeset for help on using the changeset viewer.