Changeset 1152


Ignore:
Timestamp:
Aug 7, 2010, 4:31:18 PM (13 years ago)
Author:
Peter Johansson
Message:

allow config file to override svn property (fixes #326). Adding two new files (split.h and split.cc) from yat.

Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Configuration.cc

    r1144 r1152  
    2626#include "Colors.h"
    2727#include "Functor.h"
     28
     29#include "yat/split.h"
    2830
    2931#include <algorithm>
     
    311313        }
    312314      }
     315      else if (section == "svn_props") {
     316        svn_props_.push_back(std::make_pair(lhs, empty_str_map_));
     317        std::vector<std::string> vec;
     318        yat::utility::split(vec, rhs, ';');
     319        for (size_t i=0; i<vec.size(); ++i) {
     320          std::vector<std::string> vec2;
     321          yat::utility::split(vec2, vec[i], '=');
     322          std::string key = trim(vec2[0]);
     323          std::string value("");
     324          if (vec2.size() >= 2)
     325            value = trim(vec2[1]);
     326          svn_props_.back().second[key] = value;
     327        }
     328      }
    313329      else if (section == "image") {
    314330        if (lhs == "format") {
     
    370386      instance_ = new Configuration;
    371387      instance_->load();
     388      std::cerr << "construct config\n";
    372389    }
    373390    return *instance_;
     
    507524    output_blame_information_ = true;
    508525    output_file_ = true;
     526
    509527  }
    510528
     
    522540
    523541
    524   const std::map<std::string, std::string>*
    525   Configuration::svn_properties(const std::string&) const
    526   {
    527     return NULL;
     542  const std::map<std::string, std::string>&
     543  Configuration::svn_properties(const std::string& filename) const
     544  {
     545    // reverse backwards as we prefer to to pick properties defined later
     546    std::vector<props>::const_reverse_iterator iter =
     547      find_fn(svn_props_.rbegin(), svn_props_.rend(), filename);
     548    if (iter==svn_props_.rend())
     549      return empty_str_map_;
     550    return iter->second;
    528551  }
    529552
     
    612635       << "[author-color]\n"
    613636       << "# jdoe = 000000\n";
    614     typedef std::map<std::string,std::string> str_map;
     637    typedef Configuration::str_map str_map;
    615638    for (str_map::const_iterator i(conf.author_color_.begin());
    616639         i!=conf.author_color_.end(); ++i) {
     
    618641    }
    619642
     643    typedef Configuration::props props;
    620644    os << "\n"
    621        << "### Section for setting trac environment\n"
     645       << "### Section for overriding svn properties.\n"
     646       << "### The format is the same as for section auto-props in subversion\n"
     647       << "### config file\n"
     648       << "[svn-props]\n";
     649    os << "# COPYING = svndigest:ignore\n";
     650    std::vector<props>::const_iterator p=conf.svn_props_.begin();
     651    for ( ; p!=conf.svn_props_.end(); ++p) {
     652      os << p->first << " = ";
     653      const str_map& map = p->second;
     654      str_map::const_iterator end = map.end();
     655      for (str_map::const_iterator i=map.begin(); i!=end; ++i) {
     656        if (i != map.begin())
     657          os << ";";
     658        os << i->first << "=" << i->second;
     659      }
     660      os << "\n";
     661    }
     662
     663
     664    os << "\n"
     665       << "### Section for setting trac environment.\n"
    622666       << "[trac]\n"
    623667       << "# If trac-root is set, svndigest will create anchors to "
     
    626670    if (!conf.trac_root().empty())
    627671      os << "trac-root = " << conf.trac_root() << "\n";
     672
     673    os << "\n"
     674       << "### Section for setting dictionary for file names.\n"
     675       << "### Prior looking for file name pattern in section "
     676       << "[parsing-codons],\n"
     677       << "### the file name may be translated according to the rules \n"
     678       << "### in this section. In default setting there is, for example,\n"
     679       << "### a rule to translate `<FILENAME>.in' to `<FILENAME>'.\n"
     680       << "### The format of the entries is:\n"
     681       << "###    file-name-pattern = new-name\n"
     682       << "### Left hand side may contain wildcards (such as '*' and '?').\n"
     683       << "### Right hand side may contain \"$i\", which will be replaced \n"
     684       << "### with the ith wild card in lhs string.\n";
    628685
    629686    if (!conf.dictionary_.empty()) {
  • trunk/lib/Configuration.h

    r1144 r1152  
    121121       \return NULL if there is no prop for \a filename in config
    122122     */
    123     const std::map<std::string, std::string>*
     123    const std::map<std::string, std::string>&
    124124    svn_properties(const std::string& filename) const;
    125125
     
    152152    bool equal_false(std::string) const;
    153153    bool equal_true(std::string) const;
     154
     155    /**
     156       find first element in range, [first, last) for which
     157       element->first matches filename.
     158
     159       Iterator->first must return string, i.e., [first, last) is
     160       often a range of pair<string, T>
     161
     162       If no match is found, last is returned.
     163     */
     164    template<typename Iterator>
     165    Iterator find_fn(Iterator first, Iterator last,
     166                     const std::string& filename) const;
     167
    154168    ///
    155169    /// @brief load deafult configuration
     
    190204    std::string trac_root_;
    191205
    192     std::vector<std::map<std::string, std::string> > svn_props_;
     206    typedef std::map<std::string, std::string> str_map;
     207    typedef std::pair<std::string, str_map> props;
     208    std::vector<props> svn_props_;
     209    const str_map empty_str_map_;
    193210  };
    194211
     
    226243  };
    227244
     245  // template implementation
     246
     247  template<typename Iterator>
     248  Iterator Configuration::find_fn(Iterator first, Iterator last,
     249                                  const std::string& filename) const
     250  {
     251    for( ; first!=last; ++first) {
     252      if (fnmatch(first->first.c_str(), filename.c_str()))
     253        return first;
     254    }
     255    return last;
     256  }
     257
     258
    228259}} // end of namespace svndigest and namespace theplu
    229260
  • trunk/lib/SVNproperty.cc

    r1144 r1152  
    4040    const Configuration& config = Configuration::instance();
    4141    typedef std::map<std::string, std::string> str_map;
    42     const str_map* props = config.svn_properties(file_name(path));
    43     std::map<std::string, std::string>::const_iterator i;
    44     std::map<std::string, std::string>::const_iterator e;
    45     if (props) {
    46       i = props->begin();
    47       e = props->end();
    48       // we can not use map::insert because we want config to have precedence
    49       for ( ; i!=e; ++i)
    50         property_[i->first] = i->second;
    51     }
     42    const str_map& props = config.svn_properties(file_name(path));
     43    std::map<std::string, std::string>::const_iterator i = props.begin();
     44    std::map<std::string, std::string>::const_iterator e = props.end();
     45    // we can not use map::insert because we want config to have precedence
     46    for ( ; i!=e; ++i)
     47      property_[i->first] = i->second;
    5248   
    5349    i = property_.begin();
  • trunk/test/config_test.cc

    r1119 r1152  
    2929  void test_codon(test::Suite&);
    3030  void test_read_write(test::Suite&);
     31  void test_props(test::Suite&);
    3132}} // end of namespace svndigest and theplu
    3233
     
    3940  test_codon(suite);
    4041  test_read_write(suite);
     42  test_props(suite);
    4143                                                                               
    4244  if (suite.ok()) {
     
    5456  void test_codon(test::Suite& suite)
    5557  {
     58    suite.out() << "test codons" << std::endl;
    5659    const Configuration& c(Configuration::instance());
    5760    if (!c.codon("foo.h")){
     
    6972  }
    7073 
     74  void test_props(test::Suite& suite)
     75  {
     76    suite.out() << "test props" << std::endl;
     77    Configuration& conf(Configuration::instance());
     78    std::stringstream ss;
     79    ss << "[svn_props]\n"
     80       << "foo* = svndigest:ignore\n"
     81       << "bar* = svn:eol-style=native;svncopyright:ignore\n";
     82    conf.load(ss);
     83    const std::map<std::string, std::string>& props =
     84      conf.svn_properties("foo_bla_bla");
     85    if (props.find("svndigest:ignore")==props.end()) {
     86        suite.out() << "property svndigest:ignore not set for foo_bla_bla\n";
     87        suite.add(false);
     88    }
     89    const std::map<std::string, std::string>& props2 =
     90      conf.svn_properties("bar_bla_bla");
     91    std::map<std::string, std::string>::const_iterator it =
     92      props2.find("svn:eol-style");
     93    if (!suite.add(it != props2.end()))
     94      suite.out() <<"expected property 'svn:eol-style' set for bar_bla_bla\n";
     95    else if (!suite.add(it->second == "native"))
     96      suite.out() << "expected 'svn:eol-style' set to 'native' "
     97                  << "for bar_bla_bla\n";
     98    // checking that we return empty map for files not mentioned in config
     99    suite.out() << "props3" << std::endl;
     100    const std::map<std::string, std::string>& props3 =
     101      conf.svn_properties("nothing-nothing");
     102    suite.add(props3.empty());
     103  }
     104
    71105  void test_read_write(test::Suite& suite)
    72106  {
  • trunk/yat/Makefile.am

    r1090 r1152  
    3636noinst_HEADERS += OptionHelp.h
    3737noinst_HEADERS += OptionSwitch.h
     38noinst_HEADERS += split.h
    3839noinst_HEADERS += utility.h
    3940
     
    4647libyat_a_SOURCES += OptionHelp.cc
    4748libyat_a_SOURCES += OptionSwitch.cc
     49libyat_a_SOURCES += split.cc
    4850
    4951
Note: See TracChangeset for help on using the changeset viewer.