>From 6900bf39a861855ce1e220305efbfe60f343b008 Mon Sep 17 00:00:00 2001
From: Yury V. Zaytsev <yury@shurup.com>
Date: Sun, 19 Dec 2010 20:46:45 +0100
Subject: [PATCH 2/4] Introduce new --use-epoch option

---
 git-debimport   |   57 ++++++++++++++++++++++++++++++++++++++++++++++--------
 git-debimport.1 |   16 +++++++++++++++
 2 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/git-debimport b/git-debimport
index 3ba9125..1bc2ca0 100755
--- a/git-debimport
+++ b/git-debimport
@@ -23,7 +23,7 @@ usage()
 {
     cat 1>&2 <<EOF
 
-git-debimport [-v|--verbose] [--fetch] [--late-merge] path-prefix
+git-debimport [-v|--verbose] [--fetch] [--use-epoch] [--late-merge] path-prefix
 
  This program will create a git repository of all files that match
  \${path-prefix}_*.diff.gz or \${path-prefix}_*.debian.tar.{gz,bz2,xz}
@@ -41,6 +41,12 @@ git-debimport [-v|--verbose] [--fetch] [--late-merge] path-prefix
  of mypackagename will be downloaded into mysrcdir prior to creating a
  git repository from them.
 
+ The --use-epoch option enables parsing of the dsc-files in order to
+ obtain epoch information that is not contained in the file name. It
+ might be crucial for rebuilding correct history for some packages.
+ However, this mode of operation is slower and requires the presence of
+ the dsc-files for each upload. Therefore, it is disabled by default.
+
  The --late-merge option if supplied will delay merging new upstream
  releases into the debian branch until all packages are imported.  This
  may be useful where merge conflicts would need to be manually resolved
@@ -92,6 +98,10 @@ for arg; do
 	    late_merge="yes"
 	    ;;
 
+	--use-epoch)
+	    use_epoch="yes"
+	    ;;
+
 	--*|-*)
 	    echo "ERROR: Unrecognised option '$arg'"
 	    usage 1
@@ -260,22 +270,51 @@ get_orig_type()
     fi
 }
 
+get_dsc_for_normal()
+{
+    echo "$1" | sed -re "s/(.+)(debian\.tar|diff)\.(gz|bz2|xz)$/\1dsc/"
+}
+
+get_dsc_for_native()
+{
+    echo "$1" | sed -re "s/(.+)tar\.(gz|bz2|xz)$/\1dsc/"
+}
+
+get_version_from_dsc()
+{
+    pushd $PKG_ROOT$PACKAGES_DIR  > /dev/null 2>&1
+	awk '$1=="Version:" { print $2; exit }' "$1"
+    popd > /dev/null 2>&1
+}
+
 compare_tars()
 {
-    p1="$(strip_native_suffix $1)"
-    p2="$(strip_native_suffix $2)"
-    p1="${p1##*_}"
-    p2="${p2##*_}"
+
+    if [ -n "$use_epoch" ]; then
+	p1="$(get_version_from_dsc $(get_dsc_for_native $1) )"
+	p2="$(get_version_from_dsc $(get_dsc_for_native $2) )"
+    else
+	p1="$(strip_native_suffix $1)"
+	p2="$(strip_native_suffix $2)"
+	p1="${p1##*_}"
+	p2="${p2##*_}"
+    fi
 
     dpkg --compare-versions "$p1" gt "$p2"
 }
 
 compare_diffs()
 {
-    p1="$(strip_debian_suffix $1)"
-    p2="$(strip_debian_suffix $2)"
-    p1="${p1##*_}"
-    p2="${p2##*_}"
+
+    if [ -n "$use_epoch" ]; then
+	p1="$(get_version_from_dsc $(get_dsc_for_normal $1) )"
+	p2="$(get_version_from_dsc $(get_dsc_for_normal $2) )"
+    else
+	p1="$(strip_debian_suffix $1)"
+	p2="$(strip_debian_suffix $2)"
+	p1="${p1##*_}"
+	p2="${p2##*_}"
+    fi
 
     dpkg --compare-versions "$p1" gt "$p2"
 }
diff --git a/git-debimport.1 b/git-debimport.1
index b041d04..046bce8 100644
--- a/git-debimport.1
+++ b/git-debimport.1
@@ -43,6 +43,22 @@ already without this option.  Downloaded packages will not automatically be
 removed after this operation is complete.
 
 .TP
+.B \-\-use\-epoch
+The \fB\-\-use\-epoch\fP option enables parsing of the dsc-files in
+order to obtain epoch information that is not contained in the file
+name. It might be crucial for rebuilding correct history for some
+packages. However, this mode of operation is slower and requires the
+presence of the dsc-files for each upload. Therefore, it is disabled by
+default.
+
+It should be noted, that this option works for both normal and Debian
+native packages, however in both cases you need to have corresponding
+dsc-files for each upload. In the case of a normal package, the file
+name of the dsc file is constructed by replacing the extension of the
+diff, whereas in the case of a Debian native package the file name of
+the tarball is transformed.
+
+.TP
 .B \-\-late-merge
 Early versions of \fBgit-debimport\fP would only merge the upstream and debian
 branches after the import of all packages was complete.  This avoids an import
-- 
1.7.3.4

