vc-git.el 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. ;;; vc-git.el --- VC backend for the git version control system
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Alexandre Julliard <julliard@winehq.org>
  4. ;; Keywords: vc tools
  5. ;; Package: vc
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file contains a VC backend for the git version control
  19. ;; system.
  20. ;;
  21. ;;; Installation:
  22. ;; To install: put this file on the load-path and add Git to the list
  23. ;; of supported backends in `vc-handled-backends'; the following line,
  24. ;; placed in your ~/.emacs, will accomplish this:
  25. ;;
  26. ;; (add-to-list 'vc-handled-backends 'Git)
  27. ;;; Todo:
  28. ;; - check if more functions could use vc-git-command instead
  29. ;; of start-process.
  30. ;; - changelog generation
  31. ;; Implement the rest of the vc interface. See the comment at the
  32. ;; beginning of vc.el. The current status is:
  33. ;; ("??" means: "figure out what to do about it")
  34. ;;
  35. ;; FUNCTION NAME STATUS
  36. ;; BACKEND PROPERTIES
  37. ;; * revision-granularity OK
  38. ;; STATE-QUERYING FUNCTIONS
  39. ;; * registered (file) OK
  40. ;; * state (file) OK
  41. ;; - state-heuristic (file) NOT NEEDED
  42. ;; * working-revision (file) OK
  43. ;; - latest-on-branch-p (file) NOT NEEDED
  44. ;; * checkout-model (files) OK
  45. ;; - workfile-unchanged-p (file) OK
  46. ;; - mode-line-string (file) OK
  47. ;; STATE-CHANGING FUNCTIONS
  48. ;; * create-repo () OK
  49. ;; * register (files &optional rev comment) OK
  50. ;; - init-revision (file) NOT NEEDED
  51. ;; - responsible-p (file) OK
  52. ;; - could-register (file) NOT NEEDED, DEFAULT IS GOOD
  53. ;; - receive-file (file rev) NOT NEEDED
  54. ;; - unregister (file) OK
  55. ;; * checkin (files rev comment) OK
  56. ;; * find-revision (file rev buffer) OK
  57. ;; * checkout (file &optional editable rev) OK
  58. ;; * revert (file &optional contents-done) OK
  59. ;; - rollback (files) COULD BE SUPPORTED
  60. ;; - merge (file rev1 rev2) It would be possible to merge
  61. ;; changes into a single file, but
  62. ;; when committing they wouldn't
  63. ;; be identified as a merge
  64. ;; by git, so it's probably
  65. ;; not a good idea.
  66. ;; - merge-news (file) see `merge'
  67. ;; - steal-lock (file &optional revision) NOT NEEDED
  68. ;; HISTORY FUNCTIONS
  69. ;; * print-log (files buffer &optional shortlog start-revision limit) OK
  70. ;; - log-view-mode () OK
  71. ;; - show-log-entry (revision) OK
  72. ;; - comment-history (file) ??
  73. ;; - update-changelog (files) COULD BE SUPPORTED
  74. ;; * diff (file &optional rev1 rev2 buffer) OK
  75. ;; - revision-completion-table (files) OK
  76. ;; - annotate-command (file buf &optional rev) OK
  77. ;; - annotate-time () OK
  78. ;; - annotate-current-time () NOT NEEDED
  79. ;; - annotate-extract-revision-at-line () OK
  80. ;; TAG SYSTEM
  81. ;; - create-tag (dir name branchp) OK
  82. ;; - retrieve-tag (dir name update) OK
  83. ;; MISCELLANEOUS
  84. ;; - make-version-backups-p (file) NOT NEEDED
  85. ;; - repository-hostname (dirname) NOT NEEDED
  86. ;; - previous-revision (file rev) OK
  87. ;; - next-revision (file rev) OK
  88. ;; - check-headers () COULD BE SUPPORTED
  89. ;; - clear-headers () NOT NEEDED
  90. ;; - delete-file (file) OK
  91. ;; - rename-file (old new) OK
  92. ;; - find-file-hook () NOT NEEDED
  93. (eval-when-compile
  94. (require 'cl)
  95. (require 'vc)
  96. (require 'vc-dir)
  97. (require 'grep))
  98. (defgroup vc-git nil
  99. "VC Git backend."
  100. :version "24.1"
  101. :group 'vc)
  102. (defcustom vc-git-diff-switches t
  103. "String or list of strings specifying switches for Git diff under VC.
  104. If nil, use the value of `vc-diff-switches'. If t, use no switches."
  105. :type '(choice (const :tag "Unspecified" nil)
  106. (const :tag "None" t)
  107. (string :tag "Argument String")
  108. (repeat :tag "Argument List" :value ("") string))
  109. :version "23.1"
  110. :group 'vc-git)
  111. (defcustom vc-git-program "git"
  112. "Name of the Git executable (excluding any arguments)."
  113. :version "24.1"
  114. :type 'string
  115. :group 'vc-git)
  116. (defcustom vc-git-root-log-format
  117. '("%d%h..: %an %ad %s"
  118. ;; The first shy group matches the characters drawn by --graph.
  119. ;; We use numbered groups because `log-view-message-re' wants the
  120. ;; revision number to be group 1.
  121. "^\\(?:[*/\\| ]+ \\)?\\(?2: ([^)]+)\\)?\\(?1:[0-9a-z]+\\)..: \
  122. \\(?3:.*?\\)[ \t]+\\(?4:[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)"
  123. ((1 'log-view-message-face)
  124. (2 'change-log-list nil lax)
  125. (3 'change-log-name)
  126. (4 'change-log-date)))
  127. "Git log format for `vc-print-root-log'.
  128. This should be a list (FORMAT REGEXP KEYWORDS), where FORMAT is a
  129. format string (which is passed to \"git log\" via the argument
  130. \"--pretty=tformat:FORMAT\"), REGEXP is a regular expression
  131. matching the resulting Git log output, and KEYWORDS is a list of
  132. `font-lock-keywords' for highlighting the Log View buffer."
  133. :type '(list string string (repeat sexp))
  134. :group 'vc-git
  135. :version "24.1")
  136. (defvar vc-git-commits-coding-system 'utf-8
  137. "Default coding system for git commits.")
  138. ;; History of Git commands.
  139. (defvar vc-git-history nil)
  140. ;;; BACKEND PROPERTIES
  141. (defun vc-git-revision-granularity () 'repository)
  142. (defun vc-git-checkout-model (files) 'implicit)
  143. ;;; STATE-QUERYING FUNCTIONS
  144. ;;;###autoload (defun vc-git-registered (file)
  145. ;;;###autoload "Return non-nil if FILE is registered with git."
  146. ;;;###autoload (if (vc-find-root file ".git") ; Short cut.
  147. ;;;###autoload (progn
  148. ;;;###autoload (load "vc-git")
  149. ;;;###autoload (vc-git-registered file))))
  150. (defun vc-git-registered (file)
  151. "Check whether FILE is registered with git."
  152. (let ((dir (vc-git-root file)))
  153. (when dir
  154. (with-temp-buffer
  155. (let* (process-file-side-effects
  156. ;; Do not use the `file-name-directory' here: git-ls-files
  157. ;; sometimes fails to return the correct status for relative
  158. ;; path specs.
  159. ;; See also: http://marc.info/?l=git&m=125787684318129&w=2
  160. (name (file-relative-name file dir))
  161. (str (ignore-errors
  162. (cd dir)
  163. (vc-git--out-ok "ls-files" "-c" "-z" "--" name)
  164. ;; If result is empty, use ls-tree to check for deleted
  165. ;; file.
  166. (when (eq (point-min) (point-max))
  167. (vc-git--out-ok "ls-tree" "--name-only" "-z" "HEAD"
  168. "--" name))
  169. (buffer-string))))
  170. (and str
  171. (> (length str) (length name))
  172. (string= (substring str 0 (1+ (length name)))
  173. (concat name "\0"))))))))
  174. (defun vc-git--state-code (code)
  175. "Convert from a string to a added/deleted/modified state."
  176. (case (string-to-char code)
  177. (?M 'edited)
  178. (?A 'added)
  179. (?D 'removed)
  180. (?U 'edited) ;; FIXME
  181. (?T 'edited))) ;; FIXME
  182. (defun vc-git-state (file)
  183. "Git-specific version of `vc-state'."
  184. ;; FIXME: This can't set 'ignored or 'conflict yet
  185. ;; The 'ignored state could be detected with `git ls-files -i -o
  186. ;; --exclude-standard` It also can't set 'needs-update or
  187. ;; 'needs-merge. The rough equivalent would be that upstream branch
  188. ;; for current branch is in fast-forward state i.e. current branch
  189. ;; is direct ancestor of corresponding upstream branch, and the file
  190. ;; was modified upstream. But we can't check that without a network
  191. ;; operation.
  192. (if (not (vc-git-registered file))
  193. 'unregistered
  194. (let ((diff (vc-git--run-command-string
  195. file "diff-index" "-p" "--raw" "-z" "HEAD" "--")))
  196. (if (and diff
  197. (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\([ADMUT]\\)\0[^\0]+\0\\(.*\n.\\)?"
  198. diff))
  199. (let ((diff-letter (match-string 1 diff)))
  200. (if (not (match-beginning 2))
  201. ;; Empty diff: file contents is the same as the HEAD
  202. ;; revision, but timestamps are different (eg, file
  203. ;; was "touch"ed). Update timestamp in index:
  204. (prog1 'up-to-date
  205. (vc-git--call nil "add" "--refresh" "--"
  206. (file-relative-name file)))
  207. (vc-git--state-code diff-letter)))
  208. (if (vc-git--empty-db-p) 'added 'up-to-date)))))
  209. (defun vc-git-working-revision (file)
  210. "Git-specific version of `vc-working-revision'."
  211. (let* (process-file-side-effects
  212. (str (with-output-to-string
  213. (with-current-buffer standard-output
  214. (vc-git--out-ok "symbolic-ref" "HEAD")))))
  215. (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
  216. (match-string 2 str)
  217. str)))
  218. (defun vc-git-workfile-unchanged-p (file)
  219. (eq 'up-to-date (vc-git-state file)))
  220. (defun vc-git-mode-line-string (file)
  221. "Return string for placement into the modeline for FILE."
  222. (let* ((branch (vc-git-working-revision file))
  223. (def-ml (vc-default-mode-line-string 'Git file))
  224. (help-echo (get-text-property 0 'help-echo def-ml)))
  225. (if (zerop (length branch))
  226. (propertize
  227. (concat def-ml "!")
  228. 'help-echo (concat help-echo "\nNo current branch (detached HEAD)"))
  229. (propertize def-ml
  230. 'help-echo (concat help-echo "\nCurrent branch: " branch)))))
  231. (defstruct (vc-git-extra-fileinfo
  232. (:copier nil)
  233. (:constructor vc-git-create-extra-fileinfo
  234. (old-perm new-perm &optional rename-state orig-name))
  235. (:conc-name vc-git-extra-fileinfo->))
  236. old-perm new-perm ;; Permission flags.
  237. rename-state ;; Rename or copy state.
  238. orig-name) ;; Original name for renames or copies.
  239. (defun vc-git-escape-file-name (name)
  240. "Escape a file name if necessary."
  241. (if (string-match "[\n\t\"\\]" name)
  242. (concat "\""
  243. (mapconcat (lambda (c)
  244. (case c
  245. (?\n "\\n")
  246. (?\t "\\t")
  247. (?\\ "\\\\")
  248. (?\" "\\\"")
  249. (t (char-to-string c))))
  250. name "")
  251. "\"")
  252. name))
  253. (defun vc-git-file-type-as-string (old-perm new-perm)
  254. "Return a string describing the file type based on its permissions."
  255. (let* ((old-type (lsh (or old-perm 0) -9))
  256. (new-type (lsh (or new-perm 0) -9))
  257. (str (case new-type
  258. (?\100 ;; File.
  259. (case old-type
  260. (?\100 nil)
  261. (?\120 " (type change symlink -> file)")
  262. (?\160 " (type change subproject -> file)")))
  263. (?\120 ;; Symlink.
  264. (case old-type
  265. (?\100 " (type change file -> symlink)")
  266. (?\160 " (type change subproject -> symlink)")
  267. (t " (symlink)")))
  268. (?\160 ;; Subproject.
  269. (case old-type
  270. (?\100 " (type change file -> subproject)")
  271. (?\120 " (type change symlink -> subproject)")
  272. (t " (subproject)")))
  273. (?\110 nil) ;; Directory (internal, not a real git state).
  274. (?\000 ;; Deleted or unknown.
  275. (case old-type
  276. (?\120 " (symlink)")
  277. (?\160 " (subproject)")))
  278. (t (format " (unknown type %o)" new-type)))))
  279. (cond (str (propertize str 'face 'font-lock-comment-face))
  280. ((eq new-type ?\110) "/")
  281. (t ""))))
  282. (defun vc-git-rename-as-string (state extra)
  283. "Return a string describing the copy or rename associated with INFO,
  284. or an empty string if none."
  285. (let ((rename-state (when extra
  286. (vc-git-extra-fileinfo->rename-state extra))))
  287. (if rename-state
  288. (propertize
  289. (concat " ("
  290. (if (eq rename-state 'copy) "copied from "
  291. (if (eq state 'added) "renamed from "
  292. "renamed to "))
  293. (vc-git-escape-file-name
  294. (vc-git-extra-fileinfo->orig-name extra))
  295. ")")
  296. 'face 'font-lock-comment-face)
  297. "")))
  298. (defun vc-git-permissions-as-string (old-perm new-perm)
  299. "Format a permission change as string."
  300. (propertize
  301. (if (or (not old-perm)
  302. (not new-perm)
  303. (eq 0 (logand ?\111 (logxor old-perm new-perm))))
  304. " "
  305. (if (eq 0 (logand ?\111 old-perm)) "+x" "-x"))
  306. 'face 'font-lock-type-face))
  307. (defun vc-git-dir-printer (info)
  308. "Pretty-printer for the vc-dir-fileinfo structure."
  309. (let* ((isdir (vc-dir-fileinfo->directory info))
  310. (state (if isdir "" (vc-dir-fileinfo->state info)))
  311. (extra (vc-dir-fileinfo->extra info))
  312. (old-perm (when extra (vc-git-extra-fileinfo->old-perm extra)))
  313. (new-perm (when extra (vc-git-extra-fileinfo->new-perm extra))))
  314. (insert
  315. " "
  316. (propertize (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
  317. 'face 'font-lock-type-face)
  318. " "
  319. (propertize
  320. (format "%-12s" state)
  321. 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
  322. ((eq state 'missing) 'font-lock-warning-face)
  323. (t 'font-lock-variable-name-face))
  324. 'mouse-face 'highlight)
  325. " " (vc-git-permissions-as-string old-perm new-perm)
  326. " "
  327. (propertize (vc-git-escape-file-name (vc-dir-fileinfo->name info))
  328. 'face (if isdir 'font-lock-comment-delimiter-face
  329. 'font-lock-function-name-face)
  330. 'help-echo
  331. (if isdir
  332. "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
  333. "File\nmouse-3: Pop-up menu")
  334. 'keymap vc-dir-filename-mouse-map
  335. 'mouse-face 'highlight)
  336. (vc-git-file-type-as-string old-perm new-perm)
  337. (vc-git-rename-as-string state extra))))
  338. (defun vc-git-after-dir-status-stage (stage files update-function)
  339. "Process sentinel for the various dir-status stages."
  340. (let (next-stage result)
  341. (goto-char (point-min))
  342. (case stage
  343. (update-index
  344. (setq next-stage (if (vc-git--empty-db-p) 'ls-files-added
  345. (if files 'ls-files-up-to-date 'diff-index))))
  346. (ls-files-added
  347. (setq next-stage 'ls-files-unknown)
  348. (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
  349. (let ((new-perm (string-to-number (match-string 1) 8))
  350. (name (match-string 2)))
  351. (push (list name 'added (vc-git-create-extra-fileinfo 0 new-perm))
  352. result))))
  353. (ls-files-up-to-date
  354. (setq next-stage 'diff-index)
  355. (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
  356. (let ((perm (string-to-number (match-string 1) 8))
  357. (name (match-string 2)))
  358. (push (list name 'up-to-date
  359. (vc-git-create-extra-fileinfo perm perm))
  360. result))))
  361. (ls-files-unknown
  362. (when files (setq next-stage 'ls-files-ignored))
  363. (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
  364. (push (list (match-string 1) 'unregistered
  365. (vc-git-create-extra-fileinfo 0 0))
  366. result)))
  367. (ls-files-ignored
  368. (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
  369. (push (list (match-string 1) 'ignored
  370. (vc-git-create-extra-fileinfo 0 0))
  371. result)))
  372. (diff-index
  373. (setq next-stage 'ls-files-unknown)
  374. (while (re-search-forward
  375. ":\\([0-7]\\{6\\}\\) \\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\(\\([ADMUT]\\)\0\\([^\0]+\\)\\|\\([CR]\\)[0-9]*\0\\([^\0]+\\)\0\\([^\0]+\\)\\)\0"
  376. nil t 1)
  377. (let ((old-perm (string-to-number (match-string 1) 8))
  378. (new-perm (string-to-number (match-string 2) 8))
  379. (state (or (match-string 4) (match-string 6)))
  380. (name (or (match-string 5) (match-string 7)))
  381. (new-name (match-string 8)))
  382. (if new-name ; Copy or rename.
  383. (if (eq ?C (string-to-char state))
  384. (push (list new-name 'added
  385. (vc-git-create-extra-fileinfo old-perm new-perm
  386. 'copy name))
  387. result)
  388. (push (list name 'removed
  389. (vc-git-create-extra-fileinfo 0 0
  390. 'rename new-name))
  391. result)
  392. (push (list new-name 'added
  393. (vc-git-create-extra-fileinfo old-perm new-perm
  394. 'rename name))
  395. result))
  396. (push (list name (vc-git--state-code state)
  397. (vc-git-create-extra-fileinfo old-perm new-perm))
  398. result))))))
  399. (when result
  400. (setq result (nreverse result))
  401. (when files
  402. (dolist (entry result) (setq files (delete (car entry) files)))
  403. (unless files (setq next-stage nil))))
  404. (when (or result (not next-stage))
  405. (funcall update-function result next-stage))
  406. (when next-stage
  407. (vc-git-dir-status-goto-stage next-stage files update-function))))
  408. (defun vc-git-dir-status-goto-stage (stage files update-function)
  409. (erase-buffer)
  410. (case stage
  411. (update-index
  412. (if files
  413. (vc-git-command (current-buffer) 'async files "add" "--refresh" "--")
  414. (vc-git-command (current-buffer) 'async nil
  415. "update-index" "--refresh")))
  416. (ls-files-added
  417. (vc-git-command (current-buffer) 'async files
  418. "ls-files" "-z" "-c" "-s" "--"))
  419. (ls-files-up-to-date
  420. (vc-git-command (current-buffer) 'async files
  421. "ls-files" "-z" "-c" "-s" "--"))
  422. (ls-files-unknown
  423. (vc-git-command (current-buffer) 'async files
  424. "ls-files" "-z" "-o" "--directory"
  425. "--no-empty-directory" "--exclude-standard" "--"))
  426. (ls-files-ignored
  427. (vc-git-command (current-buffer) 'async files
  428. "ls-files" "-z" "-o" "-i" "--directory"
  429. "--no-empty-directory" "--exclude-standard" "--"))
  430. ;; --relative added in Git 1.5.5.
  431. (diff-index
  432. (vc-git-command (current-buffer) 'async files
  433. "diff-index" "--relative" "-z" "-M" "HEAD" "--")))
  434. (vc-exec-after
  435. `(vc-git-after-dir-status-stage ',stage ',files ',update-function)))
  436. (defun vc-git-dir-status (dir update-function)
  437. "Return a list of (FILE STATE EXTRA) entries for DIR."
  438. ;; Further things that would have to be fixed later:
  439. ;; - how to handle unregistered directories
  440. ;; - how to support vc-dir on a subdir of the project tree
  441. (vc-git-dir-status-goto-stage 'update-index nil update-function))
  442. (defun vc-git-dir-status-files (dir files default-state update-function)
  443. "Return a list of (FILE STATE EXTRA) entries for FILES in DIR."
  444. (vc-git-dir-status-goto-stage 'update-index files update-function))
  445. (defvar vc-git-stash-map
  446. (let ((map (make-sparse-keymap)))
  447. ;; Turn off vc-dir marking
  448. (define-key map [mouse-2] 'ignore)
  449. (define-key map [down-mouse-3] 'vc-git-stash-menu)
  450. (define-key map "\C-k" 'vc-git-stash-delete-at-point)
  451. (define-key map "=" 'vc-git-stash-show-at-point)
  452. (define-key map "\C-m" 'vc-git-stash-show-at-point)
  453. (define-key map "A" 'vc-git-stash-apply-at-point)
  454. (define-key map "P" 'vc-git-stash-pop-at-point)
  455. (define-key map "S" 'vc-git-stash-snapshot)
  456. map))
  457. (defvar vc-git-stash-menu-map
  458. (let ((map (make-sparse-keymap "Git Stash")))
  459. (define-key map [de]
  460. '(menu-item "Delete Stash" vc-git-stash-delete-at-point
  461. :help "Delete the current stash"))
  462. (define-key map [ap]
  463. '(menu-item "Apply Stash" vc-git-stash-apply-at-point
  464. :help "Apply the current stash and keep it in the stash list"))
  465. (define-key map [po]
  466. '(menu-item "Apply and Remove Stash (Pop)" vc-git-stash-pop-at-point
  467. :help "Apply the current stash and remove it"))
  468. (define-key map [sh]
  469. '(menu-item "Show Stash" vc-git-stash-show-at-point
  470. :help "Show the contents of the current stash"))
  471. map))
  472. (defun vc-git-dir-extra-headers (dir)
  473. (let ((str (with-output-to-string
  474. (with-current-buffer standard-output
  475. (vc-git--out-ok "symbolic-ref" "HEAD"))))
  476. (stash (vc-git-stash-list))
  477. (stash-help-echo "Use M-x vc-git-stash to create stashes.")
  478. branch remote remote-url)
  479. (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
  480. (progn
  481. (setq branch (match-string 2 str))
  482. (setq remote
  483. (with-output-to-string
  484. (with-current-buffer standard-output
  485. (vc-git--out-ok "config"
  486. (concat "branch." branch ".remote")))))
  487. (when (string-match "\\([^\n]+\\)" remote)
  488. (setq remote (match-string 1 remote)))
  489. (when remote
  490. (setq remote-url
  491. (with-output-to-string
  492. (with-current-buffer standard-output
  493. (vc-git--out-ok "config"
  494. (concat "remote." remote ".url"))))))
  495. (when (string-match "\\([^\n]+\\)" remote-url)
  496. (setq remote-url (match-string 1 remote-url))))
  497. (setq branch "not (detached HEAD)"))
  498. ;; FIXME: maybe use a different face when nothing is stashed.
  499. (concat
  500. (propertize "Branch : " 'face 'font-lock-type-face)
  501. (propertize branch
  502. 'face 'font-lock-variable-name-face)
  503. (when remote
  504. (concat
  505. "\n"
  506. (propertize "Remote : " 'face 'font-lock-type-face)
  507. (propertize remote-url
  508. 'face 'font-lock-variable-name-face)))
  509. "\n"
  510. (if stash
  511. (concat
  512. (propertize "Stash :\n" 'face 'font-lock-type-face
  513. 'help-echo stash-help-echo)
  514. (mapconcat
  515. (lambda (x)
  516. (propertize x
  517. 'face 'font-lock-variable-name-face
  518. 'mouse-face 'highlight
  519. 'help-echo "mouse-3: Show stash menu\nRET: Show stash\nA: Apply stash\nP: Apply and remove stash (pop)\nC-k: Delete stash"
  520. 'keymap vc-git-stash-map))
  521. stash "\n"))
  522. (concat
  523. (propertize "Stash : " 'face 'font-lock-type-face
  524. 'help-echo stash-help-echo)
  525. (propertize "Nothing stashed"
  526. 'help-echo stash-help-echo
  527. 'face 'font-lock-variable-name-face))))))
  528. (defun vc-git-branches ()
  529. "Return the existing branches, as a list of strings.
  530. The car of the list is the current branch."
  531. (with-temp-buffer
  532. (call-process vc-git-program nil t nil "branch")
  533. (goto-char (point-min))
  534. (let (current-branch branches)
  535. (while (not (eobp))
  536. (when (looking-at "^\\([ *]\\) \\(.+\\)$")
  537. (if (string-equal (match-string 1) "*")
  538. (setq current-branch (match-string 2))
  539. (push (match-string 2) branches)))
  540. (forward-line 1))
  541. (cons current-branch (nreverse branches)))))
  542. ;;; STATE-CHANGING FUNCTIONS
  543. (defun vc-git-create-repo ()
  544. "Create a new Git repository."
  545. (vc-git-command nil 0 nil "init"))
  546. (defun vc-git-register (files &optional rev comment)
  547. "Register FILES into the git version-control system."
  548. (let (flist dlist)
  549. (dolist (crt files)
  550. (if (file-directory-p crt)
  551. (push crt dlist)
  552. (push crt flist)))
  553. (when flist
  554. (vc-git-command nil 0 flist "update-index" "--add" "--"))
  555. (when dlist
  556. (vc-git-command nil 0 dlist "add"))))
  557. (defalias 'vc-git-responsible-p 'vc-git-root)
  558. (defun vc-git-unregister (file)
  559. (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
  560. (declare-function log-edit-extract-headers "log-edit" (headers string))
  561. (defun vc-git-checkin (files rev comment)
  562. (let ((coding-system-for-write vc-git-commits-coding-system))
  563. (apply 'vc-git-command nil 0 files
  564. (nconc (list "commit" "-m")
  565. (log-edit-extract-headers '(("Author" . "--author")
  566. ("Date" . "--date"))
  567. comment)
  568. (list "--only" "--")))))
  569. (defun vc-git-find-revision (file rev buffer)
  570. (let* (process-file-side-effects
  571. (coding-system-for-read 'binary)
  572. (coding-system-for-write 'binary)
  573. (fullname
  574. (let ((fn (vc-git--run-command-string
  575. file "ls-files" "-z" "--full-name" "--")))
  576. ;; ls-files does not return anything when looking for a
  577. ;; revision of a file that has been renamed or removed.
  578. (if (string= fn "")
  579. (file-relative-name file (vc-git-root default-directory))
  580. (substring fn 0 -1)))))
  581. (vc-git-command
  582. buffer 0
  583. nil
  584. "cat-file" "blob" (concat (if rev rev "HEAD") ":" fullname))))
  585. (defun vc-git-checkout (file &optional editable rev)
  586. (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
  587. (defun vc-git-revert (file &optional contents-done)
  588. "Revert FILE to the version stored in the git repository."
  589. (if contents-done
  590. (vc-git-command nil 0 file "update-index" "--")
  591. (vc-git-command nil 0 file "reset" "-q" "--")
  592. (vc-git-command nil nil file "checkout" "-q" "--")))
  593. (defun vc-git-pull (prompt)
  594. "Pull changes into the current Git branch.
  595. Normally, this runs \"git pull\". If PROMPT is non-nil, prompt
  596. for the Git command to run."
  597. (let* ((root (vc-git-root default-directory))
  598. (buffer (format "*vc-git : %s*" (expand-file-name root)))
  599. (command "pull")
  600. (git-program vc-git-program)
  601. args)
  602. ;; If necessary, prompt for the exact command.
  603. (when prompt
  604. (setq args (split-string
  605. (read-shell-command "Git pull command: "
  606. (format "%s pull" git-program)
  607. 'vc-git-history)
  608. " " t))
  609. (setq git-program (car args)
  610. command (cadr args)
  611. args (cddr args)))
  612. (apply 'vc-do-async-command buffer root git-program command args)
  613. (vc-set-async-update buffer)))
  614. (defun vc-git-merge-branch ()
  615. "Merge changes into the current Git branch.
  616. This prompts for a branch to merge from."
  617. (let* ((root (vc-git-root default-directory))
  618. (buffer (format "*vc-git : %s*" (expand-file-name root)))
  619. (branches (cdr (vc-git-branches)))
  620. (merge-source
  621. (completing-read "Merge from branch: "
  622. (if (or (member "FETCH_HEAD" branches)
  623. (not (file-readable-p
  624. (expand-file-name ".git/FETCH_HEAD"
  625. root))))
  626. branches
  627. (cons "FETCH_HEAD" branches))
  628. nil t)))
  629. (apply 'vc-do-async-command buffer root vc-git-program "merge"
  630. (list merge-source))
  631. (vc-set-async-update buffer)))
  632. ;;; HISTORY FUNCTIONS
  633. (defun vc-git-print-log (files buffer &optional shortlog start-revision limit)
  634. "Get change log associated with FILES.
  635. Note that using SHORTLOG requires at least Git version 1.5.6,
  636. for the --graph option."
  637. (let ((coding-system-for-read vc-git-commits-coding-system))
  638. ;; `vc-do-command' creates the buffer, but we need it before running
  639. ;; the command.
  640. (vc-setup-buffer buffer)
  641. ;; If the buffer exists from a previous invocation it might be
  642. ;; read-only.
  643. (let ((inhibit-read-only t))
  644. (with-current-buffer
  645. buffer
  646. (apply 'vc-git-command buffer
  647. 'async files
  648. (append
  649. '("log" "--no-color")
  650. (when shortlog
  651. `("--graph" "--decorate" "--date=short"
  652. ,(format "--pretty=tformat:%s"
  653. (car vc-git-root-log-format))
  654. "--abbrev-commit"))
  655. (when limit (list "-n" (format "%s" limit)))
  656. (when start-revision (list start-revision))
  657. '("--")))))))
  658. (defun vc-git-log-outgoing (buffer remote-location)
  659. (interactive)
  660. (vc-git-command
  661. buffer 0 nil
  662. "log"
  663. "--no-color" "--graph" "--decorate" "--date=short"
  664. (format "--pretty=tformat:%s" (car vc-git-root-log-format))
  665. "--abbrev-commit"
  666. (concat (if (string= remote-location "")
  667. "@{upstream}"
  668. remote-location)
  669. "..HEAD")))
  670. (defun vc-git-log-incoming (buffer remote-location)
  671. (interactive)
  672. (vc-git-command nil 0 nil "fetch")
  673. (vc-git-command
  674. buffer 0 nil
  675. "log"
  676. "--no-color" "--graph" "--decorate" "--date=short"
  677. (format "--pretty=tformat:%s" (car vc-git-root-log-format))
  678. "--abbrev-commit"
  679. (concat "HEAD.." (if (string= remote-location "")
  680. "@{upstream}"
  681. remote-location))))
  682. (defvar log-view-message-re)
  683. (defvar log-view-file-re)
  684. (defvar log-view-font-lock-keywords)
  685. (defvar log-view-per-file-logs)
  686. (defvar log-view-expanded-log-entry-function)
  687. (define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View"
  688. (require 'add-log) ;; We need the faces add-log.
  689. ;; Don't have file markers, so use impossible regexp.
  690. (set (make-local-variable 'log-view-file-re) "\\`a\\`")
  691. (set (make-local-variable 'log-view-per-file-logs) nil)
  692. (set (make-local-variable 'log-view-message-re)
  693. (if (not (eq vc-log-view-type 'long))
  694. (cadr vc-git-root-log-format)
  695. "^commit *\\([0-9a-z]+\\)"))
  696. ;; Allow expanding short log entries
  697. (when (eq vc-log-view-type 'short)
  698. (setq truncate-lines t)
  699. (set (make-local-variable 'log-view-expanded-log-entry-function)
  700. 'vc-git-expanded-log-entry))
  701. (set (make-local-variable 'log-view-font-lock-keywords)
  702. (if (not (eq vc-log-view-type 'long))
  703. (list (cons (nth 1 vc-git-root-log-format)
  704. (nth 2 vc-git-root-log-format)))
  705. (append
  706. `((,log-view-message-re (1 'change-log-acknowledgement)))
  707. ;; Handle the case:
  708. ;; user: foo@bar
  709. '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
  710. (1 'change-log-email))
  711. ;; Handle the case:
  712. ;; user: FirstName LastName <foo@bar>
  713. ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
  714. (1 'change-log-name)
  715. (2 'change-log-email))
  716. ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
  717. (1 'change-log-name))
  718. ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
  719. (1 'change-log-name)
  720. (2 'change-log-email))
  721. ("^Merge: \\([0-9a-z]+\\) \\([0-9a-z]+\\)"
  722. (1 'change-log-acknowledgement)
  723. (2 'change-log-acknowledgement))
  724. ("^Date: \\(.+\\)" (1 'change-log-date))
  725. ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
  726. (defun vc-git-show-log-entry (revision)
  727. "Move to the log entry for REVISION.
  728. REVISION may have the form BRANCH, BRANCH~N,
  729. or BRANCH^ (where \"^\" can be repeated)."
  730. (goto-char (point-min))
  731. (prog1
  732. (when revision
  733. (search-forward
  734. (format "\ncommit %s" revision) nil t
  735. (cond ((string-match "~\\([0-9]\\)\\'" revision)
  736. (1+ (string-to-number (match-string 1 revision))))
  737. ((string-match "\\^+\\'" revision)
  738. (1+ (length (match-string 0 revision))))
  739. (t nil))))
  740. (beginning-of-line)))
  741. (defun vc-git-expanded-log-entry (revision)
  742. (with-temp-buffer
  743. (apply 'vc-git-command t nil nil (list "log" revision "-1"))
  744. (goto-char (point-min))
  745. (unless (eobp)
  746. ;; Indent the expanded log entry.
  747. (indent-region (point-min) (point-max) 2)
  748. (buffer-string))))
  749. (defun vc-git-diff (files &optional rev1 rev2 buffer)
  750. "Get a difference report using Git between two revisions of FILES."
  751. (let (process-file-side-effects)
  752. (apply #'vc-git-command (or buffer "*vc-diff*") 1 files
  753. (if (and rev1 rev2) "diff-tree" "diff-index")
  754. "--exit-code"
  755. (append (vc-switches 'git 'diff)
  756. (list "-p" (or rev1 "HEAD") rev2 "--")))))
  757. (defun vc-git-revision-table (files)
  758. ;; What about `files'?!? --Stef
  759. (let (process-file-side-effects
  760. (table (list "HEAD")))
  761. (with-temp-buffer
  762. (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
  763. (goto-char (point-min))
  764. (while (re-search-forward "^refs/\\(heads\\|tags\\|remotes\\)/\\(.*\\)$"
  765. nil t)
  766. (push (match-string 2) table)))
  767. table))
  768. (defun vc-git-revision-completion-table (files)
  769. (lexical-let ((files files)
  770. table)
  771. (setq table (lazy-completion-table
  772. table (lambda () (vc-git-revision-table files))))
  773. table))
  774. (defun vc-git-annotate-command (file buf &optional rev)
  775. (let ((name (file-relative-name file)))
  776. (vc-git-command buf 'async nil "blame" "--date=iso" "-C" "-C" rev "--" name)))
  777. (declare-function vc-annotate-convert-time "vc-annotate" (time))
  778. (defun vc-git-annotate-time ()
  779. (and (re-search-forward "[0-9a-f]+[^()]+(.* \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\) \\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) \\([-+0-9]+\\) +[0-9]+) " nil t)
  780. (vc-annotate-convert-time
  781. (apply #'encode-time (mapcar (lambda (match)
  782. (string-to-number (match-string match)))
  783. '(6 5 4 3 2 1 7))))))
  784. (defun vc-git-annotate-extract-revision-at-line ()
  785. (save-excursion
  786. (move-beginning-of-line 1)
  787. (when (looking-at "\\([0-9a-f^][0-9a-f]+\\) \\(\\([^(]+\\) \\)?")
  788. (let ((revision (match-string-no-properties 1)))
  789. (if (match-beginning 2)
  790. (let ((fname (match-string-no-properties 3)))
  791. ;; Remove trailing whitespace from the file name.
  792. (when (string-match " +\\'" fname)
  793. (setq fname (substring fname 0 (match-beginning 0))))
  794. (cons revision
  795. (expand-file-name fname (vc-git-root default-directory))))
  796. revision)))))
  797. ;;; TAG SYSTEM
  798. (defun vc-git-create-tag (dir name branchp)
  799. (let ((default-directory dir))
  800. (and (vc-git-command nil 0 nil "update-index" "--refresh")
  801. (if branchp
  802. (vc-git-command nil 0 nil "checkout" "-b" name)
  803. (vc-git-command nil 0 nil "tag" name)))))
  804. (defun vc-git-retrieve-tag (dir name update)
  805. (let ((default-directory dir))
  806. (vc-git-command nil 0 nil "checkout" name)
  807. ;; FIXME: update buffers if `update' is true
  808. ))
  809. ;;; MISCELLANEOUS
  810. (defun vc-git-previous-revision (file rev)
  811. "Git-specific version of `vc-previous-revision'."
  812. (if file
  813. (let* ((fname (file-relative-name file))
  814. (prev-rev (with-temp-buffer
  815. (and
  816. (vc-git--out-ok "rev-list" "-2" rev "--" fname)
  817. (goto-char (point-max))
  818. (bolp)
  819. (zerop (forward-line -1))
  820. (not (bobp))
  821. (buffer-substring-no-properties
  822. (point)
  823. (1- (point-max)))))))
  824. (or (vc-git-symbolic-commit prev-rev) prev-rev))
  825. (with-temp-buffer
  826. (and
  827. (vc-git--out-ok "rev-parse" (concat rev "^"))
  828. (buffer-substring-no-properties (point-min) (+ (point-min) 40))))))
  829. (defun vc-git-next-revision (file rev)
  830. "Git-specific version of `vc-next-revision'."
  831. (let* ((default-directory (file-name-directory
  832. (expand-file-name file)))
  833. (file (file-name-nondirectory file))
  834. (current-rev
  835. (with-temp-buffer
  836. (and
  837. (vc-git--out-ok "rev-list" "-1" rev "--" file)
  838. (goto-char (point-max))
  839. (bolp)
  840. (zerop (forward-line -1))
  841. (bobp)
  842. (buffer-substring-no-properties
  843. (point)
  844. (1- (point-max))))))
  845. (next-rev
  846. (and current-rev
  847. (with-temp-buffer
  848. (and
  849. (vc-git--out-ok "rev-list" "HEAD" "--" file)
  850. (goto-char (point-min))
  851. (search-forward current-rev nil t)
  852. (zerop (forward-line -1))
  853. (buffer-substring-no-properties
  854. (point)
  855. (progn (forward-line 1) (1- (point)))))))))
  856. (or (vc-git-symbolic-commit next-rev) next-rev)))
  857. (defun vc-git-delete-file (file)
  858. (vc-git-command nil 0 file "rm" "-f" "--"))
  859. (defun vc-git-rename-file (old new)
  860. (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
  861. (defvar vc-git-extra-menu-map
  862. (let ((map (make-sparse-keymap)))
  863. (define-key map [git-grep]
  864. '(menu-item "Git grep..." vc-git-grep
  865. :help "Run the `git grep' command"))
  866. (define-key map [git-sn]
  867. '(menu-item "Stash a Snapshot" vc-git-stash-snapshot
  868. :help "Stash the current state of the tree and keep the current state"))
  869. (define-key map [git-st]
  870. '(menu-item "Create Stash..." vc-git-stash
  871. :help "Stash away changes"))
  872. (define-key map [git-ss]
  873. '(menu-item "Show Stash..." vc-git-stash-show
  874. :help "Show stash contents"))
  875. map))
  876. (defun vc-git-extra-menu () vc-git-extra-menu-map)
  877. (defun vc-git-extra-status-menu () vc-git-extra-menu-map)
  878. (defun vc-git-root (file)
  879. (vc-find-root file ".git"))
  880. ;; Derived from `lgrep'.
  881. (defun vc-git-grep (regexp &optional files dir)
  882. "Run git grep, searching for REGEXP in FILES in directory DIR.
  883. The search is limited to file names matching shell pattern FILES.
  884. FILES may use abbreviations defined in `grep-files-aliases', e.g.
  885. entering `ch' is equivalent to `*.[ch]'.
  886. With \\[universal-argument] prefix, you can edit the constructed shell command line
  887. before it is executed.
  888. With two \\[universal-argument] prefixes, directly edit and run `grep-command'.
  889. Collect output in a buffer. While git grep runs asynchronously, you
  890. can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] \
  891. in the grep output buffer,
  892. to go to the lines where grep found matches.
  893. This command shares argument histories with \\[rgrep] and \\[grep]."
  894. (interactive
  895. (progn
  896. (grep-compute-defaults)
  897. (cond
  898. ((equal current-prefix-arg '(16))
  899. (list (read-from-minibuffer "Run: " "git grep"
  900. nil nil 'grep-history)
  901. nil))
  902. (t (let* ((regexp (grep-read-regexp))
  903. (files (grep-read-files regexp))
  904. (dir (read-directory-name "In directory: "
  905. nil default-directory t)))
  906. (list regexp files dir))))))
  907. (require 'grep)
  908. (when (and (stringp regexp) (> (length regexp) 0))
  909. (let ((command regexp))
  910. (if (null files)
  911. (if (string= command "git grep")
  912. (setq command nil))
  913. (setq dir (file-name-as-directory (expand-file-name dir)))
  914. (setq command
  915. (grep-expand-template "git grep -n -e <R> -- <F>"
  916. regexp files))
  917. (when command
  918. (if (equal current-prefix-arg '(4))
  919. (setq command
  920. (read-from-minibuffer "Confirm: "
  921. command nil nil 'grep-history))
  922. (add-to-history 'grep-history command))))
  923. (when command
  924. (let ((default-directory dir)
  925. (compilation-environment (cons "PAGER=" compilation-environment)))
  926. ;; Setting process-setup-function makes exit-message-function work
  927. ;; even when async processes aren't supported.
  928. (compilation-start command 'grep-mode))
  929. (if (eq next-error-last-buffer (current-buffer))
  930. (setq default-directory dir))))))
  931. (defun vc-git-stash (name)
  932. "Create a stash."
  933. (interactive "sStash name: ")
  934. (let ((root (vc-git-root default-directory)))
  935. (when root
  936. (vc-git--call nil "stash" "save" name)
  937. (vc-resynch-buffer root t t))))
  938. (defun vc-git-stash-show (name)
  939. "Show the contents of stash NAME."
  940. (interactive "sStash name: ")
  941. (vc-setup-buffer "*vc-git-stash*")
  942. (vc-git-command "*vc-git-stash*" 'async nil "stash" "show" "-p" name)
  943. (set-buffer "*vc-git-stash*")
  944. (diff-mode)
  945. (setq buffer-read-only t)
  946. (pop-to-buffer (current-buffer)))
  947. (defun vc-git-stash-apply (name)
  948. "Apply stash NAME."
  949. (interactive "sApply stash: ")
  950. (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" name)
  951. (vc-resynch-buffer (vc-git-root default-directory) t t))
  952. (defun vc-git-stash-pop (name)
  953. "Pop stash NAME."
  954. (interactive "sPop stash: ")
  955. (vc-git-command "*vc-git-stash*" 0 nil "stash" "pop" "-q" name)
  956. (vc-resynch-buffer (vc-git-root default-directory) t t))
  957. (defun vc-git-stash-snapshot ()
  958. "Create a stash with the current tree state."
  959. (interactive)
  960. (vc-git--call nil "stash" "save"
  961. (let ((ct (current-time)))
  962. (concat
  963. (format-time-string "Snapshot on %Y-%m-%d" ct)
  964. (format-time-string " at %H:%M" ct))))
  965. (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" "stash@{0}")
  966. (vc-resynch-buffer (vc-git-root default-directory) t t))
  967. (defun vc-git-stash-list ()
  968. (delete
  969. ""
  970. (split-string
  971. (replace-regexp-in-string
  972. "^stash@" " " (vc-git--run-command-string nil "stash" "list"))
  973. "\n")))
  974. (defun vc-git-stash-get-at-point (point)
  975. (save-excursion
  976. (goto-char point)
  977. (beginning-of-line)
  978. (if (looking-at "^ +\\({[0-9]+}\\):")
  979. (match-string 1)
  980. (error "Cannot find stash at point"))))
  981. (defun vc-git-stash-delete-at-point ()
  982. (interactive)
  983. (let ((stash (vc-git-stash-get-at-point (point))))
  984. (when (y-or-n-p (format "Remove stash %s ? " stash))
  985. (vc-git--run-command-string nil "stash" "drop" (format "stash@%s" stash))
  986. (vc-dir-refresh))))
  987. (defun vc-git-stash-show-at-point ()
  988. (interactive)
  989. (vc-git-stash-show (format "stash@%s" (vc-git-stash-get-at-point (point)))))
  990. (defun vc-git-stash-apply-at-point ()
  991. (interactive)
  992. (vc-git-stash-apply (format "stash@%s" (vc-git-stash-get-at-point (point)))))
  993. (defun vc-git-stash-pop-at-point ()
  994. (interactive)
  995. (vc-git-stash-pop (format "stash@%s" (vc-git-stash-get-at-point (point)))))
  996. (defun vc-git-stash-menu (e)
  997. (interactive "e")
  998. (vc-dir-at-event e (popup-menu vc-git-stash-menu-map e)))
  999. ;;; Internal commands
  1000. (defun vc-git-command (buffer okstatus file-or-list &rest flags)
  1001. "A wrapper around `vc-do-command' for use in vc-git.el.
  1002. The difference to vc-do-command is that this function always invokes
  1003. `vc-git-program'."
  1004. (apply 'vc-do-command (or buffer "*vc*") okstatus vc-git-program
  1005. file-or-list flags))
  1006. (defun vc-git--empty-db-p ()
  1007. "Check if the git db is empty (no commit done yet)."
  1008. (let (process-file-side-effects)
  1009. (not (eq 0 (vc-git--call nil "rev-parse" "--verify" "HEAD")))))
  1010. (defun vc-git--call (buffer command &rest args)
  1011. ;; We don't need to care the arguments. If there is a file name, it
  1012. ;; is always a relative one. This works also for remote
  1013. ;; directories. We enable `inhibit-null-byte-detection', otherwise
  1014. ;; Tramp's eol conversion might be confused.
  1015. (let ((inhibit-null-byte-detection t)
  1016. (process-environment (cons "PAGER=" process-environment)))
  1017. (apply 'process-file vc-git-program nil buffer nil command args)))
  1018. (defun vc-git--out-ok (command &rest args)
  1019. (zerop (apply 'vc-git--call '(t nil) command args)))
  1020. (defun vc-git--run-command-string (file &rest args)
  1021. "Run a git command on FILE and return its output as string.
  1022. FILE can be nil."
  1023. (let* ((ok t)
  1024. (str (with-output-to-string
  1025. (with-current-buffer standard-output
  1026. (unless (apply 'vc-git--out-ok
  1027. (if file
  1028. (append args (list (file-relative-name
  1029. file)))
  1030. args))
  1031. (setq ok nil))))))
  1032. (and ok str)))
  1033. (defun vc-git-symbolic-commit (commit)
  1034. "Translate COMMIT string into symbolic form.
  1035. Returns nil if not possible."
  1036. (and commit
  1037. (let ((name (with-temp-buffer
  1038. (and
  1039. (vc-git--out-ok "name-rev" "--name-only" commit)
  1040. (goto-char (point-min))
  1041. (= (forward-line 2) 1)
  1042. (bolp)
  1043. (buffer-substring-no-properties (point-min)
  1044. (1- (point-max)))))))
  1045. (and name (not (string= name "undefined")) name))))
  1046. (provide 'vc-git)
  1047. ;;; vc-git.el ends here