How to "unstage" a file with Objective-Git (libgit2)? -
i writing app allows performing basic git operations using objective-git, having trouble figuring out how "unstage" file. more specifically, have file changes have been added current gtindex , want discard changes (without losing file's current state on disk).
here rough outline of relevant logic program follows when toggling "staged" state of file:
- fetch current gtindex using gtrepository.indexwitherror: - fetch gtindexentry file using gtindex.entrywithpath: - if file has index , gtindexentry.status == gtindexentrystatusupdated: - here's i'm stuck according old unanswered question need lookup file's info in current head. in case, logic arrived at:
- fetch head's gtreference using gtrepository.headreferencewitherror: - if reference resolves gtcommit object: - fetch gttree using gtcommit.tree - fetch file's gttreeentry using gttree.entrywithpath:error: - stuck again! convert gttreeentry gtindexentry somehow? any guidance appreciated! i'm not scared of jumping directly libgit2 if have (this project has necessitated once or twice), since i'm working in swift i'd avoid if can , "unstaging" file seems such standard procedure figured must not understanding how objective-git classes tie together.
after letting sit little longer, discovered it's lot easier making it; rather comparing head tree active index, can use gtrepository.statusforfile:success:error: determine whether file staged, unstaged, etc. , add, remove, or add blob file based on that.
original approach
(on off chance contains useful info others down road.)
this turned out simpler seemed @ first, because transitioning between gttreeentry , gtindexentry unnecessary. instead, needed grab gttreeentry's gtobject (which blob files), pass blob's data gtindex.
i ended code flow goes this:
- fetch head's gtreference using gtrepository.headreferencewitherror: - if reference resolves gtcommit => - fetch gtindexentry using gtindex.entrywithpath: - if have index entry => - fetch gttree using gtcommit.tree - fetch gttreeentry using gttree.entrywithpath:error: - if gttreeentry.sha == gtindexentry.oid.sha => - gtindex.addfile:error: (using approach, it's possible end identical tree , index entries, means file unstaged) - else if gttreeentry.type == gtobjecttypeblob => - fetch gtblob using gttreeentry.gtobject:error: - pass gtblob.data gtindex.adddata:withpath:error: - else if no gttreeentry => gtindex.removefile:error: - else add file/directory gtindex (since have no index entry) - write changes disk gtindex.write: a couple of notes in hopes helpful future generations of people bashing heads against objective-git's largely undocumented hide:
- gtindexentry.status useless in scenario; references flags used internally libgit2 during construction of index not helpful determining status of files afterward (i'm frankly not sure why these flags exposed objective-git @ all)
- if want un/stage files in working directory, can have objective-git lot of legwork using gtdiff's
+diffindexfromtree:inrepository:options:error:
Comments
Post a Comment