Skip to content

feat: hide .jj directory on Windows #6514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ version_check = "0.9.5"
watchman_client = { version = "0.9.0" }
whoami = "1.6.0"
winreg = "0.52"

winapi= {version = "0.3.9", features = ["winuser"] }
# put all inter-workspace libraries, i.e. those that use 'path = ...' here in
# their own (alphabetically sorted) block

Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ rustix = { workspace = true }

[target.'cfg(windows)'.dependencies]
winreg = { workspace = true }
winapi = { workspace = true }

[dev-dependencies]
assert_matches = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: per contributing guidelines you can squash the third commit into this and probably should use workspace: <title> as the topic, since feat: just means feature which isn't something used by the project.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#![warn(missing_docs)]
#![deny(unused_must_use)]
#![forbid(unsafe_code)]
#![deny(unsafe_code)]

// Needed so that proc macros can be used inside jj_lib and by external crates
// that depend on it.
Expand Down
18 changes: 17 additions & 1 deletion lib/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#![allow(missing_docs)]

use std::collections::HashMap;
#[cfg(windows)]
use std::ffi::CString;
use std::fs;
use std::fs::File;
use std::io;
Expand All @@ -24,6 +26,10 @@ use std::path::PathBuf;
use std::sync::Arc;

use thiserror::Error;
#[cfg(windows)]
use winapi::um::fileapi::SetFileAttributesA;
#[cfg(windows)]
use winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN;

use crate::backend::BackendInitError;
use crate::backend::MergedTreeId;
Expand Down Expand Up @@ -119,7 +125,17 @@ pub struct Workspace {
fn create_jj_dir(workspace_root: &Path) -> Result<PathBuf, WorkspaceInitError> {
let jj_dir = workspace_root.join(".jj");
match std::fs::create_dir(&jj_dir).context(&jj_dir) {
Ok(()) => Ok(jj_dir),
Ok(()) => {
#[cfg(windows)]
#[allow(unsafe_code)]
{
let jj_dir = CString::new(jj_dir.to_str().unwrap()).unwrap();
unsafe {
SetFileAttributesA(jj_dir.as_ptr(), FILE_ATTRIBUTE_HIDDEN);
};
}
Ok(jj_dir)
}
Err(ref e) if e.error.kind() == io::ErrorKind::AlreadyExists => {
Err(WorkspaceInitError::DestinationExists(jj_dir))
}
Expand Down
20 changes: 20 additions & 0 deletions lib/tests/test_init.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please squash this into the parent commit

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(windows)]
use std::ffi::CString;
use std::path::Path;
use std::path::PathBuf;

Expand All @@ -26,6 +28,10 @@ use testutils::git;
use testutils::write_random_commit;
use testutils::TestRepoBackend;
use testutils::TestWorkspace;
#[cfg(windows)]
use winapi::um::fileapi::GetFileAttributesA;
#[cfg(windows)]
use winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN;

fn canonicalize(input: &Path) -> (PathBuf, PathBuf) {
let uncanonical = input.join("..").join(input.file_name().unwrap());
Expand All @@ -51,6 +57,20 @@ fn test_init_local() {
write_random_commit(tx.repo_mut());
}

#[test]
#[cfg(windows)]
fn test_init_hidden() {
let settings = testutils::user_settings();
let temp_dir = testutils::new_temp_dir();
let (canonical, uncanonical) = canonicalize(temp_dir.path());
Workspace::init_internal_git(&settings, &uncanonical).unwrap();

let dot_jj_path = CString::new(canonical.join(".jj").to_str().unwrap()).unwrap();
let dot_jj_folder_attributes = unsafe { GetFileAttributesA(dot_jj_path.as_ptr()) };

assert_ne!(dot_jj_folder_attributes & FILE_ATTRIBUTE_HIDDEN, 0);
}

#[test]
fn test_init_internal_git() {
let settings = testutils::user_settings();
Expand Down