How to emulate git status using the GitLab API

Is there a way to emulate git status via the api? I’m looking for a way to enumerate the untracked plus the staged files.

The reason I’m looking for this is so I can push all the new, deleted and modified files using this api: POST /projects/:id/repository/commits (https://docs.gitlab.com/ee/api/commits.html)

Hi Litosdesk,

There are no untracked or staged files in GitLab. These exist on user’s workstations only. GitLab has no working tree, only a “bare” repository. See more at http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository.

Moreover, you cannot push using GitLab API, because a push is done by the user’s workstation rather than the GitLab server.

2 Likes

Hi alexk,

I was intrigued and bit confused with your reply until I checked the link you provided and others that I found from additional searching about “bare repository”.

Perhaps, if I outline the workflow that I envision, you might be able to give me pointers on how to better use the GitLab API.

I have several jpg files that I want to version. This collection of files may change from time to time, like adding more files, deleting some and even changing the content of some. I want to commit and push each version of this collection to GitLab every time such changes occur. I then would want to pull any version of the collection from GitLab at any time.

So I have a local folder named “C:\MyFiles” with a number of subdirectories like “C:\MyFiles\One”, “C:\MyFiles\Two”, etc.

And here’s what I think I should do:

  1. Create a new GitLab project using the GitLab web ui (which from what I have read will default to a bare repo).
  2. Push all the files from C:\MyFiles using the api: POST /projects/:id/repository/commits
  3. Tag the last commit using the api: POST /projects/:id/repository/tags
  4. To pull all the files from any desired tag with this api: GET /projects/:id/repository/archive

The reason why I asked about git status was to determine what commit action I will use (that is, create, update or delete) for succeeding calls to “POST /projects/:id/repository/commits”.

Perhaps you can give some pointers.

Hi litosdesk,

Now I understand better. You are actually writing a script which will run on your Windows workstation, and I think your plan is OK. And of course, you are right, you can push using the API.

As to git status, it’s a local command, in which GitLab is not involved, and thus has nothing to do with the API.

Therefore I think you could simply run git status in your script and parse its output. From what I see in the documentation, you should probably use the –porcelain option.

I thought more about your plan and read again the documentation. Forget what I wrote in the previous post. From what I understand now,

POST /projects/:id/repository/commits

described in https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions does not push anything from your workstation. It only manipulates files already in GitLab.

Instead, you must run the following commands in your script:

git add --all
git commit -m "your comment"
git push -u origin --all

And then, you don’t need git status at all.

2 Likes

Hi alexk,

I will consider your suggestion to run the 3-command script. I would like to call the api’s from a C# application, if at all possible.

POST /projects/:id/repository/commits

I have tried the above POST api and found it to actually push the files to GitLab. What did you mean by “It only manipulates files already in GitLab?”

You are right, POST /projects/:id/repository/commits does upload files. However, it does not push a whole commit. It only creates a new commit on the GitLab server, without having the same commit on your computer. As a result, you will create a discrepancy between the repo on your computer and that on the server. The right way to do is first to create a commit on your computer, then push it to the server.

Although I haven’t tried myself (I think Perl and Python are more suitable for such operations), I think you can do everything from C#. Here is a discussion how to run Windows commands from within C# applications: https://stackoverflow.com/questions/1469764/run-command-prompt-commands and how to send HTTP requests: https://stackoverflow.com/questions/1273998/how-to-submit-http-form-using-c-sharp

2 Likes

Hi alexk,

Thanks for the link. I’m familiar with the topics discussed in the C# links but thanks anyway.

I was wondering if you have any thoughts on another question I posted. Here’s the link: Need help to get an LFS file using the api

Hi @litosdesk, if you find my answers useful, could you give me a like? Also regarding the previous question.

1 Like

With all seriousness, why do you want to manipulate Git repository using GitLab API, even thou GitLab partially allows that? I’m still baffled about that…

What’s wrong with standard Git approach as alexk mentioned (add, commit, push)? You don’t need connection to that GitLab server to add and commit files to repository and you don’t need to gather status of files.

If you want to manipulate Git repository from C# app, I would suggest using libgit2sharp library. That’s wrapper for libgit2 library, which is more effecient for applications like that instead of calling cmd-line version of Git, but approach is still same: add, commit, tag, push.

2 Likes

@rbukovansky you are right, libgit2sharp is the way to go.