Skip to content
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

feat: can analyse package-lock.json in a subdirectory #6

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ whatsdiff

## 📋 Roadmap
Pull requests are welcome! Here are some ideas to get you started:
- [x] Analyse composer.lock
- [x] Find releases through packagist.com
- [x] Analyse package-json.lock (javascript)
- [ ] Make a nice TUI (WIP on [#1](https://github.com/SRWieZ/whatsdiff/pull/1))
- [ ] Output format (json, markdown, no-ansi)
- [ ] Retrieve changelog with Github API
- [ ] Publish on NPM
- [ ] Analyse gradle dependencies (android)
Expand Down
55 changes: 49 additions & 6 deletions src/whatsdiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ function showHelp(): void

function gitLogOfFile(string $filename, string $beforeHash = ''): array
{
global $git_root;

$cmd = 'git log '.($beforeHash)." --pretty=format:'%h' -- '$filename'"; // TODO: escape filename

$output = shell_exec($cmd);
$output = shell_exec_cwd($cmd, $git_root);

if (is_null($output)) {
return [];
Expand All @@ -74,9 +76,11 @@ function gitLogOfFile(string $filename, string $beforeHash = ''): array

function gitLogOfFiles(array $filenames): array
{
global $git_root;

$filesString = implode('\' \'', array_map('escapeshellarg', $filenames));

$output = shell_exec("git log --pretty=format:'%h' -- '$filesString'");
$output = shell_exec_cwd("git log --pretty=format:'%h' -- '$filesString'", $git_root);

if (is_null($output)) {
return [];
Expand All @@ -91,6 +95,10 @@ function isFileHasBeenRecentlyUpdated(string $filename): bool
$output = shell_exec('git status --porcelain');

if (is_null($output)) {
if (file_exists(basename($filename))) {
return true;
}

return false;
}

Expand All @@ -101,6 +109,11 @@ function isFileHasBeenRecentlyUpdated(string $filename): bool
return [$line[1] => $line[0]];
});

// If the file exists and is not in the list of untracked files
if (file_exists(basename($filename)) && ! $status->has($filename)) {
return true;
}

return in_array($status->get($filename), [
// todo: read the docs
'AM', // Added and modified
Expand All @@ -112,6 +125,8 @@ function isFileHasBeenRecentlyUpdated(string $filename): bool

function getFileContentOfCommit(string $filename, string $commitHash): string
{
dump("git show $commitHash:'$filename'");

return shell_exec("git show $commitHash:$filename");
}

Expand All @@ -128,8 +143,13 @@ function getCommitHashToCompare(array $commitLogs, bool $recentlyUpdated): array

function getFilesToCompare(string $filename, ?string $lastHash, ?string $previousHash): array
{
$last = $lastHash ? getFileContentOfCommit($filename, $lastHash) : file_get_contents($filename);
$previous = $previousHash ? getFileContentOfCommit($filename, $previousHash) : null;
$last = $lastHash
? getFileContentOfCommit($filename, $lastHash)
: file_get_contents(basename($filename));

$previous = $previousHash
? getFileContentOfCommit($filename, $previousHash)
: null;

return [$last, $previous];
}
Expand Down Expand Up @@ -320,6 +340,16 @@ function getNpmjsReleases(string $package, string $from, string $to): array
return $returnVersions;
}

function shell_exec_cwd(string $cmd, string $cwd): string|false|null
{
$oldCwd = getcwd();
chdir($cwd);
$output = shell_exec($cmd);
chdir($oldCwd);

return $output;
}

if ($command === 'help' || $options['show_version']) {
showHelp();
exit;
Expand All @@ -342,6 +372,19 @@ function getNpmjsReleases(string $package, string $from, string $to): array
],
];

$git_root = rtrim(trim(shell_exec('git rev-parse --show-toplevel')), DIRECTORY_SEPARATOR);
$current_dir = rtrim(getcwd(), DIRECTORY_SEPARATOR);
$relative_current_dir = ltrim(str_replace($git_root, '', $current_dir), DIRECTORY_SEPARATOR);

// Relative to current directory and git root
foreach ($dependency_files as $key => $file) {
// If the file exist in the current directory
if (! empty($relative_current_dir) && file_exists($file['file'])) {
// relative path form the git root
$dependency_files[$key]['file'] = $relative_current_dir.DIRECTORY_SEPARATOR.$file['file'];
}
}

foreach ($dependency_files as $type => $file) {
$dependency_files[$type]['hasBeenRecentlyUpdated'] =
! $options['ignore_last'] && isFileHasBeenRecentlyUpdated($file['file']);
Expand Down Expand Up @@ -374,13 +417,13 @@ function getNpmjsReleases(string $package, string $from, string $to): array
$filenames = $dependency_files->where('hasBeenRecentlyUpdated', true)->pluck('file', 'type')->toArray();

// Commit logs of all files that have been recently updated
$commitLogs = gitLogOfFiles($filenames);
} else {
// Case 3 : No recent changes but at least one commit log
$filenames = $dependency_files->where('hasCommitLogs', true)->pluck('file', 'type')->toArray();
$commitLogs = gitLogOfFiles($filenames);
}

$commitLogs = gitLogOfFiles($filenames);

// Only compare the last change
[$lastHash, $previousHash] = getCommitHashToCompare($commitLogs, $recentlyUpdated);

Expand Down
Loading