Skip to content

Commit a41776e

Browse files
jirutkaShlomo Shachar
and
Shlomo Shachar
committed
Change clean command to only operate on non-encrypted files
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 <shlomo.shachar@binatix.com>
1 parent af84638 commit a41776e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

commands.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -774,19 +774,29 @@ int clean (int argc, const char** argv)
774774
unsigned char digest[Hmac_sha1_state::LEN];
775775
hmac.get(digest);
776776

777-
// Write a header that...
778-
std::cout.write("\0GITCRYPT\0", 10); // ...identifies this as an encrypted file
779-
std::cout.write(reinterpret_cast<char*>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
780-
781777
// Now encrypt the file and write to stdout
782778
Aes_ctr_encryptor aes(key->aes_key, digest);
783779

784780
// First read from the in-memory copy
785781
const unsigned char* file_data = reinterpret_cast<const unsigned char*>(file_contents.data());
786782
size_t file_data_len = file_contents.size();
783+
784+
// Check if file is decrypted (or already encrypted)
785+
const bool is_decrypted = (file_data_len < 10) || std::memcmp(file_data, "\0GITCRYPT\0", 10) != 0;
786+
787+
if (is_decrypted) {
788+
// Write a header that...
789+
std::cout.write("\0GITCRYPT\0", 10); // ...identifies this as an encrypted file
790+
std::cout.write(reinterpret_cast<char*>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
791+
}
792+
787793
while (file_data_len > 0) {
788794
const size_t buffer_len = std::min(sizeof(buffer), file_data_len);
789-
aes.process(file_data, reinterpret_cast<unsigned char*>(buffer), buffer_len);
795+
if (is_decrypted) {
796+
aes.process(file_data, reinterpret_cast<unsigned char*>(buffer), buffer_len);
797+
} else {
798+
std::memcpy(buffer, file_data, buffer_len);
799+
}
790800
std::cout.write(buffer, buffer_len);
791801
file_data += buffer_len;
792802
file_data_len -= buffer_len;
@@ -800,9 +810,11 @@ int clean (int argc, const char** argv)
800810

801811
const size_t buffer_len = temp_file.gcount();
802812

803-
aes.process(reinterpret_cast<unsigned char*>(buffer),
804-
reinterpret_cast<unsigned char*>(buffer),
805-
buffer_len);
813+
if (is_decrypted) {
814+
aes.process(reinterpret_cast<unsigned char*>(buffer),
815+
reinterpret_cast<unsigned char*>(buffer),
816+
buffer_len);
817+
}
806818
std::cout.write(buffer, buffer_len);
807819
}
808820
}

0 commit comments

Comments
 (0)