returns filter and diff attributes as a pair
| 381 | |
| 382 | // returns filter and diff attributes as a pair |
| 383 | static std::pair<std::string, std::string> get_file_attributes (const std::string& filename) |
| 384 | { |
| 385 | // git check-attr filter diff -- filename |
| 386 | std::vector<std::string> command; |
| 387 | command.push_back("git"); |
| 388 | command.push_back("check-attr"); |
| 389 | command.push_back("filter"); |
| 390 | command.push_back("diff"); |
| 391 | command.push_back("--"); |
| 392 | command.push_back(filename); |
| 393 | |
| 394 | std::stringstream output; |
| 395 | if (!successful_exit(exec_command(command, output))) { |
| 396 | throw Error("'git check-attr' failed - is this a Git repository?"); |
| 397 | } |
| 398 | |
| 399 | std::string filter_attr; |
| 400 | std::string diff_attr; |
| 401 | |
| 402 | std::string line; |
| 403 | // Example output: |
| 404 | // filename: filter: git-crypt |
| 405 | // filename: diff: git-crypt |
| 406 | while (std::getline(output, line)) { |
| 407 | // filename might contain ": ", so parse line backwards |
| 408 | // filename: attr_name: attr_value |
| 409 | // ^name_pos ^value_pos |
| 410 | const std::string::size_type value_pos(line.rfind(": ")); |
| 411 | if (value_pos == std::string::npos || value_pos == 0) { |
| 412 | continue; |
| 413 | } |
| 414 | const std::string::size_type name_pos(line.rfind(": ", value_pos - 1)); |
| 415 | if (name_pos == std::string::npos) { |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | const std::string attr_name(line.substr(name_pos + 2, value_pos - (name_pos + 2))); |
| 420 | const std::string attr_value(line.substr(value_pos + 2)); |
| 421 | |
| 422 | if (attr_value != "unspecified" && attr_value != "unset" && attr_value != "set") { |
| 423 | if (attr_name == "filter") { |
| 424 | filter_attr = attr_value; |
| 425 | } else if (attr_name == "diff") { |
| 426 | diff_attr = attr_value; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return std::make_pair(filter_attr, diff_attr); |
| 432 | } |
| 433 | |
| 434 | // returns filter and diff attributes as a pair |
| 435 | static std::pair<std::string, std::string> get_file_attributes (const std::string& filename, std::ostream& check_attr_stdin, std::istream& check_attr_stdout) |
no test coverage detected