[13/15] git-tracker: use write-tree/commit-tree/update-ref
diff mbox series

Message ID 20200221095437.8699e29264b8.I370ee81ce341d4db00a2e5193efe3c697f944121@changeid
State New
Headers show
Series
  • updates & improvements
Related show

Commit Message

Johannes Berg Feb. 21, 2020, 8:56 a.m. UTC
From: Johannes Berg <johannes.berg@intel.com>

This gives us more flexibility, so we can create pretend
merge commits later.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 devel/git-tracker.py |  4 +++-
 lib/bpgit.py         | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

Patch
diff mbox series

diff --git a/devel/git-tracker.py b/devel/git-tracker.py
index 1cb26ec82eb0..7e55fa5ccb8e 100755
--- a/devel/git-tracker.py
+++ b/devel/git-tracker.py
@@ -104,6 +104,7 @@  def handle_commit(args, msg, branch, treename, kernelobjdir, tmpdir, wgitdir, ba
         else:
             git.reset(opts=['-q'], tree=wdir)
 
+        parents = [git.rev_parse('HEAD', tree=wdir)]
         if not failure or commit_failure:
             if append_shortlog:
                 files = []
@@ -136,7 +137,8 @@  def handle_commit(args, msg, branch, treename, kernelobjdir, tmpdir, wgitdir, ba
                 if not have_changeid:
                     msg += 'Change-Id: I%s\n' % hashlib.sha1(msg).hexdigest()
 
-            git.commit(msg, tree=wdir, env=env, opts=['-q', '--allow-empty'])
+            treeid = git.write_tree(tree=wdir)
+            git.commit_tree(treeid, msg, parents, tree=wdir, env=env)
             git.push(opts=['-f', '-q', 'origin', branch], tree=wdir)
         os.rename(os.path.join(wdir, '.git'), wgitdir)
     finally:
diff --git a/lib/bpgit.py b/lib/bpgit.py
index ff5f4a0c95b3..60d4abaa7a0d 100644
--- a/lib/bpgit.py
+++ b/lib/bpgit.py
@@ -255,6 +255,40 @@  def add(fn, tree=None):
     process.wait()
     _check(process)
 
+def write_tree(tree=None):
+    process = subprocess.Popen(['git', 'write-tree'], stdout=subprocess.PIPE,
+                               close_fds=True, universal_newlines=True, cwd=tree)
+    stdout = process.communicate()[0]
+    process.wait()
+    _check(process)
+    sha = stdout.strip()
+    if not _sha_re.match(sha):
+        raise SHAError()
+    return sha
+
+def commit_tree(treeid, msg, parents, env=None, tree=None):
+    if env is None:
+        env = {}
+    opts = []
+    for p in parents:
+        opts.extend(['-p', p])
+    stdin = tempfile.NamedTemporaryFile(mode='wr')
+    stdin.write(msg)
+    stdin.seek(0)
+    process = subprocess.Popen(['git', 'commit-tree', '-F-', treeid] + opts,
+                               stdin=stdin.file, universal_newlines=True,
+                               stdout=subprocess.PIPE, env=env, cwd=tree)
+    stdout = process.communicate()[0]
+    process.wait()
+    _check(process)
+    sha = stdout.strip()
+    if not _sha_re.match(sha):
+        raise SHAError()
+    process = subprocess.Popen(['git', 'update-ref', 'HEAD', sha], cwd=tree)
+    process.wait()
+    _check(process)
+    return sha
+
 def commit(msg, tree=None, env = {}, opts=[]):
     stdin = tempfile.NamedTemporaryFile(mode='wr')
     stdin.write(msg)