Skip to content

Commit

Permalink
Add option to set another git repository
Browse files Browse the repository at this point in the history
Setting another repository will generate a changelog for that repo.
  • Loading branch information
DigiLive committed Oct 22, 2020
1 parent a574e81 commit f8e2449
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -28,6 +28,7 @@ new [issue](https://github.com/DigiLive/gitChangeLog/issues/new) is your concern

### Other

- Set path to generate a changelog another local repository.
- Set a From- and To tag to limit the changelog.
- Filter subjects by labels<sup>1</sup>.
- Set a header for the changelog (E.g. a title).
Expand Down
18 changes: 14 additions & 4 deletions src/GitChangeLog.php
Expand Up @@ -96,6 +96,10 @@ class GitChangeLog
* @var string Format of a single commit hash. {hash} is replaced by the commit hash.
*/
public $formatHash = '{hash}';
/**
* @var string Path to local git repository. Leave null for repository at current folder.
*/
public $gitPath;
/**
* @var string Value of the oldest tag to include into the generated changelog.
* @see GitChangeLog::setFromTag()
Expand Down Expand Up @@ -194,8 +198,11 @@ public function fetchTags($force = false): array
return $this->gitTags;
}

$gitPath = '--git-dir ';
$gitPath .= $this->gitPath ?? './.git';

// Get all git tags.
$this->gitTags = explode("\n", shell_exec("git tag --sort=-{$this->options['tagOrderBy']}"));
$this->gitTags = explode("\n", shell_exec("git $gitPath tag --sort=-{$this->options['tagOrderBy']}"));
array_pop($this->gitTags); // Remove empty element.

$toKey = $this->toTag == 'HEAD' ? 0 : array_search($this->toTag, $this->gitTags);
Expand Down Expand Up @@ -342,22 +349,25 @@ public function fetchCommitData($force = false): array
$gitTags[] = '';
}

$gitPath = '--git-dir ';
$gitPath .= $this->gitPath ?? './.git';

// Get tag dates and commit subjects from git log for each tag.
foreach ($gitTags as $tag) {
$tagRange = $tag == '' ? $previousTag : "$tag...$previousTag";
$includeMergeCommits = $this->options['includeMergeCommits'] ? '' : '--no-merges';

$commitData[$previousTag]['date'] =
shell_exec("git log -1 --pretty=format:%ad --date=short $previousTag");
shell_exec("git $gitPath log -1 --pretty=format:%ad --date=short $previousTag");
$commitData[$previousTag]['subjects'] =
explode(
"\n",
shell_exec("git log $tagRange $includeMergeCommits --pretty=format:%s --reverse")
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%s --reverse")
);
$commitData[$previousTag]['hashes'] =
explode(
"\n",
shell_exec("git log $tagRange $includeMergeCommits --pretty=format:%h --reverse")
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%h --reverse")
);
$previousTag = $tag;
}
Expand Down

0 comments on commit f8e2449

Please sign in to comment.