| 1116 | out << std::endl; |
| 1117 | } |
| 1118 | int lock (int argc, const char** argv) |
| 1119 | { |
| 1120 | const char* key_name = 0; |
| 1121 | bool all_keys = false; |
| 1122 | bool force = false; |
| 1123 | Options_list options; |
| 1124 | options.push_back(Option_def("-k", &key_name)); |
| 1125 | options.push_back(Option_def("--key-name", &key_name)); |
| 1126 | options.push_back(Option_def("-a", &all_keys)); |
| 1127 | options.push_back(Option_def("--all", &all_keys)); |
| 1128 | options.push_back(Option_def("-f", &force)); |
| 1129 | options.push_back(Option_def("--force", &force)); |
| 1130 | |
| 1131 | int argi = parse_options(options, argc, argv); |
| 1132 | |
| 1133 | if (argc - argi != 0) { |
| 1134 | std::clog << "Error: git-crypt lock takes no arguments" << std::endl; |
| 1135 | help_lock(std::clog); |
| 1136 | return 2; |
| 1137 | } |
| 1138 | |
| 1139 | if (all_keys && key_name) { |
| 1140 | std::clog << "Error: -k and --all options are mutually exclusive" << std::endl; |
| 1141 | return 2; |
| 1142 | } |
| 1143 | |
| 1144 | // 1. Make sure working directory is clean (ignoring untracked files) |
| 1145 | // We do this because we check out files later, and we don't want the |
| 1146 | // user to lose any changes. (TODO: only care if encrypted files are |
| 1147 | // modified, since we only check out encrypted files) |
| 1148 | |
| 1149 | // Running 'git status' also serves as a check that the Git repo is accessible. |
| 1150 | |
| 1151 | std::stringstream status_output; |
| 1152 | get_git_status(status_output); |
| 1153 | if (!force && status_output.peek() != -1) { |
| 1154 | std::clog << "Error: Working directory not clean." << std::endl; |
| 1155 | std::clog << "Please commit your changes or 'git stash' them before running 'git-crypt lock'." << std::endl; |
| 1156 | std::clog << "Or, use 'git-crypt lock --force' and possibly lose uncommitted changes." << std::endl; |
| 1157 | return 1; |
| 1158 | } |
| 1159 | |
| 1160 | // 2. deconfigure the git filters and remove decrypted keys |
| 1161 | std::vector<std::string> encrypted_files; |
| 1162 | if (all_keys) { |
| 1163 | // deconfigure for all keys |
| 1164 | std::vector<std::string> dirents = get_directory_contents(get_internal_keys_path().c_str()); |
| 1165 | |
| 1166 | for (std::vector<std::string>::const_iterator dirent(dirents.begin()); dirent != dirents.end(); ++dirent) { |
| 1167 | const char* this_key_name = (*dirent == "default" ? 0 : dirent->c_str()); |
| 1168 | remove_file(get_internal_key_path(this_key_name)); |
| 1169 | deconfigure_git_filters(this_key_name); |
| 1170 | get_encrypted_files(encrypted_files, this_key_name); |
| 1171 | } |
| 1172 | } else { |
| 1173 | // just handle the given key |
| 1174 | std::string internal_key_path(get_internal_key_path(key_name)); |
| 1175 | if (access(internal_key_path.c_str(), F_OK) == -1 && errno == ENOENT) { |
no test coverage detected