2

When I run magit commands through the magit interface it runs those commands relative to the branch/repository I am currently visiting. However, I am trying to write some code in my config file that will automatically push to or pull from a specific repo, and am not sure how to format magit commands in elisp.

For example, the documentation for magit-pull-from-upstream says:

Pull from the upstream of the current branch.

But what determines the current branch, and how can I specify it? magit-push-current-to-upstream similarly mentions the "current branch".

What I am looking for is a way to call these commands from my config file in elisp code (or similar commands, if these are not the appropriate ones) while specifying which branch of which repo to pull from/push to. Thank you!

  • Is there any reason not to use (call-process "git" ...) or similar instead of trying to do it using Magit's API? – Constantine Jan 25 '16 at 16:46
  • @Constantine Ah, I can try that. I have been using magit so much recently, I tend to think about doing everything related to git through its API, but perhaps it is not suited for this particular use case. – elethan Jan 25 '16 at 16:49
2

Like many Magit commands, the push commands are not really intended to be used non-interactively.

Currently no warning is shown if you use them non-interactively, but that might change. The commands are not considered part of any application programming interface, and I don't make any promises about their "interfaces" staying the same.

Anyway, assuming you know elisp and are also somewhat familiar with Emacs programming concepts (in particular how commands work), then I would assume it to be fairly obvious what you have to do, after looking at the various push commands:

(let ((default-directory "/path/to/local/repo/"))
  (magit-git-push (magit-get-current-branch)
                  (magit-get-upstream-branch)
                  (magit-push-arguments)))

Of course you should also handle the cases when there is no current branch or when it has no upstream. Again, look at magit-push-current-to-upstream for one way of doing that. And since you don't actually want to push to the upstream (I believe), you'll have to adjust that part too.

But what determines the current branch, and how can I specify it?

Look at the code, specifically inside the interactive form; there magit-get-current-branch is being called. That then becomes the value of the BRANCH argument, but only when the command is being called interactively. When it is being called non-interactively as in (magit-push-current-to-upstream ...), then the interactive form is irrelevant, and you have to provide the arguments yourself, which makes it kind of pointless to use this command/function in the first place.

Better use the low-level function as shown above, or if you want to avoid having to write more code than absolutely necessary, then call the command (pseudo-)interactively:

(call-interactively #'magit-push-current-to-upstream)
0

Current branch is the one currently checked out.

To use a non-current branch and non-default upstream, use the elsewhere option of push or other of pull, which in turn run magit-push-elsewhere and magit-pull, respectivelly.

(cd "/path/to/the/repo")
(magit-pull "origin" "master")
  • Thanks, but i want to know how to do it from my config file in elisp code, not interactively. I will be in a state where I am not visiting a git repo and so magit will not know (without being told) which branch to push to or pull from. I want to know how to tell it – elethan Jan 25 '16 at 16:32
  • @elethan: updated. – choroba Jan 25 '16 at 16:34
  • Again, I don't want to do it interactively. I will try to clarify in the question. – elethan Jan 25 '16 at 16:39
  • @elethan: Sure, so use the functions I gave you. – choroba Jan 25 '16 at 16:49
  • 2
    It would be better to use (let ((default-directory "/path/to/repo")) (magit-pull...)) instead of cd, I think. – npostavs Jan 25 '16 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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