How to re-evaluate the shell environment and re-evaluate the header frame format before opening each buffer in emacs

I have included my git source + repo source name in the header of the emacs frame so that I can be sure which branch / repo I'm currently working with. I do this by setting this information in the bash shell through PROMPT_COMMANDand then getting the information when emacs starts up in init.el(or .emacs) using the command getenvto set the variable frame-title-format.

Here is what I do in ~/.bashrc:


PROMPT_COMMAND="export GIT_REPO_NAME=\$(git remote -v 2> /dev/null | grep \"origin.\*fetch\" | awk '{print \$2}' | sed 's,https://github.com/\(.\*\)/\(.\*\),\1,g'); export GIT_BRANCH=\$(git branch 2> /dev/null | grep \"^*\" | awk '{print \$2}')"

And here is what I do in ~/.emacs.d/init.el:


;;; Rest of init.el code
(setq-default
  frame-title-format
  (concat 
   "%f"
   (if (getenv "GIT_BRANCH")
       (concat " in [ " (getenv "GIT_BRANCH") "/" (getenv "GIT_REPO_NAME") " ]"))))

;;; All the other goodies in init.el

, , , , env, PROMPT_COMMAND pwd emacs. emacs , /, , .

, : - emacs git / , , git /?

:


   gitRepoA (branch X)
   |-> buffer1.txt
   |-> buffer2.txt
   gitRepoB (branch Y)
   |-> buffer1.txt
   |-> buffer2.txt

, emacs buffer1.txt () gitRepoA. buffer1.txt in branch X in repo gitRepoA

buffer2.txt gitRepoB ( branch Y) emacs, buffer2.txt in branch Y in repo gitRepoB. , , buffer2.txt in branch X in repo gitRepoA. .

, git status (, ..).

/ . , - . .

+4
1

frame-title, git repos

(defun my-get-frame-title ()
  "If inside a git repo return a string containing git repo, branch and file state else
return default frame title"
  (let* ((file (buffer-file-name))
         (git-directory (when file
                          (locate-dominating-file (file-truename default-directory) ".git"))))
    (if git-directory
        (concat (file-name-nondirectory (buffer-file-name))  
                " on " (vc-working-revision file) 
                " in " (file-name-nondirectory (directory-file-name git-directory))
                " current state is " (symbol-name (vc-state file)))
      (concat invocation-name "@" system-name))))

(setq-default
  frame-title-format
  '(:eval (my-get-frame-title)))

git repos <filename> on <branch> in <repo> current state is <state>, emacs

0