Git sparse checkout^ some root folders fully recursive, some other root folders without subfolders

110 views Asked by At

I use git 2.40 and I have repo folder structure like this:

└── root_dir1
│   ├── dir11              
│   │   └── file11
│   │
│   └── dir12
│   │   └── file12
│   │   
│   └── file13
│
└── root_dir2
    ├── dir21         
    │   └── sub_dir21a
    │   │   │
    │   │   └── file21
    │   │   
    │   └── file22
    │
    └── file23

I need to checkout:

  1. Files that are placed directly in root_dir1 (exclude all subfolders with all it's content) - file13 in my specific example.
  2. Files that are placed directly in root_dir2 and directly in dir21 subfolder (exclude all other subfolders with all it's content) - file22 and file23 in my specific example.

I've tried the following:

git clone --no-checkout --sparse repo_URL
git sparse-checkout set root_dir1 root_dir2
git checkout

After that I've edited .git\info\sparse-checkout and added some negative patterns:

/*
!/*/
/root_dir1/
/root_dir2/
#added some negative patterns
!/root_dir1/*/
!/root_dir2/dir21/*/

And finally I called git sparse-checkout reapply. But I've received a warning:

warning: unrecognized negative pattern: '/root_dir2/dir21/*' warning:
disabling cone pattern matching

Despite of this warning I got exactly the files that I needed - looks like it's working. But I'm curious what's wrong with my negative patterns? Why !/root_dir1/*/ is valid but !/root_dir2/dir21/*/ not? And why it is working correctly despite of that?

1

There are 1 answers

0
Gabriel Devillers On

My understanding is that the expected patterns in cone mode are in the form (white lines added for clarity):

/*
!/*/

/dirA/
!/dirA/*/

/dirA/dirB/
!/dirA/dirB/*/

/dirA/dirB/dirC/

(this would be the result of running git sparse-checkout add dirA/dirB/dirC)

i.e. some directories where everything below is present (in this example dirC) and all their parent directories are present with their files but not their directories (in this example the root directory, dirA and dirB)

which is not what you have. To not get the warnings you would need:

/*
!/*/
/root_dir1/
!/root_dir1/*/
/root_dir2/
!/root_dir2/*/
/root_dir2/dir21/
!/root_dir2/dir21/*/

which makes git sparse-checkout reapply works without warning and leave you in cone mode:

cat .git/config.worktree
[core]
        sparseCheckout = true
        sparseCheckoutCone = true

(tested with Git 2.42.0)