lib/bb/fetch/git.py: always use english here
[vuplus_bitbake] / lib / bb / fetch / git.py
index 49235c1..b194172 100644 (file)
@@ -35,26 +35,70 @@ def prunedir(topdir):
         for name in dirs:
             os.rmdir(os.path.join(root, name))
 
-def rungitcmd(cmd,d):
+def runfetchcmd(cmd, d, quiet = False):
 
     bb.debug(1, "Running %s" % cmd)
 
     # Need to export PATH as git is likely to be in metadata paths 
     # rather than host provided
-    pathcmd = 'export PATH=%s; %s' % (data.expand('${PATH}', d), cmd)
+    cmd = 'export PATH=%s; %s' % (data.expand('${PATH}', d), cmd)
+
+    # redirect stderr to stdout
+    stdout_handle = os.popen(cmd + " 2>&1", "r")
+    output = ""
+
+    while 1:
+        line = stdout_handle.readline()
+        if not line:
+            break
+        if not quiet:
+            print line
+        output += line
+
+    status =  stdout_handle.close() or 0
+    signal = status >> 8
+    exitstatus = status & 0xff
+
+    if signal:
+        raise FetchError("Fetch command %s failed with signal %s, output:\n%s" % (cmd, signal, output))
+    elif status != 0:
+        raise FetchError("Fetch command %s failed with exit code %s, output:\n%s" % (cmd, status, output))
+
+    return output
+
+def contains_ref(tag, d):
+    output = runfetchcmd("git log --pretty=oneline -n 1 %s -- 2> /dev/null | wc -l" % tag, d, True)
+    return output.split()[0] != "0"
+
+def latest_revision(proto, user, host, path, branch, d):
+    """
+    Compute the HEAD revision for the url
+    """
+    if user:
+        username = user + '@'
+    else:
+        username = ""
+
+    output = runfetchcmd("git ls-remote %s://%s%s%s %s" % (proto, username, host, path, branch), d, True)
+    return output.split()[0]
 
-    myret = os.system(pathcmd)
+def getbranch(parm):
+    if 'branch' in parm:
+        branch = parm['branch']
+    else:
+        branch = "master"
+    if not branch:
+        branch = "master"
 
-    if myret != 0:
-        raise FetchError("Git: %s failed" % pathcmd)
+    return branch
 
-def gettag(parm):
+def gettag(parm, proto, user, host, path, branch, d):
     if 'tag' in parm:
         tag = parm['tag']
     else:
-        tag = ""
-    if not tag:
-        tag = "master"
+        tag = latest_revision(proto, user, host, path, branch, d)
+    if not tag or tag == "master":
+        tag = latest_revision(proto, user, host, path, branch, d)
 
     return tag
 
@@ -76,7 +120,9 @@ def localfile(url, d):
     if "localpath" in parm:
         return parm["localpath"]
 
-    tag = gettag(parm)
+    proto = getprotocol(parm)
+    branch = getbranch(parm)
+    tag = gettag(parm, proto, user, host, path, branch, d)
 
     return data.expand('git_%s%s_%s.tar.gz' % (host, path.replace('/', '.'), tag), d)
 
@@ -104,8 +150,14 @@ class Git(Fetch):
         for loc in urls:
             (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(loc, d))
 
-            tag = gettag(parm)
             proto = getprotocol(parm)
+            branch = getbranch(parm)
+            tag = gettag(parm, proto, user, host, path, branch, d)
+
+            if user:
+                username = user + '@'
+            else:
+                username = ""
 
             gitsrcname = '%s%s' % (host, path.replace('/', '.'))
 
@@ -116,40 +168,39 @@ class Git(Fetch):
             coname = '%s' % (tag)
             codir = os.path.join(repodir, coname)
 
-            cofile = self.localpath(loc, d)
-
-            # tag=="master" must always update
-            if (tag != "master") and Fetch.try_mirror(d, localfile(loc, d)):
-                bb.debug(1, "%s already exists (or was stashed). Skipping git checkout." % cofile)
-                continue
-
             if not os.path.exists(repodir):
                 if Fetch.try_mirror(d, repofilename):    
                     bb.mkdirhier(repodir)
                     os.chdir(repodir)
-                    rungitcmd("tar -xzf %s" % (repofile),d)
+                    runfetchcmd("tar -xzf %s" % (repofile),d)
                 else:
-                    rungitcmd("git clone -n %s://%s%s %s" % (proto, host, path, repodir),d)
+                    runfetchcmd("git clone -n %s://%s%s%s %s" % (proto, username, host, path, repodir),d)
 
             os.chdir(repodir)
-            rungitcmd("git pull %s://%s%s" % (proto, host, path),d)
-            rungitcmd("git pull --tags %s://%s%s" % (proto, host, path),d)
-            rungitcmd("git prune-packed", d)
-            # old method of downloading tags
-            #rungitcmd("rsync -a --verbose --stats --progress rsync://%s%s/ %s" % (host, path, os.path.join(repodir, ".git", "")),d)
+            # Remove all but the .git directory
+            if not contains_ref(tag, d):
+                runfetchcmd("rm * -Rf", d)
+                runfetchcmd("git fetch %s://%s%s%s %s" % (proto, username, host, path, branch), d)
+                runfetchcmd("git fetch --tags %s://%s%s%s" % (proto, username, host, path), d)
+                runfetchcmd("git prune-packed", d)
+                runfetchcmd("git pack-redundant --all | xargs -r rm", d)
 
             os.chdir(repodir)
             bb.note("Creating tarball of git repository")
-            rungitcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ),d)
+            runfetchcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ),d)
 
             if os.path.exists(codir):
                 prunedir(codir)
 
             bb.mkdirhier(codir)
             os.chdir(repodir)
-            rungitcmd("git read-tree %s" % (tag),d)
-            rungitcmd("git checkout-index -q -f --prefix=%s -a" % (os.path.join(codir, "git", "")),d)
+            copath = os.path.join(codir, "git", "")
+            runfetchcmd("git read-tree %s" % (tag),d)
+            runfetchcmd("git checkout-index -q -f --prefix=%s -a" % (copath),d)
+            bb.mkdirhier(os.path.join(copath, ".git", ""))
+            runfetchcmd("LANG=\"en\" git log %s --max-count=1 --date=iso -- > %s.git/last_commit_info" % (tag, copath),d)
+            runfetchcmd("echo %s > %s.git/branch" % (branch, copath),d)
 
             os.chdir(codir)
             bb.note("Creating tarball of git checkout")
-            rungitcmd("tar -czf %s %s" % (cofile, os.path.join(".", "*") ),d)
+            runfetchcmd("tar -czf %s %s" % (self.localpath(loc, d), os.path.join(".", "*") ),d)