Last Updated: February 25, 2016
· avsej

Fetch all active patches from gerrit

This script helps to avoid switching to browser to fetch fresh patchsets from gerrit:


#!/usr/bin/env tclsh
# GistID: 3293907
# 1. put somewhere in the PATH as git-fetch-patches
#    and make it executable
# 2. ensure that repository has remote with name 'gerrit'
# 3. execute 'git fetch-patches'

package require json

set remote gerrit
set url [exec git config --get remote.$remote.url]
set parts [split $url {: /}]
set hostname [lindex $parts 3]
set port  [lindex $parts 4]
set project [regsub \.git$ [lindex $parts 5] {}]

file delete -force [exec git rev-parse --git-dir]/refs/patches
set refs {}
set query "ssh -p $port $hostname gerrit query --format=JSON --current-patch-set status:open $project"
foreach {line} [exec sh -c $query] {
    set change [json::json2dict "{$line}"]
    if {[dict exists $change currentPatchSet]} {
        lappend refs "+[dict get $change currentPatchSet ref]:refs/patches/[dict get $change number]"
puts "Found [llength $refs] patches. Fetching..."
exec -ignorestderr sh -c "git fetch $remote $refs"