Skip to content

Commit

Permalink
Change clean command to only operate on non-encrypted files
Browse files Browse the repository at this point in the history
As recommended by gitattributes(5):

> For best results, clean should not alter its output further if it is
> run twice ("clean->clean" should be equivalent to "clean"), and
> multiple smudge commands should not alter clean's output
> ("smudge->smudge->clean" should be equivalent to "clean").

I've extracted this change from AGWA#107.

Co-Authored-By: Shlomo Shachar <[email protected]>
  • Loading branch information
jirutka and shlomosh committed Jul 28, 2019
1 parent af84638 commit c4df48c
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,19 +774,29 @@ int clean (int argc, const char** argv)
unsigned char digest[Hmac_sha1_state::LEN];
hmac.get(digest);

// Write a header that...
std::cout.write("\0GITCRYPT\0", 10); // ...identifies this as an encrypted file
std::cout.write(reinterpret_cast<char*>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce

// Now encrypt the file and write to stdout
Aes_ctr_encryptor aes(key->aes_key, digest);

// First read from the in-memory copy
const unsigned char* file_data = reinterpret_cast<const unsigned char*>(file_contents.data());
size_t file_data_len = file_contents.size();

// Check if file is decrypted (or already encrypted)
const bool is_decrypted = (file_data_len < 10) || std::memcmp(file_data, "\0GITCRYPT\0", 10) != 0;

if (is_decrypted) {
// Write a header that...
std::cout.write("\0GITCRYPT\0", 10); // ...identifies this as an encrypted file
std::cout.write(reinterpret_cast<char*>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
}

while (file_data_len > 0) {
const size_t buffer_len = std::min(sizeof(buffer), file_data_len);
aes.process(file_data, reinterpret_cast<unsigned char*>(buffer), buffer_len);
if (is_decrypted) {
aes.process(file_data, reinterpret_cast<unsigned char*>(buffer), buffer_len);
} else {
std::memcpy(buffer, file_data, buffer_len);
}
std::cout.write(buffer, buffer_len);
file_data += buffer_len;
file_data_len -= buffer_len;
Expand All @@ -800,9 +810,11 @@ int clean (int argc, const char** argv)

const size_t buffer_len = temp_file.gcount();

aes.process(reinterpret_cast<unsigned char*>(buffer),
reinterpret_cast<unsigned char*>(buffer),
buffer_len);
if (is_decrypted) {
aes.process(reinterpret_cast<unsigned char*>(buffer),
reinterpret_cast<unsigned char*>(buffer),
buffer_len);
}
std::cout.write(buffer, buffer_len);
}
}
Expand Down

0 comments on commit c4df48c

Please sign in to comment.