10
$\begingroup$

How can I publish Notebooks to Github via using Mathematica?


I'm on windows 8.1 x64, Mathematica 9.

How can I upload the current notebook to Github?

My function expected is: Notebook2Github[EvaluationNotebook[]]

Now I've installed Github on Windows, and have one test page

The annoying thing is that each time I added/modified a new file in the destination folder(E:\Users\HyperGroups\Documents\GitHub\hypergroups.github.com), I should open GitHub, and add something into the uncommitted changes fields and click the sync button(shown in the picture)

enter image description here

How can I avoid doing that and complete all such things by the function Notebook2Github.


My tries in Git Shell

I can use the following commands in the GitShell.

git add [new file] /git commit and git push and the new file will be updated to GitHub.

$\endgroup$
7
  • $\begingroup$ Making a full fledged VCS client in Mathematica is no easy task and you're asking an awful lot without showing any effort. I know that Leonid is working on such a project to share code/notebooks via gists (in github) and that is not a trivial task even for him. Besides, there are tools that are much better for git than Mathematica and tailored to work with Github/Bitbucket that you shouldn't be reinventing hte wheel... Since you already know that Mathematica can run things from the command line, what's stopping you from trying it out? You just need to learn the appropriate git commands. $\endgroup$
    – rm -rf
    Aug 21, 2013 at 5:12
  • $\begingroup$ @rm-rf I donot know how to call the GitShell in Mathematica, I only know how to call an C .exe program(generated by CCompiler) with some arguments and import the results. $\endgroup$ Aug 21, 2013 at 6:45
  • $\begingroup$ Your issue here mainly seems to be an unfamiliarity with git. Perhaps you should start with a good git guide like this one. Try following through those examples with some dummy repos. Forget Mathematica when doing all this. Once you've learnt git, you can read this answer for some general info on git + notebooks/m-files. $\endgroup$
    – rm -rf
    Aug 21, 2013 at 7:25
  • $\begingroup$ @rm-rf ah, ok, thanks,--- yes, I signed up the Git account this morning __@@ $\endgroup$ Aug 21, 2013 at 7:29
  • $\begingroup$ @rm-rf The two main problems for me so far have been the lack of time (by far, the worst one), and the right design choices, in terms of usability. I am on vacation right now, so I hope to move things along. The VCS itself has been basically ready for quite some time, but I was making a mistake of over-generalizing things, and also it wasn't clear for me until recently how to connect that seamlessly to the standard notebook-based workflow usual for most users. I seem to have some good ideas now, which should be not too hard to implement, so hopefully soon enough I will be able to say more. $\endgroup$ Aug 21, 2013 at 9:13

2 Answers 2

13
$\begingroup$

Another option for individual files and stuff is to use the GitHub API with a custom plug-in to the built-in service connection framework.

Here's a service connection I wrote for it:

PacletInstall["ServiceConnection_GitHub",
 "Site"->"http://www.wolframcloud.com/objects/b3m2a1.paclets/PacletServer"
 ]

Then we load it like this:

$gh = ServiceConnect@"GitHub"

github connection

We can see what requests are there:

In[73]:= $gh["Requests"]

Out[73]= {"AddFile", "ArchiveLink", "Authentication", "BranchInfo", \
"BranchProtection", "CreateOrganizationRepository", \
"CreateRepository", "DeleteFile", "DeleteRepository", \
"EditRepository", "ID", "ImportFile", "Information", "ListBranches", \
"ListMyRepositories", "ListOrganizationRepositories", \
"ListRepositories", "ListUserRepositories", "Name", "RawRequests", \
"ReadMeContents", "RemoveBranchProtection", "RepositoryContributors", \
"RepositoryInfo", "RepositoryLanguages", "RepositoryTags", \
"RepositoryTeams", "RequestData", "RequestParameters", \
"SetBranchProtection", "UpdateFile"}

So we'll start by making a testing repo. First we'll see what we need:

In[74]:= $gh["RequestParameters", 
  "Request" -> "CreateRepository"]["Required"]

Out[74]= {"name"}

So we'll give it a name like api-testing-repo:

In[80]:= $gh["CreateRepository", "name" -> "api-testing-repo"][{"id", 
  "name"}]//Normal

Out[80]= <|"id" -> 93099247, "name" -> "api-testing-repo"|>

