a comma-separated explicit list of branches to export. This
        mode of operation is currently not supported with collapse.
 
+       In '--quilt' mode the patches are named like the originating topgit
+       branch.  So usually they end up in subdirectories of the output
+       directory.  With option '--flatten' the names are mangled such that
+       they end up directly in the output dir (i.e. slashed are substituted by
+       underscores).
+
        Usage: tg export ([--collapse] BRANCH | --quilt DIR)
 
        TODO: Make stripping of non-essential headers configurable
 
 branches=
 output=
 driver=collapse
+flatten=false
 
 
 ## Parse options
        case "$arg" in
        -b)
                branches="$1"; shift;;
+       --flatten)
+               flatten=true;;
        --quilt)
                driver=quilt;;
        --collapse)
 [ -z "$branches" -o "$driver" = "quilt" ] ||
        die "-b works only with the quilt driver"
 
+[ "$driver" = "quilt" ] || ! "$flatten" ||
+       die "--flatten works only with the quilt driver"
+
 if [ -z "$branches" ]; then
        # this check is only needed when no branches have been passed
        name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
                return
        fi
 
-       filename="$output/$_dep.diff"
+       if "$flatten"; then
+               bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
+               dn="";
+       else
+               bn="$(basename "$_dep.diff")";
+               dn="$(dirname "$_dep.diff")/";
+               if [ "x$dn" = "x./" ]; then
+                       dn="";
+               fi;
+       fi;
+
+       filename="$output/$dn$bn";
        if [ -e "$filename" ]; then
                # We've already seen this dep
                return
                echo "Skip empty patch $_dep";
        else
                echo "Exporting $_dep"
-               mkdir -p "$(dirname "$filename")"
+               mkdir -p "$output/$dn";
                $tg patch "$_dep" >"$filename"
-               echo "$_dep.diff -p1" >>"$output/series"
+               echo "$dn$bn -p1" >>"$output/series"
        fi
 }