summaryrefslogtreecommitdiff
path: root/yamlremote
blob: 7c7c38cfeedb531a36856059eb9f73152eb9461a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/sh
keep_orig_line=y

main()
{
    if [ $# = 0 ]; then
        while read line; do
            set -- $line
            while [ "$1" = - ]
            do
                shift
            done
            (generate_yaml_remote "$1" "$line")
        done
    else
        for a; do (generate_yaml_remote "$a" "- $a"); done
    fi
}

# Example output:
#
# - location:
#     git: https://github.com/bitemyapp/esqueleto.git
#     commit: 08c9b4cdf977d5bcd1baba046a007940c1940758
#   extra-dep: true
#
# And with subdirs:
#
# - location:
#   git: d@emmy.childrenofmay.org:public_git/lambdacube-ir
#   commit: f6617496f582ad287bd8203d931a6ee037ed3a69
#   subdirs:
#   - lambdacube-ir.haskell
#   - ddl
#   extra-dep: true

# cat, except drop empty lines
cat_()
{
    sed -e '/^$/d'
}

output()
{
    local git_url="$1" git_hash="$2" subdir="$3" orig_line="$4"
    case "$git_url" in
        *@*) ;;
        *) git_url=$(id -un)@$git_url ;;
    esac
    cat_ <<EOF
${keep_orig_line:+
# $orig_line}
- git: $git_url
  commit: $git_hash
${subdir:+
  subdirs:
    - $subdir}
EOF
}

generate_yaml_remote()
{
    set -e
    orig_line=$2
    cd "$1"
    git_toplevel=$(git rev-parse --show-toplevel)
    if [ "$git_toplevel" != "$PWD" ]
    then
        subdir=${PWD#$git_toplevel/}
    fi

    local_ref=$(git rev-parse --abbrev-ref HEAD)
    remote_ref=$(git show-ref $local_ref | grep ' refs/remotes/' | head -n1)

    set -- $remote_ref
    sha1=$1
    ref=$2

    remote=${ref%/*}
    remote=${remote#refs/remotes/}
    url=$(git remote get-url $remote)

    output "$url" "$sha1" "$subdir" "$orig_line"
}

main "$@"