summaryrefslogtreecommitdiffstats
path: root/tools/patch-tool-mxe
blob: 1ba10c9222465649562eaf3f2fd7d36d1538f9eb (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash

# Tool for converting between MXE patch files and git repos
# Imports and exports patch files in "git format-patch" format.

cmd=$1
pkg=$2
patch_name=$3
if [ -z "$patch_name" ]; then
    patch_name=1-fixes
fi

setupEnv() {
  # MXE directory
  export mxedir=$(cd $(dirname $0) && cd .. && pwd)

  # directory for unpacked tarballs/git repos
  export gitsdir=${mxedir}/gits

  mkdir -p ${gitsdir}


  # John Doe <John Doe@acme.org>
  export author=`git var GIT_AUTHOR_IDENT | sed 's/^\(.* [<].*[>]\).*$/\1/'`

  export pkg_version=`grep '^$(PKG)_VERSION' $mxedir/src/$pkg.mk  | \
    sed 's/.*:= \(.*\)/\1/'`

  export pkg_short_version=`echo $pkg_version | sed s/'\(.*\)\.[^.]*$'/'\1'/`

  export pkg_subdir=`grep '^$(PKG)_SUBDIR' $mxedir/src/$pkg.mk  | \
    sed 's/.*:= \(.*\)/\1/' | \
    sed s/'$($(PKG)_VERSION)'/$pkg_version/ | \
    sed s/'$(call SHORT_PKG_VERSION,$(PKG))'/$pkg_short_version/ | \
    sed s/'$(PKG)'/$pkg/;`

  export pkg_file=`grep '^$(PKG)_FILE' $mxedir/src/$pkg.mk  | \
    sed 's/.*:= \(.*\)/\1/' | \
    sed s/'$($(PKG)_VERSION)'/$pkg_version/ | \
    sed s/'$(call SHORT_PKG_VERSION,$(PKG))'/$pkg_short_version/ | \
    sed s/'$($(PKG)_SUBDIR)'/$pkg_subdir/ | \
    sed s/'$(PKG)'/$pkg/;`

  #echo $pkg
  #echo $pkg_version
  #echo $pkg_subdir
  #echo $pkg_file

}
# init
function init_git {
  setupEnv
  cd $gitsdir

  if [ -d $gitsdir/$pkg_subdir ]; then
    echo "Error: $gitsdir/$pkg_subdir already exists. Cancelling init." >&2
    exit 1
  fi

  echo "Checking for cached $pkg_file"
  if [ ! -f $mxedir/pkg/$pkg_file ]; then
    make -C "$mxedir" download-$pkg
    echo "Building the mxe Makefile target 'download-$pkg' to get missing file"
    if [ ! $? -eq 0 ]; then
      echo "Could not build target download-$pkg - cancelling init." >&2
      exit 1
    fi
  fi

  echo "Unpacking archive..."
  (echo $pkg_file | grep "\.tar\.gz")  || (echo $pkg_file | grep "\.tgz") \
    >> /dev/null && tar xf $mxedir/pkg/$pkg_file
  (echo $pkg_file | grep "\.tar\.bz2") || (echo $pkg_file | grep "\.tbz2") \
    >> /dev/null && tar xf $mxedir/pkg/$pkg_file
  (echo $pkg_file | grep "\.tar\.xz")  || (echo $pkg_file | grep "\.txz") \
    >> /dev/null && xz -dc $mxedir/pkg/$pkg_file | tar xf -
  echo $pkg_file | grep "\.zip"      >> /dev/null && unzip  $mxedir/pkg/$pkg_file >> /dev/null

  echo "Initializing repo and adding all as first commit"
  cd $gitsdir/$pkg_subdir && \
  (git init; git add -A; git commit -m "init") > /dev/null

  echo "Creating 'dist' tag for distribution tarball state"
  git tag dist

  echo "Repository ready in $gitsdir/$pkg_subdir"
}

function export_patch {
  setupEnv
  if [ ! -d $gitsdir/$pkg_subdir ]; then
    echo "Error: $gitsdir/$pkg_subdir does not exist, so cannot export patches. Cancelling export." >&2
    exit 1
  fi

  cd $gitsdir/$pkg_subdir && \
  (
    echo 'This file is part of MXE.'
    echo 'See index.html for further information.'
    echo ''
    echo 'Contains ad hoc patches for cross building.'
    echo ''
    git format-patch -p --stdout dist..HEAD | \
    sed 's/^From: .*/From: MXE/g;'
  ) > $mxedir/src/${pkg}-${patch_name}.patch && \
  echo "Generated ${mxedir}/src/${pkg}-${patch_name}.patch"
}

function import_patch {
  setupEnv
  if [ ! -d $gitsdir/$pkg_subdir ]; then
    echo "Error: $gitsdir/$pkg_subdir does not exist, so cannot import patches. Cancelling import - try 'init' first." >&2
    exit 1
  fi

  if [ -f ${mxedir}/src/${pkg}-${patch_name}.patch ]; then
    cd $gitsdir/$pkg_subdir && \
    cat ${mxedir}/src/${pkg}-${patch_name}.patch | \
    sed '/^From/,$  !d' | \
    sed s/'^From: .*'/"From: $author"/'g;' | \
    git am --keep-cr && \
    echo "Imported ${mxedir}/src/${pkg}-${patch_name}.patch"
  else
    echo "patch file ${mxedir}/src/${pkg}-${patch_name}.patch not found."
    echo "Cancelling import." >&2
    exit 1
  fi
}

case "$cmd" in
  init)
    init_git $pkg
    ;;
  import)
    import_patch $pkg
    ;;
  export)
    export_patch $pkg
    ;;
  *)
    echo "Unrecognized command '${cmd}'" >&2
    cat <<EOS
    Usage: $0 COMMAND PACKAGENAME [PATCHNAME]
    where COMMAND is one of:
      init - create a git directory for the package with the raw source
      import - apply the "pkgname-PATCHNAME.patch" patch commits
      export - create/replace the "pkgname-PATCHNAME.patch" patch with a patch of all commits since init.

    If PATCHNAME is not set, it is default to "1-fixes".
EOS
    exit 1
    ;;
esac