Index: src/descendants.c
==================================================================
--- src/descendants.c
+++ src/descendants.c
@@ -202,11 +202,12 @@
 void compute_direct_ancestors(int rid, int N){
   Stmt ins;
   Stmt q;
   int gen = 0;
   db_multi_exec(
-    "CREATE TEMP TABLE ancestor(rid INTEGER, generation INTEGER PRIMARY KEY);"
+    "CREATE TEMP TABLE IF NOT EXISTS ancestor(rid INTEGER, generation INTEGER PRIMARY KEY);"
+    "DELETE FROM ancestor;"
     "INSERT INTO ancestor VALUES(%d, 0);", rid
   );
   db_prepare(&ins, "INSERT INTO ancestor VALUES(:rid, :gen)");
   db_prepare(&q, 
     "SELECT pid FROM plink"

Index: src/diff.c
==================================================================
--- src/diff.c
+++ src/diff.c
@@ -1068,10 +1068,11 @@
 */
 void annotate_cmd(void){
   int fnid;         /* Filename ID */
   int fid;          /* File instance ID */
   int mid;          /* Manifest where file was checked in */
+  int cid;          /* Checkout ID */
   Blob treename;    /* FILENAME translated to canonical form */
   char *zFilename;  /* Cannonical filename */
   Annotator ann;    /* The annotation of the file */
   int i;            /* Loop counter */
   const char *zLimit; /* The value to the --limit option */
@@ -1097,11 +1098,20 @@
   }
   fid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%Q", zFilename);
   if( fid==0 ){
     fossil_fatal("not part of current checkout: %s", zFilename);
   }
-  mid = db_int(0, "SELECT mid FROM mlink WHERE fid=%d AND fnid=%d", fid, fnid);
+  cid = db_lget_int("checkout", 0);
+  if (cid == 0){
+    fossil_fatal("Not in a checkout");
+  }
+  if( iLimit<=0 ) iLimit = 1000000000;
+  compute_direct_ancestors(cid, iLimit);
+  mid = db_int(0, "SELECT mlink.mid FROM mlink, ancestor "
+          " WHERE mlink.fid=%d AND mlink.fnid=%d AND mlink.mid=ancestor.rid"
+          " ORDER BY ancestor.generation ASC LIMIT 1",
+          fid, fnid);
   if( mid==0 ){
     fossil_panic("unable to find manifest");
   }
   if( fileVers ) annFlags |= ANN_FILE_VERS;
   annotate_file(&ann, fnid, mid, 0, iLimit, annFlags);