1

Specially when editing my init.org file, I usually want to push every single change to origin. So I want to write a function to do this, I am almost there but still need some help.

(defun cesco/automatic-push ()
    (magit-stage-modified)
    (magit-commit (concat "-m \"" COMMIT MESSAGE "\""  ))
    (magit-push MAGIT-CURRENT-BRANCH)
)

I want the commit message to be the current day/time and push the current branch to origin.

Update

I can successfully commit, but magit-push is running too fast so there is nothing commited yet. How can I make it wait for the commit to be done before attempting to push?

(defun cesco/push ()
    (interactive)
    (magit-stage-modified)
    (magit-commit (concat "-m \"" (format-time-string "%Y-%m-%dT%H:%M:%S")  "\""))
    (magit-push)
  )

Update 2

Code

(defun cesco/automatic-push ()
  (interactive)
  (magit-run-git "commit" "--all"
         (format-time-string "--message=%F %R"))
  (let ((current-branch (magit-get-current-branch)))
    (message "PUSHING!!!!")
    (magit-git-push current-branch
                    (concat "origin/" current-branch)
                    (list "--dry-run")
           )
    ))

1 try

  0 git … commit --all --message\=2017-05-08\ 01\:10
[master 03f269c8b701] 2017-05-08 01:10
 1 file changed, 3 insertions(+), 2 deletions(-)

  0 git … push -v --dry-run origin master\:refs/heads/master
Pushing to [email protected]:cescoferraro/dotfiles.git
To [email protected]:cescoferraro/dotfiles.git
   798f05653e9e..03f269c8b701  master -> master

Removing (list "--dry-run")

  0 git … commit --all --message\=2017-05-08\ 01\:11
2
  • 2
    emacswiki.org/emacs/InsertingTodaysDate here are some ways you can insert the date. I don't know about your other question.
    – wvxvw
    Commented May 8, 2017 at 3:14
  • @wvxvw thanks! Got the commit thing working now
    – CESCO
    Commented May 8, 2017 at 3:56

1 Answer 1

2

Here is an example that I think fits your description.

(defun cesco/automatic-push ()
  (interactive)
  (magit-run-git "commit" "--all"
                 (format-time-string "--message=%F %R"))
  (let ((current-branch (magit-get-current-branch)))
    (magit-git-push current-branch
                    (concat "origin/" current-branch)
                    (list "--dry-run"))))

I've removed the magit-stage-modified because you can do this with the --all flag to git commit. (Note that the push call has a "--dry-run" flag in there for testing.To actually push replace (list "--dry-run") with nil)

Instead of always pushing to origin, I'd recommend that you set the upstream branch for your local branch and then push there.

6
  • i have removed the (list "--dry-run") but still no push.
    – CESCO
    Commented May 8, 2017 at 4:00
  • What does the magit process buffer show ($)?
    – Kyle Meyer
    Commented May 8, 2017 at 4:01
  • i can see the pushing happen with the dry-run flag, otherwise it does not happen. I will update my question with the output
    – CESCO
    Commented May 8, 2017 at 4:12
  • Are you still using your commit code? Note that my commit call is different than yours, which is starting an editor to make the commit.
    – Kyle Meyer
    Commented May 8, 2017 at 4:16
  • 1
    I'm still confused. I guess the process output that you removed in the edit was how it changed when you stopped using your commit code, but I don't know because you didn't say whether you were indeed mixing the code. Anyway, are you getting an error now? I'd guess that you're calling magit-git-push with the wrong number of arguments because you're removing (list "--dry-run") rather than replacing it with nil.
    – Kyle Meyer
    Commented May 8, 2017 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.