1

So here's the thing, I've got a binary file that I can git diff using iconv via the git bash using this type of convertion method.

textconv = "iconv -f ISO-8859-1 -t UTF-8//IGNORE"

I'm not sure if the binary file encoding is ISO-8859-1 because there are a lot of special characters left whenever I use git diff.

Main problem: I cannot view the diff on our github. So what I did was to convert the binary file to a much readable file to get rid of all those special characters. My lil piece of code produces an .xml file. I want to include this file as a source code basis so that whenever a merge request is made, the need for a git diff via bash is eliminated.

What I'm after: I would like to execute the program upon using git add on a specific file. I've read about aliases, but I'm not exactly sure how to use the file as a parameter, and to invoke it whenever the specific file type is added. Should I modify the program to read the file type?

Summary: Upon using git add (or maybe an alias) on a specific file type, I would like to run an external command to convert the file, then invoke git add to include the converted file.

TIA :)

UPDATE: I am now experimenting with alias and assign it as i.e. add-filetype = "!../convert.er my.file && git commit my.file -m 'Auto-generated file on filetype add'" so I can just run git add-filetype my.file.

Solution: Finally I came up with this on my gitconfig:

[alias]
    add-filetype = "!f(){ \
                        ../convert.er $1; \
                        git add $1.xml; \
                        git commit $1.xml -m \"Auto Generate XML file\"; \
                    ); f"

and simple type git add-filetype my.file on git-bash.

Thanks @VonC!

Eduard
  • 614
  • 1
  • 8
  • 21
  • use a [bash function](http://stackoverflow.com/documentation/bash/472/functions#t=201608040231154732658) .. aliases cannot accept arguments.. – Sundeep Aug 04 '16 at 02:32
  • Well, I can always use the exclamation mark (!) to run a command to indicate that the command is an external shell command via alias right? Source: http://stackoverflow.com/a/4299159/1809168 – Eduard Aug 04 '16 at 02:35
  • I don't know about that, but if it solves your problem, why not? – Sundeep Aug 04 '16 at 03:09
  • Thank you, but could you please expand on your answer? I am now experimenting with alias and assign it as i.e. add-filetype = "!../convert.er my.file && git commit my.file -m 'Auto-generated file on filetype add'" so I can just run git add-filetype my.file – Eduard Aug 04 '16 at 03:13
  • I wasn't clear in earlier comment.. am not familiar with workings of git.. if what you tried worked, you can add it as answer with explanation and link to that SO answer.. my initial understanding was you needed a bash function which accepts any filename.. – Sundeep Aug 04 '16 at 03:40
  • Well I could do that, but afaik bash is for linux based machines right? Our release team uses windows, so I'm now looking into working with batch files. I'd do what your suggesting, alias an external batch command that accepts the filename as a parameter and see what happens. Apologies, I believe that I may have tagged the question incorrectly. – Eduard Aug 04 '16 at 03:43
  • yeah.. but you could use git-bash on windows if you like.. https://git-for-windows.github.io/ – Sundeep Aug 04 '16 at 03:51
  • I didn't know that bash commands applies to git-bash as well. Thanks for the info. – Eduard Aug 04 '16 at 04:40
  • 1
    This seems like the sort of problem better solved with a [build automation tool](https://en.wikipedia.org/wiki/Build_automation) such as `make`. This can fire a command anytime a file has changed, and requires no special git configuration for others work on your project. – Schwern Aug 04 '16 at 05:52
  • Thank you for this info, I'm currently reading it and will get back to you once I'm done. – Eduard Aug 04 '16 at 06:08
  • 1
    I will try your solution @Schwern. The task I am aiming to achieve has been accomplished atm. I will further tweak on `make` so that the process of executing my alias will no longer be needed. Thanks a lot! – Eduard Aug 04 '16 at 06:55

1 Answers1

1

afaik bash is for linux based machines right? Our release team uses windows, so I'm now looking into working with batch files

That will work: git uses git-bash bases on msys2, which means any Linux command in the git alias will be interpreted by that shell, even on Windows.

my_alias = "!f() { 〈your complex command〉 }; f"

See "One weird trick for powerful Git aliases "


The OP Eduard Daduya embedded the script in the alias:

[alias]
    add-filetype = "!f(){ \
                        ../convert.er $1; \
                        git add $1.xml; \
                        git commit $1.xml -m \"Auto Generate XML file\"; \
                    ); f"

But that same script could have been written as:

git-add-filetype:

/full/path/to/convert.er $1
git add $1.xml; \
git commit $1.xml -m \"Auto Generate XML file\"

Any git-xxx script (no extension, in the %PATH%) can be called as git xxx: no git alias needed in that case.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thank you for this info, I'll digest this after a few minutes and I'll get back to you once I'm done. – Eduard Aug 04 '16 at 06:09
  • @EduardDaduya Note: I have edited the answer with an alternative to git alias. – VonC Aug 04 '16 at 08:00
  • I'm sorry, I do not seem to follow. I made the `add-filetype` alias so that whenever I type `git add-filetype` those lines of codes would be executed. – Eduard Aug 04 '16 at 08:16
  • are you saying from `[alias] add-filetype = "!f(){ \ ../convert.er $1; \ git add $1.xml; \ git commit $1.xml -m \"Auto Generate XML file\"; \ ); f"` to `[alias] add-filetype = "../convert.er $1; \ git add $1.xml; \ git commit $1.xml -m \"Auto Generate XML file\";"` instead? – Eduard Aug 04 '16 at 08:18
  • @EduardDaduya Exactly: my point is: without alias, you could have made a shell script (yes, even on Windows since it will be interpreted by the Git bash), called `git-add-filetype` (no extension), to be put anywhere in your `%PATH%`, because any script called `git-xxx` can be called with `git xxx`. – VonC Aug 04 '16 at 08:19
  • @EduardDaduya so no, I am not saying "from alias to alias". I am saying; this is an alternative to git alias, especially when you have complex scripts. – VonC Aug 04 '16 at 08:20
  • I understand now, instead of putting it in my gitconfig, I can simply put the command in `%PATH%` :) – Eduard Aug 04 '16 at 08:21
  • 1
    @EduardDaduya Yes, that is the idea. – VonC Aug 04 '16 at 08:28