# git-merged-branches

[![npm version][npm-version-img]][npm-version-link]
[![npm downloads][npm-downloads-img]][npm-downloads-link]
[![build][build-img]][build-link]
[![socket][socket-img]][socket-link]

**git-merged-branches is a command-line utility to view branches merged into a selected base branch (e.g., master or main).**

- CLI usage
- customizable via `package.json`

## Installation

Install globally to use `git-merged-branches` or the shorter version `gmb`:

```bash
npm install --global git-merged-branches
```

Or use it without installation via [npx](https://docs.npmjs.com/cli/v7/commands/npx):

```bash
npx git-merged-branches
```

## Usage

By default, the command shows merged branches into the base branch (**master** or **main**).
If neither exists, it will notify you.

Example output:

```bash
$ git-merged-branches
2 branches merged into master:
fix/crash-on-start
feat/add-new-feature
```

You can also delete these merged branches with the `--delete` option:

```bash
$ git-merged-branches --delete
2 branches merged into master:
fix/crash-on-start
feature/add-new-feature

Deleting branches locally…
Deleting branches remotely…
Branches deleted successfully.
```

## Configuration

You can configure the utility in your `package.json` under `git-merged-branches`. This allows you to set:

- **issueUrlFormat**: Base URL for your issue tracker (must be a valid URL).
  - For JIRA-style trackers, you can can include the prefix in the URL: `https://your-jira-instance.net/browse/{{prefix}}{{id}}`.
  - Fir GitHub-style issues, omit the prefix in the URL: `https://github.com/org/repo/issues/{{id}}`.
- **issueUrlPrefix**: Array of prefixes for issue identifiers in branch names.

Example configuration for JIRA:

```json
"git-merged-branches": {
  "issueUrlFormat": "https://your-jira-instance.net/browse/{{prefix}}{{id}}",
  "issueUrlPrefix": ["TOKEN-", "PROJECT-"]
}
```

Example configuration for GitHub:

```json
"git-merged-branches": {
  "issueUrlFormat": "https://github.com/org/repo/issues/{{id}}",
  "issueUrlPrefix": ["#"]
}
```

With this setup, `git-merged-branches` will generate links for branches with such tokens in their name:

```bash
$ git-merged-branches
4 branches merged into master:
fix/EXTERNAL-391
fix/TOKEN-123_some-fix https://your-jira-instance.net/browse/TOKEN-123
hotfix
TOKEN-800_new-feature https://your-jira-instance.net/browse/TOKEN-800
refactor/#55_cleanup https://github.com/org/repo/issues/55
```

If the configuration is invalid, warnings will be shown and the utility will skip formatting URLs.

## Development

To contribute or test locally:

1. Clone the repository:

    ```bash
    git clone https://github.com/VChet/git-merged-branches.git
    cd git-merged-branches
    ```

1. Install dependencies:

    ```bash
    pnpm install
    ```

1. Build the project:

    ```bash
    pnpm run build
    ```

1. Link it locally for testing:

    ```bash
    npm link
    ```

Now you can run `git-merged-branches` on your local machine.

## Security notes

Git commands are executed via `child_process.execFileSync`, eliminating shell injection risks. Network access originates from Git operations, e.g. `git ls-remote`, not from the application itself.

## Contributing

If you have any ideas, bug reports, or feature requests,
feel free to [contribute](https://github.com/VChet/git-merged-branches/pulls)
or report [issues](https://github.com/VChet/git-merged-branches/issues).

<!-- Badges -->
[npm-version-img]: https://img.shields.io/npm/v/git-merged-branches?style=flat-square
[npm-version-link]: https://npmjs.com/package/git-merged-branches
[npm-downloads-img]: https://img.shields.io/npm/dm/git-merged-branches?style=flat-square
[npm-downloads-link]: https://npmjs.com/package/git-merged-branches
[build-img]: https://img.shields.io/github/actions/workflow/status/VChet/git-merged-branches/build.yaml?style=flat-square
[build-link]: https://github.com/VChet/git-merged-branches/actions/workflows/build.yml
[socket-img]: https://badge.socket.dev/npm/package/git-merged-branches
[socket-link]: https://socket.dev/npm/package/git-merged-branches