Then we upload a file (note that it has to be Base64 encoded):

$gh[
  "AddFile",
  "user" -> "b3m2a1",
  "repo" -> "api-testing-repo",
  "path" -> "test.txt",
  "content" -> Developer`EncodeBase64@"file_text_goes_here",
  "message" -> "file upload test"
  ]["content", "sha"]

And we can see it exists:

file exists

Now we'll delete the file:

$gh[
 "DeleteFile",
 "user" -> "b3m2a1",
 "repo" -> "api-testing-repo",
 "path" -> "test.txt",
 "sha" -> "6e560c960830951d7d77b0960cd961173ed86ce0",
 "message" -> "file delete test"
 ]

And we'll see it no longer exists:

no more file

And now for good measure we delete the repo:

$gh[
 "DeleteRepository",
 "user" -> "b3m2a1",
 "repo" -> "api-testing-repo"
 ]
$\endgroup$
6
  • $\begingroup$ This is very interesting $\endgroup$
    – faysou
    Jun 1, 2017 at 22:07
  • $\begingroup$ @faysou Glad to hear it. If you're interested in how I set this up here's the template notebook: wolframcloud.com/objects/… $\endgroup$
    – b3m2a1
    Jun 1, 2017 at 22:18
  • $\begingroup$ Neat! Is this usable with GitLink? $\endgroup$ Sep 6, 2017 at 1:03
  • $\begingroup$ @J.M. I've never actually used GitLink myself. As I understand it, it's working at a LibraryLink level, while I was building on the ServiceConnect framework. Presumably, though, one could use this service connection to build a repo, then use GitLink to do the rest. I do this myself when building new paclets, but using Git through RunProcess rather GitLink. $\endgroup$
    – b3m2a1
    Sep 6, 2017 at 1:06
  • $\begingroup$ I'm not sure I get the relation between service and the public api of GitHub. How can one e.g. GET /repos/:owner/:repo/releases/latest? $\endgroup$
    – Kuba
    May 25, 2018 at 11:03
2
$\begingroup$

Publish to GitHub with Mathematica on Windows


I'm on windows 8.1

Let's just concentrate on how to commit the files. [git push]

Method


1 Auto type username and password


download an assistant tool小助手[Permanently authenticating with Git repositories]

my git-credential-winstore.exe is put and installed here

"E:\\Users\\Hyper\\AppData\\Local\\GitHub\\PortableGit_\fed20eba68b3e238e49a47cdfed0a45783d93651\\bin"

2 Put commands in the .bat file


Put the commands set used in GitShell in the .bat file of the portable version of GitHub.

batFile =   "E:\\Users\\Hyper\\AppData\\Local\\GitHub\\PortableGit_\fed20eba68b3e238e49a47cdfed0a45783d93651\\git-cmd.bat";

batSample =   "@rem Do not use \"echo off\" to not affect any child calls.  @setlocal    @rem Get the abolute path to the current directory, which is \assumed to be the  @rem Git installation root.  @for /F \"delims=\" %%I in (\"%~dp0\") do @set git_install_root=%%~fI  @set PATH=%git_install_root%\\bin;%git_install_root%\\mingw\\bin;%\git_install_root%\\cmd;%PATH%    @if not exist \"%HOME%\" @set HOME=%HOMEDRIVE%%HOMEPATH%  @if not exist \"%HOME%\" @set HOME=%USERPROFILE%    @set PLINK_PROTOCOL=ssh  @if not defined TERM set TERM=msys    @cd %HOME%  ::@start %COMSPEC%    ::@start \"\" cmd /k @cd /d documents\\github && @git    cd /d documents\\github\\hypergroups.github.com  ";

commandsSet = "git add .  git commit -a -m \"" <> windowTitle[EvaluationNotebook[]] <> "\"  git push"

(*
    git add .git commit -a -m "Publish to GitHub with Mathematica on Windows"git push
*)

batFileToExport = batSample <> commandsSet(*<>"\npause"*);

Export["bat.txt", batFileToExport];DeleteFile@batFile;CopyFile["bat.txt", batFile];

3 Run the .bat file

enter image description here


batFile // SystemOpen

References


Also, I asked here

I've redone this succesfully.

$\endgroup$
1

Your Answer

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

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