Set CODEOWNERS on files but not directories? - github

I have the following structure:
.github/
CODEOWNERS
A/
files
B/
files
example.py
example2.py
I want code owners to have a specific owners on all files in the main directory.
I can do:
example.py #owner
example2.py #owner
But that means list them manually which is something I dont want to do as it doesnt offer protection against adding new files!
So my question is: How can I say all files in main directory but not folders?

The CODEOWNERS docs have an example file, with this relevant bit:
# The `docs/*` pattern will match files like
# `docs/getting-started.md` but not further nested files like
# `docs/build-app/troubleshooting.md`.
docs/* docs#example.com
So, to assign the root directory, but not its subdirectories, you could do
/* #owner

Related

.gitignore all except one specific file (via a full/fuller file path)

The Goal:
How to use .gitignore to exclude all folders & files except PowerShell $Profile?
The answer should help expand the current exception file list to more files in other subfolders. If possible, reduce wildcards to minimum, and use as specific as possible (full/fuller file path). Why? For instance, Book1.xlsx may exist in multiple subfolders but I want to be able to choose only the specific desired subfolders.
Thanks in advance!
Current status:
On Windows 10 (not Linux Distros):
git init initiated on top level directory C:\. [Please don't suggest to start from other subfolders. Just remain with C:\, as I will include more files to the exception list]
C:\.gitignore containing the below:
# Ignore All
/*
# Exception List [eg. PowerShell's $Profile (please use FULL/FULLER FILE PATH, if possible)]
!.gitignore
!Microsoft.PowerShell_profile.ps1
!Users/John/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
!Users\John\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
!C:/Users/John/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
!C:\Users\John\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
With the above codes, git status successfully returned only .gitignore as 1 untracked file. Microsoft.PowerShell_profile.ps1 remained missing from untrack listing.
I've tried alternative ways (wildcard, partial subfolder name with pattern, etc) but all failed to return the fuller file path PowerShell $Profile.

CODEOWNERS in Github doesn't work as expected

Issue: CODEOWNERS need fully qualified path for rule against a directory/subdirectory.
I am writing a sample CODEOWNERS below to show the problem.
* #global-owner
foo/bar/ #octocat
I am expecting that whenever a PR is raised for any file (even recursively) inside directory foo/bar, user should be assigned a review. However, this always defaults to the * rule.
However, when I change the file to something like this:
* #global-owner
/app/src/main/java/com/cueo/foo/bar/ #octocat
This works like a charm. But the problem with this is that I need to repeat each directory twice to something like this:
/app/src/main/java/com/cueo/foo/bar/ #octocat
/app/src/test/java/com/cueo/foo/bar/ #octocat
According to the documentation:
# In this example, #octocat owns any file in an apps directory
# anywhere in your repository.
apps/ #octocat
I believe this should work for a nested directory structure also, like:
foo/bar/apps/ #octocat
We need to prefix the paths with ** as well. This is not clear from the documentation.
So if we add a rule like:
* #global-owner
**/foo/bar/ #octocat
#octocat will be assigned for all foo/bar directories in the project.

rsync: How to NOT apply wildcard excludes recursively

I'm using rsync to do a local backup of a Matlab model (end goal is a shell script to autobackup my key PhD files).
The model has layers of folders, and I want to exclude some wildcard matches on top level but not lower levels. Is this possible?
Example file structure:
/Model % DO NOT WANT to sync *.mat files in this directory
/Model/Data/Greens % WANT to sync *.mat files in this subdirectory
Example (simplified) code:
#!/bin/bash
rsync -a --exclude="*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup
The example code excludes ALL *.mat files in ALL folder levels. How do I fix it to only apply the '*.mat' wildcard to the top folder?
TL;DR: Is there a way to apply rsync recursively to all subfolders, but apply a wildcard exclude to only the top-level folder?
By default, *.mat is going to match all .mat files in your folder and all sub-directories. If you'd only like to ignore .mat files in the parent directory, you can add the Model/ prefix to the exclude directive to only ignore .mat files in that folder
rsync -a --exclude="Model/*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup
Alternately, if you want to include .mat files only in the Model/Data/Greens folder you can use the --include option in conjunction with the --exclude option
rsync -a --include="Model/Data/Greens/*.mat" --exclude="*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup

Ignore all files and files in subdirectories except one file

Imagine the following structure:
/a/1.txt
/a/2.txt
/a/.keep
/a/b/1.txt
/a/b/2.txt
/a/b/3.txt
/a/b/.keep
/a/b/c/1.txt
/a/b/c/2.txt
/a/b/c/3.txt
/a/b/c/4.txt
/a/b/c/.keep
/d/test.txt
/d/work.txt
I want to ignore all files in a directory except .keep files to obtain the following results:
/a/.keep
/a/b/.keep
/a/b/c/.keep
/d/test.txt
/d/work.txt
My .gitignore file that doesn't work:
/a/*
!.keep
Unfortunatelly, you cannot reinclude files at directories ignored by previous rules, according to the gitignore Documentation:
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
So this
/a/*
!/a/**/.keep
will only reinclude /a/.keep but not the others.
You'll have to exclude each file pattern under /a explictly.
/a/**/*.txt
/a/**/.ignore
/a/**/.alsoignore
UPDATE: Or a better solution is to create the following .gitgnore at your /a subdirectory:
*.*
!.keep
(the only drawback is that this solution will also keep files with no extension)
In your case, you should use:
/a/*
!**/.keep
From the gitignore documentation:
A leading "**" followed by a slash means match in all directories. For
example, "**/foo" matches file or directory "foo" anywhere, the same
as pattern "foo". "**/foo/bar" matches file or directory "bar"
anywhere that is directly under directory "foo".

Deleting subdirectories and files in a given directory

I want to delete all subdirectories and files from a directory, but not the directory itself. For examnple, if I pass "Sample" directory in a variable, and "Sample" directory contains three subdirectories and 3 files, I want to delete all that 3 directories and 3 files. In practice, the "Sample" directory can contain many subdirectories and files.
ETA: This is actually in perlfaq5: How do I delete a directory tree?
Use File::Path, core module.
perl -MFile::Path=remove_tree -we
'remove_tree("Sample",{keep_root=>1}) or die $!'
The keep_root option will cause remove_tree to keep the top directory:
keep_root => $bool
When set to a true value, will cause all files and subdirectories to
be removed, except the initially specified directories. This comes in
handy when cleaning out an application's scratch directory.