Skip to content

Commit 36686c6

Browse files
committed
Improve support for selecting and downloading changes
This change adds support for selecting and downloading changes that match a custom gerrit query (e.g. "is:open"). A new interactive function called `gerrit-download-and-open-overview' was added which downloads the change and opens an magit-commit buffer using `magit-show-commit'. (It the downloaded change is at the top of a relation chain, the other commits are currently not displayed by this `gerrit-download-and-open-overview' function. Closes: #19 Change-Id: Ie3ed04a8f212cb8c0a98745d1dca4f680f420642
1 parent 60f5203 commit 36686c6

File tree

1 file changed

+82
-15
lines changed

1 file changed

+82
-15
lines changed

gerrit.el

+82-15
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,42 @@ gerrit-download is used."
148148
;; -> The test-transient allows one to cycle over all settings C-M-p / C-M-n
149149
;; and also over all the infix history
150150

151+
;; this variable is used in `gerrit-download-format-change'
152+
(defvar gerrit-change-singleline-columns
153+
'(number branch subject)
154+
"List of columns that should be displayed in functions that ask the user to select a change from a list of changes."
155+
"Currently supported columns are:"
156+
"'number (the change number)"
157+
"'branch (the branch of the change)"
158+
"'subject (the subject of the commit msg)"
159+
"'project (the project name)"
160+
)
161+
162+
;; TODO document this variable
163+
;; (setq gerrit-project-to-local-workspace-alist
164+
;; '(
165+
;; (("software/pro1" . "branch1") . "~/sandbox/pro1")
166+
;; (("software/pro2" . "branch2") . "~/sandbox/pro2")
167+
;; ))
168+
(defvar gerrit-project-to-local-workspace-alist nil)
169+
170+
;; TODO docs missing
171+
;; mention (project:A OR project:B OR project:C)
172+
(defvar gerrit-interesting-open-changes-filter "is:open")
173+
151174
(defun gerrit-download-format-change (change)
152-
(concat
153-
(propertize (number-to-string (alist-get '_number change)) 'face 'magit-hash)
154-
" "
155-
(propertize (alist-get 'branch change) 'face 'magit-branch-remote)
156-
" "
157-
(propertize (alist-get 'subject change) 'face 'magit-section-highlight)))
175+
(let (columns)
176+
;; can this be implemented in an easier way?
177+
(when (member 'number gerrit-change-singleline-columns)
178+
(add-to-list 'columns (propertize (number-to-string
179+
(alist-get '_number change)) 'face 'magit-hash)))
180+
(when (member 'project gerrit-change-singleline-columns)
181+
(add-to-list 'columns (propertize (alist-get 'project change) 'face 'magit-branch-remote)))
182+
(when (member 'branch gerrit-change-singleline-columns)
183+
(add-to-list 'columns (propertize (alist-get 'branch change) 'face 'magit-branch-remote)))
184+
(when (member 'subject gerrit-change-singleline-columns)
185+
(add-to-list 'columns (propertize (alist-get 'subject change) 'face 'magit-section-highlight)))
186+
(s-join " " (nreverse columns))))
158187

159188
(defun gerrit-download--get-refspec (change-metadata)
160189
"Return the refspec of a gerrit change from CHANGE-METADATA.
@@ -198,6 +227,7 @@ This refspec is a string of the form 'refs/changes/xx/xx/x'.
198227
;; (e.g. if ssh-add was not called) this async call runs
199228
;; magit-process-password-prompt-regexps (used in magit-process-filter)
200229
;; which is called in magit-start-process
230+
;; (see https://github.com/magit/magit/issues/4323)
201231
(magit-run-git-async "fetch"
202232
(gerrit-get-remote)
203233
(gerrit-download--get-refspec change-metadata))
@@ -1196,25 +1226,62 @@ gerrit-upload: (current cmd: %(concat (gerrit-upload-create-git-review-cmd)))
11961226

11971227

11981228

1229+
(defun gerrit--select-change-from-matching-changes (search-string)
1230+
;; see https://gerrit-review.googlesource.com/Documentation/user-search.html
1231+
;; clients can let-bind `gerrit-change-singleline-columns'
1232+
(let* ((open-changes (seq-map #'reviewgerrit-download-format-change
1233+
(gerrit-rest-change-query
1234+
(or search-string "is:open")
1235+
)))
1236+
(selected-line (completing-read
1237+
"Download Change: " open-changes nil nil))
1238+
(changenr (car (s-split " " (s-trim selected-line)))))
1239+
changenr))
1240+
11991241
(defun gerrit-download (changenr)
12001242
"Download change with CHANGENR from the gerrit server."
12011243
(interactive
12021244
(list
1203-
(let* ((open-changes
1204-
(seq-map #'gerrit-download-format-change (gerrit-rest-change-query
1205-
(concat "status:open project:"
1206-
(gerrit-get-current-project)))))
1207-
(selected-line (completing-read
1208-
"Download Change: " open-changes nil nil))
1209-
(changenr (car (s-split " " (s-trim selected-line)))))
1210-
changenr
1211-
)))
1245+
(let
1246+
((gerrit-change-singleline-columns '(number branch project subject)))
1247+
(gerrit--select-change-from-matching-changes
1248+
(concat "status:open project:"
1249+
(gerrit-get-current-project))))))
12121250

12131251
(gerrit--init-accounts)
12141252
(if gerrit-use-gitreview-interface
12151253
(gerrit-download--gitreview changenr)
12161254
(gerrit-download--new changenr)))
12171255

1256+
;; TODO split this up into two parts (one that downloads the change and one that opens the overview page using magit-show-commit
1257+
(defun gerrit-download-and-open-overview (changenr)
1258+
;; since a simple "is:open" query might return a lot of gerrit-changes, it
1259+
;; is possible to filter the returned results by setting an variable
1260+
;; called `gerrit-interesting-open-changes-filter'.
1261+
(interactive (list (gerrit--select-change-from-matching-changes
1262+
gerrit-interesting-open-changes-filter)))
1263+
1264+
;; 1) get change metadata
1265+
;; 2) determine workspace directory (based on branch and projectname)
1266+
;; 3) switch to workspace
1267+
;; 4) download change
1268+
;; 5) display commit
1269+
(let* ((change-metadata (car (gerrit-rest-change-query changenr)))
1270+
(project-name (alist-get 'project change-metadata))
1271+
(branch (alist-get 'branch change-metadata))
1272+
(workspace-directory (or (cdr (assoc (cons project-name branch)
1273+
gerrit-project-to-local-workspace-alist))
1274+
;; TODO completion + write them to file
1275+
(read-directory-name
1276+
(format "Enter directory for project %s" project-name)))))
1277+
(let ((default-directory workspace-directory))
1278+
(message "changeinfo: name: %s, branch: %s -> workspace: %s"
1279+
project-name branch workspace-directory)
1280+
(gerrit-download changenr) ;; this is async
1281+
;; TODO run magit-show-commit
1282+
;; only when the above change is downloaded to avoid window-flickering
1283+
(magit-show-commit "HEAD"))))
1284+
12181285
(defun gerrit-upload ()
12191286
(interactive)
12201287
(if gerrit-use-gitreview-interface

0 commit comments

Comments
 (0)