Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command for missing files is inconsistent with c-git #122

Open
parasharjoshi opened this issue Dec 19, 2024 · 0 comments
Open

Add command for missing files is inconsistent with c-git #122

parasharjoshi opened this issue Dec 19, 2024 · 0 comments

Comments

@parasharjoshi
Copy link

Version

6.7.0

Operating System

Linux/Unix, Windows

Bug description

When executing an add operation/command, the command line git by default removes the missing files from index where as the JGit does not.

The steps for comparison are

  1. Create a new file
  2. Add the file to index
  3. Delete the file with rm
  4. Add the file for removal from index
$ git status
On branch Test1
Your branch is up to date with 'origin/Test1'.

nothing to commit, working tree clean

$ echo "Test file for missing in index" > test2.txt

$ git status
On branch Test1
Your branch is up to date with 'origin/Test1'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test2.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git add test2.txt
warning: in the working copy of 'test2.txt', LF will be replaced by CRLF the next time Git touches it

$ rm -f test2.txt

$ git status
On branch Test1
Your branch is up to date with 'origin/Test1'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test2.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    test2.txt


$ git add test2.txt

$ git status
On branch Test1
Your branch is up to date with 'origin/Test1'.

nothing to commit, working tree clean

$

Notice that the second add command after delete of file removes the file from staged/index by default.

To replicate the same with JGit used the snippet

 public void simulateMissingFilesAdd(String dotFilename) throws IOException, GitAPIException{
        Git git = getGitRepo(dirPath);
        String filePath = dirPath+"/"+dotFilename;
        
        //create a dot file and add to index to the repo
        try ( PrintWriter writer = new PrintWriter(filePath, "UTF-8")) 
        {
            writer.println("Line1 text");
            writer.println("Line2 text");
        }
        System.out.println("Created file "+dotFilename+ " and adding it ...");
        git.add().setUpdate(false).addFilepattern(".").call();
        printStatus(git);

        System.out.println("Deleting file "+dotFilename+ " ...");
        // remove dot file and add
        File file = new File(filePath);
        file.delete();
        printStatus(git);

        System.out.println("Adding file "+dotFilename+ " ...");
        git.add().setUpdate(false).addFilepattern(dotFilename).call();
        git.commit().setMessage("After removing the file "+dotFilename).call();
        System.out.println("Committed removal of file "+dotFilename+ " ...");
        printStatus(git);
    } 
    
    void printStatus(Git git) throws GitAPIException{
        Status status = git.status().call();
        System.out.println("\nClean : "+status.isClean());
        System.out.println("getAdded : "+status.getAdded());
        System.out.println("getMissing : "+status.getMissing());
        System.out.println("getRemoved : "+status.getRemoved());
        System.out.println("getUncommittedChanges : "+status.getUncommittedChanges());
        System.out.println("getUntracked : "+status.getUntracked());
    }

And results are of the run.

--- exec-maven-plugin:3.1.0:exec (default-cli) @ JGitOps ---
Set repo for : D:/JgitRepos/MissingFiles
Created file reproMissingFile.txt and adding it ...

Clean : false
getAdded : [reproMissingFile.txt]
getMissing : []
getRemoved : []
getUncommittedChanges : [reproMissingFile.txt]
getUntracked : []
Deleting file reproMissingFile.txt ...

Clean : false
getAdded : [reproMissingFile.txt]
getMissing : [reproMissingFile.txt]
getRemoved : []
getUncommittedChanges : [reproMissingFile.txt]
getUntracked : []
Adding file reproMissingFile.txt ...
Committed removal of file reproMissingFile.txt ...

Clean : false
getAdded : []
getMissing : [reproMissingFile.txt]
getRemoved : []
getUncommittedChanges : [reproMissingFile.txt]
getUntracked : []
Closed git repo : D:/JgitRepos/MissingFiles

Notice that the add after file delete does not remove the missing file from index with not clean status.

c-git provides a switch to override default with --ignore-missing switch so something similar can be done for JGit.

Actual behavior

Actual behaviour of Jgit is not consistent with that of C-Git where by default the file is not removed during add.

Expected behavior

Expected behaviour of Jgit to be consistent with that of C-Git where by default the file is removed during add and overridden with a flag/property

Relevant log output

No response

Other information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant