I need to checkout kernel revisions by branch, commit id, or tag before
building and testing them.

Here is my patch to extend server/git.py and server/git_kernel.py

I'm very open for any kind of suggestions.

The following new methods exist in GitKernel()
* checkout - Check out a certain branch, tag or revision. Optional
             a local branch name can be given for -b
* show_branch - print the current local branch
* show_branches - print the local and/or remote branches
* show_revision - print the current tip commit id (checked out revision)
* extended install - optional a commit id / revision that is checked out
* before patching, configure, and build

Therefore the following methods were added to Git()
* get_revision() - To print the current tip commit id (aka revision).
* checkout() - Check out a certain branch, tag or revision.
* get_branch() - return the branches depending on options

Risk: Low (Just extends functionality, doesn't change existing one.)
Visibility: Users who build Linux kernels may profit.

Signed-off-by: Frank Becker <[email protected]>
---
 server/git.py        |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++
 server/git_kernel.py |   66 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 127 insertions(+), 8 deletions(-)

diff --git a/server/git.py b/server/git.py
index 93833ec..b35b79c 100644
--- a/server/git.py
+++ b/server/git.py
@@ -183,3 +183,72 @@ class GitRepo(installable_object.InstallableObject):
             return True
 
         return False
+
+
+    def get_revision(self):
+        """
+        Return current HEAD commit id
+        """
+
+        if not self.is_repo_initialized():
+            self.get()
+
+        cmd = 'rev-parse --verify HEAD'
+        gitlog = self.gitcmd(cmd, True)
+        if gitlog.exit_status != 0:
+            print gitlog.stderr
+            raise error.CmdError('Failed to find git sha1 revision', gitlog)
+        else:
+            return gitlog.stdout.strip('\n')
+
+
+    def checkout(self, remote, local=None):
+        """
+        Check out the git commit id, branch, or tag given by remote.
+
+        Optional give the local branch name as local.
+
+        Note, for git checkout tag git version >= 1.5.0 is required
+        """
+        if not self.is_repo_initialized():
+            self.get()
+
+        assert(isinstance(remote, basestring))
+        if local:
+            cmd = 'checkout -b %s %s' % (local, remote)
+        else:
+            cmd = 'checkout %s' % (remote)
+        gitlog = self.gitcmd(cmd, True)
+        if gitlog.exit_status != 0:
+            print gitlog.stderr
+            raise error.CmdError('Failed to checkout git branch', gitlog)
+        else:
+            print gitlog.stdout
+
+
+    def get_branch(self, all=False, remote_tracking=False):
+        """
+        Show the branches.
+
+        all - list both remote-tracking branches and local branches.
+        remote_tracking - lists the remote-tracking branches.
+        """
+        if not self.is_repo_initialized():
+            self.get()
+
+        cmd = 'branch --no-color'
+        if all:
+            cmd = " ".join([cmd, "-a"])
+        if remote_tracking:
+            cmd = " ".join([cmd, "-r"])
+
+        gitlog = self.gitcmd(cmd, True)
+        if gitlog.exit_status != 0:
+            print gitlog.stderr
+            raise error.CmdError('Failed to get git branch', gitlog)
+        elif all or remote_tracking:
+            return gitlog.stdout.strip('\n')
+        else:
+            branch = [b.lstrip('* ') for b in gitlog.stdout.split('\n') \
+                      if b.startswith('*')][0]
+            return branch
diff --git a/server/git_kernel.py b/server/git_kernel.py
index 1f4273c..a23881e 100644
--- a/server/git_kernel.py
+++ b/server/git_kernel.py
@@ -22,13 +22,14 @@ class GitKernel(git.GitRepo):
 
     It is used to pull down a local copy of a git repo, check if the local repo
     is up-to-date, if not update and then build the kernel from the git repo.
-
     """
     def __init__(self, repodir, giturl, weburl):
         git.GitRepo.__init__(self, repodir, giturl, weburl)
         self.__patches = []
         self.__config = None
         self.__build = None
+        self.__branch = None
+        self.__revision = None
 
 
     def configure(self, config):
@@ -39,20 +40,69 @@ class GitKernel(git.GitRepo):
         self.__patches.append(patch)
 
 
-    def install(self, host, build=True, builddir=None):
-        # use tmpdir if no builddir specified
-        # NB: pass a builddir to install() method if you
-        # need to ensure the build remains after the completion
-        # of a job
+    def checkout(self, revision, local=None):
+        """
+        Checkout the commit id, branch, or tag
+
+        revision:str - name of the git remote branch, revision or tag
+        local:str - name of the local branch, implies -b
+        """
+        print 'checking out %s' % revision
+        super(GitKernel, self).checkout(revision)
+        self.__revision = super(GitKernel, self).get_revision()
+        self.__branch = super(GitKernel, self).get_branch()
+        print 'checked out %s on branch %s' % (self.__revision, self.__branch)
+
+    def show_branch(self):
+        """
+        Print the current local branch name
+        """
+        self.__branch = super(GitKernel, self).get_branch()
+        print self.__branch
+
+
+    def show_branches(self, all=True):
+        """
+        Print the local and remote branches
+        """
+        self.__branch = super(GitKernel, self).get_branch()
+        print super(GitKernel, self).get_branch(all=all)
+
+
+    def show_revision(self):
+        """
+        Show the current git revision
+        """
+        self.__revision = super(GitKernel, self).get_revision()
+        print self.__revision
+
+
+    def install(self, host, build=True, builddir=None, revision=None):
+        """
+        Install the git tree in a host.
+
+        @param host: Host object
+        @param build: Whether to build the source tree
+        @param builddir: Specify a build dir. If no build dir is specified,
+                the job temporary directory will be used, so the build won't
+                be persistent.
+        @param revision: Desired commit hash. If ommited, will build from HEAD
+                of the branch.
+        """
+        if revision:
+            self.checkout(revision)
+            self.__revision = super(GitKernel, self).get_revision()
+            print 'checked out revision: %s' %(self.__revision)
+
         if not builddir:
             self.__build = os.path.join(host.get_tmp_dir(),"build")
             print 'warning: builddir %s is not persistent' %(self.__build)
 
         # push source to host for install
-        print 'pushing %s to host' %(self.source_material)
+        print 'pushing %s to host' % self.source_material
         host.send_file(self.source_material, self.__build)
         remote_source_material= os.path.join(self.__build,
-                        os.path.basename(self.source_material))
+                                        os.path.basename(self.source_material))
 
         # use a source_kernel to configure, patch, build and install.
         sk = source_kernel.SourceKernel(remote_source_material)
-- 
1.7.2.2

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to