| 955 | } |
| 956 | |
| 957 | int init (int argc, const char** argv) |
| 958 | { |
| 959 | const char* key_name = 0; |
| 960 | Options_list options; |
| 961 | options.push_back(Option_def("-k", &key_name)); |
| 962 | options.push_back(Option_def("--key-name", &key_name)); |
| 963 | |
| 964 | int argi = parse_options(options, argc, argv); |
| 965 | |
| 966 | if (!key_name && argc - argi == 1) { |
| 967 | std::clog << "Warning: 'git-crypt init' with a key file is deprecated as of git-crypt 0.4" << std::endl; |
| 968 | std::clog << "and will be removed in a future release. Please get in the habit of using" << std::endl; |
| 969 | std::clog << "'git-crypt unlock KEYFILE' instead." << std::endl; |
| 970 | return unlock(argc, argv); |
| 971 | } |
| 972 | if (argc - argi != 0) { |
| 973 | std::clog << "Error: git-crypt init takes no arguments" << std::endl; |
| 974 | help_init(std::clog); |
| 975 | return 2; |
| 976 | } |
| 977 | |
| 978 | if (key_name) { |
| 979 | validate_key_name_or_throw(key_name); |
| 980 | } |
| 981 | |
| 982 | std::string internal_key_path(get_internal_key_path(key_name)); |
| 983 | if (access(internal_key_path.c_str(), F_OK) == 0) { |
| 984 | // TODO: add a -f option to reinitialize the repo anyways (this should probably imply a refresh) |
| 985 | // TODO: include key_name in error message |
| 986 | std::clog << "Error: this repository has already been initialized with git-crypt." << std::endl; |
| 987 | return 1; |
| 988 | } |
| 989 | |
| 990 | // 1. Generate a key and install it |
| 991 | std::clog << "Generating key..." << std::endl; |
| 992 | Key_file key_file; |
| 993 | key_file.set_key_name(key_name); |
| 994 | key_file.generate(); |
| 995 | |
| 996 | mkdir_parent(internal_key_path); |
| 997 | if (!key_file.store_to_file(internal_key_path.c_str())) { |
| 998 | std::clog << "Error: " << internal_key_path << ": unable to write key file" << std::endl; |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
| 1002 | // 2. Configure git for git-crypt |
| 1003 | configure_git_filters(key_name); |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | void help_unlock (std::ostream& out) |
| 1009 | { |
no test coverage detected