Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created May 4, 2017 17:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/fad7bc69e5f12fc2331e2c6330bd8419 to your computer and use it in GitHub Desktop.
Save jaytaylor/fad7bc69e5f12fc2331e2c6330bd8419 to your computer and use it in GitHub Desktop.
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
Copy link

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.

@SuperSandro2000
Copy link

@4U6U57 Switched the pipe with sponge.

I also fixed an annoyance that it would add an extra space after the tab character by removing the comma.

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 }' \
    | sponge .gitmodules

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