Search code examples
rubygitrake

Generating release notes from git commits


I've created the following rake task below to generate our release notes for each sprint.
I'm pulling in all commits to master older than 2 weeks.

The problem is when a branch has been developed on for more than 2-week sprints, the older commits won't be included.

Can anyone suggest a way I can get these commits in?

task :new_release_note do

  puts "Creating new release note"
  puts "..."

  git_log = `git log --since="two weeks ago" --no-merges --format=%B`
  git_log.gsub!(/^$\n/, '')
  git_log.gsub!(/^/, "* ") 

  current_time = DateTime.now 
  current_date = current_time.strftime "%Y-%m-%d"
  current_date_UK = current_time.strftime "%d-%m-%Y"

  template = "__Release Notes__
  =======================
  #{current_date_UK}

  __New Features__
  ----------------

  * -


  __Improvements__
  ----------------

  * -


  __Fixes__
  ---------

  * -


  __Change Log__
  ----------------

  Detailed release notes below, listing all commit messages for this release.


  #{git_log}
  "

  out_file = File.new("./doc/release_notes/release-notes-#{current_date}.md", "w")
  out_file.puts(template)

  if File.exist?(out_file) 
    puts "New release note generated successfully at /doc/release-notes/release-notes-#{current_date}.md"
  else 
    puts "Error - file not generated."
  end 

end

Solution

  • Can anyone suggest a way I can get these commits in?

    Few options:

    1. git tag
    2. git notes
    3. git whatchanged

    git tag

    Read this answer on what is git tag and how to use it: What is git tag, How to create tags & How to checkout git remote tag(s)

    In short: git tag allows you to mark commit which can be later on to perform your merge. As you know

    git pull = git fetch + git merge
    

    So once you have marked your last merge with the tag you can pull out all the changes form the last merge

    # "Merge" the last X commits based upon your previous tag
    git cherry-pick <tag_name>..master
    

    git notes

    git notes allow us to add content to commit without updating the SHA-1 of the commit, meaning we can attach content to the commit while leaving the SHA-1 unmodified.

    enter image description here

    Now once you have your notes you can find out the last commit which you "merged" previously and grab the changes from this point on using the above cherry-pick.

    You can search and find your notes with git log --grep


    git whatchanged

    Once you what is your referenced commit you can see the list of files which were updated during this time period with the git whatchanged command

    # Print out a list of files which was updated/added between the 2 commits
    git whatchanged <TAG_NAME>...HEAD
    

    enter image description here