Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to add to "git status --porcelain" to make it behave like "git status"?

Tags:

git

An earlier question resulted in some ideas on how to check whether your Git repo contains a dirty index or untracked files. The answer I adopted from that discussion was the following:

#!/bin/sh
exit $(git status --porcelain | wc -l)

The idea behind that answer was to emulate what the programmer would do: run git status and then examine the output.

Unfortunately git status --porcelain and git status do not do exactly the same thing. In particular, git status will report unpushed changes, while git status --porcelain will not. Here is an example

[jarmo@localhost math-hl]$ git status --porcelain
[jarmo@localhost math-hl]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

So my question is: what do I need to add to the original script to also identify when the repo has unpushed changes? In particular, would this be a correct approach?

#!/bin/bash
if [ $(git status --porcelain | wc -l) != "0" \
        -o $(git log @{u}.. | wc -l) != "0" ];  then
        echo "local repo is not clean"

Would it be ok, in the long run, to rely on git log, or should I use a "plumbing" command instead?

like image 811
user2851270 Avatar asked Oct 06 '13 08:10

user2851270


People also ask

What does git status -- porcelain do?

--porcelain guarantees that there won't be backward-incompatible changes to the output so that your scripts won't break with a git update. For compact display with color guidance, you can use git status -s , and if you're automating git workflows, you can use git status --porcelain .

What is porcelain in git?

For instance: git status --porcelain , which designates an output meant to be parsed. --porcelain. Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across git versions and regardless of user configuration.

What is correct command to check the current git status?

To check the status, open the git bash, and run the status command on your desired directory. It will run as follows: $ git status.

What does git status UNO do?

Show untracked files. The mode parameter is optional (defaults to all), and is used to specify the handling of untracked files; when -u is not used, the default is normal, i.e. show untracked files and directories.


1 Answers

The idea in git scripts is to always use:

  • low-level "plumbing" commands,
  • or porcelain commands with plumbing options

See "What does the term porcelain mean in Git?".
As I explain, the --porcelain option is, unfortunately (because its naming is confusing), the "plumbing" way to get a status, that is the "reliable" way, meaning its format won't change over time, which is why it is the recommended command for scripting.

For git log, it is better to use git rev-list:

git rev-list HEAD@{upstream}..HEAD

See "How to know if git repository has changes that have not been synchronized with server (origin)?"

like image 189
VonC Avatar answered Oct 27 '22 01:10

VonC