Skip to content

Instantly share code, notes, and snippets.

@avsej
Created August 8, 2012 09:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avsej/3293907 to your computer and use it in GitHub Desktop.
Save avsej/3293907 to your computer and use it in GitHub Desktop.
This script helps to avoid switching to browser to fetch fresh patchsets from gerrit
#!/usr/bin/env tclsh
# GistID: 3293907
#
# USAGE:
#
# 1. put somewhere in the PATH
# 2. ensure that repository has remote with name 'gerrit'
# 3. execute 'git fetch-patches'
package require json
set remote gerrit
catch {
set remote [lindex $argv 0]
}
catch {
cd [lindex $argv 1]
}
catch {
set url [exec git config --get remote.$remote.url]
}
if {![info exists url]} {
exit 0
}
puts stderr [pwd]
set parts [split $url {: /}]
set hostname [lindex $parts 3]
set port [lindex $parts 4]
set project [regsub \.git$ [lindex $parts 5] {}]
set old {}
catch {
set old [exec git show-ref | grep refs/patches]
}
set new {}
set query "ssh -p $port $hostname gerrit query --format=JSON --current-patch-set status:open project:$project"
foreach {line} [split [exec sh -c $query] \n] {
set change [json::json2dict $line]
if {[dict exists $change currentPatchSet]} {
set num [dict get $change number]
set ref [dict get $change currentPatchSet ref]
set rev [dict get $change currentPatchSet revision]
if {[dict exists $old $rev]
&& [string equal [string trim [dict get $old $rev]] "refs/patches/$num"]} {
set old [dict remove $old $rev]
} else {
lappend new "+$ref:refs/patches/$num"
}
}
}
if {[dict size $old] > 0} {
puts stderr "Prune [dict size $old] closed patches"
set gitdir [exec git rev-parse --git-dir]
dict for {rev ref} $old {
file delete -force $gitdir/$ref
}
}
if {[llength $new] > 0} {
puts stderr "Found [llength $new] new patches. Fetching..."
exec -ignorestderr sh -c "git fetch $remote $new"
} else {
puts stderr "Patch references are up to date"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment