void test_filter_blob__ident(void) { git_oid id; git_blob *blob; git_buf buf = { 0 }; cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n"); cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident")); cl_git_pass(git_blob_lookup(&blob, g_repo, &id)); cl_assert_equal_s( "Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob)); git_blob_free(blob); cl_git_mkfile("crlf/test.ident", "Some text\n$Id: Any old just you want$\nGoes there\n"); cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident")); cl_git_pass(git_blob_lookup(&blob, g_repo, &id)); cl_assert_equal_s( "Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob)); cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.bin", 1)); cl_assert_equal_s( "Some text\n$Id$\nGoes there\n", buf.ptr); cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.identcrlf", 1)); cl_assert_equal_s( "Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845$\r\nGoes there\r\n", buf.ptr); cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.identlf", 1)); cl_assert_equal_s( "Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845$\nGoes there\n", buf.ptr); git_buf_free(&buf); git_blob_free(blob); }
void test_filter_blob__sanitizes(void) { git_blob *blob; git_buf buf; cl_git_pass(git_revparse_single( (git_object **)&blob, g_repo, "e69de29")); /* zero-byte */ cl_assert_equal_i(0, git_blob_rawsize(blob)); cl_assert_equal_s("", git_blob_rawcontent(blob)); memset(&buf, 0, sizeof(git_buf)); cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1)); cl_assert_equal_sz(0, buf.size); cl_assert_equal_s("", buf.ptr); git_buf_free(&buf); memset(&buf, 0, sizeof(git_buf)); cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1)); cl_assert_equal_sz(0, buf.size); cl_assert_equal_s("", buf.ptr); git_buf_free(&buf); memset(&buf, 0, sizeof(git_buf)); cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1)); cl_assert_equal_sz(0, buf.size); cl_assert_equal_s("", buf.ptr); git_buf_free(&buf); git_blob_free(blob); }
void test_filter_blob__all_crlf(void) { git_blob *blob; git_buf buf = { 0 }; cl_git_pass(git_revparse_single( (git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */ cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob)); cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1)); cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr); cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1)); /* in this case, raw content has crlf in it already */ cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr); cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1)); /* we never convert CRLF -> LF on platforms that have LF */ cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr); git_buf_free(&buf); git_blob_free(blob); }
void test_filter_custom__order_dependency(void) { git_index *index; git_blob *blob; git_buf buf = { 0 }; /* so if ident and reverse are used together, an interesting thing * happens - a reversed "$Id$" string is no longer going to trigger * ident correctly. When checking out, the filters should be applied * in order CLRF, then ident, then reverse, so ident expansion should * work correctly. On check in, the content should be reversed, then * ident, then CRLF filtered. Let's make sure that works... */ cl_git_mkfile( "empty_standard_repo/.gitattributes", "hero.*.rev-ident text ident prereverse eol=lf\n"); cl_git_mkfile( "empty_standard_repo/hero.1.rev-ident", "This is a test\n$Id$\nHave fun!\n"); cl_git_mkfile( "empty_standard_repo/hero.2.rev-ident", "Another test\n$dI$\nCrazy!\n"); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_add_bypath(index, "hero.1.rev-ident")); cl_git_pass(git_index_add_bypath(index, "hero.2.rev-ident")); cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Filter chains\n"); git_index_free(index); cl_git_pass(git_blob_lookup(&blob, g_repo, & git_index_get_bypath(index, "hero.1.rev-ident", 0)->oid)); cl_assert_equal_s( "\n!nuf evaH\n$dI$\ntset a si sihT", git_blob_rawcontent(blob)); cl_git_pass(git_blob_filtered_content(&buf, blob, "hero.1.rev-ident", 0)); /* no expansion because id was reversed at checkin and now at ident * time, reverse is not applied yet */ cl_assert_equal_s( "This is a test\n$Id$\nHave fun!\n", buf.ptr); git_blob_free(blob); cl_git_pass(git_blob_lookup(&blob, g_repo, & git_index_get_bypath(index, "hero.2.rev-ident", 0)->oid)); cl_assert_equal_s( "\n!yzarC\n$Id$\ntset rehtonA", git_blob_rawcontent(blob)); cl_git_pass(git_blob_filtered_content(&buf, blob, "hero.2.rev-ident", 0)); /* expansion because reverse was applied at checkin and at ident time, * reverse is not applied yet */ cl_assert_equal_s( "Another test\n$59001fe193103b1016b27027c0c827d036fd0ac8 :dI$\nCrazy!\n", buf.ptr); cl_assert_equal_i(0, git_oid_strcmp( git_blob_id(blob), "8ca0df630d728c0c72072b6101b301391ef10095")); git_blob_free(blob); git_buf_free(&buf); }
bool CatCommand::Execute() { if (!GitAdminDir::IsWorkingTreeOrBareRepo(g_Git.m_CurrentDir)) { CMessageBox::Show(GetExplorerHWND(), IDS_NOGITREPO, IDS_APPNAME, MB_ICONERROR); return false; } CString savepath = CPathUtils::GetLongPathname(parser.GetVal(L"savepath")); CString revision = parser.GetVal(L"revision"); if (g_Git.UsingLibGit2(CGit::GIT_CMD_GETONEFILE)) { CAutoRepository repo(g_Git.GetGitRepository()); if (!repo) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), g_Git.GetLibGit2LastErr(L"Could not open repository."), L"TortoiseGit", MB_ICONERROR); return false; } CAutoObject obj; if (git_revparse_single(obj.GetPointer(), repo, CUnicodeUtils::GetUTF8(revision))) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), g_Git.GetLibGit2LastErr(L"Could not parse revision."), L"TortoiseGit", MB_ICONERROR); return false; } if (git_object_type(obj) == GIT_OBJECT_BLOB) { CAutoFILE file = _wfsopen(savepath, L"wb", SH_DENYRW); if (file == nullptr) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), L"Could not open file for writing.", L"TortoiseGit", MB_ICONERROR); return false; } CAutoBuf buf; if (git_blob_filtered_content(buf, reinterpret_cast<git_blob*>(static_cast<git_object*>(obj)), CUnicodeUtils::GetUTF8(cmdLinePath.GetGitPathString()), 0)) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), g_Git.GetLibGit2LastErr(L"Could not get filtered content."), L"TortoiseGit", MB_ICONERROR); return false; } if (fwrite(buf->ptr, sizeof(char), buf->size, file) != buf->size) { ::DeleteFile(savepath); CString err = CFormatMessageWrapper(); CMessageBox::Show(GetExplorerHWND(), L"Could not write to file: " + err, L"TortoiseGit", MB_ICONERROR); return false; } return true; } if (g_Git.GetOneFile(revision, cmdLinePath, savepath)) { MessageBox(GetExplorerHWND(), g_Git.GetGitLastErr(L"Could get file.", CGit::GIT_CMD_GETONEFILE), L"TortoiseGit", MB_ICONERROR); return false; } return true; } CString cmd, output, err; cmd.Format(L"git.exe cat-file -t %s", static_cast<LPCTSTR>(revision)); if (g_Git.Run(cmd, &output, &err, CP_UTF8)) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), output + L'\n' + err, L"TortoiseGit", MB_ICONERROR); return false; } if (CStringUtils::StartsWith(output, L"blob")) cmd.Format(L"git.exe cat-file -p %s", static_cast<LPCTSTR>(revision)); else cmd.Format(L"git.exe show %s -- \"%s\"", static_cast<LPCTSTR>(revision), static_cast<LPCTSTR>(this->cmdLinePath.GetWinPathString())); if (g_Git.RunLogFile(cmd, savepath, &err)) { ::DeleteFile(savepath); MessageBox(GetExplorerHWND(), L"Cat file failed:\n" + err, L"TortoiseGit", MB_ICONERROR); return false; } return true; }