Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sort .gitmodules, also easily adaptable to sort files in blocks (e.g. 4 lines at a time, or arbitrary region at a time).

.gitmodules sorter

Sort .gitmodules, also easily adaptable to sort files in blocks (e.g. 4 lines at a time, or arbitrary region at a time).

See also:

#!/usr/bin/env bash

awk 'BEGIN { I=0 ; J=0 ; K="" } ; /^\[submodule/{ N+=1 ; J=1 ; K=$2 ; gsub(/("vendor\/|["\]])/, "", K) } ; { print K, N, J, $0 } ; { J+=1 }' .gitmodules \
    | sort \
    | awk '{ $1="" ; $2="" ; $3="" ; print }' \
    | sed 's/^ *//g' \
    | awk '/^\[/{ print ; next } { print "\t", $0 }' \
    > .gitmodules

4U6U57 commented Nov 23, 2017

Hey, just wanted to say I found this very useful, except for the fact that having that piped command immediately overwriting > .gitmodules deletes the contents of .gitmodules before the command actually runs. See https://serverfault.com/questions/135507/linux-how-to-use-a-file-as-input-and-output-at-the-same-time

The first time I ran it I basically nuked my .gitmodules folder. Might want to edit this so others don't do the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment