Skip to content

Is there a way to automatically prefix commits? #792

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

Closed
ismail-codinglab opened this issue Sep 22, 2019 · 3 comments
Closed

Is there a way to automatically prefix commits? #792

ismail-codinglab opened this issue Sep 22, 2019 · 3 comments

Comments

@ismail-codinglab
Copy link

If you have a branch e.g. feature/123-internationalization it would be nice if all commit messages would be automatically prefixed with feat(123-internationalization): . It would detect the type
and context depending on the branch name

This saves me time and prevents typos.

Is there some way i can do this?

@marionebl
Copy link
Contributor

marionebl commented Sep 23, 2019

Interesting idea! commitlint primarily concerns itself with checking if your commits conform to a specific convention.

To my mind your suggestion is based on the idea to deduce parts of the commit message from other information, which is better suited to a commitizen adapter that reads the branch name and prepopulates the type and scope fields accordingly.

I’d like to bring the concept back up when we tackle #584.

@ismail-codinglab
Copy link
Author

Can you maybe give me some guidance on how to write my own little script

like reading the string before the / and after the /.
That is the most simplified one and will fit most of my usecases.

They are mostly named fix/123-some-name or feature/123-some-name

@marionebl
Copy link
Contributor

On a whim I'd break this down into the following problems:

  • determine the branch name reliably
    git symbolic-ref --short HEAD seems to be a good bet (StackOverflow). In JS-land that might look like this:

    const execa = require('execa');
    
    async function main() {
      const branch = await getBranch();
      // do something with it 
    }
    
    async function getBranch() {
    try {
        const branch = await execa('git', ['symbolic-ref', '--short', 'HEAD']);
        return branch === 'HEAD' ? undefined : branch;
     } catch (err) {
        return;
     }
    }
    
    main().then(err => setTimeout(() => {  throw err }));
  • extract the desired information

    const [type, scope] = branch.split('/').filter(Boolean);

There is some glue code involved, especially for handling cases where

  • the branch name as a whole can not be determined (e.g. detached HEAD state)
  • the git command exits with non-zero (e.g. fresh repositories)

On how to provide that information to commitizen appropriately you'll have to consult their docs.

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

No branches or pull requests

2 participants