git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Difficulties of scripting git

From
AMAlan Mackenzie <acm@muc.de>
Date
Jan 9, 2021, 19:14 UTC
Message-ID
<X/oAhdZlLwcFRCrR@ACM>
Hello, git.

I want to write a bash script (more precisely, correct an existing script) which uses git, and it is DIFFICULT!!!

I need a git pull in the script. Fine, but how does the script know if it's worked or not? I couldn't find any description of a return code in the git-pull man page (or in the git-merge man page).

The big problem is when I have modified, uncommitted files in the target repo, and those same files are to be updated in commits coming from the source repo. Sadly, git is unable to merge these changes. It just fails, putting an error message onto stderr, but doesn't tell the calling script in any way that I can see.

One idea would be always to call git stash before doing the pull, then git stash pop afterwards. Trouble is, git stash is unreliable - it doesn't always add a new stash entry, so the stash pop at the end would sometimes/often pop off an entry it shouldn't. git stash doesn't have a --force argument. git stash doesn't set a result code, either, that I can see. One way around this would be to do

    $ git stash list | wc -l
both before and after the git stash and compare the answers, but really?

So, next idea, feed the output from git status --porcelain through grep before and after the git pull, so as to find out whether there are any modified files before the git pull (thus making a stash necessary) and any files with conflicts after the git stash pop. Shouldn't be too difficult.

Except, how does one recognise a file with conflicts from this git status output? The man page says that

    "   For paths with merge conflicts, `X' and `Y' show the modification
    states of each side of the merge.  For paths that do not have merge
    conflicts, `X' shows the status of the index, and `Y' shows the status
    of the work tree.  For untracked paths, `XY' are `??'.  Other status
    codes can be interpreted as follows:  ...."

I've spent nearly an hour trying to make sense of this bit of man page. How is one meant to distinguish an XY of a merge conflict from the XY of an index/work tree entry? I can't find that key bit of information anywhere.

What am I missing? Why does writing scripts using git have to be so hard?

-- 
Alan Mackenzie (Nuremberg, Germany).
Next: brian m. carlson
Message 1 of 10 in “Difficulties of scripting git”
  1. Alan MackenzieJan 9, 2021
  2. brian m. carlsonJan 9, 2021
  3. docs: add description of status output tablebrian m. carlson, Jan 9, 2021
  4. Junio C HamanoJan 10, 2021
  5. brian m. carlsonJan 10, 2021
  6. Alan MackenzieJan 10, 2021
  7. brian m. carlsonJan 10, 2021
  8. docs: rephrase and clarify the git status --short formatbrian m. carlson, Jan 10, 2021
  9. Junio C HamanoJan 11, 2021
  10. brian m. carlsonJan 16, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.