Skip to content

Instantly share code, notes, and snippets.

@mack-at-pieces
Created April 5, 2023 16:38
Show Gist options
  • Save mack-at-pieces/cd700ea26e89e715c8dd384e1f43b3ed to your computer and use it in GitHub Desktop.
Save mack-at-pieces/cd700ea26e89e715c8dd384e1f43b3ed to your computer and use it in GitHub Desktop.
/// Use `git ls-files` to get all of the files in source control
Future<List<String>> gitLsFiles(String directory) async {
/// Check that git is available on the command line
if (await Process.run('git', []).then((value) => false).catchError((e) => true)) {
return [];
}
/// Run Git ls-files to get all files not ignored by git ignore
return Process.run('git', ['ls-files'], workingDirectory: directory).then((value) {
/// If there is no git or some other error occurs, return an empty list,
/// meaning no files in source control in this particular directory
if (value.stderr.isNotEmpty) {
return [];
}
/// Otherwise split the response by new lines
return value.stdout.toString().split('\n');
});
}
@mack-at-pieces
Copy link
Author

@tsavo-at-pieces This is a great snippet we use in Snippet Discovery to parse out non-git-tracked files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment