How to use commit-filter in CGIT to add a link to the bug number

Cgit is a great tool. If you want to make it better to add a link to the bug number if the commit message contains a given pattern (e.g., tdf#123456), then you should:

First, Create an executable script file which accepts STDIN as input and generate an output to STDOUT. For instance, I use the following python code, and place it in anywhere (e.g., /home/bin/cgit-commit-filter.py)

#!/usr/bin/env python3
import sys
import re

regs = [
        {"exp": r"tdf\#([0-9]+)", "replace_to": r"<a href='https://bugs.documentfoundation.org/show_bug.cgi?id=\1'>tdf#\1</a>"},
    ]

for s in sys.stdin:
    s = s.strip()
    for reg in regs:
        if re.findall(reg["exp"], s):
            s = re.sub(reg["exp"], reg["replace_to"], s)
            break
    print(s)

The above code stores the string accepted from STDIN in variable s, then defines some sets of regular expression as well as the replacement rule in variable regs. Then, it tests whether we can find the text in s; if it finds, then the text is replaced according to the defined rule and print the replaced text in STDOUT; otherwise, the original text is printed. You can define several pattern and replacement rule pairs in “regs” above.

Then make the script executable:

$ chmod +x /home/bin/cgit-commit-filter.py

To test, run this script from command line, type in some test strings, hit ENTER to see the result:

$ ./cgit-commit-filter.py 
tdf#12345 make libreoffice better with this commit
<a href='https://bugs.documentfoundation.org/show_bug.cgi?id=12345'>tdf#12345</a> make libreoffice better with this commit

And finally, add the following line in /etc/cgitrc:

commit-filter=/home/bin/cgit-commit-filter.py

Wait several minutes, then I can see that every commit message with the pattern “tdf#123456” will have a link which goes to the TDF bug tracker.

I use python code here, but if you want you may use shell script. See: https://git.zx2c4.com/cgit/commit/cgit.h?id=f35db1cd2b75aac6952aa07713e44ca01fd89727

11条评论

  1. You’re so cool! I do not think I’ve truly read through something like this before.
    So good to discover somebody with original thoughts on this subject.
    Really.. many thanks for starting this up. This website is one
    thing that is required on the internet, someone with a little
    originality!

    Visit my blog: best cbd oil for sleep

  2. Howdy would you mind sharing which blog platform you’re using?

    I’m planning to start my own blog soon but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and
    Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something unique.

    P.S Sorry for being off-topic but I had to ask!

  3. Awesome blog! Do you have any recommendations for aspiring writers?
    I’m planning to start my own website soon but I’m
    a little lost on everything. Would you suggest starting with
    a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely confused
    .. Any suggestions? Many thanks!

  4. Heya! I’m at work surfing around your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look forward to all
    your posts! Keep up the fantastic work!

  5. Excellent post. I used to be checking continuously this weblog and
    I’m impressed! Very useful info particularly the ultimate part 🙂 I care for such information much.
    I used to be looking for this certain information for a very
    long time. Thanks and good luck.

    My homepage – CBD for dogs

  6. Thanks for the auspicious writeup. It in fact was a entertainment account it.
    Glance advanced to more delivered agreeable
    from you! By the way, how could we communicate?

  7. Wonderful goods from you, man. I have understand your stuff previous to and you are just extremely excellent.
    I really like what you have acquired here, certainly like what you’re saying and the way in which you say it.

    You make it enjoyable and you still care for to keep
    it sensible. I cant wait to read much more from you.
    This is actually a terrific web site.

发表评论

邮箱地址不会被公开。 必填项已用*标注